:root {
  --bg: #eef6fc;
  --card: #ffffff;
  --text: #0f2536;
  --muted: #5b7488;
  --line: #d5e6f3;
  --accent: #0284c7;
  --accent-soft: #e0f2fe;
  --on-accent: #ffffff;
  --good: #16a34a;
  --good-soft: #dcfce7;
  --bad: #dc2626;
  --bad-soft: #fee2e2;
  --warn-soft: #fef3c7;
  --shadow: 0 2px 10px rgba(10, 60, 100, .08);
  --radius: 16px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1724;
    --card: #132332;
    --text: #e6f2fa;
    --muted: #8ea8bb;
    --line: #21394d;
    --accent: #38bdf8;
    --accent-soft: #0c3550;
    --on-accent: #04263a;
    --good: #4ade80;
    --good-soft: #143a24;
    --bad: #f87171;
    --bad-soft: #3d1a1a;
    --warn-soft: #3a3014;
    --shadow: none;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
}
a { color: var(--accent); text-decoration: none; }
button, input, textarea, select { font: inherit; color: inherit; }

.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
}
.top h1 { flex: 1; margin: 0; font-size: 20px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.logo { font-size: 24px; }
.top-actions { display: flex; gap: 4px; }
main { max-width: 640px; margin: 0 auto; padding: 4px 16px 32px; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 40px; height: 40px; border-radius: 12px;
  border: 0; background: transparent; color: var(--text);
  font-size: 22px; cursor: pointer;
}
.icon-btn:active { background: var(--line); }
.icon-btn.small { width: 30px; height: 30px; font-size: 14px; color: var(--muted); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 46px; padding: 10px 16px;
  border: 1px solid var(--line); border-radius: 14px;
  background: var(--card); color: var(--text);
  font-weight: 600; cursor: pointer; text-align: center;
  box-shadow: var(--shadow);
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .55; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.good { background: var(--good-soft); border-color: transparent; color: var(--good); }
.btn.bad { background: var(--bad-soft); border-color: transparent; color: var(--bad); }
.btn.wide { width: 100%; }
.link { border: 0; background: none; color: var(--accent); padding: 8px; cursor: pointer; font-weight: 600; }

.row { display: flex; gap: 10px; flex-wrap: wrap; }
.row > .btn { flex: 1; }
.stack { display: grid; gap: 10px; }
.mt { margin-top: 16px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.hidden { display: none !important; }
.hint { color: var(--muted); font-size: 14px; }

.panel {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow);
}

.set-item {
  display: block; margin-top: 10px; padding: 14px 16px;
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); color: var(--text); box-shadow: var(--shadow);
}
.set-title { font-weight: 700; font-size: 17px; }
.bar { height: 6px; border-radius: 6px; background: var(--line); overflow: hidden; margin-top: 10px; }
.bar > div { height: 100%; background: var(--good); border-radius: 6px; transition: width .3s; }
.bar.accent > div { background: var(--accent); }

.empty { text-align: center; padding: 40px 10px; color: var(--muted); }
.empty .big { font-size: 48px; }

.modes { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 12px 0 16px; }
.mode {
  display: flex; flex-direction: column; gap: 4px; padding: 14px;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  color: var(--text); font-weight: 600; box-shadow: var(--shadow);
}
.mode .ico { font-size: 26px; }
.mode.ai { grid-column: span 2; background: linear-gradient(135deg, var(--accent-soft), var(--card)); }
.mode small { color: var(--muted); font-weight: 400; }

.seg { display: flex; background: var(--line); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; background: none; padding: 8px 4px; border-radius: 9px; font-size: 13px; font-weight: 600; color: var(--muted); cursor: pointer; }
.seg button.on { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

.card-row { display: grid; grid-template-columns: 1fr 1fr auto; gap: 10px; align-items: start; padding: 12px 0; border-bottom: 1px solid var(--line); }
.card-row:last-child { border-bottom: 0; }
.dot { width: 9px; height: 9px; border-radius: 50%; margin-top: 7px; background: var(--line); }
.dot.learning { background: #f59e0b; }
.dot.known { background: var(--good); }

.field { display: grid; gap: 6px; margin-top: 12px; }
.field > span { font-size: 14px; color: var(--muted); font-weight: 600; }
input[type=text], input[type=password], input:not([type]), textarea, select {
  width: 100%; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--card);
  outline: none; font-size: 16px;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }
textarea { resize: vertical; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.edit-card { margin-top: 12px; padding: 10px 12px 12px; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); display: grid; gap: 8px; }
.edit-head { display: flex; justify-content: space-between; align-items: center; }
.sticky-bottom { position: sticky; bottom: 0; padding: 12px 0 calc(12px + env(safe-area-inset-bottom)); background: linear-gradient(transparent, var(--bg) 30%); }

details.howto { background: var(--accent-soft); border-radius: var(--radius); padding: 12px 14px; }
details.howto summary { font-weight: 700; cursor: pointer; }
details.howto ol { margin: 8px 0 0; padding-left: 20px; }
.preview { margin-top: 12px; font-size: 14px; }
.preview table { width: 100%; border-collapse: collapse; }
.preview td { padding: 6px 4px; border-bottom: 1px solid var(--line); vertical-align: top; }

/* flashcards */
.flip-wrap { perspective: 1200px; margin: 12px 0 16px; }
.flip { position: relative; height: 340px; transition: transform .45s; transform-style: preserve-3d; cursor: pointer; }
.flip.flipped { transform: rotateY(180deg); }
.face {
  position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden;
  display: grid; place-items: center; padding: 24px; text-align: center;
  background: var(--card); border: 1px solid var(--line); border-radius: 22px; box-shadow: var(--shadow);
  font-size: 26px; font-weight: 600; overflow-wrap: anywhere; white-space: pre-wrap;
}
.face.back { transform: rotateY(180deg); background: var(--accent-soft); }
.face .speak { position: absolute; top: 10px; right: 10px; }
.face .side { position: absolute; top: 16px; left: 18px; font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .05em; }
.swipe-hint { text-align: center; color: var(--muted); font-size: 13px; margin-top: -6px; margin-bottom: 12px; }

/* question */
.qcard { padding: 22px 18px; margin: 12px 0; background: var(--card); border: 1px solid var(--line); border-radius: 20px; box-shadow: var(--shadow); }
.qcard .q { font-size: 24px; font-weight: 650; margin-top: 6px; overflow-wrap: anywhere; white-space: pre-wrap; }
.answer { display: grid; gap: 10px; }
.verdict { padding: 14px 16px; border-radius: 16px; margin: 10px 0; }
.verdict.ok { background: var(--good-soft); }
.verdict.no { background: var(--bad-soft); }
.verdict.ai { background: var(--accent-soft); }
.verdict b.title { display: block; font-size: 18px; margin-bottom: 4px; }
.verdict.ok b.title { color: var(--good); }
.verdict.no b.title { color: var(--bad); }
.verdict .line { margin-top: 4px; overflow-wrap: anywhere; }

.options { display: grid; gap: 10px; }
.opt { text-align: left; justify-content: flex-start; font-weight: 500; min-height: 54px; overflow-wrap: anywhere; }
.opt.right { background: var(--good-soft); border-color: var(--good); color: var(--text); }
.opt.wrong { background: var(--bad-soft); border-color: var(--bad); color: var(--text); }

/* match */
.match-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 12px; }
.tile {
  min-height: 92px; padding: 8px; border-radius: 14px;
  border: 2px solid var(--line); background: var(--card);
  font-size: 14px; font-weight: 600; cursor: pointer; overflow-wrap: anywhere;
  transition: opacity .25s, transform .15s;
}
.tile.sel { border-color: var(--accent); background: var(--accent-soft); }
.tile.bad { border-color: var(--bad); background: var(--bad-soft); animation: shake .3s; }
.tile.gone { opacity: 0; transform: scale(.85); pointer-events: none; }
@keyframes shake { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }
.timer { font-variant-numeric: tabular-nums; font-weight: 700; font-size: 18px; }

.result-big { text-align: center; padding: 28px 10px 10px; }
.result-big .num { font-size: 52px; font-weight: 800; }
.mistakes { margin-top: 12px; }
.mistakes div { padding: 8px 0; border-bottom: 1px solid var(--line); }

.badge { display: inline-block; font-size: 12px; font-weight: 700; padding: 3px 9px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); }
.spinner { width: 34px; height: 34px; border: 4px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; margin: 30px auto 12px; }
@keyframes spin { to { transform: rotate(360deg); } }

#toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px;
  font-size: 14px; opacity: 0; pointer-events: none; transition: .25s; max-width: calc(100% - 32px); z-index: 20;
}
#toast.show { opacity: 1; transform: translateX(-50%); }
.sync-key { font-family: ui-monospace, Menlo, monospace; font-size: 17px; letter-spacing: 1px; text-align: center; padding: 12px; margin-top: 10px; border-radius: 12px; background: var(--accent-soft); word-break: break-all; user-select: all; -webkit-user-select: all; }
h2.section { font-size: 15px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 22px 4px 8px; }
.folder-item .set-title { font-weight: 700; }
.folder-line { display: block; margin: 10px 0 2px; color: var(--accent); font-weight: 600; }
label.check { display: flex; gap: 12px; align-items: center; padding: 10px 2px; border-bottom: 1px solid var(--line); cursor: pointer; }
label.check:last-child { border-bottom: 0; }
label.check input { width: 22px; height: 22px; accent-color: var(--accent); flex: none; margin: 0; }
