.lang {
  display: inline-block;
}

.lang__trigger {
  appearance: none;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 40px;
  padding: 0 12px 0 10px;
  background: rgba(255, 255, 255, 0.10);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: var(--r-md);
  color: var(--text-on-dark);
  font-family: var(--font-sans);
  cursor: pointer;
  transition: background-color 120ms ease, border-color 120ms ease, transform 80ms ease;
  -webkit-tap-highlight-color: transparent;
}

.lang__trigger:hover {
  background: rgba(255, 255, 255, 0.18);
  border-color: rgba(255, 255, 255, 0.30);
}

.lang__trigger:active {
  transform: translateY(1px);
}

.lang__trigger:focus-visible {
  outline: 2px solid var(--text-on-dark);
  outline-offset: 2px;
}

.lang--light .lang__trigger {
  background: var(--paper);
  border-color: var(--hairline);
  color: var(--ink);
}

.lang--light .lang__trigger:hover {
  background: var(--fog);
  border-color: var(--divider);
}

.lang--light .lang__trigger:focus-visible {
  outline: 2px solid var(--primary);
}

.lang--light .lang__trigger-flag {
  filter: none;
}

.lang--light .lang__trigger-code {
  color: var(--text-dim);
  opacity: 1;
}

.lang__trigger-flag {
  font-size: 18px;
  line-height: 1;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.25));
}

.lang__trigger-code {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 1px;
  opacity: 0.9;
}

/* Overlay — replaces native <dialog>/::backdrop so the picker works on
   older Safari and Android WebViews. The [hidden] attribute toggles
   open/closed; the script adds/removes it. */

.lang__overlay {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  color: var(--text);
}

.lang__overlay[hidden] {
  display: none;
}

.lang__backdrop {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(14, 61, 82, 0.55);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  animation: lang-backdrop-in 220ms ease both;
}

@keyframes lang-backdrop-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.lang__sheet {
  position: relative;
  background: var(--paper);
  border-radius: 24px 24px 0 0;
  box-shadow: 0 -16px 48px rgba(14, 61, 82, 0.22);
  width: 100%;
  max-height: 86vh;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 8px);
  font-family: var(--font-sans);
  animation: lang-sheet-up 320ms cubic-bezier(0.22, 1, 0.36, 1) both;
}

@keyframes lang-sheet-up {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

.lang__handle {
  width: 36px;
  height: 4px;
  border-radius: 999px;
  background: var(--hairline);
  margin: 8px auto 4px;
  flex-shrink: 0;
}

.lang__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px 14px;
  border-bottom: 1px solid var(--divider);
  flex-shrink: 0;
}

.lang__title {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.3px;
  color: var(--ink);
  margin: 0;
}

.lang__close {
  appearance: none;
  background: var(--fog);
  border: none;
  border-radius: 999px;
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--ink);
  cursor: pointer;
  transition: background-color 120ms ease;
}

.lang__close:hover { background: var(--hairline); }

.lang__close:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.lang__list {
  list-style: none;
  margin: 0;
  padding: 8px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.lang__list form {
  margin: 0;
}

.lang__option {
  appearance: none;
  width: 100%;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--r-md);
  padding: 10px 14px 10px 16px;
  display: grid;
  grid-template-columns: 36px 1fr auto auto;
  align-items: center;
  column-gap: 12px;
  text-align: left;
  cursor: pointer;
  color: var(--text);
  font-family: inherit;
  position: relative;
  min-height: var(--tap-target);
  transition: background-color 120ms ease, border-color 120ms ease;
  animation: lang-option-in 360ms cubic-bezier(0.22, 1, 0.36, 1) both;
}

@keyframes lang-option-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.lang__option:hover { background: var(--fog); }

.lang__option:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  background: var(--fog);
}

.lang__option:active {
  background: var(--primary-soft-2);
}

.lang__option--active {
  background: var(--primary-soft);
  border-color: var(--primary-edge);
}

.lang__option--active::before {
  content: "";
  position: absolute;
  left: 4px;
  top: 14px;
  bottom: 14px;
  width: 3px;
  border-radius: 999px;
  background: var(--primary);
}

.lang__flag {
  font-size: 26px;
  line-height: 1;
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  filter: drop-shadow(0 1px 1px rgba(14, 61, 82, 0.14));
}

.lang__option-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.lang__option-native {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.1px;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.lang__option-english {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 1px;
}

.lang__option-code {
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 1px;
  color: var(--label);
  background: var(--fog);
  padding: 4px 7px;
  border-radius: 6px;
}

.lang__option--active .lang__option-code {
  background: var(--primary-soft-3);
  color: var(--primary);
}

.lang__check {
  width: 22px;
  height: 22px;
  border-radius: 999px;
  background: var(--primary);
  color: var(--paper);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

@media (min-width: 640px) {
  .lang__overlay {
    align-items: center;
    justify-content: center;
    padding: 24px;
  }

  .lang__sheet {
    max-width: 420px;
    max-height: 90vh;
    border-radius: 20px;
    box-shadow: 0 24px 64px rgba(14, 61, 82, 0.30);
    animation: lang-card-in 240ms cubic-bezier(0.22, 1, 0.36, 1) both;
    padding-bottom: 8px;
  }

  .lang__handle { display: none; }

  @keyframes lang-card-in {
    from { opacity: 0; transform: translateY(12px) scale(0.98); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
  }
}

@media (prefers-reduced-motion: reduce) {
  .lang__sheet,
  .lang__option,
  .lang__backdrop {
    animation: none;
  }

  .lang__trigger {
    transition: none;
  }
}
