:root {
  --bg: #f6f6fb;
  --surface: #ffffff;
  --surface-2: #f0f0f7;
  --text: #16161d;
  --muted: #64647a;
  --border: #e2e2ee;
  --accent: #4f46e5;
  --accent-text: #ffffff;
  --good: #16a34a;
  --warn: #d97706;
  --danger: #dc2626;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 20, 40, .06), 0 8px 24px rgba(20, 20, 40, .08);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101014;
    --surface: #1a1a21;
    --surface-2: #23232c;
    --text: #f2f2f7;
    --muted: #9a9aad;
    --border: #2f2f3a;
    --accent: #7c74f5;
    --accent-text: #0f0f14;
    --good: #22c55e;
    --warn: #f59e0b;
    --danger: #ef4444;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .45);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  overscroll-behavior-y: none;
}

body {
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto,
        "Noto Sans", "Noto Sans CJK SC", "Noto Sans JP", "Noto Sans KR",
        "Noto Sans Thai", "Noto Sans Devanagari", "Noto Sans Arabic",
        "Noto Sans Hebrew", "Apple Color Emoji", "Segoe UI Emoji", sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

#app {
  max-width: 720px;
  margin: 0 auto;
  padding: calc(12px + var(--safe-t)) 14px calc(20px + var(--safe-b));
  min-height: 100%;
}

/* ---------- chrome ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
  min-height: 40px;
}

.topbar h1 {
  font-size: 20px;
  margin: 0;
  flex: 1;
  letter-spacing: -.01em;
}

.topbar .sub {
  display: block;
  font-size: 12px;
  font-weight: 400;
  color: var(--muted);
  letter-spacing: 0;
}

button {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 13px;
  cursor: pointer;
  touch-action: manipulation;
}

button:active { transform: translateY(1px); }
button:disabled { opacity: .45; cursor: not-allowed; }

button.primary {
  background: var(--accent);
  border-color: transparent;
  color: var(--accent-text);
  font-weight: 600;
}

button.ghost { background: transparent; }

button.icon {
  padding: 8px 10px;
  background: transparent;
  border-color: transparent;
  color: var(--muted);
  font-size: 15px;
}

button.icon:hover { color: var(--text); background: var(--surface-2); }
button.danger { color: var(--danger); }

.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.spacer { flex: 1; }

input[type="text"], select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  width: 100%;
  min-width: 0;
}

input[type="text"]:focus, select:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

label.field { display: block; margin-bottom: 8px; }
label.field span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }

.card-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}

.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }

.empty {
  text-align: center;
  color: var(--muted);
  padding: 36px 16px;
}

.empty .big { font-size: 34px; margin-bottom: 8px; }

/* ---------- list rows ---------- */

.deck {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 10px 12px 14px;
  margin-bottom: 8px;
  box-shadow: var(--shadow);
}

.deck .name { font-weight: 600; }
.deck .meta { font-size: 12px; color: var(--muted); }
.deck .open { flex: 1; min-width: 0; cursor: pointer; }

.word {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 10px 4px 10px 12px;
  border-bottom: 1px solid var(--border);
}

.word:last-child { border-bottom: 0; }
.word .front { font-weight: 600; word-break: break-word; }
.word .back { color: var(--muted); font-size: 14px; word-break: break-word; }

.pill {
  display: inline-block;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  border: 1px solid var(--border);
}

.checkrow {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 8px;
  background: var(--surface);
  cursor: pointer;
}

.checkrow input { width: 20px; height: 20px; accent-color: var(--accent); }

/* ---------- revise stage ---------- */

.stage {
  position: relative;
  height: min(62vh, 440px);
  margin: 6px 0 14px;
  touch-action: pan-y;
}

.flashcard {
  position: absolute;
  inset: 0;
  border-radius: 20px;
  transform-style: preserve-3d;
  transition: transform .28s cubic-bezier(.2, .8, .3, 1), opacity .28s;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
}

.flashcard.dragging { transition: none; cursor: grabbing; }
.flashcard.flipped .face-front { transform: rotateY(180deg); }
.flashcard.flipped .face-back { transform: rotateY(360deg); }

.face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 24px;
  border-radius: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  transition: transform .4s;
  text-align: center;
}

.face-front { transform: rotateY(0deg); }
.face-back { transform: rotateY(180deg); background: var(--surface-2); }

.face .term {
  font-size: clamp(26px, 8vw, 44px);
  font-weight: 650;
  line-height: 1.25;
  word-break: break-word;
  max-width: 100%;
}

.face .tag { position: absolute; top: 12px; left: 14px; }
.face .hint { position: absolute; bottom: 14px; font-size: 12px; color: var(--muted); }

.swipe-tag {
  position: absolute;
  top: 18px;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: .08em;
  padding: 6px 12px;
  border-radius: 8px;
  border: 2px solid currentColor;
  opacity: 0;
  transition: opacity .12s;
  pointer-events: none;
  z-index: 3;
}

.swipe-tag.know { right: 18px; color: var(--good); transform: rotate(9deg); }
.swipe-tag.unsure { left: 18px; color: var(--warn); transform: rotate(-9deg); }

.progress {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 4px;
}

.bar {
  flex: 1;
  height: 6px;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
  border: 1px solid var(--border);
}

.bar > i {
  display: block;
  height: 100%;
  background: var(--accent);
  transition: width .25s;
}

.actions { display: flex; gap: 10px; }
.actions button { flex: 1; padding: 14px 10px; font-weight: 600; }
.actions .unsure { color: var(--warn); }
.actions .know { color: var(--good); }

.stat-grid { display: flex; gap: 10px; text-align: center; }
.stat-grid > div { flex: 1; background: var(--surface-2); border-radius: 12px; padding: 14px 8px; }
.stat-grid b { display: block; font-size: 26px; }
.stat-grid span { font-size: 12px; color: var(--muted); }

/* ---------- modal + toast ---------- */

.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10, 10, 20, .55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  z-index: 50;
}

.modal {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  width: 100%;
  max-width: 420px;
}

.modal h3 { margin: 0 0 8px; font-size: 17px; }
.modal .body { font-size: 14px; color: var(--muted); }
.modal .foot { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; flex-wrap: wrap; }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + var(--safe-b));
  transform: translate(-50%, 20px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 60;
  max-width: 90vw;
  text-align: center;
}

#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  * { transition-duration: .01ms !important; }
}
