:root {
  --bg: #f6f3ee;
  --fg: #1d1b19;
  --muted: #7a736b;
  --card: #fff;
  --line: #e4ded5;
  --accent: #c0392b;
  --ok: #2e8b57;
  --ko: #c0392b;
  --hanzi-font: "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Source Han Sans SC", "Microsoft YaHei", sans-serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161514;
    --fg: #efebe6;
    --muted: #9a938b;
    --card: #23211f;
    --line: #34312e;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

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

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; cursor: pointer; }

.bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
}

.brand {
  font-weight: 800;
  font-size: 24px;
  line-height: 1;
  letter-spacing: -0.02em;
  white-space: nowrap;
}

/* HSK + Kuai (快, fast) share the K: it is split along its diagonal leg,
   left half in the text colour (HSK), right half in the accent colour (Kuai). */
.brand-kuai { color: var(--accent); }
@supports ((background-clip: text) or (-webkit-background-clip: text)) {
  .brand-k {
    background: linear-gradient(115deg, var(--fg) 52%, var(--accent) 52%);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
  }
}

.badge {
  font-weight: 600;
  font-size: 13px;
  padding: 3px 9px;
  border-radius: 99px;
  background: var(--accent);
  color: #fff;
}

.counts { flex: 1; color: var(--muted); font-size: 14px; }

/* Narrow phones: keep brand, level and counts on one line. */
@media (max-width: 400px) {
  .bar { gap: 8px; padding: 10px 12px; }
  .brand { font-size: 22px; }
  .counts { font-size: 13px; }
}

.icon-btn {
  border: 0;
  background: none;
  padding: 6px;
  color: var(--muted);
}

.banner {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin: 0 16px;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--card);
  border: 1px solid var(--line);
  font-size: 14px;
  color: var(--muted);
}

.link-btn {
  border: 0;
  background: none;
  color: var(--accent);
  font-weight: 600;
  padding: 0;
}

main {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px 16px;
  min-height: 0;
}

.card {
  position: relative;
  width: 100%;
  max-width: 520px;
  height: 100%;
  max-height: 560px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 40px 24px 28px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: 0 6px 24px rgb(0 0 0 / 6%);
  text-align: center;
  user-select: none;
  touch-action: pan-y;
  transition: transform .15s ease, border-color .15s;
  overflow-y: auto;
  outline: none;
}

.card:not(.flipped):not(.empty) { cursor: pointer; }
.card[data-swipe="right"] { border-color: var(--ok); }
.card[data-swipe="left"] { border-color: var(--ko); }

.tag {
  position: absolute;
  top: 12px;
  left: 16px;
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .05em;
}

.new {
  margin-left: 8px;
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--accent);
  color: #fff;
}

.level-tag {
  position: absolute;
  top: 12px;
  right: 16px;
  font-size: 12px;
  color: var(--muted);
}

.hanzi {
  font-family: var(--hanzi-font);
  font-size: clamp(64px, 20vw, 128px);
  line-height: 1.1;
}

.flipped .hanzi { font-size: clamp(56px, 16vw, 104px); }

.pinyin {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  font-size: 26px;
  color: var(--accent);
}

.meaning { font-size: 20px; }
.meaning.big { font-size: 26px; font-weight: 500; }

.pos {
  font-size: 13px;
  color: var(--muted);
  font-style: italic;
  margin-right: 8px;
}

.alt {
  font-size: 15px;
  color: var(--muted);
}

.alt-py {
  color: var(--accent);
  margin-right: 8px;
}

.alt-also { color: var(--muted); margin-right: 4px; }

.hint { color: var(--muted); margin: 0; }

.card.dir-tn:not(.flipped) { cursor: default; }
.dir-tn .hanzi { font-size: clamp(48px, 14vw, 88px); }

.options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  width: 100%;
  max-width: 360px;
}

.option {
  min-height: 52px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  font-size: 20px;
}

.option kbd { margin: 0 8px 0 0; }
.option:disabled { cursor: default; opacity: .5; }
.option.right, .option.wrong { opacity: 1; color: #fff; }
.option.right { background: var(--ok); border-color: var(--ok); }
.option.wrong { background: var(--ko); border-color: var(--ko); }

.play {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--accent);
  padding: 0;
}

.play svg { width: 55%; height: 55%; }

.play.off {
  color: var(--muted);
  opacity: .6;
  cursor: not-allowed;
}

.play.off svg { width: 75%; height: 75%; }

.play-alt {
  width: 26px;
  height: 26px;
  margin-right: 8px;
  vertical-align: middle;
}

.play-big.off {
  background: var(--line);
  color: var(--muted);
}

.play-big {
  width: 112px;
  height: 112px;
  background: var(--accent);
  border: 0;
  color: #fff;
}

.actions {
  display: flex;
  gap: 12px;
  padding: 8px 16px 16px;
  max-width: 552px;
  width: 100%;
  margin: 0 auto;
}

.btn {
  flex: 1;
  min-height: 60px;
  border: 0;
  border-radius: 14px;
  font-size: 18px;
  font-weight: 600;
}

.btn-reveal { background: var(--fg); color: var(--bg); }
.btn-wrong { background: var(--ko); color: #fff; }
.btn-right { background: var(--ok); color: #fff; }
.btn-next { background: var(--fg); color: var(--bg); }

kbd {
  font: 12px system-ui, sans-serif;
  opacity: .6;
  margin-left: 6px;
}

@media (hover: none) { kbd { display: none; } }

body.ok .card { animation: flash-ok .3s; }
body.ko .card { animation: flash-ko .3s; }
@keyframes flash-ok { from { box-shadow: 0 0 0 3px var(--ok); } }
@keyframes flash-ko { from { box-shadow: 0 0 0 3px var(--ko); } }

/* Settings */
dialog {
  border: 0;
  border-radius: 16px;
  padding: 0;
  width: min(440px, calc(100% - 32px));
  background: var(--card);
  color: var(--fg);
}

dialog::backdrop { background: rgb(0 0 0 / 40%); }

.settings {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 20px;
}

.settings h2 { margin: 0; font-size: 20px; }

fieldset { border: 0; padding: 0; margin: 0; }
legend { margin-bottom: 6px; font-weight: 500; }
legend small { color: var(--muted); font-weight: 400; }

.seg { display: flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg label { flex: 1; }
.seg input { position: absolute; opacity: 0; }
.seg span { display: block; padding: 8px 0; text-align: center; cursor: pointer; }
.seg input:checked + span { background: var(--accent); color: #fff; }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }

.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.row input[type="number"] { width: 80px; padding: 6px 8px; font: inherit; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--fg); }
.row input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--accent); }

.stats { margin: 0; color: var(--muted); font-size: 14px; white-space: pre-line; }

.buttons { justify-content: flex-start; flex-wrap: wrap; }

.btn-small {
  padding: 7px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: transparent;
  font-size: 14px;
  cursor: pointer;
}

.btn-small.danger { color: var(--ko); }
.btn-small:disabled, .row input:disabled { opacity: .5; }
.btn-download { display: block; text-align: center; text-decoration: none; color: var(--accent); font-weight: 600; }
.btn-close { background: var(--fg); color: var(--bg); min-height: 48px; }
