:root {
  --paper: #fff3ea;
  --sheet: #fffdfb;
  --ink: #6b4038;
  --muted: #b08980;
  --line: #ffd7cc;
  --accent: #ff8d84;
  --accent-deep: #f06d78;
  --mint: #7dceb4;
  --sun: #ffe08a;
  color-scheme: light;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

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

body {
  font-family: "Yuanti SC", "PingFang SC", "Hiragino Maru Gothic ProN", "Microsoft YaHei", sans-serif;
  color: var(--ink);
  background: var(--paper);
}

body::before,
body::after {
  content: "";
  position: fixed;
  z-index: 0;
  pointer-events: none;
}

body::before {
  width: 220px;
  height: 220px;
  top: -28px;
  right: -24px;
  background: url("./assets/bird.png") center / contain no-repeat;
}

body::after {
  width: 250px;
  height: 250px;
  left: -46px;
  bottom: -36px;
  background: url("./assets/squirrel.png") center / contain no-repeat;
}

body[data-page="learn"]::before,
body[data-page="learn"]::after,
body[data-page="custom"]::before,
body[data-page="custom"]::after {
  content: none;
}

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

.page {
  position: relative;
  z-index: 1;
  width: min(1040px, calc(100% - 28px));
  margin: 0 auto;
  padding: 22px 0 40px;
}

.mast {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 14px;
}

.mascot {
  width: 88px;
  height: 88px;
  flex: none;
  object-fit: contain;
}

.mast h1 {
  margin: 0 0 4px;
  font-size: 32px;
  font-weight: 700;
  letter-spacing: 0.06em;
}

.eyebrow, .hint, .pinyin, .progress, .level-note {
  margin: 0;
}

.eyebrow {
  color: var(--accent-deep);
  font-size: 13px;
  letter-spacing: 0.18em;
}

.hint, .level-note {
  color: var(--muted);
  line-height: 1.6;
}

.level-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  margin-top: 22px;
}

.level-grid .level-card em {
  margin-top: 8px;
  color: var(--accent-deep);
  font-style: normal;
  font-size: 13px;
}

.level-animal {
  width: 84px;
  height: 84px;
  flex: none;
  object-fit: contain;
  pointer-events: none;
}

.level-copy { min-width: 0; }

.level-card {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 8px;
  min-height: 108px;
  padding: 12px 16px;
  border: 2px solid transparent;
  border-radius: 18px;
  background: #fff;
  color: inherit;
  text-align: left;
  text-decoration: none;
  cursor: pointer;
}

.level-card strong {
  display: block;
  font-size: 16px;
}

.level-card span {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
}

.level-card.active {
  border-color: var(--accent);
  background: #fff6f4;
}

.level-note { min-height: 1.6em; margin-top: 8px; font-size: 14px; }

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 18px;
  align-items: start;
  margin-top: 12px;
}

.stage {
  width: min(100%, calc(100dvh - 340px));
  margin-inline: auto;
}

.panel { min-width: 0; }

.custom-link,
.back {
  display: inline-flex;
  align-items: center;
  height: 40px;
  padding: 0 14px;
  border: 2px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--ink);
  text-decoration: none;
}

.custom-link { margin-left: auto; }

.back {
  flex: none;
  justify-content: center;
  width: 56px;
  height: 56px;
  padding: 0;
  border: 0;
  background: transparent;
}

.back img {
  width: 56px;
  height: 56px;
  object-fit: contain;
  pointer-events: none;
}

.composer {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  margin-top: 18px;
}

.field {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 14px;
  color: var(--muted);
}

.field input {
  height: 46px;
  padding: 0 14px;
  border: 2px solid var(--line);
  border-radius: 16px;
  background: #fff;
  outline: none;
}

.field input:focus { border-color: var(--accent); }

.btn {
  height: 42px;
  padding: 0 14px;
  border: 2px solid var(--line);
  border-radius: 999px;
  background: #fff;
  cursor: pointer;
}

.btn:disabled { cursor: default; opacity: 0.45; }

