:root {
  color-scheme: light;
  --bg: #f4f7fb;
  --panel: #ffffff;
  --panel-soft: #f9fbff;
  --text: #172033;
  --muted: #687385;
  --line: #dce4ef;
  --primary: #315fce;
  --primary-dark: #2248a7;
  --primary-soft: #edf3ff;
  --accent: #0f9f9a;
  --accent-soft: #e7f7f5;
  --good: #107a56;
  --warn: #b46618;
  --bad: #bd3d35;
  --shadow: 0 18px 48px rgba(28, 40, 68, .11);
  --shadow-soft: 0 8px 24px rgba(28, 40, 68, .07);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(135deg, #f8fafc 0%, #eef4f8 48%, #f8fbff 100%);
  color: var(--text);
  font-family: "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
}
button, input, select, textarea {
  font: inherit;
}
button {
  border: 0;
  border-radius: 8px;
  background: linear-gradient(180deg, #3c6fe0, var(--primary));
  color: #fff;
  padding: 10px 16px;
  cursor: pointer;
  text-transform: none;
  box-shadow: 0 8px 18px rgba(49, 95, 206, .18);
  transition: transform .16s ease, box-shadow .16s ease, background .16s ease, border-color .16s ease;
}
button:hover {
  background: linear-gradient(180deg, #345fca, var(--primary-dark));
  transform: translateY(-1px);
  box-shadow: 0 12px 22px rgba(49, 95, 206, .22);
}
button.secondary {
  background: var(--primary-soft);
  color: var(--primary-dark);
  box-shadow: none;
}
button.ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
  box-shadow: none;
}
button.danger {
  background: #fff1f0;
  color: var(--bad);
  box-shadow: none;
}
input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, .9);
  color: var(--text);
  padding: 11px 12px;
  outline: none;
  transition: border-color .16s ease, box-shadow .16s ease, background .16s ease;
}
input:focus, select:focus, textarea:focus {
  border-color: rgba(49, 95, 206, .65);
  box-shadow: 0 0 0 4px rgba(49, 95, 206, .12);
  background: #fff;
}
textarea {
  min-height: 180px;
  resize: vertical;
  line-height: 1.5;
}
label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 14px;
}
h1, h2, h3, p { margin: 0; }
#app {
  display: grid;
  grid-template-columns: 280px 1fr;
  min-height: 100vh;
}
.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 22px;
  padding: 26px 22px;
  background:
    linear-gradient(180deg, rgba(18, 27, 46, .98), rgba(22, 32, 54, .98)),
    #151f34;
  color: #fff;
  box-shadow: 18px 0 42px rgba(18, 27, 46, .16);
}
.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}
.brand-mark {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: linear-gradient(135deg, #7dd3fc, #5eead4);
  color: #102033;
  font-weight: 800;
  box-shadow: 0 12px 30px rgba(94, 234, 212, .18);
}
.brand h1 { font-size: 21px; letter-spacing: .2px; }
.brand p { color: #b7c0d1; font-size: 13px; margin-top: 3px; }
.nav {
  display: grid;
  gap: 8px;
}
.nav-btn {
  width: 100%;
  text-align: left;
  background: transparent;
  color: #cbd5e1;
  box-shadow: none;
  border: 1px solid transparent;
}
.nav-btn:hover, .nav-btn.active {
  background: rgba(255, 255, 255, .09);
  color: #fff;
  border-color: rgba(255, 255, 255, .08);
  box-shadow: none;
}
.user-panel {
  margin-top: auto;
  display: grid;
  gap: 10px;
  color: #cbd5e1;
  font-size: 14px;
}
.main {
  padding: 34px;
}
.view { display: none; }
.view.active { display: block; }
.auth-view {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background:
    linear-gradient(135deg, rgba(248, 250, 252, .96), rgba(238, 244, 248, .96));
  z-index: 5;
}
.auth-view.hidden { display: none; }
.auth-box {
  width: min(390px, calc(100vw - 32px));
  display: grid;
  gap: 20px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 30px;
}
.auth-box h2 {
  text-align: center;
}
#authForm, .settings-form {
  display: grid;
  gap: 14px;
}
.auth-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  margin-bottom: 24px;
}
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: flex-end;
  min-width: min(520px, 100%);
  flex-wrap: wrap;
}
.topbar-actions select {
  min-width: 132px;
  flex: 1 1 132px;
}
.topbar-actions button {
  flex: 0 0 auto;
}
.topbar h2 {
  font-size: 31px;
  line-height: 1.15;
  letter-spacing: .2px;
}
.muted { color: var(--muted); }
.hidden { display: none !important; }
.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(140px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}
.stats article {
  display: grid;
  gap: 4px;
  background: rgba(255, 255, 255, .86);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px;
  box-shadow: var(--shadow-soft);
  backdrop-filter: blur(10px);
}
.stats strong {
  font-size: 34px;
  line-height: 1;
}
.stats span { color: var(--muted); }
.dashboard-grid, .library-layout {
  display: grid;
  grid-template-columns: minmax(280px, .55fr) minmax(420px, 1.45fr);
  gap: 20px;
  align-items: start;
}
.dashboard-grid > section, .editor, .book-list, .study-card, .game-panel, .settings-form, .admin-panel {
  background: rgba(255, 255, 255, .88);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 20px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(12px);
}
.book-list {
  position: sticky;
  top: 24px;
  align-self: start;
}
.book-picker {
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}
.book-select {
  min-height: 48px;
}
.book-list-scroll {
  display: grid;
  gap: 10px;
  max-height: min(62vh, 620px);
  overflow: auto;
  padding-right: 4px;
  overscroll-behavior: contain;
}
.dashboard-grid h3 { margin-bottom: 12px; }
.list, .book-list, #unitList {
  display: grid;
  gap: 10px;
}
.empty-state {
  color: var(--muted);
  border: 1px dashed var(--line);
  border-radius: 14px;
  padding: 22px;
  text-align: center;
  background: rgba(249, 251, 255, .7);
}
.row, .book-item, .unit-card, .word-row {
  display: grid;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px;
  background: rgba(255, 255, 255, .92);
  box-shadow: 0 4px 14px rgba(28, 40, 68, .04);
}
.book-item {
  cursor: pointer;
  transition: transform .16s ease, border-color .16s ease, box-shadow .16s ease;
}
.book-item:hover {
  transform: translateY(-1px);
  border-color: rgba(49, 95, 206, .35);
  box-shadow: var(--shadow-soft);
}
.book-item.active {
  border-color: var(--primary);
  box-shadow: inset 4px 0 0 var(--primary), var(--shadow-soft);
}
.unit-card {
  overflow: hidden;
}
.unit-card.inactive-unit {
  opacity: .72;
  background: rgba(248, 250, 252, .82);
}
.unit-card.collapsed .unit-body {
  display: none;
}
.unit-toggle {
  min-width: 220px;
  display: grid;
  grid-template-columns: auto minmax(120px, 1fr) auto;
  align-items: center;
  gap: 10px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  box-shadow: none;
  text-align: left;
}
.unit-toggle:hover {
  transform: none;
  box-shadow: none;
  background: transparent;
}
.unit-toggle small {
  color: var(--muted);
  font-weight: 500;
  white-space: nowrap;
}
.unit-body {
  display: grid;
  gap: 10px;
  max-height: min(58vh, 680px);
  overflow: auto;
  padding-right: 4px;
}
.row-head, .editor-head, .word-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.row-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: center;
}
.row-actions button {
  white-space: nowrap;
  padding-inline: 12px;
}
.word-main {
  min-width: 0;
}
.word-main strong { display: block; }
.word-main span {
  display: block;
  color: var(--muted);
  margin-top: 3px;
}
.unit-card h4 {
  margin: 0;
  font-size: 18px;
}
.study-card {
  min-height: 380px;
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 20px;
  text-align: center;
}
.prompt {
  font-size: clamp(34px, 7vw, 70px);
  font-weight: 800;
  letter-spacing: 0;
}
.answer {
  font-size: 28px;
  color: var(--good);
}
.review-actions, .mode-tabs, .match-grid {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
}
.limit-picker {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 10px 0;
}
.limit-picker button {
  padding: 8px 12px;
}
.limit-picker .active-limit {
  background: linear-gradient(180deg, #3c6fe0, var(--primary));
  color: #fff;
  box-shadow: 0 8px 18px rgba(49, 95, 206, .16);
}
.upcoming-block {
  display: grid;
  gap: 10px;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}
.upcoming-block h4 {
  margin: 0;
  font-size: 16px;
}
.quiz-summary {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, .86);
}
.quiz-summary strong {
  font-size: 26px;
  color: var(--text);
}
.quiz-row {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  align-items: flex-start;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, .9);
}
.quiz-row + .quiz-row {
  margin-top: 10px;
}
.quiz-row p {
  margin: 8px 0 0;
  color: var(--muted);
}
.quiz-practice-card .answer {
  margin-top: 14px;
  text-align: left;
}
.quiz-practice-card {
  min-height: 0;
  align-content: start;
  justify-items: stretch;
  text-align: left;
  gap: 16px;
  padding: 28px;
}
.quiz-practice-card > .muted {
  text-align: center;
}
.quiz-practice-card .prompt {
  max-width: 980px;
  margin: 8px auto 4px;
  font-size: clamp(22px, 2.1vw, 30px);
  line-height: 1.45;
  font-weight: 750;
  text-align: left;
  overflow-wrap: anywhere;
}
.quiz-practice-card .option-grid {
  width: 100%;
  margin-top: 8px;
  grid-template-columns: repeat(2, minmax(220px, 1fr));
}
.quiz-practice-card .option-grid button {
  min-height: 56px;
  justify-content: flex-start;
  text-align: left;
  padding: 14px 18px;
}
.accent-picker {
  width: auto;
  min-width: 96px;
  background: #fff;
}
.mode-tabs {
  justify-content: flex-start;
  margin-bottom: 16px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(255, 255, 255, .62);
  width: fit-content;
}
.mode-btn {
  background: transparent;
  color: var(--text);
  box-shadow: none;
}
.mode-btn.active {
  background: #fff;
  color: #fff;
  background: linear-gradient(180deg, #3c6fe0, var(--primary));
  box-shadow: 0 8px 18px rgba(49, 95, 206, .18);
}
.game-panel {
  min-height: 360px;
}
.arena-shell {
  display: grid;
  gap: 18px;
}
.arena-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  color: var(--muted);
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
}
.arena-meta strong {
  color: var(--text);
  font-size: 18px;
}
.compact-study {
  min-height: 300px;
  box-shadow: none;
  border: 1px solid rgba(220, 228, 239, .82);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, .96), rgba(249, 251, 255, .96));
}
.option-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
  width: min(880px, 100%);
}
.option-grid button {
  min-height: 64px;
  background: #f8fafc;
  color: var(--text);
  border: 1px solid var(--line);
  box-shadow: none;
}
.option-grid button.correct {
  background: #dcfce7;
  color: var(--good);
  border-color: #86efac;
}
.option-grid button.wrong {
  background: #fee2e2;
  color: var(--bad);
  border-color: #fecaca;
}
.match-board {
  position: relative;
  width: 100%;
  padding: 8px 0;
}
.match-lines {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: visible;
  z-index: 0;
}
.match-line {
  stroke: var(--primary);
  stroke-width: 4;
  stroke-linecap: round;
  opacity: .72;
  stroke-dasharray: 12;
  animation: dashIn .38s ease both;
}
.match-line.fade {
  opacity: .22;
}
@keyframes dashIn {
  from { stroke-dashoffset: 60; opacity: 0; }
  to { stroke-dashoffset: 0; opacity: .72; }
}
.match-grid {
  position: relative;
  z-index: 1;
}
.match-grid button.selected {
  outline: 3px solid #38bdf8;
}
.match-grid button {
  min-width: 140px;
  min-height: 58px;
  background: #f8fafc;
  color: var(--text);
  border: 1px solid var(--line);
  box-shadow: none;
}
.match-grid button.done {
  background: #dcfce7;
  color: var(--good);
}
.check-row {
  display: flex;
  align-items: center;
}
.check-row input {
  width: auto;
}
.admin-grid {
  display: grid;
  grid-template-columns: minmax(280px, .8fr) 1.2fr;
  gap: 18px;
}
.admin-import-panel {
  grid-column: 1 / -1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
}
.admin-list {
  display: grid;
  gap: 12px;
  margin-top: 14px;
}
.admin-card, .admin-word {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: linear-gradient(180deg, #fff 0%, #f8fbff 100%);
  box-shadow: 0 4px 14px rgba(28, 40, 68, .04);
}
.admin-card > div, .admin-word > div {
  display: grid;
  gap: 4px;
}
.admin-word {
  align-items: flex-start;
}
dialog {
  width: min(460px, calc(100vw - 28px));
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 22px;
}
dialog::backdrop { background: rgba(15, 23, 42, .35); }
#modalForm, #modalFields {
  display: grid;
  gap: 14px;
}
menu {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 0;
  margin: 8px 0 0;
}
@media (max-width: 860px) {
  #app { grid-template-columns: 1fr; }
  .sidebar {
    position: static;
    height: auto;
    padding: 18px;
  }
  .nav {
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }
  .nav-btn {
    text-align: center;
    padding: 9px 4px;
    font-size: 13px;
  }
  .main { padding: 18px; }
  .topbar { align-items: stretch; flex-direction: column; }
  .topbar-actions { align-items: stretch; flex-direction: column; }
  .stats, .dashboard-grid, .library-layout, .option-grid {
    grid-template-columns: 1fr;
  }
  .book-list {
    position: static;
  }
  .book-list-scroll, .unit-body {
    max-height: 52vh;
  }
  .row-head, .word-row {
    align-items: stretch;
    flex-direction: column;
  }
  .quiz-practice-card {
    padding: 20px;
  }
  .quiz-practice-card .prompt {
    font-size: clamp(19px, 5vw, 24px);
    line-height: 1.5;
  }
  .quiz-practice-card .option-grid {
    grid-template-columns: 1fr;
  }
  .unit-toggle {
    min-width: 0;
    width: 100%;
  }
  .row-actions {
    justify-content: flex-start;
  }
}
