:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --text: #1f2328;
  --muted: #6b7280;
  --border: #e5e1d8;
  --primary: #c0392b;
  --primary-dark: #a93226;
  --known: #2e7d32;
  --forgot: #e08e0b;
  --unknown: #8e2a2a;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0, 0, 0, .08), 0 4px 16px rgba(0, 0, 0, .05);
  --hanzi: "Noto Sans SC", "PingFang SC", "Microsoft YaHei", "Hiragino Sans GB", sans-serif;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
}

* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.center { text-align: center; }
[lang="zh"], .vc-word, .card-word, .card-word-small, .brand-mark { font-family: var(--hanzi); }

/* ---------- Buttons & inputs ---------- */
.btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: .55rem 1rem; border-radius: 8px; font: inherit; cursor: pointer;
  transition: background .15s, border-color .15s, opacity .15s;
}
.btn:hover:not(:disabled) { border-color: #cfc8b8; background: #faf8f4; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); border-color: var(--primary-dark); }
.btn-ghost { background: transparent; }
.btn-danger { color: var(--unknown); background: transparent; }
.btn-small { padding: .3rem .65rem; font-size: .875rem; }
.btn-icon { padding: .25rem .5rem; background: transparent; border-color: transparent; }
.btn-block { width: 100%; }
kbd { font-size: .75em; opacity: .7; border: 1px solid currentColor; border-radius: 4px; padding: 0 .3em; margin-left: .3em; }

input[type="text"], input[type="search"], input[type="password"], select {
  font: inherit; padding: .55rem .7rem; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); min-width: 0;
}
input:focus, select:focus, .card:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
label { display: flex; flex-direction: column; gap: .3rem; font-size: .875rem; color: var(--muted); }
label > input, label > select { color: var(--text); font-size: 1rem; }

