:root {
  --bg: #f4f4f8;
  --surface: #ffffff;
  --surface-2: #ececf3;
  --text: #17171c;
  --muted: #6b6b7b;
  --border: #dedee8;
  --accent: #4f46e5;
  --accent-soft: #e6e5ff;
  --again: #e5484d;
  --hard: #f08c00;
  --good: #2f9e66;
  --easy: #3b82f6;
  --new: #3b82f6;
  --learning: #e5484d;
  --review: #2f9e66;
  --shadow: 0 10px 30px rgba(20, 20, 50, .10), 0 2px 6px rgba(20, 20, 50, .06);
  --radius: 22px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f0f14;
    --surface: #1b1b23;
    --surface-2: #262632;
    --text: #ececf2;
    --muted: #9a9aab;
    --border: #2e2e3a;
    --accent: #8b85ff;
    --accent-soft: #2a2850;
    --shadow: 0 10px 30px rgba(0, 0, 0, .45);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0 0 .5rem; }
.muted { color: var(--muted); }
.error { color: var(--again); margin: 0; }

.splash { position: fixed; inset: 0; display: grid; place-items: center; }
.spinner { width: 36px; height: 36px; border-radius: 50%; border: 3px solid var(--border); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.btn { border: 1px solid var(--border); background: var(--surface); border-radius: 12px; padding: .7rem 1rem; font-weight: 600; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--again); }
.btn.small { padding: .4rem .7rem; font-size: .9rem; border-radius: 10px; }
.btn:disabled { opacity: .5; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 1.5rem; }
.login-box { width: 100%; max-width: 360px; background: var(--surface); border-radius: var(--radius); padding: 2rem 1.5rem; box-shadow: var(--shadow); display: grid; gap: .9rem; text-align: center; }
.login-logo { width: 72px; height: 72px; margin: 0 auto; border-radius: 18px; }
.login-box h1 { margin: 0; font-size: 1.5rem; }
.login-box p { margin: 0; }
.login-box label { display: grid; gap: .35rem; text-align: left; font-size: .9rem; color: var(--muted); }
input[type=text], input[type=password], input[type=search], input[type=number], input:not([type]), select {
  border: 1px solid var(--border); background: var(--bg); border-radius: 12px; padding: .7rem .8rem; font-size: 16px; width: 100%;
}

/* App layout */
.app { height: 100dvh; display: grid; grid-template-rows: auto 1fr auto; }
.topbar { display: grid; gap: .45rem; padding: calc(var(--safe-top) + .55rem) 1rem .4rem; }
.topbar-row { display: flex; align-items: center; justify-content: space-between; gap: .6rem; }
.topbar.compact .counts, .topbar.compact .day-progress, .topbar.compact .focus { display: none; }
.topbar.compact .topbar-row { justify-content: flex-end; }
.focus button { padding: .35rem .8rem; }
.counts { display: flex; align-items: center; gap: .5rem; font-weight: 700; font-variant-numeric: tabular-nums; font-size: .92rem; }
.deck-count { display: inline-flex; align-items: center; gap: .45rem; background: var(--surface); border-radius: 10px; padding: .25rem .55rem; }
.deck-count.off { opacity: .35; }
.deck-name { font-size: .72rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.legend-mini { margin-left: auto; font-size: .66rem; font-weight: 500; display: inline-flex; gap: .35rem; }
.legend-mini i { font-style: normal; }
.day-progress { height: 3px; border-radius: 2px; background: var(--surface-2); overflow: hidden; }
.day-progress i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .3s; }
.c-new { color: var(--new); } .c-learn { color: var(--learning); } .c-rev { color: var(--review); }
.sync-indicator { border: 0; background: var(--surface-2); border-radius: 999px; padding: .3rem .7rem; font-size: .78rem; color: var(--muted); display: inline-flex; gap: .35rem; align-items: center; }
.sync-indicator::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--good); }
.sync-indicator.pending::before { background: var(--hard); }
.sync-indicator.offline::before { background: var(--muted); }
.sync-indicator.error::before { background: var(--again); }
.sync-indicator.syncing::before { background: var(--easy); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .3; } }

main { min-height: 0; position: relative; }
.view { height: 100%; }
.scroll { overflow-y: auto; padding: .5rem 1rem 2rem; -webkit-overflow-scrolling: touch; }

.tabbar { display: grid; grid-template-columns: repeat(4, 1fr); background: var(--surface); border-top: 1px solid var(--border); padding-bottom: var(--safe-bottom); }
.tabbar button { border: 0; background: none; padding: .5rem 0 .55rem; display: grid; justify-items: center; gap: .15rem; font-size: .7rem; color: var(--muted); }
.tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabbar button.active { color: var(--accent); }

