:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #edeff4;
  --text: #2e3856;
  --muted: #586380;
  --border: #d9dde8;
  --primary: #4255ff;
  --primary-press: #3447e0;
  --primary-soft: #e3e7ff;
  --good: #18ae79;
  --good-soft: #dff6ee;
  --bad: #ff725b;
  --bad-soft: #ffe9e5;
  --warn: #ffcd1f;
  --shadow: 0 2px 10px rgba(46, 56, 86, .08);
  --shadow-lg: 0 10px 30px rgba(46, 56, 86, .14);
  --radius: 16px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0a092d;
    --surface: #2e3856;
    --surface-2: #1d2346;
    --text: #f6f7fb;
    --muted: #a8b1cc;
    --border: #3f4a70;
    --primary: #4255ff;
    --primary-press: #5a6bff;
    --primary-soft: #262d63;
    --good: #23c48b;
    --good-soft: #10392f;
    --bad: #ff8a75;
    --bad-soft: #43262a;
    --shadow: 0 2px 10px rgba(0, 0, 0, .3);
    --shadow-lg: 0 10px 30px rgba(0, 0, 0, .45);
    color-scheme: dark;
  }
}

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

html, body { margin: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;
}

button, input, textarea, select { font: inherit; color: inherit; }
button { touch-action: manipulation; cursor: pointer; }

#app {
  max-width: 640px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ---------- top bar ---------- */
.bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(var(--safe-top) + 8px) 12px 8px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
}
.bar h1 { flex: 1; margin: 0; font-size: 17px; font-weight: 700; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar .spacer { width: 44px; }
.brand { display: flex; align-items: center; gap: 8px; flex: 1; font-weight: 800; font-size: 22px; }
.brand-mark { width: 32px; height: 32px; border-radius: 9px; background: var(--primary); color: #fff; display: grid; place-items: center; font-size: 18px; }

.icon-btn {
  width: 44px; height: 44px;
  border: 0; border-radius: 50%;
  background: transparent;
  display: grid; place-items: center;
  flex-shrink: 0;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg { width: 24px; height: 24px; }
.icon-btn.on { color: var(--primary); }

.content { flex: 1; padding: 8px 16px calc(var(--safe-bottom) + 24px); }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 20px;
  border-radius: 12px; border: 2px solid transparent;
  background: var(--primary); color: #fff; font-weight: 700;
  transition: transform .08s ease, background .15s;
  text-decoration: none;
}
.btn:active { transform: scale(.97); background: var(--primary-press); }
.btn.secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn.secondary:active { background: var(--surface-2); }
.btn.ghost { background: transparent; color: var(--primary); }
.btn.danger { background: var(--bad-soft); color: var(--bad); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .45; pointer-events: none; }

/* ---------- home ---------- */
.section-title { font-size: 14px; font-weight: 700; color: var(--muted); margin: 20px 4px 10px; text-transform: uppercase; letter-spacing: .04em; }

.set-card {
  display: block; width: 100%; text-align: left;
  background: var(--surface); border: 2px solid transparent; border-radius: var(--radius);
  padding: 16px; margin-bottom: 12px; box-shadow: var(--shadow);
  transition: transform .08s;
}
.set-card:active { transform: scale(.98); border-color: var(--primary-soft); }
.set-card h3 { margin: 0 0 6px; font-size: 18px; }
.set-meta { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 14px; }
.pill { background: var(--surface-2); border-radius: 999px; padding: 2px 10px; font-weight: 600; font-size: 13px; color: var(--text); }
.mini-progress { flex: 1; height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.mini-progress > i { display: block; height: 100%; background: var(--good); border-radius: 3px; }

.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.empty .big { font-size: 56px; margin-bottom: 8px; }

.fab {
  position: fixed;
  right: max(16px, calc(50% - 320px + 16px));
  bottom: calc(var(--safe-bottom) + 20px);
  width: 60px; height: 60px; border-radius: 50%;
  border: 0; background: var(--primary); color: #fff;
  box-shadow: var(--shadow-lg);
  display: grid; place-items: center; z-index: 20;
}
.fab:active { transform: scale(.94); }
.fab svg { width: 28px; height: 28px; }

/* ---------- set page ---------- */
.set-head h2 { margin: 4px 0 4px; font-size: 26px; line-height: 1.2; }
.set-head p { margin: 0 0 4px; color: var(--muted); }

.modes { display: grid; grid-template-columns: 1fr; gap: 10px; margin: 18px 0; }
.mode {
  display: flex; align-items: center; gap: 14px;
  background: var(--surface); border: 0; border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow); text-align: left;
  transition: transform .08s;
}
.mode:active { transform: scale(.98); }
.mode-icon { width: 44px; height: 44px; border-radius: 12px; display: grid; place-items: center; font-size: 22px; flex-shrink: 0; }
.mode b { display: block; font-size: 17px; }
.mode small { color: var(--muted); }
.mode .chev { margin-left: auto; color: var(--muted); }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 8px 0 4px; }
.stat { background: var(--surface); border-radius: 12px; padding: 10px; text-align: center; box-shadow: var(--shadow); }
.stat b { display: block; font-size: 22px; }
.stat span { font-size: 12px; color: var(--muted); font-weight: 600; }

.term-row {
  display: flex; gap: 12px; align-items: flex-start;
  background: var(--surface); border-radius: 12px; padding: 14px; margin-bottom: 8px;
  box-shadow: var(--shadow);
}
.term-row .t { flex: 1; min-width: 0; }
.term-row .t b { display: block; font-weight: 600; word-wrap: break-word; }
.term-row .t span { display: block; color: var(--muted); margin-top: 4px; word-wrap: break-word; white-space: pre-wrap; }
.star { color: var(--muted); }
.star.on { color: var(--warn); }
.star svg { width: 22px; height: 22px; }

.row-actions { display: flex; gap: 8px; margin: 14px 0; }
.row-actions .btn { flex: 1; }

/* ---------- editor ---------- */
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 13px; font-weight: 700; color: var(--muted); margin: 0 4px 6px; text-transform: uppercase; letter-spacing: .04em; }
.input, textarea.input {
  width: 100%; font-size: 16px;
  background: var(--surface); border: 2px solid var(--border); border-radius: 12px;
  padding: 12px 14px; outline: none; resize: none;
}
.input:focus { border-color: var(--primary); }

