:root {
  --bg:        #f6f0e6;
  --bg-2:      #f1e8d9;
  --card:      #fffdfa;
  --ink:       #2b2620;
  --ink-soft:  #4d463d;
  --muted:     #978d7f;
  --hair:      #ebe1d2;
  --hair-2:    #e0d4c1;
  --clay:      #c0573b;
  --clay-deep: #a4452c;
  --clay-soft: #f7e7dc;
  --clay-tint: #fbf0e8;
  --amber:     #dd9a44;
  --green:     #4f8a5b;

  --shadow-sm: 0 1px 2px rgba(43,38,32,.05), 0 1px 3px rgba(43,38,32,.05);
  --shadow-md: 0 6px 22px rgba(43,38,32,.09);
  --shadow-fab: 0 8px 20px rgba(176,72,44,.34);

  --serif: "Fraunces", "Iowan Old Style", Georgia, "Songti SC", serif;
  --sans:  "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;

  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
button, select, input { touch-action: manipulation; font-family: inherit; }

html, body { margin: 0; height: 100%; }
body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  color: var(--ink);
  font-family: var(--sans);
  background:
    radial-gradient(120% 80% at 85% -10%, var(--bg-2) 0%, rgba(241,232,217,0) 60%),
    var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* ---------------- header ---------------- */
.app-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(12px + var(--safe-t)) 18px 12px;
  background: rgba(246,240,230,.82);
  backdrop-filter: saturate(140%) blur(12px);
  -webkit-backdrop-filter: saturate(140%) blur(12px);
  border-bottom: 1px solid var(--hair);
  z-index: 5;
}
.brand { display: flex; align-items: baseline; gap: 8px; white-space: nowrap; }
.brand .logo { font-family: var(--serif); font-weight: 600; font-size: 22px; letter-spacing: -.01em; }
.brand .badge {
  font-size: 10.5px; font-weight: 700; letter-spacing: .06em; color: #fff;
  background: linear-gradient(160deg, var(--clay), var(--clay-deep));
  padding: 2px 6px; border-radius: 6px; transform: translateY(-2px);
}
.brand .zh { font-size: 12.5px; font-weight: 500; color: var(--muted); }

.track-wrap { position: relative; min-width: 0; flex: 0 1 auto; }
.track-select {
  appearance: none; -webkit-appearance: none;
  max-width: 60vw; padding: 9px 32px 9px 14px;
  border: 1px solid var(--hair-2); border-radius: 11px;
  background: var(--card); color: var(--ink);
  font-size: 14px; font-weight: 500; box-shadow: var(--shadow-sm);
  cursor: pointer; text-overflow: ellipsis;
}
.track-wrap .chev {
  position: absolute; right: 11px; top: 50%; transform: translateY(-50%);
  width: 16px; height: 16px; pointer-events: none;
  fill: none; stroke: var(--muted); stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round;
}

/* ---------------- generic view ---------------- */
.view { flex: 1 1 auto; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }

/* ---------------- sentence list ---------------- */
.sentences { padding: 14px 14px 18px; scroll-padding: 80px 0; }
.hint { color: var(--muted); text-align: center; padding: 48px 16px; font-size: 15px; }

.sentence {
  position: relative; display: flex; gap: 13px;
  padding: 16px 14px 17px 16px; margin: 10px 0;
  background: var(--card); border: 1px solid var(--hair); border-radius: 16px;
  box-shadow: var(--shadow-sm); cursor: pointer; overflow: hidden;
  transition: transform .12s ease, box-shadow .2s ease, border-color .2s ease, background .2s ease;
}
.sentence::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px;
  background: var(--clay); transform: scaleY(0);
  transition: transform .22s cubic-bezier(.4,0,.2,1);
}
.sentence:active { transform: scale(.99); }
@media (hover: hover) { .sentence:hover { box-shadow: var(--shadow-md); border-color: var(--hair-2); } }

.sentence .idx {
  flex: 0 0 auto; width: 26px; height: 26px; margin-top: 2px; border-radius: 50%;
  background: var(--bg-2); color: var(--muted); font-size: 12.5px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; transition: background .2s, color .2s;
}
.sentence .body { flex: 1 1 auto; min-width: 0; }
.sentence .fr {
  font-family: var(--serif); font-size: 20px; line-height: 1.52; letter-spacing: -.005em;
  color: var(--ink-soft); word-break: break-word; transition: color .2s;
}
.sentence .zh {
  max-height: 0; opacity: 0; overflow: hidden; margin-top: 0;
  font-size: 14.5px; line-height: 1.5; color: var(--clay-deep);
  border-left: 2px solid var(--clay-soft); padding-left: 0;
  transition: max-height .26s ease, opacity .22s ease, margin .26s ease, padding .26s ease;
}
.sentence.show-tr .zh { max-height: 320px; opacity: 1; margin-top: 9px; padding-left: 10px; }

