:root {
  --bg: #f4f7fb;
  --card: #ffffff;
  --ink: #2c333a;
  --muted: #7b8694;
  --line: #e6edf5;
  --blue: #5b93e6;
  --blue-2: #3f78d4;
  --blue-soft: #e8f1fc;
  --ok: #3d9a6a;
  --warn: #d9853a;
  --shadow: 0 10px 30px rgba(44, 74, 120, 0.06);
  --radius: 18px;
  --side: 248px;
}

html[data-theme="dark"] {
  --bg: #171d27;
  --card: #222a36;
  --ink: #e7eef6;
  --muted: #9aa6b5;
  --line: #313b4a;
  --blue: #7eacf0;
  --blue-2: #9cc0f5;
  --blue-soft: #2a384c;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.28);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: "Noto Sans SC", "Microsoft YaHei", "Sarabun", "Leelawadee UI", sans-serif;
  font-size: 16px;
  line-height: 1.6;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3, p { margin: 0; }
h1 { font-size: 28px; font-weight: 700; letter-spacing: -0.02em; }
h2 { font-size: 20px; font-weight: 700; }
a { color: var(--blue-2); }

.app { display: grid; grid-template-columns: var(--side) 1fr; min-height: 100vh; }
.side {
  position: sticky;
  top: 0;
  height: 100vh;
  background: var(--card);
  border-right: 1px solid var(--line);
  padding: 22px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow: auto;
}
.side-label { color: var(--muted); font-size: 12px; padding: 12px 10px 4px; }
.book-btn, .nav-link {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  border-radius: 12px;
  padding: 10px 12px;
}
.book-btn small, .nav-link small { color: var(--muted); font-size: 12px; }
.book-btn.is-on, .nav-link.is-on { background: var(--blue-soft); color: var(--blue-2); }
.book-btn:hover, .nav-link:hover { background: var(--blue-soft); }

.main { min-width: 0; display: flex; flex-direction: column; }
.top {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 16px 28px 8px;
}
.logo {
  border: 0;
  background: transparent;
  padding: 0;
  display: flex;
  align-items: baseline;
  gap: 8px;
}
.logo b { font-family: "Sarabun", "Leelawadee UI", sans-serif; font-size: 28px; font-weight: 700; color: var(--blue-2); }
.logo span { color: var(--muted); font-size: 13px; }
.top-links { display: flex; gap: 8px; margin-left: auto; }
.top-links button, .text-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  border-radius: 999px;
  padding: 8px 12px;
}
.top-links button.is-on, .text-btn.is-on { background: var(--blue-soft); color: var(--blue-2); }

.progress-block { padding: 4px 28px 0; }
.progress-meta { display: flex; justify-content: space-between; color: var(--muted); font-size: 13px; margin-bottom: 6px; }
.track { height: 6px; background: var(--line); border-radius: 99px; overflow: hidden; }
.track span { display: block; height: 100%; background: var(--blue); border-radius: 99px; }

.stage { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 28px 20px 88px; }
.page { padding: 12px 28px 48px; max-width: 1080px; }

.flip { width: min(560px, 100%); perspective: 1000px; }
.flip-inner {
  position: relative;
  min-height: 340px;
  transform-style: preserve-3d;
  transition: transform 0.35s ease;
}
.flip.is-back .flip-inner { transform: rotateY(180deg); }
.face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: var(--shadow);
  padding: 36px 32px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 8px;
}
.face-back { transform: rotateY(180deg); align-items: stretch; text-align: left; justify-content: flex-start; overflow: auto; }
.thai-xl {
  font-family: "Sarabun", "Leelawadee UI", sans-serif;
  font-size: 64px;
  line-height: 1.15;
  font-weight: 600;
}
.roman { color: var(--blue-2); font-size: 20px; }
.ipa { color: var(--muted); font-size: 15px; }
.pos { color: var(--blue-2); font-size: 13px; background: var(--blue-soft); border-radius: 999px; padding: 2px 10px; align-self: flex-start; }
.zh-lg { font-size: 28px; font-weight: 700; }
.example { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
.example .th { font-family: "Sarabun", "Leelawadee UI", sans-serif; font-size: 18px; }
.hint { color: var(--muted); font-size: 13px; }

.actions { display: flex; gap: 10px; margin-top: 18px; width: min(560px, 100%); }
.btn {
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--card);
  padding: 12px 16px;
  flex: 1;
}
.btn-primary { background: var(--blue); border-color: var(--blue); color: white; }
.btn-ghost { background: transparent; }
.btn-warn { color: var(--warn); }
.btn:disabled { opacity: 0.5; cursor: default; }