.edit-card { background: var(--surface); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 12px; box-shadow: var(--shadow); }
.edit-card-head { display: flex; align-items: center; justify-content: space-between; color: var(--muted); font-weight: 700; font-size: 14px; }
.edit-card textarea {
  display: block; width: 100%; border: 0; border-bottom: 2px solid var(--border);
  background: transparent; padding: 8px 2px; font-size: 16px; resize: none; outline: none; min-height: 40px;
}
.edit-card textarea:focus { border-bottom-color: var(--primary); }
.edit-card label { display: block; font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; margin: 2px 2px 10px; }

.save-bar {
  position: sticky; bottom: 0;
  padding: 12px 0 calc(var(--safe-bottom) + 12px);
  background: linear-gradient(transparent, var(--bg) 30%);
}

/* ---------- sheets ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(10, 9, 45, .45);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade .2s ease;
}
.sheet {
  width: 100%; max-width: 640px; max-height: 88dvh; overflow: auto;
  background: var(--bg); border-radius: 22px 22px 0 0;
  padding: 10px 16px calc(var(--safe-bottom) + 20px);
  animation: up .25s cubic-bezier(.2, .9, .3, 1.1);
}
.sheet .grabber { width: 40px; height: 5px; border-radius: 3px; background: var(--border); margin: 0 auto 12px; }
.sheet h3 { margin: 4px 0 14px; font-size: 20px; }
@keyframes fade { from { opacity: 0; } }
@keyframes up { from { transform: translateY(100%); } }

.opt-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--surface); border-radius: 12px; padding: 12px 14px; margin-bottom: 8px; min-height: 52px;
}
.opt-row > span { font-weight: 600; }
.opt-row small { display: block; color: var(--muted); font-weight: 400; font-size: 13px; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; }
.seg button { border: 0; background: transparent; padding: 7px 12px; border-radius: 8px; font-weight: 600; font-size: 14px; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.toggle { position: relative; width: 51px; height: 31px; flex-shrink: 0; }
.toggle input { opacity: 0; width: 0; height: 0; position: absolute; }
.toggle i { position: absolute; inset: 0; border-radius: 16px; background: var(--border); transition: background .2s; }
.toggle i::after { content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%; background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,.2); transition: transform .2s; }
.toggle input:checked + i { background: var(--good); }
.toggle input:checked + i::after { transform: translateX(20px); }

/* ---------- study shared ---------- */
.study { flex: 1; display: flex; flex-direction: column; padding: 0 16px calc(var(--safe-bottom) + 16px); }
.progress-track { height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; margin: 2px 0 12px; }
.progress-track > i { display: block; height: 100%; background: var(--primary); border-radius: 4px; transition: width .35s ease; }
.progress-label { text-align: center; font-weight: 700; color: var(--muted); font-size: 14px; }

/* ---------- flashcards ---------- */
.fc-counts { display: flex; justify-content: space-between; align-items: center; margin: 6px 0 12px; font-weight: 700; }
.count-chip { min-width: 44px; padding: 4px 12px; border-radius: 999px; text-align: center; border: 2px solid; }
.count-chip.bad { color: var(--bad); border-color: var(--bad); }
.count-chip.good { color: var(--good); border-color: var(--good); }

