/* ===== Design tokens ===== */
:root {
  color-scheme: light;
  --bg: #f4f5f9;
  --surface: #ffffff;
  --surface-2: #f0f2f6;
  --surface-hover: #e6e9f0;
  --border: #dde1ea;
  --text: #1a1d24;
  --muted: #646b78;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --on-primary: #ffffff;
  --focus-ring: rgba(37, 99, 235, 0.35);
  --danger: #c62828;
  --success: #15803d;
  --toast-success-bg: #dcfce7;
  --toast-success-text: #14532d;
  --toast-warning-bg: #fef3c7;
  --toast-warning-text: #78350f;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 6px 20px rgba(16, 24, 40, 0.06);
  --radius: 14px;
  --radius-sm: 10px;
}

/* Dark tokens: OS preference unless the user picked light, or an explicit dark choice */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0e1015;
    --surface: #171a21;
    --surface-2: #1f232c;
    --surface-hover: #2a2f3a;
    --border: #2c313c;
    --text: #e7e9ee;
    --muted: #9aa1ad;
    --primary: #6ea8fe;
    --primary-hover: #8fbcff;
    --on-primary: #0b1220;
    --focus-ring: rgba(110, 168, 254, 0.4);
    --danger: #f87171;
    --success: #4ade80;
    --toast-success-bg: #14532d;
    --toast-success-text: #dcfce7;
    --toast-warning-bg: #4a3500;
    --toast-warning-text: #fde68a;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 20px rgba(0, 0, 0, 0.25);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0e1015;
  --surface: #171a21;
  --surface-2: #1f232c;
  --surface-hover: #2a2f3a;
  --border: #2c313c;
  --text: #e7e9ee;
  --muted: #9aa1ad;
  --primary: #6ea8fe;
  --primary-hover: #8fbcff;
  --on-primary: #0b1220;
  --focus-ring: rgba(110, 168, 254, 0.4);
  --danger: #f87171;
  --success: #4ade80;
  --toast-success-bg: #14532d;
  --toast-success-text: #dcfce7;
  --toast-warning-bg: #4a3500;
  --toast-warning-text: #fde68a;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 20px rgba(0, 0, 0, 0.25);
}

/* ===== Base ===== */
*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.5;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

a {
  color: var(--primary);
}

.hidden {
  display: none !important;
}

.muted {
  color: var(--muted);
}

.page-wrap {
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
  padding: 0 16px;
}

/* ===== Header ===== */
.site-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.site-header .page-wrap {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 60px;
}
.brand {
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--text);
  text-decoration: none;
  white-space: nowrap;
}
.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}
.user-welcome {
  color: var(--muted);
  font-size: 0.95rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 40vw;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 40px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--text);
  font-weight: 500;
  text-decoration: none;
  cursor: pointer;
  transition: background-color 0.15s, border-color 0.15s;
}
.btn:hover:not(:disabled) {
  background: var(--surface-hover);
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--on-primary);
}
.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}
.btn-ghost {
  background: transparent;
  border-color: transparent;
}
.btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}
.btn-danger:hover:not(:disabled) {
  background: var(--danger);
  opacity: 0.9;
}
.btn-success-outline {
  background: transparent;
  border-color: var(--success);
  color: var(--success);
}
.btn-danger-outline {
  background: transparent;
  border-color: var(--danger);
  color: var(--danger);
}
.icon-btn {
  width: 40px;
  padding: 0;
  font-size: 1.1rem;
}
.link-btn {
  background: none;
  border: none;
  padding: 0;
  color: var(--primary);
  text-decoration: underline;
  cursor: pointer;
}

/* Spinner shown by apiCall() while a request runs */
.is-loading::after {
  content: "";
  width: 14px;
  height: 14px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

/* ===== Form controls ===== */
textarea,
input,
select {
  width: 100%;
  min-height: 42px;
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
textarea:focus,
input:focus,
select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--focus-ring);
}

/* ===== Cards ===== */
/* Grid tracks are minmax(0, 1fr) throughout: the default minimum (auto) lets unbreakable
   content such as the nowrap recent items widen the column past the viewport on phones. */
.app-main {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 20px;
  padding-top: 24px;
  padding-bottom: 48px;
}
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}
.card-header h2 {
  margin: 0;
  font-size: 1.15rem;
}
.empty-state {
  margin: 0;
  color: var(--muted);
}

