:root {
  --bg: #f5f5f4;
  --fg: #1c1917;
  --muted: #78716c;
  --border: #e7e5e4;
  --accent: #2563eb;
  --accent-fg: #ffffff;
  --card: #ffffff;
  --hover: #fafaf9;
  --panel: #ffffff;
  --sidebar-w: 240px;

  /* Overlay that keeps header text legible on top of header.jpg */
  --head-scrim: linear-gradient(
    100deg,
    rgba(255, 255, 255, 0.96) 0%,
    rgba(255, 255, 255, 0.93) 38%,
    rgba(255, 255, 255, 0.74) 66%,
    rgba(255, 255, 255, 0.45) 100%
  );
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1917;
    --fg: #f5f5f4;
    --muted: #a8a29e;
    --border: #44403c;
    --accent: #3b82f6;
    --accent-fg: #ffffff;
    --card: #292524;
    --hover: #332e2b;
    --panel: #221f1d;

    --head-scrim: linear-gradient(
      100deg,
      rgba(28, 25, 23, 0.95) 0%,
      rgba(28, 25, 23, 0.92) 38%,
      rgba(28, 25, 23, 0.78) 66%,
      rgba(28, 25, 23, 0.55) 100%
    );
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.6 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ================= sidebar ================= */

.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: var(--sidebar-w);
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 20px 12px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  z-index: 30;
}

.brand {
  display: block;
  padding: 6px 10px 18px;
  color: var(--fg);
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  text-decoration: none;
}

.sidebar nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-link {
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 9px 10px;
  border-radius: 8px;
  color: var(--fg);
  text-decoration: none;
  line-height: 1.35;
}

.nav-link:hover { background: var(--hover); }

.nav-link.active {
  background: var(--accent);
  color: var(--accent-fg);
}

.nav-label { font-size: 0.92rem; }

.nav-desc {
  font-size: 0.75rem;
  color: var(--muted);
}

.nav-link.active .nav-desc {
  color: color-mix(in srgb, var(--accent-fg) 78%, transparent);
}

/* mobile drawer */

.nav-toggle {
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 40;
  display: none;
  place-items: center;
  width: 38px;
  height: 38px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  color: var(--fg);
  cursor: pointer;
}

.nav-backdrop {
  position: fixed;
  inset: 0;
  z-index: 25;
  background: rgba(0, 0, 0, 0.4);
}

[hidden] { display: none !important; }

/* ================= content ================= */

.content {
  margin-left: var(--sidebar-w);
  padding: 0 24px 64px;
}

.page {
  max-width: 760px;
  margin: 0 auto;
}

/*
 * Header banner. The artwork is busy, so a scrim sits between it and the text:
 * near-opaque on the left where the heading and controls are, fading to the
 * right so the illustration still reads.
 */
.page-head {
  position: relative;
  margin: 28px 0 0;
  padding: 26px 26px 22px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background-image: var(--head-scrim), url("header.jpg");
  background-size: cover;
  background-position: center 30%;
  background-repeat: no-repeat;
}

h1 {
  margin: 0 0 4px;
  font-size: 1.75rem;
  letter-spacing: -0.02em;
}

.sub {
  margin: 0 0 20px;
  color: var(--muted);
  font-size: 0.9rem;
}

/* ================= toolbar ================= */

.toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
}

#search {
  flex: 1;
  min-width: 0;
  font: inherit;
  padding: 9px 13px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  color: var(--fg);
}

.count {
  color: var(--muted);
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.speed-bar { margin-top: 10px; }

.seed-bar {
  margin-top: 12px;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 10px;
}

.seed-bar .param input { width: 11rem; }

.speed-label {
  color: var(--muted);
  font-size: 0.85rem;
}

.speeds {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.speed {
  font: inherit;
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
  padding: 4px 11px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--card);
  color: var(--muted);
  cursor: pointer;
}

.speed:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.speed[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-fg);
}

/* ================= parameter controls ================= */

.params {
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 12px;
}