.status { min-height: 1.2em; margin: .5rem 0; font-size: .9rem; }
.status.error { color: var(--unknown); }
.status.loading { color: var(--muted); }
.status.loading::before {
  content: ""; display: inline-block; width: .9em; height: .9em; margin-right: .5em; vertical-align: -.1em;
  border: 2px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Layout ---------- */
.topbar {
  display: flex; align-items: center; gap: 1.5rem; padding: .75rem 1.5rem;
  background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 700; font-size: 1.15rem; display: flex; align-items: center; gap: .5rem; }
.brand.center { justify-content: center; }
.brand-mark { background: var(--primary); color: #fff; border-radius: 8px; padding: .1rem .45rem; }
.tabs { display: flex; gap: .25rem; flex: 1; }
.tab { border: 0; background: transparent; font: inherit; padding: .5rem .9rem; border-radius: 8px; cursor: pointer; color: var(--muted); }
.tab.active { background: #f3e5e3; color: var(--primary); font-weight: 600; }
.user { display: flex; align-items: center; gap: .5rem; color: var(--muted); }

main { max-width: 1100px; margin: 0 auto; padding: 1.5rem; }

.toolbar { display: flex; flex-wrap: wrap; gap: .6rem; align-items: center; }
.toolbar input[type="search"] { flex: 1 1 240px; }
.toolbar .btn-primary { margin-left: auto; }

.empty { text-align: center; padding: 3rem 1rem; color: var(--muted); }
.pager { display: flex; justify-content: center; align-items: center; gap: 1rem; margin-top: 1.5rem; }

/* ---------- Vocabulary list ---------- */
.vocab-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(330px, 1fr)); gap: 1rem; }
.vocab-card {
  display: flex; gap: .9rem; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: .9rem; box-shadow: var(--shadow);
}
.vc-thumb { width: 84px; height: 84px; border-radius: 10px; object-fit: contain; background: #faf8f4; flex-shrink: 0; }
.vc-thumb-empty { display: grid; place-items: center; font-family: var(--hanzi); font-size: 2.2rem; color: #c8bfae; }
.vc-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: .35rem; }
.vc-head { display: flex; align-items: center; gap: .4rem; flex-wrap: wrap; }
.vc-word { font-size: 1.6rem; font-weight: 600; }
.pinyin { color: var(--primary); font-weight: 500; }
.badge { font-size: .75rem; background: #eef0f3; color: #4b5563; padding: .1rem .45rem; border-radius: 999px; white-space: nowrap; }
.vc-foot { display: flex; align-items: center; gap: .5rem; margin-top: auto; flex-wrap: wrap; }
.vc-actions { display: flex; justify-content: flex-end; gap: .4rem; }
.spacer { flex: 1; }
.counts { font-size: .8rem; }

.meanings { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .3rem; }
.meanings.compact { font-size: .9rem; }
.meanings.compact li { overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.word-type { font-size: .75rem; color: #7c5e10; background: #fbf3dc; padding: .05rem .4rem; border-radius: 4px; white-space: nowrap; }

.fluency { font-size: .75rem; padding: .1rem .5rem; border-radius: 999px; white-space: nowrap; }
.fluency-new { background: #eef0f3; color: #4b5563; }
.fluency-learning { background: #fde8e8; color: var(--unknown); }
.fluency-familiar { background: #fff3d6; color: #8a5a00; }
.fluency-mastered { background: #e3f4e4; color: var(--known); }

/* ---------- Dialog ---------- */
.dialog {
  border: 0; border-radius: var(--radius); padding: 1.5rem; width: min(720px, calc(100vw - 2rem));
  box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}
.dialog::backdrop { background: rgba(20, 20, 20, .45); }
.dialog h2 { margin-top: 0; }
.row { display: flex; gap: .6rem; }
.row input { flex: 1; font-size: 1.4rem; font-family: var(--hanzi); }
.dialog-actions { display: flex; justify-content: flex-end; gap: .6rem; margin-top: 1rem; }

.form-grid { display: grid; grid-template-columns: 1fr 220px; gap: 1.25rem; }
.form-fields { display: flex; flex-direction: column; gap: .8rem; }
#f-word { font-size: 1.4rem; font-family: var(--hanzi); }
.image-box { display: flex; flex-direction: column; align-items: center; gap: .4rem; }
.preview-image, .image-empty {
  width: 200px; height: 200px; border-radius: 10px; border: 1px solid var(--border); background: #faf8f4; object-fit: contain;
}
.image-empty { display: grid; place-items: center; color: var(--muted); }
.image-actions { display: flex; gap: .3rem; flex-wrap: wrap; justify-content: center; }
.file-btn { position: relative; overflow: hidden; color: var(--text); flex-direction: row; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

.meanings-editor { border: 1px solid var(--border); border-radius: 10px; margin: 1rem 0 0; padding: .75rem; }
.meanings-editor legend { padding: 0 .4rem; color: var(--muted); font-size: .875rem; }
.meaning-row { display: grid; grid-template-columns: 150px 1fr auto; gap: .5rem; margin-bottom: .5rem; }

/* ---------- Study ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: .75rem; margin-bottom: 1.5rem; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .75rem; text-align: center; }
.stat-value { font-size: 1.5rem; font-weight: 700; }
.stat-label { font-size: .8rem; color: var(--muted); }
.stat.fluency-learning .stat-value { color: var(--unknown); }
.stat.fluency-familiar .stat-value { color: #8a5a00; }
.stat.fluency-mastered .stat-value { color: var(--known); }

.study-setup { display: flex; gap: 1rem; align-items: flex-end; justify-content: center; flex-wrap: wrap; margin: 2rem 0; }

.progress { height: 6px; background: var(--border); border-radius: 999px; overflow: hidden; max-width: 560px; margin: 0 auto; }
#study-progress-bar { height: 100%; width: 0; background: var(--primary); transition: width .3s; }

.card { perspective: 1200px; max-width: 560px; height: 420px; margin: 1rem auto; cursor: pointer; border-radius: var(--radius); }
.card-inner { position: relative; width: 100%; height: 100%; transition: transform .45s; transform-style: preserve-3d; }
.card.flipped .card-inner { transform: rotateY(180deg); }
.card-face {
  position: absolute; inset: 0; backface-visibility: hidden; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 1.5rem; gap: .75rem;
}
.card-back { transform: rotateY(180deg); justify-content: flex-start; overflow-y: auto; }
.card-word { font-size: clamp(3rem, 12vw, 6rem); font-weight: 600; line-height: 1.1; text-align: center; word-break: keep-all; }
.hint { font-size: .85rem; }
.card-back-head { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; justify-content: center; }
.card-word-small { font-size: 2rem; font-weight: 600; }
.card-back .pinyin { font-size: 1.4rem; }
.card-image { width: 160px; height: 160px; object-fit: contain; }
.card-back .meanings { align-self: stretch; font-size: 1.05rem; }

.answers { display: flex; justify-content: center; gap: .75rem; flex-wrap: wrap; }
.answers .btn { min-width: 130px; color: #fff; font-weight: 600; }
.btn-known { background: var(--known); border-color: var(--known); }
.btn-forgot { background: var(--forgot); border-color: var(--forgot); }
.btn-unknown { background: var(--unknown); border-color: var(--unknown); }
.answers .btn:hover:not(:disabled) { filter: brightness(1.08); }
.btn-known:hover:not(:disabled) { background: var(--known); }
.btn-forgot:hover:not(:disabled) { background: var(--forgot); }
.btn-unknown:hover:not(:disabled) { background: var(--unknown); }

.study-done { text-align: center; margin: 3rem 0; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 1.5rem; left: 50%; transform: translateX(-50%); background: #1f2328; color: #fff;
  padding: .7rem 1.2rem; border-radius: 8px; box-shadow: var(--shadow); z-index: 100; max-width: calc(100vw - 2rem);
}
.toast-error { background: var(--unknown); }
.toast-success { background: var(--known); }

/* ---------- Login ---------- */
.login-page { display: grid; place-items: center; padding: 1rem; }
.login-card {
  background: var(--surface); padding: 2rem; border-radius: var(--radius); box-shadow: var(--shadow);
  width: min(380px, 100%); display: flex; flex-direction: column; gap: .9rem;
}
.login-card h1 { margin: .25rem 0; font-size: 1.3rem; text-align: center; }

/* ---------- Small screens ---------- */
@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; gap: .5rem; padding: .6rem 1rem; }
  .tabs { order: 3; flex-basis: 100%; }
  main { padding: 1rem; }
  .vocab-grid { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  .meaning-row { grid-template-columns: 1fr auto; }
  .meaning-row .m-text { grid-column: 1 / -1; grid-row: 2; }
  .card { height: 380px; }
  .answers .btn { min-width: 0; flex: 1; }
}