/* ===== Translator ===== */
.lang-bar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  gap: 8px;
  align-items: center;
  margin-bottom: 12px;
}
.panes {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}
.pane {
  display: flex;
  flex-direction: column;
  min-height: 220px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
}
.pane-input {
  position: relative;
}
.clear-input {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 32px;
  min-height: 32px;
  font-size: 0.95rem;
  color: var(--muted);
}
/* Room for the ✕ button (more specific than `.pane textarea` below) */
.pane.pane-input textarea {
  padding-right: 44px;
}
.pane-input:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--focus-ring);
}
.pane textarea {
  flex: 1;
  min-height: 170px;
  padding: 14px;
  border: none;
  background: transparent;
  font-size: 1.1rem;
  resize: vertical;
}
.pane textarea:focus {
  box-shadow: none;
}
.did-you-mean {
  margin: 0;
  padding: 0 14px 10px;
  color: var(--muted);
  font-size: 0.92rem;
}
.did-you-mean .link-btn {
  font-weight: 600;
  text-align: left;
}
.pane-output {
  background: var(--surface-2);
}
.output-body {
  flex: 1;
  padding: 14px;
}
.output-text {
  font-size: 1.1rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.pronunciation {
  color: var(--muted);
  font-style: italic;
  overflow-wrap: anywhere;
}
#pronunciation {
  margin-top: 6px;
}
.output-text:empty::before {
  content: "Translation";
  color: var(--muted);
}
.pane-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 48px;
  padding: 4px 8px 4px 14px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.85rem;
}
.char-count {
  margin-left: auto;
  white-space: nowrap;
}
.char-count.near-limit {
  color: var(--danger);
}
.selection-chip {
  display: inline-block;
  min-height: 32px;
  max-width: 70%;
  padding: 0 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.85rem;
  line-height: 30px;
}
.output-actions {
  display: flex;
  gap: 2px;
  margin-left: auto;
}
.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}
.result {
  position: relative;
  margin-top: 12px;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.result h3 {
  margin: 0 0 4px;
  font-size: 0.78rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}
.result p,
.result ul {
  margin: 0 0 12px;
}
.result-close {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 32px;
  min-height: 32px;
  font-size: 0.9rem;
  color: var(--muted);
}
.result-close + h3 {
  padding-right: 32px;
}
.pair-list {
  display: grid;
  gap: 10px;
  padding: 0;
  list-style: none;
}
.result > :last-child {
  margin-bottom: 0;
}

/* ===== Recent ===== */
.recent-list {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}
.recent-item {
  display: flex;
  align-items: center;
  gap: 4px;
  padding-right: 4px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}
.recent-open {
  display: flex;
  flex: 1;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
  padding: 10px 12px;
  border: none;
  border-radius: var(--radius-sm);
  background: none;
  text-align: left;
  cursor: pointer;
}
.recent-open:hover {
  background: var(--surface-hover);
}
.recent-text,
.recent-translation {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.recent-text {
  flex-shrink: 0;
  max-width: 45%;
  font-weight: 600;
}
.recent-translation {
  color: var(--muted);
}
.recent-item .icon-btn {
  flex-shrink: 0;
  width: 36px;
  min-height: 36px;
}

/* ===== Favorites ===== */
.fav-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(220px, 100%), 1fr));
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
}
.fav-item {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}
.fav-open {
  display: flex;
  flex: 1;
  flex-direction: column;
  min-width: 0;
  padding: 6px 8px;
  border: none;
  border-radius: var(--radius-sm);
  background: none;
  text-align: left;
  cursor: pointer;
}
.fav-open:hover {
  background: var(--surface-hover);
}
.fav-text {
  font-weight: 600;
  overflow-wrap: anywhere;
}
.fav-translation {
  color: var(--muted);
  overflow-wrap: anywhere;
}
.fav-item .icon-btn {
  flex-shrink: 0;
  width: 32px;
  min-height: 32px;
  font-size: 0.9rem;
}