.param {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.param > span {
  color: var(--muted);
  font-size: 0.75rem;
}

.param input {
  width: 6.5rem;
  font: inherit;
  font-size: 0.9rem;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  color: var(--fg);
}

.param-actions {
  display: flex;
  gap: 8px;
  margin-left: auto;
}

.btn {
  font: inherit;
  font-size: 0.85rem;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  color: var(--fg);
  cursor: pointer;
  white-space: nowrap;
}

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

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

.btn-primary:hover {
  color: var(--accent-fg);
  filter: brightness(1.08);
}

.status-line { display: block; margin: 14px 0 0; }
.status-line.error { color: #dc2626; }

@media (prefers-color-scheme: dark) {
  .status-line.error { color: #f87171; }
}

/* ================= cloze blanks ================= */

.cloze { line-height: 1.95; }

/* gap:0 so the per-letter underlines butt together into one continuous rule */
.blank-group {
  display: inline-flex;
  gap: 0;
  margin: 0 1px;
  vertical-align: baseline;
}

/*
 * Empty blanks use a uniform width; once a letter is typed, practice.js sets
 * an inline width equal to that glyph's natural width in the sentence font,
 * so a completed word is spaced exactly like the surrounding text.
 */
.blank {
  width: 0.62em;
  height: 1.35em;
  padding: 0;
  border: none;
  border-bottom: 2px solid var(--muted);
  border-radius: 0;
  background: transparent;
  color: var(--fg);
  font: inherit;
  text-align: center;
}

.blank:focus {
  outline: none;
  border-bottom-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
}

.blank.ok {
  color: #15803d;
  border-bottom-color: #15803d;
}

.blank.bad {
  color: #dc2626;
  border-bottom-color: #dc2626;
}

@media (prefers-color-scheme: dark) {
  .blank.ok { color: #4ade80; border-bottom-color: #4ade80; }
  .blank.bad { color: #f87171; border-bottom-color: #f87171; }
}

.cloze-row .num { padding-top: 9px; }

/* ================= sentence list ================= */

.list {
  padding: 0;
  list-style: none;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--card);
  overflow: hidden;
}

.row {
  display: grid;
  grid-template-columns: 2.5rem 1fr auto;
  align-items: start;
  gap: 12px;
  padding: 14px 16px;
  border-top: 1px solid var(--border);
}

.row:first-child { border-top: none; }
.row:hover { background: var(--hover); }

.row.playing {
  background: color-mix(in srgb, var(--accent) 10%, transparent);
}

.num {
  color: var(--muted);
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
  padding-top: 5px;
}

.text {
  margin: 0;
  padding-top: 1px;
}

.text mark {
  background: color-mix(in srgb, var(--accent) 28%, transparent);
  color: inherit;
  border-radius: 3px;
  padding: 0 1px;
}

/* ================= play button ================= */

.play {
  flex: none;
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--bg);
  color: var(--fg);
  cursor: pointer;
}

.play:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.play.is-playing {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-fg);
}

.play svg { width: 15px; height: 15px; fill: currentColor; }
.play .icon-pause { display: none; }
.play.is-playing .icon-play { display: none; }
.play.is-playing .icon-pause { display: block; }

/* ================= exercise sections ================= */

.exercise-section { margin: 36px 0 0; }

.section-head {
  display: flex;
  align-items: baseline;
  gap: 10px;
}

.section-head h2 {
  margin: 0;
  font-size: 1.1rem;
  letter-spacing: -0.01em;
}

.section-range {
  color: var(--muted);
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
}

.exercise-section .sub { margin: 2px 0 12px; }

.row-body { min-width: 0; }

.row-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.muted-text { color: var(--muted); font-size: 0.9rem; }

/* ================= API key panel ================= */

.key-panel {
  margin: 0 0 16px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--card);
}

.key-panel summary {
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
}

.key-note {
  margin: 10px 0;
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.5;
}

.key-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.key-row input {
  flex: 1;
  min-width: 12rem;
  font: inherit;
  font-size: 0.9rem;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--fg);
}

#key-status { display: block; margin: 10px 0 0; }
#key-status.error { color: #dc2626; }

/* ================= recording ================= */

.record {
  flex: none;
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--bg);
  color: var(--fg);
  cursor: pointer;
}