/* Learn */
.learn { display: grid; grid-template-rows: 1fr auto; }
.card-area { padding: .5rem 1rem; display: grid; min-height: 0; perspective: 1400px; }
.card { position: relative; width: 100%; max-width: 560px; justify-self: center; height: 100%; outline: none; user-select: none; -webkit-user-select: none; }
.card-inner { position: absolute; inset: 0; transition: transform .45s cubic-bezier(.3, .7, .2, 1); transform-style: preserve-3d; }
.card.flipped .card-inner { transform: rotateY(180deg); }
.card.no-anim .card-inner { transition: none; }
.card.enter { animation: enter .25s ease-out; }
@keyframes enter { from { opacity: 0; transform: translateY(14px) scale(.98); } }
.card-face {
  position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden;
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  display: flex; flex-direction: column; justify-content: center; align-items: center; gap: .8rem;
  padding: 2rem 1.4rem; text-align: center; overflow-y: auto;
}
.card-back { transform: rotateY(180deg); }
.card-face .tag { position: absolute; top: .9rem; left: 1rem; font-size: .72rem; color: var(--muted); letter-spacing: .02em; }
.card-face .side-label { position: absolute; top: .9rem; right: 1rem; font-size: .72rem; color: var(--muted); }
.primary-field { font-size: clamp(1.5rem, 7vw, 2.3rem); font-weight: 700; line-height: 1.25; overflow-wrap: anywhere; }
.primary-field.long { font-size: clamp(1.15rem, 5vw, 1.6rem); }
.fields { display: grid; gap: .45rem; width: 100%; max-width: 420px; text-align: left; }
.field { display: grid; grid-template-columns: 7.2rem 1fr; gap: .6rem; font-size: .95rem; padding-top: .45rem; border-top: 1px solid var(--border); }
.field .k { color: var(--muted); font-size: .8rem; padding-top: .1rem; }
.field .v { overflow-wrap: anywhere; }

.done { align-self: center; justify-self: center; text-align: center; display: grid; gap: .8rem; max-width: 360px; padding: 1rem; }
.done .big { font-size: 3rem; }
.done .actions { display: flex; flex-wrap: wrap; gap: .5rem; justify-content: center; }