/* ===== Practice ===== */
.due-count {
  margin: 0 0 12px;
  color: var(--muted);
}
.due-count.has-due {
  color: var(--success);
  font-weight: 500;
}
.practice-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
#practiceArea {
  margin-top: 16px;
}
.practice-card {
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}
.practice-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}
.counter {
  font-weight: 600;
  color: var(--muted);
}
.practice-label {
  margin: 0;
  color: var(--muted);
}
.practice-word {
  margin: 2px 0 14px;
  font-size: 1.35rem;
  font-weight: 600;
  overflow-wrap: anywhere;
}
.practice-nav-actions {
  display: flex;
  gap: 4px;
}
kbd {
  padding: 1px 6px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 6px;
  background: var(--surface);
  color: var(--muted);
  font-family: inherit;
  font-size: 0.75rem;
  line-height: 1.4;
}
.practice-answer {
  margin: 10px 0;
  font-size: 1.15rem;
}
.practice-feedback {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.practice-completion {
  text-align: center;
}
.practice-completion h3 {
  margin: 0 0 4px;
}
.practice-completion p {
  margin: 4px 0;
}
.last-practice {
  font-weight: 500;
}
.next-review {
  color: var(--muted);
  font-size: 0.9rem;
}

/* ===== Toast ===== */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 1000;
  max-width: min(440px, calc(100vw - 32px));
  padding: 12px 18px;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  font-weight: 500;
  transform: translateX(-50%);
  animation: toast-in 0.2s ease-out;
}
.toast.success {
  background: var(--toast-success-bg);
  color: var(--toast-success-text);
}
.toast.warning {
  background: var(--toast-warning-bg);
  color: var(--toast-warning-text);
}
.toast-action {
  margin-left: 14px;
  padding: 0;
  border: none;
  background: none;
  color: inherit;
  font-weight: 700;
  text-decoration: underline;
  cursor: pointer;
}
@keyframes toast-in {
  from {
    opacity: 0;
    transform: translate(-50%, 8px);
  }
}

/* ===== Dialog ===== */
dialog {
  max-width: min(380px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}
dialog::backdrop {
  background: rgba(0, 0, 0, 0.45);
}
.dialog-body {
  padding: 20px;
}
.dialog-body p {
  margin: 0;
}
.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 18px;
}

/* ===== Landing page ===== */
.hero {
  max-width: 720px;
  margin: 56px auto;
  padding: 0 16px;
  text-align: center;
}
.hero h1 {
  margin: 0 0 12px;
  font-size: clamp(2rem, 6vw, 2.8rem);
  line-height: 1.15;
}
.lead {
  margin: 0;
  color: var(--muted);
  font-size: 1.15rem;
}
.hero-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin: 28px 0 40px;
}
.features {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(240px, 100%), 1fr));
  gap: 12px;
  text-align: left;
}
.feature {
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.feature h3 {
  margin: 6px 0 4px;
  font-size: 1rem;
}
.feature p {
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
}

/* ===== Auth page ===== */
.auth-card {
  max-width: 420px;
  margin: 40px auto;
  padding: 28px 24px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.auth-title {
  margin: 0 0 20px;
  font-size: 1.6rem;
  text-align: center;
}
.auth-hint {
  margin: -8px 0 16px;
  color: var(--muted);
  font-size: 0.95rem;
  text-align: center;
}
.auth-fields {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.auth-label {
  margin-top: 10px;
  color: var(--muted);
  font-size: 0.9rem;
}
.auth-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 20px;
}
.auth-error,
.auth-info {
  margin: 12px 0 0;
  font-size: 0.95rem;
  text-align: center;
}
.auth-error {
  color: var(--danger);
}
.auth-info {
  color: var(--success);
}
.auth-error:empty,
.auth-info:empty {
  display: none;
}
.auth-switch {
  margin: 12px 0 0;
  font-size: 0.92rem;
  text-align: center;
}

/* Login/signup/reset share one form; data-mode decides which parts show */
#authForm:not([data-mode="login"]) .login-only,
#authForm:not([data-mode="signup"]) .signup-only,
#authForm:not([data-mode="reset"]) .reset-only,
#authForm[data-mode="reset"] .no-reset {
  display: none;
}

/* Autofill keeps theme colors */
input:-webkit-autofill {
  -webkit-box-shadow: 0 0 0 1000px var(--surface) inset;
  -webkit-text-fill-color: var(--text);
}

/* ===== Small screens ===== */
@media (max-width: 720px) {
  .panes {
    grid-template-columns: minmax(0, 1fr);
  }
  .pane {
    min-height: 160px;
  }
}

@media (max-width: 480px) {
  .card {
    padding: 16px;
  }
  /* Stack the language selects so long labels ("English — detected") fit */
  .lang-bar {
    grid-template-columns: minmax(0, 1fr);
  }
  #swapBtn {
    justify-self: center;
    transform: rotate(90deg);
  }
  .actions .btn,
  .practice-buttons .btn,
  .hero-actions .btn {
    flex: 1 1 100%;
  }
  .auth-card {
    margin: 24px 12px;
    padding: 20px 16px;
  }
}