.record:hover { border-color: #dc2626; color: #dc2626; }
.record:disabled { opacity: 0.5; cursor: default; }

.record.is-recording {
  background: #dc2626;
  border-color: #dc2626;
  color: #ffffff;
  animation: pulse 1.4s ease-in-out infinite;
}

@keyframes pulse {
  50% { opacity: 0.55; }
}

@media (prefers-reduced-motion: reduce) {
  .record.is-recording { animation: none; }
}

.row.recording { background: color-mix(in srgb, #dc2626 8%, transparent); }

.result:empty { display: none; }

.result {
  margin-top: 8px;
  padding: 9px 12px;
  border-radius: 8px;
  background: var(--bg);
  font-size: 0.9rem;
  color: var(--muted);
}

.result .heard { margin: 0; }
.result .verdict { margin: 4px 0 0; font-size: 0.78rem; opacity: 0.85; }

.result-ok {
  background: color-mix(in srgb, #15803d 12%, transparent);
  color: #15803d;
}

.result-off {
  background: color-mix(in srgb, #ea580c 12%, transparent);
  color: #ea580c;
}

.result-error {
  background: color-mix(in srgb, #dc2626 10%, transparent);
  color: #dc2626;
}

.word-off {
  text-decoration: underline wavy currentColor;
  text-underline-offset: 3px;
  font-weight: 600;
}

@media (prefers-color-scheme: dark) {
  .result-ok { color: #4ade80; }
  .result-off { color: #fb923c; }
  .result-error, #key-status.error { color: #f87171; }
}

/* ================= chinese translation fold ================= */

.translation { margin-top: 5px; }

.translation summary {
  display: inline-block;
  color: var(--muted);
  font-size: 0.76rem;
  cursor: pointer;
  user-select: none;
  list-style-position: outside;
}

.translation summary:hover { color: var(--accent); }
.translation summary::marker { font-size: 0.7em; }

.translation-text {
  margin: 6px 0 2px;
  padding-left: 10px;
  border-left: 2px solid var(--border);
  color: var(--fg);
  font-size: 0.92rem;
  line-height: 1.75;
  /* Windows/macOS/Linux CJK faces; system-ui alone can fall back badly. */
  font-family: "Microsoft YaHei", "PingFang SC", "Hiragino Sans GB",
    "Noto Sans CJK SC", "Source Han Sans SC", system-ui, sans-serif;
}

/* ================= dictionary popover (currently unused) ================= */

.define {
  position: absolute;
  z-index: 60;
  width: min(360px, calc(100vw - 24px));
  max-height: 340px;
  overflow-y: auto;
  padding: 14px 16px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.16);
  font-size: 0.88rem;
  line-height: 1.5;
}

.define-close {
  position: absolute;
  top: 6px;
  right: 8px;
  padding: 0 4px;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 1.15rem;
  line-height: 1;
  cursor: pointer;
}

.define-close:hover { color: var(--fg); }

.define-word {
  margin: 0 0 8px;
  padding-right: 20px;
  font-weight: 600;
  font-size: 0.98rem;
}

.define-from {
  color: var(--muted);
  font-weight: 400;
  font-size: 0.82rem;
}

.define-senses {
  margin: 0;
  padding-left: 18px;
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.define-senses li::marker { color: var(--muted); font-size: 0.8rem; }

.define-pos {
  display: inline-block;
  margin-right: 6px;
  padding: 0 6px;
  border-radius: 4px;
  background: var(--hover);
  color: var(--muted);
  font-size: 0.7rem;
  font-style: italic;
  vertical-align: 1px;
}

.define-example {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-style: italic;
  font-size: 0.83rem;
}

.define-loading { margin: 0; padding-right: 20px; color: var(--muted); }

/* ================= states ================= */

.status {
  padding: 32px 16px;
  text-align: center;
  color: var(--muted);
  font-size: 0.9rem;
}

.status.error { color: #dc2626; }

@media (prefers-color-scheme: dark) {
  .status.error { color: #f87171; }
}

.placeholder {
  padding: 48px 24px;
  border: 1px dashed var(--border);
  border-radius: 12px;
  background: var(--card);
  color: var(--muted);
  text-align: center;
}

.placeholder h2 {
  margin: 0 0 8px;
  color: var(--fg);
  font-size: 1.05rem;
}

.placeholder p { margin: 0; font-size: 0.9rem; }

/* ================= responsive ================= */

@media (max-width: 820px) {
  .sidebar {
    transform: translateX(-100%);
    transition: transform 0.18s ease;
  }

  body.nav-open .sidebar { transform: translateX(0); }

  .nav-toggle { display: grid; }
  .content { margin-left: 0; padding: 0 16px 64px; }

  /* Extra top padding clears the fixed hamburger button. */
  .page-head { padding: 62px 20px 20px; }
}

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

@media (max-width: 480px) {
  .row { grid-template-columns: 2rem 1fr auto; gap: 8px; padding: 12px; }
  h1 { font-size: 1.4rem; }
}
