:root {
  --ink: #1a1612;
  --ink-soft: #3d342c;
  --paper: #f7f3ec;
  --paper-2: #efe8dc;
  --line: #d9cfc0;
  --muted: #7a6f63;
  --cinnabar: #c23b22;
  --cinnabar-deep: #9a2e1a;
  --jade: #2f6b5a;
  --jade-soft: #e4f0ec;
  --shadow: 0 16px 40px rgba(26, 22, 18, 0.1);
  --radius: 14px;
  --font: "Be Vietnam Pro", system-ui, sans-serif;
  --font-cn: "Noto Sans SC", "Be Vietnam Pro", sans-serif;
  --font-lo: "Noto Sans Lao", "Be Vietnam Pro", sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;
}

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(ellipse 70% 40% at 0% 0%, rgba(194,59,34,.08), transparent 50%),
    radial-gradient(ellipse 50% 30% at 100% 0%, rgba(47,107,90,.07), transparent 45%),
    linear-gradient(180deg, #fbf8f2, var(--paper) 40%, #f3eee4);
  background-attachment: fixed;
}

a { color: inherit; text-decoration: none; }
.app-shell { max-width: 1100px; margin: 0 auto; padding: 1.1rem 1.1rem 3rem; }

.topbar {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: 1rem; margin-bottom: 1.25rem;
}
.brand {
  display: flex;
  align-items: center;
  gap: .75rem;
  min-width: 0;
}
.brand-home { display: block; flex-shrink: 0; }
.brand-mark {
  width: 2.8rem; height: 2.8rem; display: grid; place-items: center;
  background: var(--ink); color: var(--paper); border-radius: 12px;
  font-family: var(--font-cn); font-weight: 700; font-size: 1.25rem;
  box-shadow: var(--shadow);
}
.brand-mark-home svg {
  width: 1.35rem;
  height: 1.35rem;
  display: block;
}
.brand-text {
  display: flex;
  flex-direction: column;
  gap: .15rem;
  min-width: 0;
}
.brand-title {
  display: block;
  font-size: 1.15rem;
  font-weight: 700;
  line-height: 1.2;
  color: var(--ink);
}
.brand-user {
  font-size: .8rem;
  font-weight: 500;
  color: var(--ink-soft);
}
.brand-meta {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
}
.btn-gear {
  display: inline-grid;
  place-items: center;
  width: 1.35rem;
  height: 1.35rem;
  border-radius: 6px;
  color: var(--ink-soft);
  flex-shrink: 0;
}
.btn-gear svg { width: 1rem; height: 1rem; fill: currentColor; }
.btn-gear:hover { color: var(--cinnabar); background: rgba(194,59,34,.08); }
.nav { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; }
.nav a { font-weight: 500; color: var(--ink-soft); }
.btn-icon-nav {
  display: inline-grid;
  place-items: center;
  width: 2.35rem;
  height: 2.35rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--ink-soft);
}
.btn-icon-nav svg {
  width: 1.15rem;
  height: 1.15rem;
}
.btn-icon-nav:hover {
  color: var(--cinnabar);
  border-color: #e8b4ab;
  background: #fff5f3;
}

