/* Ortho Cards — clinical teal on a cool neutral ground.
   Mobile-first: this lives on a phone, held one-handed, several times a day. */

:root {
  --bg: #F4F6F6;
  --surface: #FFFFFF;
  --surface-2: #FAFBFB;
  --ink: #16202B;
  --muted: #566970;
  --faint: #8A9AA0;
  --line: #DDE4E4;
  --accent: #0E8F84;
  --accent-ink: #0A6B63;
  --accent-soft: #DCEFEC;

  --again: #C0392B;
  --hard: #B77413;
  --good: #0E8F84;
  --easy: #2D6FB0;

  --shadow: 0 1px 2px rgba(22, 32, 43, .05), 0 10px 30px rgba(22, 32, 43, .07);
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans Thai", sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", Consolas, monospace;
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0F161C;
    --surface: #18222B;
    --surface-2: #141D25;
    --ink: #E9EEEF;
    --muted: #9AA9AF;
    --faint: #6C7C83;
    --line: #26323A;
    --accent: #34BEB0;
    --accent-ink: #6FD8CD;
    --accent-soft: #123430;

    --again: #E06052;
    --hard: #D69537;
    --good: #34BEB0;
    --easy: #5B9AD8;

    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 12px 34px rgba(0, 0, 0, .38);
  }
}

* { box-sizing: border-box; }

/* Any element we give a `display` to (.empty, .grades, .deck-list) would beat
   the UA stylesheet's `[hidden] { display: none }`, because author rules win.
   Toggling the hidden attribute would then do nothing at all. Settle it once. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  min-height: 100%;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

.screen {
  max-width: 680px;
  margin: 0 auto;
  padding: 16px 16px calc(24px + env(safe-area-inset-bottom));
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.screen[hidden] { display: none; }

/* ── top bar ──────────────────────────────────────────────────────────────── */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-top: env(safe-area-inset-top);
}

.wordmark {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -.02em;
  margin: 0;
}
.wordmark span { color: var(--accent); }

.deck-title {
  font-size: 14px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  text-align: center;
}

.progress {
  font-family: var(--mono);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  color: var(--faint);
  white-space: nowrap;
}

/* ── buttons ──────────────────────────────────────────────────────────────── */

button {
  font: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

button:focus-visible,
.deck-open:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 8px;
}

.icon-btn {
  font-size: 13px;
  font-weight: 600;
  color: var(--accent-ink);
  background: var(--accent-soft);
  padding: 7px 12px;
  border-radius: 999px;
  white-space: nowrap;
}
.icon-btn.ghost {
  background: transparent;
  color: var(--muted);
  padding-left: 0;
}
.icon-btn.danger {
  color: var(--again);
  background: transparent;
}

.btn {
  padding: 14px 20px;
  border-radius: 12px;
  font-weight: 600;
  font-size: 15px;
  transition: transform .08s ease, filter .15s ease;
}
.btn:active { transform: translateY(1px); }
.btn.primary {
  background: var(--accent);
  color: #fff;
  box-shadow: var(--shadow);
}
.btn.primary:hover { filter: brightness(1.06); }
.btn.wide { width: 100%; }

kbd {
  font-family: var(--mono);
  font-size: 10px;
  opacity: .65;
  border: 1px solid currentColor;
  border-radius: 4px;
  padding: 0 4px;
  margin-left: 6px;
}

/* ── home: stats ──────────────────────────────────────────────────────────── */

.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 10px;
  text-align: center;
  box-shadow: var(--shadow);
}
.stat b {
  display: block;
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-size: 26px;
  line-height: 1.2;
  color: var(--accent-ink);
}
.stat small {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--faint);
}

/* ── home: deck list ──────────────────────────────────────────────────────── */