.btn.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.btn.primary:hover { background: var(--accent-deep); border-color: var(--accent-deep); }

.btn.ghost { background: transparent; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  padding: 0;
  flex: none;
}

.icon-btn svg {
  width: 22px;
  height: 22px;
  display: none;
}

.icon-btn.solo svg,
.icon-btn[data-icon="play"] svg[data-glyph="play"],
.icon-btn[data-icon="pause"] svg[data-glyph="pause"],
.icon-btn[data-icon="next"] svg[data-glyph="next"],
.icon-btn[data-icon="replay"] svg[data-glyph="replay"],
.icon-btn[data-icon="practice"] svg[data-glyph="practice"],
.icon-btn[data-icon="rewrite"] svg[data-glyph="rewrite"],
.icon-btn[data-icon="dictate"] svg[data-glyph="dictate"] {
  display: block;
}

.icon-btn svg path {
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-btn svg[data-glyph="play"] path,
.icon-btn svg[data-glyph="next"] path:nth-child(2) {
  fill: currentColor;
  stroke: none;
}

.btn.speak.icon-btn {
  width: 40px;
  height: 40px;
}

.glyph-line .btn.speak.icon-btn {
  width: 32px;
  height: 32px;
}

.glyph-line .btn.speak.icon-btn svg {
  width: 18px;
  height: 18px;
}

.hint { min-height: 1.5em; margin-top: 8px; font-size: 14px; }

.pager {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 14px;
  color: var(--muted);
  font-size: 14px;
}

.bank {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(46px, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.bank-char {
  height: 46px;
  border: 2px solid var(--line);
  border-radius: 14px;
  background: #fff;
  font-size: 20px;
  cursor: pointer;
}

.bank-char.current {
  border-color: var(--accent);
  background: #fff1ee;
}

.bank-char.learned {
  border-color: #b7eadc;
  color: #3d8f78;
}

.bank-char.wrong,
.chip.wrong .glyph {
  color: #c4533a;
}

.home-task {
  margin: 12px 0 0;
  color: var(--ink-soft, #6b4038);
  font-size: 15px;
}

.review-link {
  display: inline-flex;
  align-items: center;
  margin-top: 8px;
  padding: 8px 14px;
  border-radius: 999px;
  background: #fff1ee;
  color: #c4533a;
  font-weight: 700;
  text-decoration: none;
}

.review-link[hidden] { display: none; }

.done-card {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 5;
  width: min(420px, calc(100% - 32px));
  padding: 16px 18px;
  border-radius: 18px;
  background: #fff;
  box-shadow: 0 12px 40px rgba(80, 40, 30, 0.18);
  transform: translateX(-50%);
}

.done-card[hidden] { display: none; }

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

.done-card p {
  margin: 8px 0 12px;
}

.chars {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  margin-bottom: 8px;
}

.chip {
  position: relative;
  flex: 0 0 56px;
  width: 56px;
  height: 56px;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
}

.mini-grid {
  position: absolute;
  inset: 0;
  border: 2px solid #ffc7be;
  border-radius: 14px;
  background:
    linear-gradient(#ffc7be, #ffc7be) center / 100% 1px no-repeat,
    linear-gradient(#ffc7be, #ffc7be) center / 1px 100% no-repeat,
    var(--sheet);
}

.chip.current .mini-grid { border-color: var(--accent); }

.chip.learned .glyph { color: #3d8f78; }

.glyph {
  position: relative;
  z-index: 1;
  font-size: 26px;
  line-height: 56px;
  color: #d7b8b2;
}

.chip.done .glyph,
.chip.current .glyph { color: var(--ink); }

.meta {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 12px;
  margin: 8px 0 10px;
}

.reading-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.reading {
  margin: 0;
  font-size: 26px;
  letter-spacing: 0.08em;
}

.btn.speak {
  height: 36px;
  border-color: #ffd48a;
  background: #fff6df;
  color: #c47a2a;
}

.pinyin { margin-top: 2px; color: var(--muted); }

.progress { text-align: right; line-height: 1.5; }

.sheet-wrap { display: flex; justify-content: center; }

.sheet {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  overflow: hidden;
  border: 4px solid #ffd0c6;
  border-radius: 28px;
  background: var(--sheet);
}

.grid, #writer { position: absolute; inset: 0; }

.grid {
  z-index: 0;
  display: block;
  width: 100%;
  height: 100%;
}

#writer { z-index: 1; cursor: default; pointer-events: none; touch-action: none; }

#writer,
#writer svg { background: transparent; touch-action: none; }

body.is-practice #writer,
body.is-practice #writer svg {
  cursor: crosshair;
  pointer-events: auto;
}

#writer svg { display: block; }

.grid-paper {
  fill: none;
  stroke: #f08b84;
  stroke-width: 2px;
  vector-effect: non-scaling-stroke;
}

.grid-guide {
  stroke: #f3b0a8;
  stroke-width: 3px;
  stroke-dasharray: 8 6;
  vector-effect: non-scaling-stroke;
}

.sheet-msg {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: grid;
  place-items: center;
  margin: 0;
  padding: 24px;
  text-align: center;
  color: var(--muted);
  background: rgba(255, 253, 251, 0.9);
}

.sheet-msg[hidden] { display: none; }

.voice-name {
  margin: 8px auto 0;
  font-size: 13px;
  text-align: center;
}

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

.glyph-note {
  margin: 12px 0 0;
  padding: 12px 14px;
  border: 2px solid #ffd0c6;
  border-radius: 18px;
  background: #fff;
  text-align: left;
}

.glyph-line {
  display: grid;
  grid-template-columns: 3em 1fr;
  gap: 8px;
  margin: 0;
  line-height: 1.55;
}

.glyph-line + .glyph-line { margin-top: 6px; }

.glyph-line span {
  color: #c47a2a;
  font-size: 13px;
}

.glyph-line strong { font-weight: 500; }

.glyph-line.sentence-line { grid-template-columns: 3em minmax(0, 1fr) auto; }

.glyph-line.sentence-line .btn.speak {
  height: 28px;
  padding: 0 10px;
  font-size: 13px;
}

.strokes {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  min-height: 36px;
}

.stroke {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 58px;
  min-height: 58px;
  padding: 4px 8px 6px;
  border: 2px solid var(--line);
  border-radius: 16px;
  background: #fff;
  color: #c4a49e;
  font-size: 13px;
  line-height: 1;
}

.stroke-form {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 2px;
  min-height: 28px;
  font-family: "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Songti SC", serif;
}

.stroke-glyph { font-size: 26px; line-height: 1; }

.stroke-glyph.alt { font-size: 16px; opacity: 0.72; }

.stroke-name { margin-top: 4px; font-size: 11px; }

.stroke.done { color: var(--ink); background: #fff8f5; }

.stroke.current {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.controls {
  display: flex;
  justify-content: center;
  flex-wrap: nowrap;
  gap: 8px;
  margin: 12px 0;
}

.tools {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 14px 22px;
  margin-top: 8px;
  color: var(--muted);
  font-size: 14px;
}

.speed, .check { display: flex; align-items: center; gap: 8px; }

.check[hidden] { display: none; }

.speed input[type="range"] { width: 120px; accent-color: var(--accent); }

.check input { accent-color: var(--accent); }

body[data-page="custom"] .stage {
  width: min(100%, 460px, calc(100dvh - 280px));
  margin-top: 8px;
}

@media (max-width: 1100px) {
  body::before,
  body::after {
    content: none;
  }
}

@media (max-width: 640px) {
  .custom-link {
    margin-left: 0;
    flex-basis: 100%;
  }
}

@media (min-width: 720px) {
  .level-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (min-width: 860px) {
  .workspace {
    grid-template-columns: minmax(280px, 460px) minmax(300px, 1fr);
    gap: 28px;
  }

  .stage { width: min(100%, calc(100dvh - 210px)); }
}

@media (max-width: 859px) {
  .stage { width: 100%; }
  .meta { align-items: flex-start; }
  .progress { text-align: left; }
  .tools { justify-content: flex-start; }
}

@media (min-width: 768px) and (max-width: 1400px) and (min-height: 640px) and (max-height: 1400px) {
  html:has(body[data-page="learn"]),
  body[data-page="learn"] {
    height: 100dvh;
    overflow: hidden;
  }

  body[data-page="learn"] .page {
    display: grid;
    grid-template-rows: auto auto minmax(0, 1fr);
    width: min(1180px, calc(100% - 20px));
    height: 100dvh;
    padding: 10px 0;
    overflow: hidden;
  }

  body[data-page="learn"] .mast { gap: 10px; }

  body[data-page="learn"] .back,
  body[data-page="learn"] .back img {
    width: 48px;
    height: 48px;
  }

  body[data-page="learn"] .mast h1 {
    margin: 0;
    font-size: 24px;
  }

  body[data-page="learn"] .level-note {
    display: -webkit-box;
    margin-top: 4px;
    overflow: hidden;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    min-height: 0;
    font-size: 13px;
    line-height: 1.4;
  }

  body[data-page="learn"] .workspace {
    grid-template-columns: minmax(0, 1.2fr) minmax(260px, 0.8fr);
    gap: 16px;
    align-items: stretch;
    height: 100%;
    min-height: 0;
    margin-top: 8px;
    overflow: hidden;
  }

  body[data-page="learn"] .stage {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    width: auto;
    height: 100%;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
  }

  body[data-page="learn"] .stage > * { min-width: 0; }

  body[data-page="learn"] .chars {
    gap: 6px;
    margin-bottom: 6px;
  }

  body[data-page="learn"] .chip {
    flex-basis: 40px;
    width: 40px;
    height: 40px;
  }

  body[data-page="learn"] .glyph {
    font-size: 20px;
    line-height: 40px;
  }

  body[data-page="learn"] .sheet-wrap {
    container-type: size;
    align-items: center;
    height: 100%;
    min-height: 0;
  }

  body[data-page="learn"] .sheet {
    width: min(100cqw, 100cqh);
    height: min(100cqw, 100cqh);
    aspect-ratio: auto;
    border-width: 3px;
    border-radius: 20px;
  }

  body[data-page="learn"] .meta { margin: 4px 0; }

  body[data-page="learn"] .reading { font-size: 22px; }

  body[data-page="learn"] .strokes {
    gap: 4px;
    min-height: 0;
  }

  body[data-page="learn"] .stroke {
    min-width: 42px;
    min-height: 40px;
    padding: 2px 4px 3px;
    border-radius: 12px;
  }

  body[data-page="learn"] .stroke-form { min-height: 18px; }

  body[data-page="learn"] .stroke-glyph { font-size: 18px; }

  body[data-page="learn"] .controls {
    flex-wrap: nowrap;
    margin: 4px 0;
  }

  body[data-page="learn"] .controls .icon-btn,
  body[data-page="learn"] .pager .icon-btn {
    width: 40px;
    height: 40px;
  }

  body[data-page="learn"] .tools {
    gap: 8px 14px;
    margin-top: 2px;
    font-size: 13px;
  }

  body[data-page="learn"] .glyph-note {
    margin-top: 6px;
    padding: 8px 10px;
  }

  body[data-page="learn"] .glyph-line,
  body[data-page="learn"] .glyph-line + .glyph-line {
    margin-top: 2px;
    font-size: 13px;
    line-height: 1.35;
  }

  body[data-page="learn"] .panel {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
    overflow: hidden;
  }

  body[data-page="learn"] .pager { margin-top: 0; }

  body[data-page="learn"] .bank {
    flex: none;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    grid-template-rows: repeat(4, 52px);
    align-content: start;
    gap: 6px;
    min-height: 0;
    margin-top: 8px;
  }

  body[data-page="learn"] .bank-char {
    height: 52px;
    min-height: 0;
    font-size: 20px;
  }

  body[data-page="learn"] .hint {
    min-height: 0;
    margin-top: 4px;
  }
}
