/* Mobile-first. Designed for iPhone Safari, but scales up. */

:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-hi: #334155;
  --text: #f1f5f9;
  --text-dim: #94a3b8;
  --accent: #60a5fa;
  --accent-hi: #93c5fd;
  --good: #34d399;
  --bad: #f87171;
  --warn: #fbbf24;
  --radius: 14px;
  --tap: 56px;
  --safe-top: env(safe-area-inset-top, 0);
  --safe-bottom: env(safe-area-inset-bottom, 0);
  --safe-left: env(safe-area-inset-left, 0);
  --safe-right: env(safe-area-inset-right, 0);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  min-height: 100vh;
  /* Prevent iOS pull-to-refresh from interrupting flashcard flow on the app page. */
  overscroll-behavior-y: contain;
  -webkit-tap-highlight-color: transparent;
}

button, input, select {
  font: inherit;
  color: inherit;
}

button {
  background: var(--surface);
  border: 1px solid var(--surface-hi);
  border-radius: var(--radius);
  padding: 14px 18px;
  min-height: var(--tap);
  cursor: pointer;
  touch-action: manipulation;
}

button:active { background: var(--surface-hi); }

button.primary {
  background: var(--accent);
  color: #0b1220;
  border-color: var(--accent);
  font-weight: 600;
}
button.primary:active { background: var(--accent-hi); }

.hidden { display: none !important; }

/* ===== Auth ===== */

body.auth-page {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(24px + var(--safe-top)) 20px calc(24px + var(--safe-bottom));
}

.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px 20px 28px;
}

.brand {
  font-size: 36px;
  margin: 0 0 4px;
  text-align: center;
  font-weight: 700;
  letter-spacing: -0.5px;
}
.brand-sub {
  color: var(--accent);
  font-size: 20px;
  vertical-align: middle;
  margin-left: 6px;
}

.tabs {
  display: flex;
  gap: 8px;
  margin: 18px 0;
}
.tab {
  flex: 1;
  background: transparent;
  border: 1px solid var(--surface-hi);
  color: var(--text-dim);
}
.tab.active {
  background: var(--surface-hi);
  color: var(--text);
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.auth-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 14px;
  color: var(--text-dim);
}
.auth-form input {
  background: #0b1220;
  border: 1px solid var(--surface-hi);
  border-radius: 10px;
  padding: 14px;
  font-size: 16px; /* 16px to prevent iOS zoom on focus */
}
.auth-form input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 0;
  border-color: var(--accent);
}

.hint { font-size: 12px; color: var(--text-dim); }
.msg { min-height: 1.2em; margin: 4px 0 0; font-size: 14px; }
.msg.error { color: var(--bad); }
.msg.ok { color: var(--good); }

/* ===== App page ===== */

body.app-page,
body.stats-page {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  padding-top: var(--safe-top);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--surface);
  gap: 10px;
}
.topbar-left, .topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}
.topbar .title { font-weight: 600; }

.iconbtn {
  min-width: 44px;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  color: var(--text);
  background: transparent;
  border: none;
  font-size: 22px;
  padding: 0;
  border-radius: 10px;
}
.iconbtn:active { background: var(--surface); }
.iconbtn.on {
  background: var(--warn);
  color: #1a1408;
}
.iconbtn.on:active { background: #fcd34d; }

.pill {
  background: var(--surface);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 14px;
  color: var(--text-dim);
}

/* ===== Flashcard ===== */

.card-stage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  min-height: 0;
  position: relative; /* anchor for the absolute drawing canvas */
}

.card {
  width: 100%;
  max-width: 480px;
  background: var(--surface);
  border-radius: 20px;
  padding: 28px 20px 32px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  text-align: center;
}

.card-level {
  color: var(--text-dim);
  font-size: 13px;
  letter-spacing: 1px;
  text-transform: uppercase;
}

.card-front {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 12px 0;
}
.card-english {
  font-size: 28px;
  font-weight: 500;
  line-height: 1.2;
  color: var(--text);
  text-align: center;
}
.card-pinyin {
  font-size: 22px;
  color: var(--accent-hi);
  text-align: center;
}

.card-back {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.card-hanzi {
  font-size: 120px;
  line-height: 1;
  font-weight: 500;
  font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Heiti SC", system-ui, sans-serif;
  user-select: none;
}

.empty {
  text-align: center;
  padding: 40px 20px;
}

.loading {
  color: var(--text-dim);
  padding: 40px;
}

/* ===== Whiteboard overlay ===== */

.draw-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  /* Block iOS scroll/zoom inside the canvas so single-finger drags draw
     instead of scrolling, and 2-finger gestures clear instead of pinching. */
  touch-action: none;
}
.draw-canvas.active {
  pointer-events: auto;
}

.draw-hint {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.85);
  color: var(--text-dim);
  font-size: 12px;
  padding: 6px 10px;
  border-radius: 999px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 200ms ease;
}
.draw-hint.visible {
  opacity: 1;
}

/* ===== Action bar ===== */

.actions {
  padding: 12px 14px calc(14px + var(--safe-bottom));
  background: linear-gradient(180deg, transparent, var(--bg) 30%);
}

.reveal-btn {
  width: 100%;
  background: var(--accent);
  color: #0b1220;
  border: none;
  font-weight: 600;
  font-size: 18px;
  min-height: 64px;
  border-radius: 16px;
}
.reveal-btn:active { background: var(--accent-hi); }

.rate-row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}

.rate {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 10px 4px;
  min-height: 76px;
  border-radius: 14px;
  font-size: 13px;
}
.rate .r-num {
  font-size: 20px;
  font-weight: 600;
}
.rate .r-label {
  font-size: 11px;
  color: var(--text-dim);
}
.rate .r-int {
  font-size: 10px;
  color: var(--text-dim);
  min-height: 12px;
}
.rate[data-rating="1"] { border-color: #7f1d1d; }
.rate[data-rating="2"] { border-color: #92400e; }
.rate[data-rating="3"] { border-color: #3f6212; }
.rate[data-rating="4"] { border-color: #14532d; }
.rate[data-rating="5"] { border-color: #1e3a8a; }

/* ===== Stats ===== */

.stats-main {
  padding: 16px 16px calc(24px + var(--safe-bottom));
}
.stats-main h2 {
  font-size: 17px;
  margin: 24px 0 10px;
  color: var(--text-dim);
  font-weight: 600;
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.stat {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
.stat-num { font-size: 24px; font-weight: 700; }
.stat-lbl { font-size: 12px; color: var(--text-dim); }

.levels {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.level-row {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.level-row-top {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
}
.bar {
  height: 8px;
  background: #0b1220;
  border-radius: 999px;
  overflow: hidden;
}
.bar-fill {
  height: 100%;
  background: var(--good);
}

.char-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(36px, 1fr));
  gap: 4px;
  margin-top: 8px;
}
.char-cell {
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Heiti SC", system-ui, sans-serif;
  border-radius: 6px;
  background: #1e293b;
  color: var(--text-dim);
}
.char-cell.known { background: #14532d; color: #d1fae5; }
.char-cell.learning { background: #78350f; color: #fef3c7; }