.deck-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.deck {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.deck-open {
  width: 100%;
  text-align: left;
  padding: 16px 18px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.deck-name {
  font-weight: 600;
  font-size: 16px;
  line-height: 1.35;
}

.deck-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.count {
  font-family: var(--mono);
  font-size: 11.5px;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
}
.count.due {
  background: var(--accent-soft);
  border-color: transparent;
  color: var(--accent-ink);
  font-weight: 600;
}
.count.new {
  background: color-mix(in srgb, var(--easy) 14%, transparent);
  border-color: transparent;
  color: var(--easy);
  font-weight: 600;
}

.deck-actions {
  display: flex;
  justify-content: flex-end;
  gap: 4px;
  padding: 0 12px 10px;
  border-top: 1px solid var(--line);
  padding-top: 8px;
}
.deck-actions .icon-btn {
  font-size: 12px;
  background: transparent;
  color: var(--muted);
}
.deck-actions .icon-btn:hover { color: var(--accent-ink); }
.deck-actions .icon-btn.danger:hover { color: var(--again); }

/* ── empty / done ─────────────────────────────────────────────────────────── */

.empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 12px;
  padding: 40px 20px;
}
.empty h2 { margin: 0; font-size: 20px; }
.empty p {
  margin: 0;
  color: var(--muted);
  max-width: 42ch;
}
.empty .hint { font-size: 13px; color: var(--faint); }
.empty code {
  font-family: var(--mono);
  font-size: 12px;
  background: var(--accent-soft);
  color: var(--accent-ink);
  padding: 2px 6px;
  border-radius: 5px;
}

.tick svg {
  width: 56px;
  height: 56px;
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.tick circle { opacity: .25; }

/* ── review: the card ─────────────────────────────────────────────────────── */

.card-face {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow-y: auto;
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.badge {
  font-family: var(--mono);
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: .08em;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 999px;
}
.badge.is-new {
  background: color-mix(in srgb, var(--easy) 15%, transparent);
  color: var(--easy);
}
.badge.is-review {
  background: var(--accent-soft);
  color: var(--accent-ink);
}

.source {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--faint);
}

.qa {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
}

.front {
  font-size: 21px;
  line-height: 1.5;
  font-weight: 600;
  text-wrap: pretty;
  margin: 0;
}

.answer hr {
  border: none;
  border-top: 1px solid var(--line);
  margin: 20px 0;
}

.back {
  font-size: 18px;
  line-height: 1.65;
  color: var(--muted);
  text-wrap: pretty;
  margin: 0;
  white-space: pre-wrap;
}

@media (prefers-reduced-motion: no-preference) {
  .answer { animation: fade .18s ease-out; }
  @keyframes fade {
    from { opacity: 0; transform: translateY(-4px); }
    to { opacity: 1; transform: none; }
  }
}

/* ── review: grading ──────────────────────────────────────────────────────── */

.actions { display: flex; flex-direction: column; }

.grades {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.grade {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 12px 4px;
  border-radius: 12px;
  color: #fff;
  box-shadow: var(--shadow);
  transition: transform .08s ease, filter .15s ease;
}
.grade:active { transform: translateY(1px); }
.grade:hover { filter: brightness(1.08); }

.grade.again { background: var(--again); }
.grade.hard  { background: var(--hard); }
.grade.good  { background: var(--good); }
.grade.easy  { background: var(--easy); }

.glabel { font-weight: 600; font-size: 14px; }

.interval {
  font-family: var(--mono);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  opacity: .85;
}

.grade kbd { border-color: rgba(255, 255, 255, .5); }

@media (max-width: 380px) {
  .grade kbd { display: none; }
}

/* ── cloze ────────────────────────────────────────────────────────────────── */

.cloze-blank {
  display: inline-block;
  min-width: 3.5em;
  padding: 0 .35em;
  border-radius: 5px;
  background: var(--accent-soft);
  color: var(--accent-ink);
  font-family: var(--mono);
  font-size: .82em;
  vertical-align: baseline;
}

.cloze-answer {
  background: var(--accent-soft);
  color: var(--accent-ink);
  padding: 0 .25em;
  border-radius: 4px;
  font-weight: 700;
}

.card-img {
  max-width: 100%;
  border-radius: 10px;
  border: 1px solid var(--line);
  margin-top: 14px;
  align-self: center;
}

/* ── browse ───────────────────────────────────────────────────────────────── */

.topbar-actions { display: flex; gap: 4px; align-items: center; }

.card-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
}

.card-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 4px 10px 4px 4px;
  box-shadow: var(--shadow);
}

.card-row-open {
  flex: 1;
  min-width: 0;
  text-align: left;
  padding: 12px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.row-top { display: flex; gap: 5px; flex-wrap: wrap; }

.type-tag {
  font-family: var(--mono);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: .06em;
  padding: 2px 7px;
  border-radius: 999px;
  font-weight: 600;
}
.type-tag.basic { background: var(--accent-soft); color: var(--accent-ink); }
.type-tag.cloze { background: color-mix(in srgb, var(--easy) 16%, transparent); color: var(--easy); }
.type-tag.muted { background: transparent; border: 1px solid var(--line); color: var(--faint); }

.row-front {
  font-size: 14.5px;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.sticky {
  position: sticky;
  bottom: 0;
  margin-top: auto;
}

/* ── form ─────────────────────────────────────────────────────────────────── */

.form-deck { margin: 0; color: var(--faint); font-size: 13px; }
.form-deck span { color: var(--accent-ink); font-weight: 600; }

.type-switch {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 4px;
}
.type-opt {
  padding: 9px;
  border-radius: 7px;
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
}
.type-opt.active {
  background: var(--surface);
  color: var(--accent-ink);
  box-shadow: var(--shadow);
}

.fields { display: flex; flex-direction: column; gap: 14px; }

.field { display: flex; flex-direction: column; gap: 6px; }

.fl {
  font-size: 11px;
  font-family: var(--mono);
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--faint);
}

textarea, input[type="text"], select {
  font: inherit;
  font-size: 16px;              /* below 16px iOS Safari zooms the page on focus */
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 11px 13px;
  width: 100%;
  resize: vertical;
  line-height: 1.55;
}
textarea:focus, input:focus, select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-color: transparent;
}

.btn.ghost-btn {
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--muted);
  padding: 10px 16px;
  font-size: 14px;
}
.btn.ghost-btn:hover { color: var(--accent-ink); border-color: var(--accent); }
.btn.ghost-btn.danger:hover { color: var(--again); border-color: var(--again); }

