:root {
  --brand: #b91c1c;
  --brand-dark: #991b1b;
  --brand-soft: #fef2f2;
  --gold: #d97706;
  --bg: #f8fafc;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #475569;
  --border: #e2e8f0;
  --success: #15803d;
  --success-bg: #dcfce7;
  --danger: #b91c1c;
  --danger-bg: #fee2e2;
  --topbar-bg: rgba(255, 255, 255, 0.94);
  --surface-alt: #f1f5f9;
  --radius: 16px;
}

[data-theme="dark"] {
  --brand: #ef4444;
  --brand-dark: #dc2626;
  --brand-soft: rgba(239, 68, 68, 0.14);
  --gold: #fbbf24;
  --bg: #0b1120;
  --card: #111827;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --border: #1e293b;
  --success: #4ade80;
  --success-bg: rgba(34, 197, 94, 0.15);
  --danger: #f87171;
  --danger-bg: rgba(239, 68, 68, 0.16);
  --topbar-bg: rgba(17, 24, 39, 0.92);
  --surface-alt: #1e293b;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Kantumruy Pro', 'Noto Sans Khmer', -apple-system, BlinkMacSystemFont, sans-serif;
  line-height: 1.75;
  font-size: 16px;
}

a {
  color: inherit;
  text-decoration: none;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--topbar-bg);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  padding: max(10px, env(safe-area-inset-top)) 16px 10px;
}

.topbar-inner {
  max-width: 960px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 18px;
  color: var(--brand);
}

.brand-badge {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: linear-gradient(135deg, #dc2626, #991b1b);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 20px;
  font-weight: 700;
}

.theme-switch {
  display: inline-flex;
  align-items: center;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px;
  gap: 2px;
}

.theme-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  border-radius: 999px;
  padding: 4px 8px;
  font-size: 13px;
  line-height: 1.2;
}

.theme-btn.active {
  background: var(--card);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.14);
}

[data-theme="dark"] .btn-outline,
[data-theme="dark"] .input,
[data-theme="dark"] select,
[data-theme="dark"] textarea {
  background: var(--card);
  color: var(--text);
  border-color: var(--border);
}

[data-theme="dark"] .bottom-nav {
  background: var(--topbar-bg);
  border-top-color: var(--border);
}

[data-theme="dark"] .pill-light,
[data-theme="dark"] .tab-btn:not(.active) {
  background: var(--surface-alt);
  color: var(--text);
  border-color: var(--border);
}

[data-theme="dark"] .card {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
}

.desktop-nav {
  display: none;
  gap: 18px;
  font-weight: 600;
  color: var(--muted);
}

.desktop-nav a.active,
.desktop-nav a:hover {
  color: var(--brand);
}

@media (min-width: 768px) {
  .desktop-nav { display: flex; }
  .bottom-nav { display: none !important; }
  .container { padding-bottom: 48px !important; }
}

.container {
  max-width: 960px;
  margin: 0 auto;
  padding: 18px 16px calc(92px + env(safe-area-inset-bottom));
}

.hero {
  background: linear-gradient(135deg, #b91c1c 0%, #7f1d1d 100%);
  color: #fff;
  border-radius: 20px;
  padding: 22px;
  margin-bottom: 20px;
  box-shadow: 0 10px 25px rgba(185, 28, 28, 0.18);
}

.hero h1 {
  margin: 0 0 6px;
  font-size: 22px;
  line-height: 1.5;
}

.hero p {
  margin: 0;
  opacity: 0.92;
  font-size: 14px;
}

.stats-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 14px;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 12px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
}

.pill-light {
  background: var(--brand-soft);
  color: var(--brand);
}

.grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
}

@media (min-width: 640px) {
  .grid-2 { grid-template-columns: repeat(2, 1fr); }
  .grid-3 { grid-template-columns: repeat(3, 1fr); }
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: 0 2px 8px rgba(15, 23, 42, 0.03);
}

.card-title {
  margin: 0 0 6px;
  font-size: 18px;
  font-weight: 700;
}

.muted {
  color: var(--muted);
  font-size: 14px;
}

