:root {
  /* Palette from dancheong (단청), the painted woodwork of Korean palaces and temples:
     green-blue beams (뇌록), cinnabar (주홍), ochre-yellow (황), iron-red (석간주), on ink (먹) and hanji paper. */
  --bg: #16130f;          /* ink */
  --surface: #201b15;
  --surface-2: #2c251c;
  --line: #3d3427;
  --text: #f2e9d8;        /* hanji */
  --muted: #a99c84;
  --accent: #5bb09a;      /* noerok green */
  --accent-ink: #10201b;
  --warm: #e4a53a;        /* ochre / amber */
  --good: #4fae74;
  --bad: #db5a43;         /* cinnabar */
  --b0: #db5a43; --b1: #e08a3c; --b2: #e4b33a; --b3: #a9b04a; --b4: #4fae74; --b5: #3e9c94;
  --radius: 16px;
  --kr: "Apple SD Gothic Neo", "Noto Sans KR", "Noto Sans CJK KR", "Malgun Gothic", sans-serif;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4ecdb; --surface: #fffaf0; --surface-2: #ece1c9; --line: #dacbab;
    --text: #231e18; --muted: #6e6352; --accent: #1f6b58; --accent-ink: #fffaf0; --warm: #b5361f;
    --good: #2f8a50; --bad: #b5361f;
    --b0: #c0412a; --b1: #cf7428; --b2: #c99a14; --b3: #7f9a2e; --b4: #2f8a50; --b5: #1f7a70;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
.hangul { font-family: var(--kr); }

.app {
  max-width: 600px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) max(16px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(16px, env(safe-area-inset-left));
}

/* header */
.top { display: flex; align-items: center; gap: 8px; padding: 12px 0; }
.brand { font-weight: 700; letter-spacing: .02em; color: var(--muted); }
.brand.hangul { color: var(--accent); font-size: 1.25em; white-space: nowrap; }
.seg.style { padding: 3px; background: var(--surface); }
.seg.style button { padding: 6px 12px; font-size: 14px; }
.score { margin-left: auto; white-space: nowrap; color: var(--muted); font-variant-numeric: tabular-nums; display: flex; gap: 8px; align-items: baseline; }
.score b { color: var(--text); }
#streak { color: var(--warm); }
@media (max-width: 480px) { #streak { display: none; } } /* no room next to Learn/Play */
.icon { width: 40px; height: 40px; border: 0; border-radius: 12px; background: var(--surface); display: grid; place-items: center; }
.icon svg, .play svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.play svg path:first-child { fill: currentColor; }

.banner { display: flex; align-items: center; gap: 8px; background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; margin-bottom: 8px; }
.banner > span { flex: 1; }
.unlock .sample { margin-left: 4px; letter-spacing: .08em; color: var(--accent); }
.unlock small { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
.banner button { border: 0; border-radius: 10px; padding: 6px 12px; background: transparent; color: var(--muted); }
.banner .yes { background: var(--accent); color: var(--accent-ink); font-weight: 600; }

/* prompt */
main { flex: 1; display: flex; flex-direction: column; justify-content: center; gap: 20px; padding: 8px 0 16px; }
.prompt { position: relative; text-align: center; min-height: 38vh; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.tag { height: 1.4em; font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .12em; color: var(--warm); }
.glyph { line-height: 1.1; min-height: 1em; font-size: clamp(96px, 34vw, 190px); font-weight: 500; }
.glyph.latin { font-size: clamp(60px, 18vw, 110px); font-weight: 600; letter-spacing: .02em; }
.glyph:empty { display: none; }
.play { width: 120px; height: 120px; border-radius: 50%; border: 2px solid var(--accent); background: var(--surface); color: var(--accent); display: grid; place-items: center; margin: 12px 0; }
.play[hidden] { display: none; }
.glyph:not(:empty) + .play { width: 48px; height: 48px; border-width: 1px; margin: 0; }
.play svg { width: 44%; height: 44%; }
.answer { min-height: 1.5em; font-size: 26px; font-weight: 600; color: var(--accent); font-family: var(--kr); }
.note { min-height: 2.8em; max-width: 30em; color: var(--muted); font-size: 15px; }

/* choices */
.choices { display: grid; gap: 12px; grid-template-columns: repeat(2, 1fr); }
.choices.n1 { grid-template-columns: 1fr; }
.choices.n3 { grid-template-columns: repeat(3, 1fr); }
.choices.n5 { grid-template-columns: repeat(6, 1fr); }
.choices.n5 .choice { grid-column: span 2; }
.choices.n5 .choice:nth-child(n+4) { grid-column: span 3; }
.choice {
  min-height: 76px;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 30px;
  font-weight: 600;
  transition: transform .08s, background-color .15s, border-color .15s, opacity .2s;
}
.choice.hangul { font-size: 42px; font-weight: 500; }
.choice:active { transform: scale(.97); }
@media (hover: hover) { .choice:not(:disabled):hover { border-color: var(--accent); } }
.choice.right { background: var(--good); border-color: var(--good); color: #fff; }
.choice.wrong { border-color: var(--bad); color: var(--bad); opacity: .35; animation: shake .3s; }
.choices.done .choice:not(.right) { opacity: .25; }
.choice.gotit { font-size: 20px; background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
@keyframes shake { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }
@media (prefers-reduced-motion: reduce) { .choice.wrong { animation: none; } .choice { transition: none; } }

/* circle layout: answers in a ring around the letter */
main.ring {
  flex: none;
  position: relative;
  /* Explicit equal sides: Safari doesn't reliably size a flex item from aspect-ratio alone. */
  --ring: min(calc(100vw - 32px), 568px, calc(100dvh - 260px));
  width: var(--ring);
  height: var(--ring);
  margin: auto; /* centred; the height cap leaves room below for the note */
  padding: 0;
  container-type: inline-size;
}
.ring .prompt { position: absolute; inset: 0; min-height: 0; }
.ring .glyph.hangul { font-size: 24cqi; }
.ring .glyph.latin { font-size: 12cqi; }
.ring .tag { font-size: 3.4cqi; }
.ring .answer { min-height: 0; font-size: 6cqi; }
.ring .note { position: absolute; top: 100%; left: 0; right: 0; margin: 0 auto; min-height: 0; padding-top: 10px; }
.ring .play { width: 30cqi; height: 30cqi; }
.ring .choices { position: absolute; inset: 0; display: block; pointer-events: none; }
.ring .choice {
  position: absolute;
  width: 23%;
  height: auto;
  aspect-ratio: 1 / 1;
  min-height: 0;
  -webkit-appearance: none;
  appearance: none;
  margin: -11.5% 0 0 -11.5%;
  padding: 0;
  border-radius: 50%;
  pointer-events: auto;
  font-size: calc(6.5cqi * min(1, 4.2 / var(--len, 1))); /* shrink long romanizations to fit */
}
.ring .choice.hangul { font-size: 10cqi; }

/* direction switch */
.modes { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; padding: 4px; margin: 4px 0 12px; background: var(--surface); border-radius: 14px; }
.modes button, .seg button, .seg span { border: 0; border-radius: 10px; padding: 10px 0; background: transparent; color: var(--muted); font-weight: 600; text-align: center; }
.modes button[aria-pressed=true], .seg button[aria-pressed=true], .seg input:checked + span { background: var(--surface-2); color: var(--text); box-shadow: inset 0 0 0 1px var(--line); }
.modes button { display: flex; flex-direction: column; align-items: center; gap: 1px; padding: 7px 0; }
.modes .hint { font-size: 17px; }
.modes small { font-size: 11px; font-weight: 500; text-transform: uppercase; letter-spacing: .08em; }
.modes button:disabled { opacity: .35; cursor: default; }

/* dialogs */
dialog {
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100dvh - 48px);
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--surface);
  color: var(--text);
  padding: 0;
}
dialog::backdrop { background: rgb(0 0 0 / .55); }
dialog form { padding: 16px 20px 20px; }
dialog header { display: flex; align-items: center; }
dialog h2 { margin: 0; font-size: 20px; flex: 1; }
dialog h3 { margin: 20px 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); }
dialog h3 small { text-transform: none; letter-spacing: 0; font-weight: 400; }
.close { border: 0; background: var(--accent); color: var(--accent-ink); font-weight: 600; border-radius: 10px; padding: 6px 14px; }
.group { display: grid; grid-template-columns: auto 1fr; column-gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
.group input { grid-row: span 2; width: 22px; height: 22px; accent-color: var(--accent); align-self: center; }
.gsample { color: var(--muted); font-size: 18px; letter-spacing: .1em; overflow-wrap: anywhere; }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px; padding: 4px; background: var(--bg); border-radius: 12px; }
.seg label { display: contents; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { cursor: pointer; }
.toggle { display: flex; gap: 12px; align-items: center; margin-top: 16px; cursor: pointer; }
.toggle input { width: 22px; height: 22px; accent-color: var(--accent); flex: none; }
.muted { color: var(--muted); }
.small { font-size: 13px; margin-top: 20px; }
.small.hint { margin-top: 6px; }
.link { border: 0; background: none; padding: 0; color: var(--bad); text-decoration: underline; }
a { color: var(--accent); }

#statdirs { margin-top: 12px; }
.cells { display: grid; grid-template-columns: repeat(auto-fill, minmax(48px, 1fr)); gap: 6px; }
.cell { aspect-ratio: 1; display: grid; place-items: center; font-size: 24px; border-radius: 10px; background: var(--bg); color: var(--muted); border: 1px solid var(--line); }
.cell:not([data-box=new]) { color: #fff; border-color: transparent; }
[data-box="0"] { background: var(--b0); } [data-box="1"] { background: var(--b1); } [data-box="2"] { background: var(--b2); }
[data-box="3"] { background: var(--b3); } [data-box="4"] { background: var(--b4); } [data-box="5"] { background: var(--b5); }
.legend { display: flex; gap: 6px 12px; flex-wrap: wrap; align-items: center; }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: -6px; border: 1px solid var(--line); }
.legend i[data-box=new] { background: var(--bg); }