.img-row { display: flex; gap: 8px; }
.img-row .btn { flex: 1; }

.img-preview-wrap {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px;
  background: var(--surface-2);
}
.img-preview-wrap img { width: 100%; border-radius: 6px; display: block; }

.hint { font-size: 12.5px; color: var(--faint); margin: 0; }

.lib-loading {
  list-style: none;
  padding: 20px;
  text-align: center;
  color: var(--faint);
  font-family: var(--mono);
  font-size: 13px;
}
.icon-btn:disabled { opacity: .5; cursor: default; }

.empty-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }

/* ── stats ────────────────────────────────────────────────────────────────── */

#stats-body { display: flex; flex-direction: column; gap: 16px; }

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}
.panel h3 { margin: 0; font-size: 15px; }
.panel-sub { margin: 4px 0 16px; font-size: 12.5px; color: var(--faint); }

.forecast {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
  align-items: end;
  height: 130px;
}
.fc-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  gap: 5px;
  height: 100%;
}
.fc-count {
  font-family: var(--mono);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  min-height: 14px;
}
.fc-bar {
  width: 100%;
  background: var(--accent-soft);
  border-radius: 5px 5px 2px 2px;
  min-height: 3px;
}
.fc-bar.today { background: var(--accent); }
.fc-day {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--faint);
}

.activity {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
}
.activity .sq {
  aspect-ratio: 1;
  border-radius: 4px;
  background: var(--surface-2);
  border: 1px solid var(--line);
}
.activity .sq.l1 { background: var(--accent-soft); border-color: transparent; }
.activity .sq.l2 { background: color-mix(in srgb, var(--accent) 55%, var(--surface)); border-color: transparent; }
.activity .sq.l3 { background: var(--accent); border-color: transparent; }

.facts { margin: 0; display: flex; flex-direction: column; gap: 10px; }
.facts > div {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  border-bottom: 1px dashed var(--line);
  padding-bottom: 8px;
}
.facts > div:last-child { border-bottom: none; padding-bottom: 0; }
.facts dt { color: var(--muted); font-size: 14px; }
.facts dd {
  margin: 0;
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--accent-ink);
}

/* ── toast ────────────────────────────────────────────────────────────────── */

/* Anchored to the top, not the bottom: the bottom of the screen is where the
   thumb lives and where every primary action sits, and a toast there covers the
   button you are reaching for. pointer-events: none so it can never eat a tap
   even while it is on screen. */
.toast {
  position: fixed;
  left: 50%;
  top: calc(12px + env(safe-area-inset-top));
  transform: translate(-50%, -180%);
  max-width: min(90vw, 480px);
  background: var(--ink);
  color: var(--bg);
  padding: 12px 18px;
  border-radius: 10px;
  font-size: 14px;
  box-shadow: var(--shadow);
  transition: transform .28s cubic-bezier(.2, .8, .2, 1);
  pointer-events: none;
  z-index: 10;
}
.toast.show { transform: translate(-50%, 0); }
.toast.error { background: var(--again); color: #fff; }