.answer-bar { padding: .5rem .75rem .75rem; min-height: 5.2rem; display: grid; align-content: center; gap: .4rem; }
.flip-hint { text-align: center; color: var(--muted); margin: 0; font-size: .9rem; }
.rating { display: grid; grid-template-columns: repeat(4, 1fr); gap: .45rem; }
.rate { border: 0; border-radius: 14px; padding: .65rem .2rem; color: #fff; font-weight: 700; font-size: .92rem; display: grid; gap: .1rem; justify-items: center; transition: transform .08s; }
.rate:active { transform: scale(.95); }
.rate .iv { font-size: .72rem; font-weight: 500; opacity: .9; }
.rate.again { background: var(--again); } .rate.hard { background: var(--hard); } .rate.good { background: var(--good); } .rate.easy { background: var(--easy); }
.undo { justify-self: center; border: 0; background: none; color: var(--muted); font-size: .85rem; text-decoration: underline; padding: .1rem .5rem; }

/* Panels */
.panel { background: var(--surface); border-radius: 18px; padding: 1rem; margin-bottom: 1rem; box-shadow: 0 1px 2px rgba(0, 0, 0, .04); }
.panel h2 { font-size: 1.05rem; }
.panel h3 { font-size: .9rem; color: var(--muted); font-weight: 600; margin-top: .8rem; }
.panel p { margin: .3rem 0; }
.page-title { font-size: 1.6rem; margin: .3rem 0 1rem; }
.row { display: flex; align-items: center; justify-content: space-between; gap: .8rem; padding: .55rem 0; }
.row + .row { border-top: 1px solid var(--border); }
.row label { flex: 1; }
.row input[type=number] { width: 6rem; text-align: right; }
.row select { width: auto; }
.hstack { display: flex; flex-wrap: wrap; gap: .5rem; }

.seg { display: inline-grid; grid-auto-flow: column; background: var(--surface-2); border-radius: 10px; padding: 2px; }
.seg button { border: 0; background: none; padding: .35rem .6rem; border-radius: 8px; font-size: .82rem; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }

.chips { display: grid; grid-template-columns: repeat(auto-fill, minmax(2.6rem, 1fr)); gap: .35rem; }
.chip { border: 1px solid var(--border); background: var(--bg); border-radius: 10px; padding: .45rem 0; font-size: .85rem; font-variant-numeric: tabular-nums; }
.chip.on { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.band-head { display: flex; justify-content: space-between; align-items: center; margin: .9rem 0 .45rem; }
.band-head:first-of-type { margin-top: .3rem; }
.check { display: flex; align-items: center; gap: .55rem; padding: .35rem 0; }
.check input { width: 1.15rem; height: 1.15rem; accent-color: var(--accent); }
input[type=range] { width: 100%; accent-color: var(--accent); }

/* Stats */
.kpis { display: grid; grid-template-columns: repeat(2, 1fr); gap: .6rem; margin-bottom: 1rem; }
.kpi { background: var(--surface); border-radius: 16px; padding: .85rem 1rem; }
.kpi .n { font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.kpi .l { font-size: .8rem; color: var(--muted); }
.bars { display: flex; align-items: flex-end; gap: 2px; height: 130px; margin-top: .6rem; }
.bar { flex: 1; display: flex; flex-direction: column-reverse; min-width: 0; border-radius: 3px 3px 0 0; overflow: hidden; background: transparent; }
.bar > i { display: block; }
.bar-labels { display: flex; justify-content: space-between; font-size: .7rem; color: var(--muted); margin-top: .3rem; }
.legend { display: flex; flex-wrap: wrap; gap: .8rem; font-size: .75rem; color: var(--muted); margin-top: .5rem; }
.legend i { display: inline-block; width: .7rem; height: .7rem; border-radius: 3px; margin-right: .3rem; vertical-align: -1px; }
.stack { display: flex; height: 14px; border-radius: 7px; overflow: hidden; background: var(--surface-2); margin: .5rem 0; }
.stack > i { display: block; }
.progress-row { display: grid; grid-template-columns: 4.5rem 1fr 3rem; align-items: center; gap: .6rem; font-size: .85rem; padding: .25rem 0; }
.progress { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--accent); }
.hard-list { margin: 0; padding: 0; list-style: none; }
.hard-list li { display: flex; justify-content: space-between; gap: 1rem; padding: .45rem 0; border-top: 1px solid var(--border); font-size: .92rem; }
.hard-list li:first-child { border-top: 0; }

/* List */
.search-bar { position: sticky; top: -.5rem; background: var(--bg); padding: .5rem 0 .6rem; z-index: 1; display: grid; gap: .5rem; }
.vocab-list { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: 18px; overflow: hidden; }
.vocab-list li { padding: .7rem 1rem; border-top: 1px solid var(--border); }
.vocab-list li:first-child { border-top: 0; }
.vocab-item { display: flex; justify-content: space-between; gap: .8rem; align-items: flex-start; }
.vocab-item .lat { font-weight: 600; }
.vocab-item .de { color: var(--muted); font-size: .9rem; }
.badge { font-size: .7rem; padding: .15rem .45rem; border-radius: 999px; white-space: nowrap; background: var(--surface-2); color: var(--muted); }
.badge.new { color: var(--new); } .badge.learning { color: var(--learning); } .badge.young { color: var(--hard); } .badge.mature { color: var(--good); }
.vocab-detail { margin-top: .6rem; }
.vocab-detail .field { grid-template-columns: 6.5rem 1fr; }

.toast { position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 5.5rem); transform: translateX(-50%); background: var(--text); color: var(--bg); padding: .6rem 1rem; border-radius: 12px; font-size: .9rem; z-index: 10; max-width: 90vw; text-align: center; }

@media (min-width: 700px) {
  .scroll { padding-left: max(1rem, calc(50% - 320px)); padding-right: max(1rem, calc(50% - 320px)); }
  .kpis { grid-template-columns: repeat(4, 1fr); }
}
@media (max-height: 620px) {
  .card-face { padding: 1.4rem 1rem; }
  .field { font-size: .88rem; }
}

/* Decks & roles */
.role-badge { font-size: .7rem; font-weight: 600; padding: .15rem .5rem; border-radius: 999px; white-space: nowrap; }
.role-badge.main { background: var(--accent); color: #fff; }
.role-badge.side { background: var(--accent-soft); color: var(--accent); }
.role-badge.off { background: var(--surface-2); color: var(--muted); text-decoration: line-through; }
.card-face > .role-badge { position: absolute; top: .8rem; right: 1rem; }
.chip.main { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.chip.side { background: var(--accent-soft); border: 2px dashed var(--accent); color: var(--accent); font-weight: 600; }
.chip-demo { display: inline-block; font-size: .75rem; padding: .05rem .45rem; border-radius: 6px; border: 1px solid var(--accent); }
.chip-demo.main { background: var(--accent); color: #fff; }
.chip-demo.side { background: var(--accent-soft); color: var(--accent); }
.deck-summary { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem; margin: .7rem 0 .2rem; }
.deck-summary div { background: var(--bg); border-radius: 12px; padding: .55rem .8rem; display: flex; align-items: baseline; gap: .45rem; }
.deck-summary strong { font-size: 1.3rem; font-variant-numeric: tabular-nums; }
.deck-summary span { color: var(--muted); font-size: .85rem; }
.btn.tiny { padding: .22rem .5rem; font-size: .75rem; border-radius: 8px; font-weight: 600; }
.seg.wide { display: grid; grid-auto-columns: 1fr; }
.hstack.nowrap { flex-wrap: nowrap; }
.hstack.nowrap select { flex: 1; min-width: 0; padding: .55rem .6rem; }
.bulk { display: flex; flex-wrap: wrap; align-items: center; gap: .4rem; font-size: .85rem; color: var(--muted); }
.vi-main { min-width: 0; }
.vi-side { display: grid; justify-items: end; gap: .25rem; flex-shrink: 0; }
.vi-side .meta { font-size: .7rem; color: var(--muted); }
.vocab-list li.open { background: var(--bg); }
.role-picker { display: flex; align-items: center; justify-content: space-between; gap: .6rem; margin-bottom: .3rem; }
.small { font-size: .8rem; }
.center { text-align: center; }
.pw-form { display: grid; gap: .5rem; margin-top: .8rem; }
.progress { position: relative; }
.progress > b { position: absolute; left: 0; top: 0; bottom: 0; background: var(--good); }
