:root {
  color-scheme: light;
  --page: #fff;
  --surface: #fff;
  --ink: #21362a;
  --muted: #506457;
  --line: #dcd8cf;
  --forest: #173e30;
  --forest-deep: #102f24;
  --mint: #edf5e9;
  --sage: #dcebd5;
  --sand: #f3e7d9;
  --linen: #f7f2ec;
  --brown: #644633;
  --brown-deep: #4d3527;
  --gold: #f3d890;
  --accent: #245b45;
  font: 100%/1.5 system-ui, sans-serif;
  color: var(--ink);
  background: var(--page);
  accent-color: var(--accent);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; }
h1, h2, h3, p { margin: 0; }
button, select { font: inherit; }
button, select, input, summary { cursor: pointer; }
button { color: inherit; }
button:focus-visible, select:focus-visible, input:focus-visible, summary:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 3px;
}
::selection { color: var(--forest-deep); background: var(--gold); }

.wrap { width: min(100% - 2rem, 69rem); margin-inline: auto; }
.masthead { border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: .8rem; min-height: 4.2rem; }
.brand h1 { font-size: 1.35rem; line-height: 1.15; letter-spacing: -.025em; }
.brand p { margin-top: .1rem; color: var(--muted); font-size: .85rem; }
.brand-mark { width: 2.6rem; height: 2.6rem; flex: none; }

.layout { display: grid; gap: .85rem; max-width: 48rem; margin: 1.25rem auto 2rem; }
.round-panel {
  display: grid;
  justify-items: start;
  gap: .65rem;
  padding: 1.3rem;
  border-radius: 1rem;
  background: var(--mint);
}
.round-panel h2 { font-size: 1.6rem; line-height: 1.2; letter-spacing: -.025em; }
.round-panel p { color: var(--muted); }
.daily-greeting {
  display: flex;
  align-items: center;
  gap: .55rem;
  font-size: .9rem;
}
.daily-greeting svg { flex: none; }
#end-settings { width: 100%; }
#end-settings .settings { margin-top: 0; background: var(--surface); }
#next-selection { font-size: .85rem; }
.round-panel .next-question { color: var(--ink); font-weight: 650; }
.round-actions { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .25rem; }
.settings, .learning { padding: 0 .95rem; border-radius: .85rem; background: var(--linen); }
.settings { margin-top: .85rem; }
details summary { padding-block: .75rem; font-weight: 650; }
details summary::marker { color: var(--brown); }
details[open] { padding-bottom: .85rem; }
.settings[open] .settings-content { border-top: 1px solid var(--line); padding-top: .75rem; }
h2 { font-size: 1.15rem; font-weight: 700; letter-spacing: -.02em; }
.settings-content { display: grid; grid-template-columns: 1fr 1fr; align-items: start; gap: .6rem .65rem; }
.control { display: grid; min-width: 0; gap: .25rem; }
.control > label { font-size: .8rem; font-weight: 650; }
select, .reverse {
  min-height: 2.75rem;
  border: 1px solid #ada59a;
  border-radius: .55rem;
  color: var(--ink);
  background: var(--surface);
}
select { width: 100%; min-width: 0; padding-inline: .35rem .1rem; font-size: .875rem; }
.direction-control { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: .3rem; }
.reverse { padding-inline: .35rem; white-space: nowrap; font-weight: 700; font-size: .7rem; }
.reverse:hover, select:hover { background: var(--mint); }
.toggle-row { display: flex; align-items: center; gap: .5rem; min-height: 2.75rem; font-size: .875rem; cursor: pointer; grid-column: 1 / -1; }
.toggle-row input { width: 1.15rem; height: 1.15rem; margin: 0; }
#filter-wrap { grid-column: 1 / -1; }

.round-heading { display: flex; align-items: baseline; justify-content: space-between; gap: .5rem; }
.round-heading h2 { font-size: 1.65rem; }
.round-heading p { color: var(--muted); font-size: .85rem; font-variant-numeric: tabular-nums; }
progress {
  display: block;
  appearance: none;
  width: 100%;
  height: .27rem;
  margin: .6rem 0 .85rem;
  border: 0;
  border-radius: 1rem;
  background: var(--line);
  color: var(--accent);
}
progress::-webkit-progress-bar { background: var(--line); border-radius: 1rem; }
progress::-webkit-progress-value { background: var(--accent); border-radius: 1rem; }
progress::-moz-progress-bar { background: var(--accent); border-radius: 1rem; }

