:root {
  --bg: #f3ecdf;
  --bg-2: #eae1cf;
  --card: #fffbf3;
  --card-back: #fdf6e6;
  --ink: #221b14;
  --ink-2: #6b6054;
  --ink-3: #9a8f81;
  --line: rgba(60, 40, 20, 0.12);
  --accent: #7d1f2e;        /* Gryffindor-ish burgundy */
  --gold: #b8892b;
  --yes: #2f8f5b;
  --yes-soft: rgba(47, 143, 91, 0.16);
  --no: #d0642a;
  --no-soft: rgba(208, 100, 42, 0.16);
  --shadow: 0 18px 40px -18px rgba(50, 30, 10, 0.45), 0 2px 6px rgba(50, 30, 10, 0.08);
  --radius: 26px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16120f;
    --bg-2: #211b16;
    --card: #2a231d;
    --card-back: #302820;
    --ink: #f1e9dc;
    --ink-2: #b7aa99;
    --ink-3: #7f7466;
    --line: rgba(255, 235, 200, 0.12);
    --accent: #d8455c;
    --gold: #e0b04d;
    --yes: #4fc084;
    --yes-soft: rgba(79, 192, 132, 0.18);
    --no: #f08a4b;
    --no-soft: rgba(240, 138, 75, 0.18);
    --shadow: 0 18px 40px -18px rgba(0, 0, 0, 0.8), 0 2px 6px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Inter, Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  overscroll-behavior: none;
  -webkit-user-select: none; user-select: none;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
[hidden] { display: none !important; }

.app {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  padding: calc(var(--sat) + 8px) 16px calc(var(--sab) + 12px);
  max-width: 560px;
  margin: 0 auto;
}

/* ---- header ---- */
.top { display: grid; grid-template-columns: 44px 1fr 44px; align-items: center; gap: 8px; }
.title { text-align: center; min-width: 0; }
.book { font-size: 13px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; letter-spacing: 0.01em; }
.counter { font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; margin-top: 2px; }
.icon-btn {
  width: 44px; height: 44px; border-radius: 14px; display: grid; place-items: center;
  color: var(--ink-2); transition: background 0.15s, transform 0.15s;
}
.icon-btn:active { background: var(--bg-2); transform: scale(0.94); }

.progress { height: 4px; border-radius: 2px; background: var(--line); margin: 10px 4px 0; overflow: hidden; }
.progress-bar { height: 100%; width: 0; background: var(--gold); border-radius: 2px; transition: width 0.35s cubic-bezier(.2,.8,.2,1); }

/* ---- stage & cards ---- */
.stage { position: relative; min-height: 0; padding: 18px 0 14px; touch-action: none; }
.deck { position: relative; height: 100%; perspective: 1400px; }
.card {
  position: absolute; inset: 0;
  transform-style: preserve-3d;
  will-change: transform;
  border-radius: var(--radius);
  transition: transform 0.45s cubic-bezier(.2,.8,.2,1);
  touch-action: none;
}
.card.dragging { transition: none; }
.card.flipped { transform: rotateY(180deg); }
.card.behind-1 { transform: translateY(16px) scale(0.96); }
.card.behind-2 { transform: translateY(32px) scale(0.92); }
.card.behind-1, .card.behind-2 { pointer-events: none; }
.card.behind-1 .face { filter: brightness(0.975); }
.card.behind-2 .face { filter: brightness(0.95); }
.card.fly-right { transform: translateX(130vw) rotate(18deg); transition: transform 0.4s cubic-bezier(.4,0,.6,1); }
.card.fly-left  { transform: translateX(-130vw) rotate(-18deg); transition: transform 0.4s cubic-bezier(.4,0,.6,1); }
.card.fly-right.flipped { transform: translateX(130vw) rotate(18deg) rotateY(180deg); }
.card.fly-left.flipped  { transform: translateX(-130vw) rotate(-18deg) rotateY(180deg); }
.card.enter { animation: enter 0.4s cubic-bezier(.2,.8,.2,1); }
@keyframes enter { from { transform: translateY(16px) scale(0.96); } to { transform: none; } }

.face {
  position: absolute; inset: 0;
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
  padding: 28px 26px;
  display: flex; flex-direction: column;
  overflow: hidden;
}
.face.back { transform: rotateY(180deg); background: var(--card-back); }

.face .corner {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 12px; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-3);
}
.chip { padding: 4px 9px; border-radius: 999px; background: var(--bg-2); color: var(--ink-2); font-size: 11.5px; letter-spacing: 0.06em; }
.chip.gender { background: var(--gold); color: #fff; }
@media (prefers-color-scheme: dark) { .chip.gender { color: #1a1408; } }

.face .body { flex: 1; display: flex; flex-direction: column; justify-content: center; align-items: center; text-align: center; min-height: 0; }
.word {
  font-family: "New York", "Iowan Old Style", "Palatino", Georgia, serif;
  font-size: clamp(30px, 8.5vw, 46px); line-height: 1.12; font-weight: 500; letter-spacing: -0.01em;
  overflow-wrap: anywhere;
}
.word.long { font-size: clamp(22px, 5.5vw, 30px); }
.sub { margin-top: 10px; color: var(--ink-2); font-size: 15px; }
.note { margin-top: 18px; font-size: 14px; color: var(--ink-3); font-style: italic; }
.face .foot { font-size: 12.5px; color: var(--ink-3); text-align: center; }

/* back face content */
.back .body { justify-content: flex-start; align-items: stretch; text-align: left; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-top: 6px; }
.meaning {
  font-family: "New York", "Iowan Old Style", "Palatino", Georgia, serif;
  font-size: clamp(24px, 6.5vw, 34px); line-height: 1.18; font-weight: 500; letter-spacing: -0.01em;
  color: var(--accent);
}
.lemma { margin-top: 10px; font-size: 15px; color: var(--ink-2); }
.lemma b { color: var(--ink); font-weight: 600; }
.forms { margin-top: 4px; font-size: 14px; color: var(--ink-3); font-variant-numeric: tabular-nums; }
.example { margin-top: 20px; padding: 14px 16px; border-radius: 16px; background: var(--bg); border: 1px solid var(--line); }
.example .da { font-size: 16px; line-height: 1.4; }
.example .da mark { background: none; color: var(--accent); font-weight: 600; }
.example .en { margin-top: 6px; font-size: 13.5px; color: var(--ink-2); line-height: 1.4; }
.notes { margin-top: 14px; font-size: 13.5px; color: var(--ink-2); line-height: 1.45; display: flex; gap: 8px; }
.notes::before { content: "✦"; color: var(--gold); flex: none; }

/* swipe feedback stamps */
.stamp {
  position: absolute; top: 26px; padding: 8px 14px; border-radius: 12px; font-weight: 800; font-size: 20px; letter-spacing: 0.06em;
  border: 3px solid currentColor; opacity: 0; transform: rotate(-12deg); pointer-events: none;
}
.stamp.yes { left: 22px; color: var(--yes); }
.stamp.no { right: 22px; color: var(--no); transform: rotate(12deg); }
.face.back .stamp.yes { left: auto; right: 22px; transform: rotate(12deg); }
.face.back .stamp.no { right: auto; left: 22px; transform: rotate(-12deg); }
.card[data-tilt="yes"] .face { box-shadow: var(--shadow), 0 0 0 3px var(--yes-soft) inset; }
.card[data-tilt="no"]  .face { box-shadow: var(--shadow), 0 0 0 3px var(--no-soft) inset; }

/* ---- controls ---- */
.controls { display: flex; justify-content: center; align-items: center; gap: 14px; padding-top: 4px; }
.ctl {
  width: 56px; height: 56px; border-radius: 50%; display: grid; place-items: center;
  background: var(--card); border: 1px solid var(--line); color: var(--ink-2);
  box-shadow: 0 6px 16px -8px rgba(0,0,0,0.3);
  transition: transform 0.12s, background 0.15s, color 0.15s;
}
.ctl:active { transform: scale(0.92); }
.ctl-no { width: 68px; height: 68px; color: var(--no); }
.ctl-yes { width: 68px; height: 68px; color: var(--yes); }
.ctl-no:active { background: var(--no-soft); }
.ctl-yes:active { background: var(--yes-soft); }
.ctl-undo, .ctl-flip { width: 48px; height: 48px; }
.ctl-spacer { width: 48px; }
.ctl:disabled { opacity: 0.35; }

/* ---- empty / done ---- */
.empty {
  position: absolute; inset: 18px 0 14px; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  padding: 24px; border-radius: var(--radius); background: var(--card); border: 1px dashed var(--line);
}
.empty h2 { font-family: "New York", "Iowan Old Style", Palatino, Georgia, serif; font-weight: 500; font-size: 28px; margin: 0 0 6px; }
.empty p { color: var(--ink-2); margin: 0 0 20px; line-height: 1.45; }
.empty .num { font-size: 40px; font-weight: 700; font-variant-numeric: tabular-nums; }
.empty .row2 { display: flex; gap: 28px; margin-bottom: 22px; }
.empty .row2 .lab { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--ink-3); }
.empty .yes-c { color: var(--yes); } .empty .no-c { color: var(--no); }
.btn { padding: 12px 20px; border-radius: 14px; background: var(--accent); color: #fff; font-weight: 600; }
.btn.secondary { background: var(--bg-2); color: var(--ink); margin-top: 10px; }
.btn:active { transform: scale(0.97); }

/* ---- sheet ---- */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.35); z-index: 10; animation: fade 0.2s; }
@keyframes fade { from { opacity: 0; } }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 11; max-width: 560px; margin: 0 auto;
  background: var(--card); border-radius: 22px 22px 0 0; padding: 10px 22px calc(var(--sab) + 22px);
  box-shadow: 0 -10px 40px rgba(0,0,0,0.25);
  animation: slide 0.3s cubic-bezier(.2,.8,.2,1);
}
@keyframes slide { from { transform: translateY(40px); opacity: 0; } }
.sheet-handle { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 0 auto 14px; }
.sheet h2 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--ink-3); margin: 18px 0 8px; font-weight: 600; }
.seg { display: flex; padding: 3px; border-radius: 12px; background: var(--bg-2); gap: 2px; }
.seg-btn { flex: 1; padding: 9px 8px; border-radius: 10px; font-size: 14px; font-weight: 500; color: var(--ink-2); transition: background 0.15s, color 0.15s; white-space: nowrap; }
.seg-btn[aria-checked="true"] { background: var(--card); color: var(--ink); box-shadow: 0 1px 4px rgba(0,0,0,0.12); }
.seg.small { flex: none; } .seg.small .seg-btn { padding: 7px 12px; font-size: 13px; }
.row { display: flex; align-items: center; justify-content: space-between; padding: 8px 0; font-size: 15px; }
.stats { display: flex; gap: 14px; margin-top: 10px; font-size: 13.5px; color: var(--ink-2); }
.stats b { color: var(--ink); }
.muted { color: var(--ink-3); font-size: 13px; }
.text-btn { font-size: 14px; font-weight: 600; padding: 8px 12px; border-radius: 10px; }
.text-btn.danger { color: var(--accent); }
.hint { font-size: 13px; color: var(--ink-3); line-height: 1.45; margin: 14px 0 0; }

@media (prefers-reduced-motion: reduce) {
  .card, .progress-bar, .sheet, .sheet-backdrop { transition: none !important; animation: none !important; }
}