.flash {
  padding: .75rem 1rem; border-radius: 10px; margin-bottom: 1rem;
  border: 1px solid var(--line); background: #fff;
}
.flash-error { border-color: #e8b4ab; background: #fff5f3; color: var(--cinnabar-deep); }
.flash-ok { border-color: #b7d4cb; background: var(--jade-soft); color: var(--jade); }

.main { min-height: 50vh; }
.footer { margin-top: 2.5rem; color: var(--muted); font-size: .85rem; text-align: center; }

.hero {
  padding: 2rem 1.5rem; border-radius: var(--radius);
  background: rgba(255,255,255,.65); border: 1px solid var(--line);
  box-shadow: var(--shadow); margin-bottom: 1.5rem;
}
.hero h1 { margin: 0 0 .5rem; font-size: clamp(1.6rem, 3vw, 2.1rem); letter-spacing: -.02em; }
.hero p { margin: 0; color: var(--muted); max-width: 36rem; line-height: 1.55; }
.hero-actions { display: flex; gap: .6rem; margin-top: 1.1rem; flex-wrap: wrap; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  border: 1px solid transparent; border-radius: 10px;
  padding: .7rem 1.05rem; font: inherit; font-weight: 600; cursor: pointer;
}
.btn-sm { padding: .4rem .75rem; font-size: .85rem; }
.btn-primary { background: var(--cinnabar); color: #fff; }
.btn-primary:hover { background: var(--cinnabar-deep); }
.btn-secondary { background: var(--jade); color: #fff; }
.btn-ghost { background: transparent; border-color: var(--line); color: var(--ink-soft); }
.btn-ghost:hover { background: var(--paper-2); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-danger { background: #fff; border-color: #e8b4ab; color: var(--cinnabar); }

.auth-card, .panel {
  max-width: 420px; margin: 0 auto;
  background: rgba(255,255,255,.75); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 1.4rem; box-shadow: var(--shadow);
}
.auth-card h1, .panel h1 { margin: 0 0 .35rem; font-size: 1.35rem; }
.auth-card .lede, .panel .lede { color: var(--muted); margin: 0 0 1.1rem; font-size: .92rem; }
.field { display: flex; flex-direction: column; gap: .35rem; margin-bottom: .9rem; }
.field label { font-size: .85rem; font-weight: 500; color: var(--ink-soft); }
.field input, .field textarea, .field select {
  border: 1px solid var(--line); border-radius: 10px; padding: .7rem .85rem;
  font: inherit; background: #fff;
}
.field input:focus { outline: none; border-color: var(--cinnabar); box-shadow: 0 0 0 3px rgba(194,59,34,.12); }
.auth-alt { margin-top: 1rem; font-size: .9rem; color: var(--muted); text-align: center; }
.auth-alt a { color: var(--cinnabar); font-weight: 600; }

.lesson-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1rem;
}
.lesson-card {
  background: rgba(255,255,255,.72); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 1.1rem; transition: transform .15s, box-shadow .15s;
}
.lesson-card:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.lesson-card h2 { margin: 0 0 .4rem; font-size: 1.05rem; line-height: 1.35; }
.lesson-card p { margin: 0; color: var(--muted); font-size: .88rem; line-height: 1.45; }
.lesson-meta { display: flex; gap: .5rem; margin: .85rem 0; flex-wrap: wrap; }
.badge {
  font-size: .72rem; font-weight: 600; padding: .2rem .5rem; border-radius: 999px;
  background: var(--paper-2); border: 1px solid var(--line); color: var(--ink-soft);
}
.badge-jade { background: var(--jade-soft); border-color: #b7d4cb; color: var(--jade); }

.lesson-head { margin-bottom: .85rem; }
.lesson-back { color: var(--muted); font-size: .9rem; }
.lesson-head h1 { margin: .35rem 0 0; font-size: clamp(1.15rem, 3vw, 1.35rem); line-height: 1.35; }

.practice-toolbar {
  display: none;
  align-items: center;
  gap: .75rem;
  margin-bottom: .85rem;
  padding: .55rem .7rem;
  background: rgba(255,255,255,.75);
  border: 1px solid var(--line);
  border-radius: 12px;
}
.practice-toolbar-meta {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: .1rem;
}
.practice-toolbar-meta strong { font-size: .82rem; color: var(--ink-soft); }
.practice-toolbar-meta span {
  font-family: var(--font-cn); font-weight: 700; font-size: 1rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.btn-hamburger {
  flex-shrink: 0;
  width: 2.6rem; height: 2.6rem;
  display: inline-flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 5px;
  border: 1px solid var(--line); border-radius: 10px;
  background: #fff; cursor: pointer; padding: 0;
}
.btn-hamburger span {
  display: block; width: 1.15rem; height: 2px;
  background: var(--ink); border-radius: 2px;
}

.practice-layout {
  display: grid;
  grid-template-columns: minmax(260px, 300px) 1fr;
  gap: 1.1rem;
  align-items: start;
}

.sidebar-backdrop { display: none; }

.item-list {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0;
  max-height: min(78vh, 720px);
  display: flex;
  flex-direction: column;
  min-height: 0;
  box-shadow: 0 8px 24px rgba(26,22,18,.04);
}
.item-list-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: .5rem;
  padding: .85rem .9rem .55rem;
  border-bottom: 1px solid var(--line);
}
.item-list-head h2 { margin: 0; font-size: 1rem; }
.item-list-sub { margin: .2rem 0 0; font-size: .78rem; color: var(--muted); }
.btn-icon-close {
  display: none;
  place-items: center;
  width: 2rem; height: 2rem;
  border: 0; border-radius: 8px;
  background: transparent; color: var(--muted);
  font-size: 1.4rem; line-height: 1; cursor: pointer;
}
.btn-icon-close:hover { background: var(--paper-2); color: var(--ink); }
.item-list-scroll {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: .45rem .5rem .65rem;
}

.item-link {
  display: grid;
  grid-template-columns: 1.6rem 1fr;
  gap: .45rem;
  align-items: start;
  width: 100%;
  text-align: left;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 10px;
  padding: .5rem .55rem;
  cursor: pointer;
  font: inherit;
  color: inherit;
  margin-bottom: .2rem;
}
.item-num {
  font-family: var(--mono);
  font-size: .72rem;
  color: var(--muted);
  padding-top: .2rem;
}
.item-body { min-width: 0; display: flex; flex-direction: column; gap: .1rem; }
.item-link:hover { background: var(--paper); }
.item-link.is-active { background: var(--jade-soft); border-color: rgba(47,107,90,.25); }
.item-link .hanzi-sm,
.item-primary {
  font-weight: 700;
  font-size: 1.02rem;
  line-height: 1.35;
}
.item-primary.is-hanzi { font-family: var(--font-cn); }
.item-primary.is-pinyin { font-family: var(--mono); font-weight: 500; font-size: .95rem; }
.item-primary.is-lo { font-family: var(--font-lo); }
.item-link .meta,
.item-secondary {
  font-size: .72rem; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.item-secondary.is-hanzi { font-family: var(--font-cn); }
.item-secondary.is-pinyin { font-family: var(--mono); }
.item-secondary.is-lo { font-family: var(--font-lo); }
.item-link.has-rec .dot {
  display: inline-block; width: .45rem; height: .45rem; border-radius: 50%;
  background: var(--jade); margin-right: .3rem; vertical-align: middle;
}

.practice-card {
  background: rgba(255,255,255,.78); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 1.25rem; box-shadow: var(--shadow);
}
.nav-phrase {
  margin-top: 1.1rem;
  justify-content: space-between;
}

@media (max-width: 860px) {
  .practice-toolbar { display: flex; }
  .practice-layout { grid-template-columns: 1fr; }

  .sidebar-backdrop:not([hidden]) {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 40;
    background: rgba(26, 22, 18, 0.45);
  }

  .item-list {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    z-index: 50;
    width: min(320px, 88vw);
    max-height: none;
    border-radius: 0 var(--radius) var(--radius) 0;
    transform: translateX(-105%);
    transition: transform .25s ease;
    box-shadow: var(--shadow);
    background: #fff;
  }
  .item-list.is-open { transform: translateX(0); }
  .btn-icon-close { display: grid; }

  body.sidebar-open { overflow: hidden; }
}

.practice-part + .practice-part {
  margin-top: 1.35rem;
  padding-top: 1.25rem;
  border-top: 1px dashed var(--line);
}
.part-heading {
  margin: 0 0 .35rem;
  font-size: .82rem;
  font-weight: 700;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--jade);
}
.phrase-block { text-align: center; padding: 1rem 0 1.25rem; }
.phrase-block .line-pinyin,
.record-lines .line-pinyin {
  font-family: var(--mono); color: var(--ink-soft); font-size: 1rem; margin-bottom: .35rem;
}
.phrase-block .hanzi,
.phrase-block .line-hanzi {
  font-family: var(--font-cn); font-size: clamp(2rem, 5vw, 2.8rem);
  font-weight: 700; letter-spacing: .06em; margin: 0 0 .5rem;
}
.meanings { display: grid; gap: .35rem; justify-items: center; }
.meanings span { color: var(--muted); font-size: .95rem; }
.meanings .lo, .line-lo { font-family: var(--font-lo); }
.line.is-hidden { display: none !important; }

.record-lines {
  text-align: center;
  margin: 0 0 .75rem;
  display: grid;
  gap: .25rem;
  justify-items: center;
}
.record-lines:not(:has(.line:not(.is-hidden))) { display: none; }
.record-lines .line-hanzi {
  font-family: var(--font-cn);
  font-size: 1.35rem;
  font-weight: 700;
  margin: 0;
}
.record-lines .line-en,
.record-lines .line-vi,
.record-lines .line-lo {
  color: var(--muted);
  font-size: .9rem;
}
.audio-box-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: .35rem;
}
.audio-box-head h3 { margin: 0; font-size: .9rem; }

.settings-page { max-width: 560px; }
.settings-page h1 { margin: 0 0 .35rem; font-size: 1.35rem; }
.settings-page .lede { color: var(--muted); margin: 0 0 1.2rem; line-height: 1.5; }
.settings-section {
  background: rgba(255,255,255,.75);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.1rem 1.15rem;
  margin-bottom: 1rem;
}
.settings-section h2 { margin: 0 0 .25rem; font-size: 1.05rem; }
.settings-section .hint { margin: 0 0 .85rem; color: var(--muted); font-size: .88rem; }
.toggle-grid { display: grid; gap: .45rem; }
.toggle-row {
  display: flex;
  align-items: center;
  gap: .65rem;
  padding: .55rem .65rem;
  border-radius: 10px;
  background: var(--paper);
  border: 1px solid transparent;
  cursor: pointer;
  font-weight: 500;
}
.toggle-row:hover { border-color: var(--line); }
.toggle-row input { width: 1.1rem; height: 1.1rem; accent-color: var(--jade); }
.settings-section .field { margin-bottom: .75rem; }
.settings-section .field:last-child { margin-bottom: 0; }
.settings-section select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: .7rem .85rem;
  font: inherit;
  background: #fff;
}

.compare-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin-top: .5rem;
}
@media (max-width: 640px) { .compare-grid { grid-template-columns: 1fr; } }

.audio-box {
  border: 1px solid var(--line); border-radius: 12px; padding: 1rem; background: var(--paper);
}
.audio-box h3 { margin: 0 0 .65rem; font-size: .9rem; }
.audio-box audio { width: 100%; margin-top: .5rem; }
.rec-controls { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .75rem; }
.rec-controls-icons {
  flex-wrap: nowrap;
  justify-content: flex-start;
  gap: .45rem;
}
.icon-btn {
  width: 2.55rem;
  height: 2.55rem;
  flex: 0 0 2.55rem;
  display: inline-grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--ink-soft);
  cursor: pointer;
  padding: 0;
  transition: background .15s, color .15s, opacity .15s, transform .1s;
}
.icon-btn svg {
  width: 1.15rem;
  height: 1.15rem;
  fill: currentColor;
}
.icon-btn:hover:not(:disabled) { background: var(--paper-2); }
.icon-btn:active:not(:disabled) { transform: scale(0.96); }
.icon-btn:disabled { opacity: .38; cursor: not-allowed; }
.icon-btn-record {
  background: var(--cinnabar);
  border-color: var(--cinnabar);
  color: #fff;
}
.icon-btn-record:hover:not(:disabled) { background: var(--cinnabar-deep); color: #fff; }
.icon-btn-stop {
  background: var(--jade);
  border-color: var(--jade);
  color: #fff;
}
.icon-btn-stop:hover:not(:disabled) { filter: brightness(1.08); color: #fff; }
.icon-btn-delete { color: var(--cinnabar); border-color: #e8b4ab; }
.icon-btn-delete:hover:not(:disabled) { background: #fff5f3; }
.icon-btn-delete svg { fill: none; }
.rec-status { font-size: .85rem; color: var(--muted); min-height: 1.2em; margin-top: .5rem; }
.rec-status.is-rec { color: var(--cinnabar); font-weight: 600; }

@media (max-width: 640px) {
  .rec-controls-icons {
    justify-content: space-between;
    gap: 0;
    width: 100%;
  }
  .icon-btn {
    width: 2.75rem;
    height: 2.75rem;
    flex: 0 0 2.75rem;
  }
}

.progress-table {
  width: 100%; border-collapse: collapse; background: rgba(255,255,255,.7);
  border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden;
}
.progress-table th, .progress-table td {
  padding: .7rem .85rem; text-align: left; border-bottom: 1px solid var(--line); font-size: .9rem;
}
.progress-table th {
  background: var(--paper-2); font-size: .75rem; text-transform: uppercase;
  letter-spacing: .04em; color: var(--muted);
}
.hanzi { font-family: var(--font-cn); }

.admin-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}
.admin-head h1 { margin: 0 0 .25rem; font-size: 1.35rem; }
.admin-head .lede { margin: 0; color: var(--muted); font-size: .92rem; }
.admin-grid {
  display: grid;
  grid-template-columns: minmax(260px, 340px) 1fr;
  gap: 1rem;
  align-items: start;
}
.admin-grid .panel h2 {
  margin: 0 0 .9rem;
  font-size: 1.05rem;
}
.admin-form-actions {
  display: flex;
  gap: .5rem;
  flex-wrap: wrap;
}
.admin-list-panel { overflow: hidden; }
.table-wrap { overflow-x: auto; }
.admin-users-table td, .admin-users-table th { vertical-align: middle; }
.admin-users-table tr.is-editing { background: var(--jade-soft); }
.admin-row-actions {
  display: flex;
  gap: .35rem;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.inline-form { display: inline; margin: 0; }
.btn-danger-text { color: var(--cinnabar-deep) !important; }
.meta-cell { font-size: .8rem; color: var(--muted); white-space: nowrap; }
@media (max-width: 860px) {
  .admin-grid { grid-template-columns: 1fr; }
}

.empty {
  text-align: center; color: var(--muted); padding: 2.5rem 1rem;
  border: 1px dashed var(--line); border-radius: var(--radius);
}