.fc-stage { flex: 1; position: relative; perspective: 1400px; min-height: 340px; margin-bottom: 16px; }
.fc {
  position: absolute; inset: 0;
  touch-action: none; user-select: none; -webkit-user-select: none;
  will-change: transform;
}
.fc-inner { position: absolute; inset: 0; transform-style: preserve-3d; transition: transform .45s cubic-bezier(.3, .7, .3, 1.15); }
.fc.flipped .fc-inner { transform: rotateY(180deg); }
.fc-face {
  position: absolute; inset: 0;
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
  background: var(--surface); border-radius: 20px; box-shadow: var(--shadow-lg);
  display: flex; align-items: center; justify-content: center; text-align: center;
  padding: 56px 24px; font-size: 26px; font-weight: 500; line-height: 1.3;
  overflow: auto; white-space: pre-wrap; word-wrap: break-word;
  border: 3px solid transparent; transition: border-color .15s;
}
.fc-face.back { transform: rotateY(180deg); }
.fc-face .corner { position: absolute; top: 10px; left: 16px; font-size: 13px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.fc-face .face-actions { position: absolute; top: 4px; right: 4px; display: flex; }
.fc .stamp {
  position: absolute; top: 18px; padding: 6px 14px; border-radius: 10px; font-weight: 800; font-size: 18px;
  opacity: 0; z-index: 3; pointer-events: none; border: 3px solid;
}
.fc .stamp.know { left: 18px; color: var(--good); border-color: var(--good); transform: rotate(-10deg); background: var(--surface); }
.fc .stamp.learn { right: 18px; color: var(--bad); border-color: var(--bad); transform: rotate(10deg); background: var(--surface); }
.fc.fly { transition: transform .35s ease-in, opacity .35s ease-in; }
.fc.enter { animation: cardIn .3s cubic-bezier(.2, .9, .3, 1.2); }
@keyframes cardIn { from { transform: scale(.9) translateY(20px); opacity: 0; } }

.fc-controls { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.round-btn {
  width: 64px; height: 64px; border-radius: 50%;
  border: 2px solid var(--border); background: var(--surface);
  display: grid; place-items: center; transition: transform .08s;
}
.round-btn:active { transform: scale(.92); }
.round-btn svg { width: 28px; height: 28px; }
.round-btn.bad { color: var(--bad); border-color: var(--bad); }
.round-btn.good { color: var(--good); border-color: var(--good); }
.fc-hint { text-align: center; color: var(--muted); font-size: 13px; margin-top: 10px; }

/* ---------- learn & test questions ---------- */
.q-card { background: var(--surface); border-radius: 20px; padding: 18px; box-shadow: var(--shadow); margin-bottom: 14px; }
.q-label { font-size: 13px; font-weight: 700; color: var(--muted); display: flex; justify-content: space-between; align-items: center; min-height: 28px; }
.q-prompt { font-size: 22px; font-weight: 500; margin: 10px 0 24px; white-space: pre-wrap; word-wrap: break-word; }
.q-sub { font-size: 14px; font-weight: 700; color: var(--muted); margin-bottom: 10px; }

.choices { display: grid; gap: 10px; }
.choice {
  display: flex; align-items: center; gap: 12px;
  width: 100%; min-height: 56px; padding: 12px 14px;
  border: 2px solid var(--border); border-radius: 12px; background: var(--surface);
  text-align: left; font-size: 17px; transition: transform .08s, border-color .15s, background .15s;
  word-wrap: break-word;
}
.choice:active { transform: scale(.98); }
.choice .num { width: 26px; height: 26px; border-radius: 50%; background: var(--surface-2); display: grid; place-items: center; font-size: 13px; font-weight: 700; flex-shrink: 0; color: var(--muted); }
.choice.selected { border-color: var(--primary); background: var(--primary-soft); }
.choice.correct { border-color: var(--good); background: var(--good-soft); }
.choice.correct .num { background: var(--good); color: #fff; }
.choice.wrong { border-color: var(--bad); background: var(--bad-soft); animation: shake .4s; }
.choice.wrong .num { background: var(--bad); color: #fff; }
.choice.dim { opacity: .5; }
.choices.locked .choice { pointer-events: none; }

.tf { grid-template-columns: 1fr 1fr; }
.tf .choice { justify-content: center; font-weight: 700; }

.write-row { display: flex; gap: 8px; }
.write-row .input { flex: 1; }
.input.correct { border-color: var(--good); background: var(--good-soft); }
.input.wrong { border-color: var(--bad); background: var(--bad-soft); animation: shake .4s; }

@keyframes shake {
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}

.feedback { border-radius: 14px; padding: 14px; margin-top: 14px; animation: pop .3s cubic-bezier(.2, .9, .3, 1.3); }
.feedback.good { background: var(--good-soft); }
.feedback.bad { background: var(--bad-soft); }
.feedback .fb-title { font-weight: 800; font-size: 17px; display: flex; align-items: center; gap: 8px; }
.feedback.good .fb-title { color: var(--good); }
.feedback.bad .fb-title { color: var(--bad); }
.feedback .answer { margin-top: 8px; font-size: 17px; white-space: pre-wrap; }
.feedback .yours { margin-top: 6px; color: var(--muted); font-size: 15px; text-decoration: line-through; }
@keyframes pop { from { transform: scale(.9); opacity: 0; } }

.continue-bar { position: sticky; bottom: 0; padding: 10px 0 calc(var(--safe-bottom) + 10px); background: linear-gradient(transparent, var(--bg) 25%); }

.streak { display: inline-flex; align-items: center; gap: 4px; color: #ff8a00; font-weight: 800; animation: pop .3s; }

.praise {
  position: fixed; left: 50%; top: calc(var(--safe-top) + 120px); z-index: 40;
  transform: translate(-50%, -50%);
  font-size: 22px; font-weight: 900; color: var(--good);
  background: var(--surface); padding: 10px 20px; border-radius: 999px; box-shadow: var(--shadow-lg);
  pointer-events: none; white-space: nowrap;
  animation: praise 1.1s ease forwards;
}
@keyframes praise {
  0% { opacity: 0; transform: translate(-50%, -30%) scale(.6); }
  25% { opacity: 1; transform: translate(-50%, -50%) scale(1.1); }
  70% { opacity: 1; transform: translate(-50%, -60%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -80%) scale(1); }
}

/* ---------- match ---------- */
.match { display: grid; grid-template-columns: 1.5fr 1fr; gap: 10px; margin-top: 12px; }
.match.by-def { grid-template-columns: 1fr 1.4fr; }
.match-col { display: flex; flex-direction: column; gap: 10px; }
.tile {
  flex: 1; min-height: 64px; padding: 10px 12px;
  border: 2px solid var(--border); border-radius: 14px; background: var(--surface);
  font-size: 15px; font-weight: 600; line-height: 1.25; text-align: center;
  box-shadow: var(--shadow); word-wrap: break-word; hyphens: auto;
  transition: transform .1s, border-color .15s, background .15s, opacity .35s;
}
.match-col.right .tile { color: var(--primary); }
.match.by-def .match-col.right .tile { font-size: 13px; font-weight: 500; color: var(--text); text-align: left; }
.tile:active { transform: scale(.96); }
.tile.selected { border-color: var(--primary); background: var(--primary-soft); transform: scale(1.03); }
.tile.wrong { border-color: var(--bad); background: var(--bad-soft); animation: shake .4s; }
.tile.correct { border-color: var(--good); background: var(--good-soft); animation: pop .3s; }
.tile.done { opacity: .35; pointer-events: none; box-shadow: none; }
.match .tile.done { transition-delay: .25s; }

/* ---------- summaries ---------- */
.summary { text-align: center; padding: 16px 0; }
.summary .big { font-size: 64px; line-height: 1; margin: 10px 0; }
.summary h2 { margin: 6px 0; font-size: 26px; }
.summary p { color: var(--muted); margin: 4px 0 18px; }
.summary .btn { margin-top: 10px; }

.ring { width: 140px; height: 140px; margin: 12px auto; position: relative; }
.ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 12; stroke-linecap: round; }
.ring .bg { stroke: var(--surface-2); }
.ring .fg { stroke: var(--good); transition: stroke-dashoffset 1s cubic-bezier(.3, .7, .3, 1); }
.ring b { position: absolute; inset: 0; display: grid; place-items: center; font-size: 32px; font-weight: 800; }

.mastery { display: grid; gap: 8px; margin: 16px 0; text-align: left; }
.mastery-row { display: flex; align-items: center; gap: 10px; background: var(--surface); border-radius: 12px; padding: 12px 14px; font-weight: 600; }
.mastery-row .dot { width: 12px; height: 12px; border-radius: 50%; }
.mastery-row b { margin-left: auto; }

.review-list { text-align: left; margin-top: 12px; }

/* ---------- login ---------- */
.login { flex: 1; display: flex; flex-direction: column; justify-content: center; padding: 24px; gap: 14px; }
.login .brand { justify-content: center; font-size: 30px; margin-bottom: 18px; flex: 0; }
.login .error { color: var(--bad); text-align: center; min-height: 20px; font-weight: 600; }

/* ---------- toast & confetti ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 84px); z-index: 100;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); font-weight: 600;
  padding: 12px 18px; border-radius: 12px; box-shadow: var(--shadow-lg);
  transition: transform .3s cubic-bezier(.2, .9, .3, 1.2), opacity .2s; max-width: calc(100% - 32px);
}
#toast.show { transform: translate(-50%, 0); opacity: 1; }

.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 60; }


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