.hanzi {
  font-family: 'Noto Sans SC', 'PingFang SC', 'Hiragino Sans GB', sans-serif;
  font-size: 32px;
  font-weight: 700;
  line-height: 1.3;
  color: var(--text);
}

.hanzi-lg {
  font-size: 56px;
}

.pinyin {
  color: var(--brand);
  font-weight: 600;
  font-size: 16px;
}

.khmer-hint {
  display: inline-block;
  background: #fef3c7;
  color: #92400e;
  border-radius: 8px;
  padding: 2px 8px;
  font-size: 13px;
  font-weight: 600;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 16px;
  border-radius: 12px;
  border: 1px solid transparent;
  font-family: inherit;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s ease, background 0.15s ease;
}

.btn:active { transform: scale(0.98); }

.btn-primary {
  background: var(--brand);
  color: #fff;
}

.btn-primary:hover { background: var(--brand-dark); }

.btn-outline {
  background: #fff;
  border-color: var(--border);
  color: var(--text);
}

.btn-outline:hover { background: #f1f5f9; }

.btn-sm {
  padding: 6px 12px;
  font-size: 13px;
  border-radius: 10px;
}

.tabs {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 6px;
  margin-bottom: 16px;
}

.tab-btn {
  flex: 0 0 auto;
  padding: 8px 16px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: #fff;
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  color: var(--muted);
}

.tab-btn.active {
  background: var(--brand);
  color: #fff;
  border-color: var(--brand);
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

.progress-track {
  height: 8px;
  background: #e2e8f0;
  border-radius: 999px;
  overflow: hidden;
  margin-top: 8px;
}

.progress-fill {
  height: 100%;
  background: var(--brand);
  border-radius: 999px;
}

/* Flashcard */
.flashcard {
  min-height: 270px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  cursor: pointer;
  user-select: none;
  padding: 28px 20px;
}

.srs-actions {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-top: 16px;
}

.srs-btn-again { background: #fee2e2; color: #991b1b; }
.srs-btn-hard  { background: #fef3c7; color: #92400e; }
.srs-btn-good  { background: #dcfce7; color: #166534; }
.srs-btn-easy  { background: #dbeafe; color: #1e40af; }

/* Stroke writer canvas - prevent iOS WebClip scroll while drawing */
.writer-box {
  width: 280px;
  height: 280px;
  margin: 0 auto;
  border: 2px dashed #cbd5e1;
  border-radius: 18px;
  background: #fff;
  touch-action: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.char-chip {
  min-width: 52px;
  height: 52px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: #fff;
  font-size: 24px;
  font-weight: 700;
  cursor: pointer;
}

.char-chip.active {
  border-color: var(--brand);
  background: var(--brand-soft);
  color: var(--brand);
}

/* Forms */
.input {
  width: 100%;
  padding: 11px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  font-family: inherit;
  font-size: 15px;
  background: #fff;
}

.input:focus {
  outline: 2px solid rgba(185, 28, 28, 0.2);
  border-color: var(--brand);
}

.alert {
  padding: 12px 14px;
  border-radius: 12px;
  margin-bottom: 14px;
  font-size: 14px;
}
.alert-error { background: var(--danger-bg); color: var(--danger); }
.alert-success { background: var(--success-bg); color: var(--success); }

/* Mobile & iOS WebClip Bottom Navigation */
.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 40;
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  padding: 8px 6px calc(8px + env(safe-area-inset-bottom));
}

.bottom-nav a {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  gap: 2px;
}

.bottom-nav a.active {
  color: var(--brand);
}

.bottom-nav .nav-icon {
  font-size: 20px;
  line-height: 1.1;
}

.ios-banner {
  display: none;
  background: #fffbeb;
  border: 1px solid #fde68a;
  color: #78350f;
  border-radius: 14px;
  padding: 12px 14px;
  margin-bottom: 16px;
  font-size: 13px;
}

.lang-switch {
  display: inline-flex;
  align-items: center;
  background: #f1f5f9;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px;
  gap: 2px;
}

.lang-switch a {
  padding: 4px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
  white-space: nowrap;
}

.lang-switch a.active {
  background: var(--brand);
  color: #fff;
}