.card-stack { perspective: 70rem; padding-bottom: .45rem; }
.card {
  width: 100%;
  display: grid;
  border: 0;
  padding: 0;
  background: none;
  border-radius: 1rem;
  transform-style: preserve-3d;
}
.card.animate-flip { transition: transform 230ms cubic-bezier(.2,.8,.2,1); }
.card[aria-pressed="true"] { transform: rotateY(180deg); }
.face {
  grid-area: 1 / 1;
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: .7rem;
  align-items: center;
  min-height: 19rem;
  padding: 1.15rem 1rem 1rem;
  border-radius: inherit;
  box-shadow: 0 .36rem 0 -.05rem #bacdb0, 0 .85rem 1.7rem -.9rem #233e2f5c;
  backface-visibility: hidden;
}
.front { color: var(--forest-deep); background: var(--sage); }
.back { color: var(--brown-deep); background: var(--sand); transform: rotateY(180deg); box-shadow: 0 .36rem 0 -.05rem #dac4ae, 0 .85rem 1.7rem -.9rem #4d35275c; }
.face-label {
  justify-self: start;
  border: 1px solid currentColor;
  border-radius: 10rem;
  padding: .15rem .65rem;
  font-size: .75rem;
  font-weight: 650;
}
.front .face-label { color: var(--accent); }
.back .face-label, .back .translation { color: var(--brown); }
.face-main { display: grid; gap: .21rem; width: 100%; justify-items: center; text-align: center; }
.word { font-size: 1.8rem; line-height: 1.35; letter-spacing: -.025em; font-weight: 650; overflow-wrap: anywhere; }
.word[lang="th"] { font: 650 2.05rem/1.5 "Thonburi", "Noto Sans Thai", Tahoma, sans-serif; overflow-wrap: normal; }
.roman { font-size: 1rem; }
.front .roman { color: var(--muted); }
.translation { font-size: .9rem; color: var(--muted); }
#answer { order: -3; }
#extra[lang="th"] { order: -2; font-size: 1.08rem; }
#back-roman { order: -1; }
.flip-hint { justify-self: center; font-size: .82rem; font-weight: 650; text-underline-offset: .24em; text-decoration: underline; }
.front .flip-hint { color: var(--accent); }

.ratings { margin-top: .8rem; }
.ratings h3 { margin-bottom: .45rem; font-size: .98rem; }
.rating-buttons { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: .35rem; }
.rating-buttons button {
  display: grid;
  grid-template-rows: auto 1fr;
  align-items: start;
  gap: .15rem;
  padding: .3rem .05rem;
  min-height: 3.1rem;
  border: 1px solid #ada59a;
  border-radius: .55rem;
  background: var(--surface);
  line-height: 1.2;
}
.rating-buttons button:hover { border-color: var(--accent); background: var(--mint); }
.rating-buttons strong { font-size: 1.1rem; }
.rating-buttons small { color: var(--muted); font-size: .69rem; }
.primary {
  min-height: 2.75rem;
  padding: .45rem .9rem;
  border: 0;
  border-radius: .55rem;
  background: var(--brown);
  color: var(--surface);
  font-weight: 650;
}
.primary:hover { background: var(--brown-deep); }
.secondary {
  min-height: 2.75rem;
  padding: .45rem .9rem;
  border: 1px solid var(--brown);
  border-radius: .55rem;
  background: var(--surface);
  color: var(--brown-deep);
  font-weight: 650;
}
.secondary:hover { background: var(--linen); }
.text-button {
  min-height: 2.75rem;
  margin-top: .85rem;
  padding: .4rem .65rem;
  border: 0;
  border-radius: .55rem;
  background: transparent;
  color: var(--brown);
  text-decoration: underline;
  text-underline-offset: .18em;
}
.text-button:hover { background: var(--linen); }
.distribution { margin-bottom: .8rem; padding: .8rem; border-radius: .7rem; background: var(--surface); }
#distribution-summary { margin-top: .6rem; color: var(--muted); font-size: .85rem; }
.distribution-bar { display: flex; height: 1.38rem; overflow: hidden; margin-top: .45rem; border-radius: .4rem; }
.distribution-bar span { flex: 0 1 0; }
.distribution-legend { display: flex; flex-wrap: wrap; gap: .3rem .8rem; margin: .55rem 0 0; padding: 0; list-style: none; font-size: .8rem; }
.distribution-legend li { display: flex; align-items: center; gap: .3rem; }
.distribution-legend span { width: .65rem; height: .65rem; border-radius: .14rem; }
.distribution [data-rating="1"] { background: #9a5948; }
.distribution [data-rating="2"] { background: #b77f5d; }
.distribution [data-rating="3"] { background: #d4b48a; }
.distribution [data-rating="4"] { background: #9fbb8c; }
.distribution [data-rating="5"] { background: #d2e9bc; }
.frequency { padding: .65rem 0 .9rem; border-top: 1px solid var(--line); }
.frequency p { color: var(--muted); font-size: .85rem; }
.week {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  max-width: 26rem;
  gap: .28rem;
  margin: .55rem 0 0;
  padding: 0;
  list-style: none;
}
.week li {
  display: grid;
  justify-items: center;
  padding: .28rem .08rem;
  border: 1px solid var(--line);
  border-radius: .38rem;
  background: var(--surface);
  color: var(--muted);
  font-size: .72rem;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}
.week strong { font-size: .9rem; }
.week .active-day { background: var(--sage); color: var(--forest-deep); }
.week .today { border-color: var(--accent); }
.learning h2 { margin-bottom: .25rem; }
.stats-help { margin-bottom: .45rem; color: var(--muted); font-size: .8rem; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; font-variant-numeric: tabular-nums; }
thead th { color: var(--muted); font-size: .8rem; }
th, td { padding: .43rem .45rem; border-bottom: 1px solid var(--line); text-align: right; }
th:first-child { text-align: left; }
tbody th { font-weight: 550; }
tbody tr.current { background: var(--mint); }
.local-note { margin-top: .65rem; color: var(--muted); font-size: .8rem; }
.local-dot { display: inline-block; width: .4rem; height: .4rem; margin-inline: .15rem .3rem; border-radius: 50%; background: var(--accent); vertical-align: .12rem; }
.notice { margin-top: 1rem; padding: .7rem; border-radius: .55rem; background: var(--gold); }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

@media (max-width: 430px) {
  #end-settings .settings-content { grid-template-columns: 1fr; }
}

@media (max-width: 380px) {
  .settings-content { grid-template-columns: 1fr; }
  .face { min-height: 16rem; }
  .word[lang="th"] { font-size: 1.8rem; }
}

@media (min-width: 48rem) {
  .wrap { width: min(100% - 4rem, 69rem); }
  .brand { min-height: 5rem; }
  .layout { margin-block: 2rem 3rem; }
  .round-panel { padding: 2rem; }
  .round-panel h2 { font-size: 2rem; }
  .settings, .learning { padding-inline: 1.25rem; }
  select, .reverse { padding-inline: .6rem; }
  .round-heading h2 { font-size: 2rem; }
  .face { min-height: 25rem; padding: 1.4rem; }
  .word { font-size: 2.4rem; }
  .word[lang="th"] { font-size: 3rem; }
  .roman { font-size: 1.15rem; }
  .rating-buttons { gap: .65rem; }
}

@media (prefers-reduced-motion: no-preference) {
  .elephant-hop { animation: elephant-hop 720ms ease-in-out; }
  .elephant-trunk {
    transform-origin: 50px 30px;
    animation: elephant-trunk 720ms ease-in-out;
  }

  @keyframes elephant-hop {
    0%, 100% { transform: translateY(0); }
    15% { transform: translateY(3px); }
    48% { transform: translateY(-13px); }
    80% { transform: translateY(1px); }
  }

  @keyframes elephant-trunk {
    0%, 100% { transform: rotate(0); }
    48%, 62% { transform: rotate(-41deg); }
  }
}

@media (prefers-reduced-motion: reduce) {
  .card.animate-flip { transition: none; }
}

.login-form {
  display: grid;
  gap: .65rem;
  width: min(100%, 26rem);
}
.login-form input {
  width: 100%;
  min-height: 2.75rem;
  padding: .55rem .65rem;
  border: 1px solid #ada59a;
  border-radius: .55rem;
  background: var(--surface);
  color: var(--ink);
  cursor: text;
  font: inherit;
}
.account-panel {
  padding: .25rem .15rem 0;
  color: var(--muted);
  font-size: .85rem;
}
.account-panel .text-button { margin-top: 0; }
#login-status:empty, #connection-status:empty { display: none; }
#connection-status { margin-top: .4rem; }
#review-error p { margin-bottom: .45rem; }
button:disabled { opacity: .65; cursor: default; }