.sentence .actions { flex: 0 0 auto; display: flex; flex-direction: column; align-items: center; gap: 8px; }
.tr-toggle {
  width: 30px; height: 30px; border-radius: 9px; border: 1px solid var(--hair-2);
  background: var(--card); color: var(--muted);
  font-family: var(--serif); font-size: 14px; font-weight: 600; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; transition: all .15s;
}
.sentence.show-tr .tr-toggle { background: var(--clay); border-color: var(--clay); color: #fff; }

.sentence .eq { display: none; gap: 2px; align-items: flex-end; height: 14px; }
.sentence .eq span { width: 3px; height: 100%; background: var(--clay); border-radius: 2px; transform-origin: bottom; animation: eq 900ms ease-in-out infinite; }
.sentence .eq span:nth-child(2) { animation-delay: .2s; }
.sentence .eq span:nth-child(3) { animation-delay: .4s; }
@keyframes eq { 0%,100% { transform: scaleY(.35); } 50% { transform: scaleY(1); } }
body.playing .sentence.active .eq { display: flex; }

.sentence .prog {
  position: absolute; left: 0; bottom: 0; height: 3px; width: 0%;
  background: linear-gradient(90deg, var(--clay), var(--amber)); border-radius: 0 3px 3px 0;
  transition: width .12s linear;
}
.sentence.active { background: var(--clay-tint); border-color: var(--clay-soft); box-shadow: var(--shadow-md); }
.sentence.active::before { transform: scaleY(1); }
.sentence.active .idx { background: var(--clay); color: #fff; }
.sentence.active .fr { color: var(--ink); font-weight: 500; }

@media (prefers-reduced-motion: reduce) {
  .sentence, .sentence::before, .sentence .prog, .sentence .zh { transition: none; }
  .sentence .eq span { animation: none; }
}

/* ---------------- audio controls ---------------- */
.controls {
  flex: 0 0 auto;
  background: linear-gradient(180deg, rgba(255,253,250,.7), var(--card) 38%);
  border-top: 1px solid var(--hair);
  padding: 10px 16px 12px; box-shadow: 0 -8px 24px rgba(43,38,32,.05); z-index: 4;
}
.row { display: flex; align-items: center; gap: 12px; }
.row + .row { margin-top: 11px; }
.row.time .t { font-size: 11.5px; font-weight: 500; font-variant-numeric: tabular-nums; color: var(--muted); width: 38px; text-align: center; }

.seek { flex: 1 1 auto; height: 22px; background: transparent; cursor: pointer; -webkit-appearance: none; appearance: none; }
.seek::-webkit-slider-runnable-track { height: 5px; border-radius: 999px; background: linear-gradient(var(--clay),var(--clay)) 0/var(--fill,0%) 100% no-repeat, var(--hair-2); }
.seek::-moz-range-track { height: 5px; border-radius: 999px; background: var(--hair-2); }
.seek::-moz-range-progress { height: 5px; border-radius: 999px; background: var(--clay); }
.seek::-webkit-slider-thumb { -webkit-appearance: none; appearance: none; width: 15px; height: 15px; margin-top: -5px; border-radius: 50%; background: #fff; border: 2.5px solid var(--clay); box-shadow: 0 1px 4px rgba(164,69,44,.4); }
.seek::-moz-range-thumb { width: 15px; height: 15px; border-radius: 50%; background: #fff; border: 2.5px solid var(--clay); box-shadow: 0 1px 4px rgba(164,69,44,.4); }

.row.primary { justify-content: center; gap: 22px; }
.ctrl { width: 50px; height: 50px; border: none; border-radius: 50%; background: transparent; color: var(--ink); display: inline-flex; align-items: center; justify-content: center; cursor: pointer; transition: background .15s, transform .08s; }
.ctrl svg { width: 25px; height: 25px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.ctrl:active { transform: scale(.92); }
@media (hover: hover) { .ctrl:hover { background: var(--clay-soft); } }
.ctrl.play { width: 64px; height: 64px; background: radial-gradient(120% 120% at 30% 20%, #cf6347, var(--clay) 55%, var(--clay-deep)); color: #fff; box-shadow: var(--shadow-fab); }
.ctrl.play svg { width: 28px; height: 28px; fill: currentColor; stroke: none; }
.ctrl.play:active { transform: scale(.95); }

.row.toggles { flex-wrap: wrap; justify-content: center; gap: 9px; }
.chip.toggle { display: inline-flex; align-items: center; gap: 7px; border: 1px solid var(--hair-2); background: var(--card); color: var(--ink-soft); border-radius: 999px; padding: 9px 15px 9px 12px; font-size: 13.5px; font-weight: 500; cursor: pointer; box-shadow: var(--shadow-sm); transition: background .16s, color .16s, border-color .16s; }
.chip.toggle svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.chip.toggle[aria-pressed="true"] { background: linear-gradient(160deg, var(--clay), var(--clay-deep)); border-color: var(--clay-deep); color: #fff; box-shadow: 0 4px 12px rgba(176,72,44,.28); }

.speeds { display: inline-flex; align-items: center; gap: 2px; background: var(--bg-2); border: 1px solid var(--hair-2); border-radius: 999px; padding: 3px; }
.speed { border: none; background: transparent; cursor: pointer; border-radius: 999px; padding: 6px 11px; font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; color: var(--muted); transition: color .15s, background .15s; }
.speed.active { background: var(--ink); color: #fff; box-shadow: var(--shadow-sm); }

/* ---------------- tab bar ---------------- */
.tabbar {
  flex: 0 0 auto; display: flex; align-items: stretch;
  background: rgba(255,253,250,.92); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--hair);
  padding-bottom: var(--safe-b); z-index: 6;
}
.tab {
  position: relative; flex: 1 1 0; display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 9px 4px 8px; border: none; background: transparent; cursor: pointer; color: var(--muted);
  font-size: 11px; font-weight: 600; transition: color .15s;
}
.tab svg { width: 23px; height: 23px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.tab.active { color: var(--clay); }
.tab.active::before { content: ""; position: absolute; top: 0; left: 50%; transform: translateX(-50%); width: 30px; height: 3px; border-radius: 0 0 3px 3px; background: var(--clay); }
.tab-count { position: absolute; top: 5px; right: calc(50% - 24px); min-width: 16px; height: 16px; padding: 0 4px; border-radius: 999px; background: var(--clay); color: #fff; font-size: 10px; font-style: normal; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; }

/* ---------------- selection chip ---------------- */
.sel-add {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--safe-b) + 132px); z-index: 30;
  max-width: 86vw; border: none; border-radius: 999px;
  background: linear-gradient(160deg, var(--clay), var(--clay-deep)); color: #fff;
  font-size: 14px; font-weight: 600; padding: 11px 18px; box-shadow: var(--shadow-fab);
  cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.sel-add:active { transform: translateX(-50%) scale(.96); }

/* ---------------- vocab ---------------- */
.vocab { padding: 14px 14px calc(18px + var(--safe-b)); }
.vocab-add {
  position: sticky; top: 0; z-index: 2; display: flex; gap: 10px; align-items: center;
  padding: 6px 2px 12px; background: linear-gradient(var(--bg) 70%, rgba(246,240,230,0));
}
.vocab-add input {
  flex: 1 1 auto; min-width: 0; height: 46px; padding: 0 16px;
  border: 1px solid var(--hair-2); border-radius: 13px; background: var(--card);
  font-size: 16px; color: var(--ink); box-shadow: var(--shadow-sm);
}
.vocab-add input:focus { outline: 2px solid var(--clay-soft); border-color: var(--clay); }
.btn-add { flex: 0 0 auto; width: 46px; height: 46px; border: none; border-radius: 13px; background: linear-gradient(160deg, var(--clay), var(--clay-deep)); color: #fff; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; box-shadow: var(--shadow-fab); }
.btn-add svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; }
.btn-add:active { transform: scale(.94); }
.vocab-tip { margin: 2px 2px 12px; font-size: 12.5px; color: var(--muted); line-height: 1.5; }

.vocab-list { display: flex; flex-direction: column; gap: 10px; }
.vocab-empty { text-align: center; color: var(--muted); padding: 40px 16px; font-size: 15px; line-height: 1.6; }
.vocab-card {
  display: flex; align-items: center; gap: 10px; padding: 13px 12px 13px 16px;
  background: var(--card); border: 1px solid var(--hair); border-radius: 14px; box-shadow: var(--shadow-sm);
}
.vc-main { flex: 1 1 auto; min-width: 0; }
.vc-word { font-family: var(--serif); font-size: 19px; font-weight: 600; color: var(--ink); word-break: break-word; }
.vc-zh { margin-top: 3px; font-size: 13.5px; color: var(--muted); word-break: break-word; }
.vc-zh.editable { cursor: text; }
.vc-zh.loading { color: var(--clay); font-style: italic; }
.vc-btn { flex: 0 0 auto; width: 40px; height: 40px; border: 1px solid var(--hair-2); border-radius: 11px; background: var(--card); color: var(--ink-soft); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; transition: all .15s; }
.vc-btn svg { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.vc-btn.speak:active { background: var(--clay); border-color: var(--clay); color: #fff; }
.vc-btn.del { color: #b24a3a; }
.vc-btn.del:active { background: #fbe9e5; }
.vc-btn.speaking { background: var(--clay); border-color: var(--clay); color: #fff; }

/* ---------------- quiz ---------------- */
.quiz { padding: 18px 16px calc(18px + var(--safe-b)); }
.quiz-content { max-width: 560px; margin: 0 auto; }
.quiz-hero { text-align: center; padding: 28px 8px 8px; }
.quiz-hero h2 { font-family: var(--serif); font-weight: 600; font-size: 26px; margin: 0 0 8px; color: var(--ink); }
.quiz-hero p { color: var(--muted); font-size: 14.5px; line-height: 1.6; margin: 0 0 22px; }
.btn-primary { border: none; border-radius: 14px; background: linear-gradient(160deg, var(--clay), var(--clay-deep)); color: #fff; font-size: 16px; font-weight: 600; padding: 14px 26px; cursor: pointer; box-shadow: var(--shadow-fab); }
.btn-primary:active { transform: scale(.97); }
.btn-ghost { border: 1px solid var(--hair-2); border-radius: 14px; background: var(--card); color: var(--ink-soft); font-size: 15px; font-weight: 600; padding: 13px 22px; cursor: pointer; box-shadow: var(--shadow-sm); }

.quiz-progress { display: flex; align-items: center; gap: 10px; margin: 4px 0 18px; }
.quiz-progress .bar { flex: 1 1 auto; height: 7px; border-radius: 999px; background: var(--hair-2); overflow: hidden; }
.quiz-progress .bar > i { display: block; height: 100%; background: linear-gradient(90deg, var(--clay), var(--amber)); border-radius: 999px; transition: width .3s ease; }
.quiz-progress .count { font-size: 12.5px; font-weight: 600; color: var(--muted); font-variant-numeric: tabular-nums; }

.quiz-card { background: var(--card); border: 1px solid var(--hair); border-radius: 18px; box-shadow: var(--shadow-md); padding: 26px 20px; text-align: center; }
.quiz-prompt-label { font-size: 12.5px; color: var(--muted); letter-spacing: .04em; text-transform: uppercase; }
.quiz-prompt { font-size: 23px; font-weight: 600; color: var(--ink); margin: 8px 0 6px; line-height: 1.4; }
.quiz-speak { margin: 4px auto 18px; width: 46px; height: 46px; border-radius: 50%; border: 1px solid var(--hair-2); background: var(--bg-2); color: var(--clay); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; }
.quiz-speak svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.quiz-input { width: 100%; height: 52px; text-align: center; border: 2px solid var(--hair-2); border-radius: 13px; background: #fff; font-family: var(--serif); font-size: 22px; color: var(--ink); }
.quiz-input:focus { outline: none; border-color: var(--clay); }
.quiz-input.ok { border-color: var(--green); background: #eef6ef; }
.quiz-input.no { border-color: var(--clay); background: var(--clay-tint); }
.quiz-feedback { min-height: 24px; margin: 12px 0 4px; font-size: 14.5px; font-weight: 600; }
.quiz-feedback.ok { color: var(--green); }
.quiz-feedback.no { color: var(--clay-deep); }
.quiz-feedback .ans { font-family: var(--serif); }
.quiz-actions { margin-top: 16px; display: flex; gap: 10px; justify-content: center; }
.quiz-actions .btn-primary, .quiz-actions .btn-ghost { flex: 1 1 0; }
.quiz-score { font-family: var(--serif); font-size: 40px; font-weight: 600; color: var(--clay); text-align: center; margin: 10px 0 4px; }

/* ---------------- larger screens ---------------- */
@media (min-width: 720px) {
  .sentences, .vocab, .controls > .row { max-width: 760px; margin-left: auto; margin-right: auto; width: 100%; }
  .controls { display: flex; flex-direction: column; }
  .sentence .fr { font-size: 21px; }
}