.fab {
  position: fixed;
  right: 28px;
  bottom: 28px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 0;
  background: var(--blue);
  color: white;
  box-shadow: var(--shadow);
  font-size: 22px;
  z-index: 4;
}
.bottom-nav { display: none; }

.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.grid-2 { display: grid; grid-template-columns: 1.4fr 1fr; gap: 14px; }
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}
.card button.block, .block { width: 100%; }
.muted { color: var(--muted); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0; }
.chip {
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 999px;
  padding: 6px 12px;
  color: var(--muted);
}
.chip.is-on { background: var(--blue-soft); color: var(--blue-2); border-color: transparent; }

.auth {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 1.1fr 0.9fr;
}
.auth-brand, .auth-form { display: flex; flex-direction: column; justify-content: center; padding: 48px; }
.auth-brand { background: var(--blue-soft); }
.field { display: flex; flex-direction: column; gap: 6px; margin-top: 12px; }
.field input, .field textarea, .search {
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 12px;
  padding: 12px 14px;
}
.form-error { color: #c45b4a; font-size: 14px; min-height: 1.2em; }

.modal-back {
  position: fixed;
  inset: 0;
  background: rgba(23, 29, 39, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 5;
}
.modal { width: min(420px, 100%); }

.list { display: flex; flex-direction: column; gap: 8px; }
.word-row, .check-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--card);
}
.word-row .th, .thai {
  font-family: "Sarabun", "Leelawadee UI", sans-serif;
  font-size: 22px;
}
.grow { flex: 1; min-width: 0; }
.calendar { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.day {
  aspect-ratio: 1;
  border-radius: 8px;
  background: var(--line);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  font-size: 11px;
  color: var(--muted);
  padding-bottom: 4px;
}
.day.is-on { background: var(--blue); color: white; }
.bars { display: flex; align-items: flex-end; gap: 8px; height: 120px; }
.bar { flex: 1; background: var(--blue-soft); border-radius: 8px 8px 4px 4px; min-height: 6px; position: relative; }
.bar span { position: absolute; left: 0; right: 0; bottom: 0; background: var(--blue); border-radius: inherit; }
.badges { display: flex; gap: 8px; flex-wrap: wrap; }
.badge { background: var(--blue-soft); color: var(--blue-2); border-radius: 999px; padding: 6px 10px; font-size: 13px; }

.quiz-option {
  width: 100%;
  text-align: left;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 14px;
  padding: 14px 16px;
  margin-top: 8px;
}
.quiz-option.is-on { border-color: var(--blue); background: var(--blue-soft); }

#toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%) translateY(12px);
  background: var(--ink);
  color: var(--bg);
  border-radius: 999px;
  padding: 10px 16px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 6;
}
#toast.show { opacity: 1; transform: translateX(-50%); }

.mobile-books { display: none; }

@media (max-width: 900px) {
  .app { display: block; }
  .side { display: none; }
  .mobile-books { display: flex; gap: 8px; overflow: auto; padding: 0 16px 8px; }
  .mobile-books button { flex: none; }
  .grid, .grid-2, .auth { grid-template-columns: 1fr; }
  .auth-brand { min-height: auto; padding: 28px 20px; }
  .auth-form, .page { padding: 16px 16px 96px; }
  .stage { padding-bottom: 108px; }
  .top { padding: 12px 16px 8px; }
  .progress-block { padding: 0 16px; }
  .thai-xl { font-size: 48px; }
  .actions { display: grid; grid-template-columns: 1fr 1fr; }
  .fab { right: 16px; bottom: 78px; }
  .bottom-nav {
    display: flex;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 5;
    background: var(--card);
    border-top: 1px solid var(--line);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  }
  .bottom-nav button {
    flex: 1;
    border: 0;
    background: transparent;
    color: var(--muted);
    border-radius: 10px;
    padding: 8px 0;
    font-size: 13px;
  }
  .bottom-nav button.is-on { color: var(--blue-2); background: var(--blue-soft); }
  .top-links button span { display: none; }
}
