:root {
  --bg: #f5f5f5;
  --panel: #ffffff;
  --panel2: #fafafa;
  --text: #202020;
  --muted: #6b6b6b;
  --border: #d6d6d6;
  --accent: #2b6cb0;
  --accent-text: #ffffff;
  --hover: #ececec;
  --new: #1e6bd6;
  --learn: #c62828;
  --review: #2e7d32;
  --again: #c62828;
  --hard: #6b6b6b;
  --good: #2e7d32;
  --easy: #1e6bd6;
  --danger: #c62828;
  --shadow: 0 1px 3px rgba(0, 0, 0, .12);
  --sel: #cfe0f7;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1f1f1f; --panel: #2a2a2a; --panel2: #262626; --text: #e8e8e8; --muted: #a0a0a0; --border: #3d3d3d;
    --accent: #5b9bea; --accent-text: #0e1a2a; --hover: #343434; --new: #6aa7ff; --learn: #ff7a7a; --review: #6fcf73;
    --again: #ff7a7a; --hard: #b5b5b5; --good: #6fcf73; --easy: #6aa7ff; --danger: #ff7a7a; --shadow: 0 1px 3px rgba(0,0,0,.5); --sel: #2d4565;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1f1f1f; --panel: #2a2a2a; --panel2: #262626; --text: #e8e8e8; --muted: #a0a0a0; --border: #3d3d3d;
  --accent: #5b9bea; --accent-text: #0e1a2a; --hover: #343434; --new: #6aa7ff; --learn: #ff7a7a; --review: #6fcf73;
  --again: #ff7a7a; --hard: #b5b5b5; --good: #6fcf73; --easy: #6aa7ff; --danger: #ff7a7a; --shadow: 0 1px 3px rgba(0,0,0,.5); --sel: #2d4565;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column; min-height: 100dvh;
}
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
h1 { font-size: 1.5rem; margin: 0 0 1rem; }
h2 { font-size: 1.15rem; margin: 1.5rem 0 .6rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; }
.right { margin-left: auto; }
.hidden { display: none !important; }

#topbar {
  display: flex; align-items: center; gap: 1rem; padding: .45rem 1rem; background: var(--panel);
  border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20;
}
#topbar .brand { display: flex; align-items: center; gap: .4rem; font-weight: 700; text-decoration: none; color: var(--text); }
#topbar nav { display: flex; gap: .15rem; margin: 0 auto; overflow-x: auto; scrollbar-width: none; }
#topbar nav a { padding: .35rem .7rem; border-radius: 6px; color: var(--text); text-decoration: none; white-space: nowrap; }
#topbar nav a:hover { background: var(--hover); }
#topbar nav a.active { background: var(--sel); }
body.studying #topbar { display: none; }
@media (max-width: 640px) {
  #topbar { padding: .35rem .5rem; gap: .5rem; }
  #topbar .brand { display: none; }
  #topbar nav { margin: 0; flex: 1; justify-content: space-between; gap: 0; }
  #topbar nav a { padding: .35rem .4rem; font-size: .82rem; }
}

#app { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.page { width: 100%; max-width: 900px; margin: 0 auto; padding: 1.25rem 16px 3rem; }
.page.wide { max-width: none; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .35rem; padding: .45rem .9rem;
  border: 1px solid var(--border); background: var(--panel); border-radius: 6px; cursor: pointer; text-decoration: none; color: var(--text);
  box-shadow: var(--shadow); min-height: 36px;
}
.btn:hover { background: var(--hover); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.small { padding: .2rem .55rem; min-height: 28px; font-size: .85rem; }
.btn.icon { padding: .2rem .45rem; min-height: 28px; box-shadow: none; border-color: transparent; background: transparent; }
.btn.icon:hover { background: var(--hover); }

input[type=text], input[type=number], input[type=search], input[type=password], select, textarea {
  background: var(--panel); border: 1px solid var(--border); border-radius: 6px; padding: .4rem .55rem; min-height: 36px; max-width: 100%;
}
textarea { width: 100%; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .85rem; }
input:focus, select:focus, textarea:focus, .field-editor:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label.check { display: flex; gap: .5rem; align-items: center; cursor: pointer; }

.panel { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 1rem; box-shadow: var(--shadow); }

/* deck list */
table.decks { width: 100%; border-collapse: collapse; background: var(--panel); border-radius: 8px; overflow: hidden; box-shadow: var(--shadow); }
table.decks th { text-align: right; font-weight: 600; color: var(--muted); font-size: .85rem; padding: .5rem .6rem; border-bottom: 1px solid var(--border); }
table.decks th:first-child { text-align: left; }
table.decks td { padding: .45rem .6rem; border-bottom: 1px solid var(--border); }
table.decks tr:last-child td { border-bottom: 0; }
table.decks tr:hover td { background: var(--hover); }
table.decks td.num { text-align: right; width: 4.5rem; font-variant-numeric: tabular-nums; }
table.decks td.gear { width: 2.5rem; text-align: right; }
.deckname { display: flex; align-items: center; gap: .25rem; }
.deckname a { color: var(--text); text-decoration: none; cursor: pointer; }
.deckname a:hover { text-decoration: underline; }
.collapse { width: 1.2rem; display: inline-block; text-align: center; cursor: pointer; color: var(--muted); user-select: none; }
.c-new { color: var(--new); } .c-learn { color: var(--learn); } .c-review { color: var(--review); }
.zero { color: var(--muted); opacity: .45; }
.deck-actions { margin-top: 1rem; justify-content: center; }
.dropzone { border: 2px dashed var(--border); border-radius: 8px; padding: 1rem; text-align: center; color: var(--muted); margin-top: 1rem; }
.dropzone.over { border-color: var(--accent); color: var(--accent); }

/* overview */
.overview { text-align: center; }
.overview .counts { display: inline-grid; grid-template-columns: auto auto; gap: .2rem 1.25rem; text-align: left; margin: 1rem 0 1.5rem; font-size: 1.05rem; }
.overview .counts b { text-align: right; font-variant-numeric: tabular-nums; }
.overview .descr { max-width: 600px; margin: 0 auto 1rem; text-align: left; }
.congrats { text-align: center; padding: 2rem 1rem; }

/* study */
.study { flex: 1; display: flex; flex-direction: column; min-height: 0; height: 100dvh; }
.study-top { display: flex; align-items: center; gap: .5rem; padding: .35rem .75rem; background: var(--panel); border-bottom: 1px solid var(--border); font-size: .9rem; }
.study-top .deck { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.study-frame { flex: 1; border: 0; width: 100%; min-height: 0; background: var(--panel); }
.study-bottom { background: var(--panel); border-top: 1px solid var(--border); padding: .5rem .75rem calc(.5rem + env(safe-area-inset-bottom)); }
.study-bottom .bar { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: .5rem; max-width: 800px; margin: 0 auto; }
.study-bottom .left { justify-self: start; } .study-bottom .rightside { justify-self: end; }
.qcounts { text-align: center; font-variant-numeric: tabular-nums; font-size: .95rem; margin-bottom: .35rem; }
.qcounts span.cur { text-decoration: underline; text-underline-offset: 3px; font-weight: 700; }
.answer-buttons { display: flex; gap: .4rem; justify-content: center; }
.ans { display: flex; flex-direction: column; align-items: center; min-width: 5.2rem; padding: .25rem .6rem; }
.ans .ivl { font-size: .75rem; color: var(--muted); }
.ans .lbl { font-weight: 600; }
.ans.e1 .lbl { color: var(--again); } .ans.e2 .lbl { color: var(--hard); } .ans.e3 .lbl { color: var(--good); } .ans.e4 .lbl { color: var(--easy); }
.show-answer { min-width: 12rem; font-weight: 600; }
.kbd { font-size: .7rem; color: var(--muted); border: 1px solid var(--border); border-radius: 3px; padding: 0 .25rem; margin-left: .25rem; }
.timer { font-variant-numeric: tabular-nums; color: var(--muted); }
@media (max-width: 640px) {
  .ans { min-width: 0; flex: 1; padding: .35rem .2rem; }
  .answer-buttons { width: 100%; }
  .study-bottom .bar { grid-template-columns: 1fr 1fr; row-gap: .35rem; }
  .study-bottom .mid { grid-column: 1 / -1; order: -1; }
  .study-bottom .left .btn, .study-bottom .rightside .btn { min-height: 30px; padding: .15rem .6rem; font-size: .85rem; box-shadow: none; }
  .show-answer { min-width: 0; width: 100%; }
  .kbd { display: none; }
}

/* add / editor */
.editor-fields { display: flex; flex-direction: column; gap: .6rem; }
.field-label { font-size: .85rem; color: var(--muted); margin-bottom: .15rem; display: flex; align-items: center; gap: .5rem; }
.field-editor { background: var(--panel); border: 1px solid var(--border); border-radius: 6px; padding: .5rem .6rem; min-height: 2.6rem; overflow-wrap: anywhere; }
.field-editor img { max-width: 100%; height: auto; }
.field-html { min-height: 5rem; }
.toolbar { display: flex; flex-wrap: wrap; gap: .2rem; margin-bottom: .6rem; }
.toolbar .btn { min-width: 34px; padding: .2rem .5rem; min-height: 30px; box-shadow: none; }
.dupe { color: var(--danger); font-size: .85rem; }
.sticky-btn { opacity: .35; cursor: pointer; border: 0; background: none; padding: 0 .2rem; }
.sticky-btn.on { opacity: 1; }

/* browse */
.browse { display: grid; grid-template-columns: 200px minmax(0, 1fr) minmax(0, 420px); gap: 0; flex: 1; min-height: 0; height: calc(100dvh - 49px); }
.browse .side { border-right: 1px solid var(--border); overflow: auto; padding: .5rem; background: var(--panel2); font-size: .9rem; }
.browse .side h4 { margin: .75rem 0 .25rem; font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.browse .side a { display: block; padding: .15rem .4rem; border-radius: 4px; color: var(--text); text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.browse .side a:hover { background: var(--hover); }
.browse .list { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.browse .searchbar { display: flex; gap: .4rem; padding: .5rem; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.browse .searchbar input { flex: 1; min-width: 10rem; }
.browse .tablewrap { flex: 1; overflow: auto; }
.browse table { width: 100%; border-collapse: collapse; font-size: .88rem; }
.browse th { position: sticky; top: 0; background: var(--panel); text-align: left; padding: .35rem .5rem; border-bottom: 1px solid var(--border); cursor: pointer; white-space: nowrap; user-select: none; }
.browse td { padding: .3rem .5rem; border-bottom: 1px solid var(--border); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 22rem; }
.browse tr.sel td { background: var(--sel); }
.browse tr.susp td { background: rgba(255, 220, 0, .18); }
.browse tr.marked td:first-child { box-shadow: inset 3px 0 0 #a855f7; }
.browse tr.flag1 td { background: rgba(255, 80, 80, .18); } .browse tr.flag2 td { background: rgba(255, 160, 40, .2); }
.browse tr.flag3 td { background: rgba(60, 200, 80, .18); } .browse tr.flag4 td { background: rgba(60, 130, 255, .18); }
.browse .edit { border-left: 1px solid var(--border); overflow: auto; padding: .75rem; background: var(--panel2); }
.browse .actions { display: flex; flex-wrap: wrap; gap: .3rem; padding: .4rem .5rem; border-top: 1px solid var(--border); background: var(--panel); }
@media (max-width: 1000px) {
  .browse { grid-template-columns: minmax(0, 1fr); height: auto; }
  .browse .side { display: none; }
  .browse .edit { border-left: 0; border-top: 1px solid var(--border); }
  .browse .tablewrap { max-height: 55dvh; }
}

/* modal */
.modal-back { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); display: flex; align-items: flex-start; justify-content: center; z-index: 100; padding: 5vh 16px; overflow: auto; }
.modal { background: var(--panel); border-radius: 10px; width: 100%; max-width: 640px; padding: 1.1rem 1.2rem; box-shadow: 0 10px 40px rgba(0, 0, 0, .35); }
.modal.wide { max-width: 1000px; }
.modal h2 { margin-top: 0; }
.modal .foot { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1rem; flex-wrap: wrap; }

#toast { position: fixed; bottom: 1.25rem; left: 50%; transform: translateX(-50%); background: #222; color: #fff; padding: .55rem 1rem; border-radius: 6px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 200; max-width: calc(100vw - 32px); }
#toast.show { opacity: .95; }
#toast.err { background: #a01d1d; }

/* forms */
.form { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 220px); gap: .55rem 1rem; align-items: center; }
.form label { color: var(--text); }
.form .hint { grid-column: 1 / -1; color: var(--muted); font-size: .8rem; margin-top: -.4rem; }
@media (max-width: 560px) { .form { grid-template-columns: 1fr; } .form .hint { margin-top: -.3rem; } }
.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--border); margin-bottom: 1rem; overflow-x: auto; }
.tabs button { border: 0; background: none; padding: .5rem .8rem; cursor: pointer; border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs button.on { border-color: var(--accent); color: var(--accent); font-weight: 600; }

/* keybinds */
.binds { width: 100%; border-collapse: collapse; }
.binds td { padding: .4rem .3rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
.chip { display: inline-flex; align-items: center; gap: .25rem; background: var(--hover); border: 1px solid var(--border); border-radius: 4px; padding: .05rem .2rem .05rem .45rem; margin: .1rem; font-family: ui-monospace, monospace; font-size: .85rem; }
.chip button { border: 0; background: none; cursor: pointer; color: var(--muted); padding: 0 .2rem; }
.listening { outline: 2px solid var(--accent); }

/* stats */
.cards-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .75rem; }
.stat { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: .75rem; }
.stat b { display: block; font-size: 1.4rem; font-variant-numeric: tabular-nums; }
.stat span { color: var(--muted); font-size: .85rem; }
.chart { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: .75rem; }
.chart svg { width: 100%; height: 160px; display: block; }
.legend { display: flex; gap: 1rem; flex-wrap: wrap; font-size: .8rem; color: var(--muted); margin-top: .35rem; }
.legend i { display: inline-block; width: .7rem; height: .7rem; border-radius: 2px; margin-right: .3rem; vertical-align: -1px; }

/* devices */
.codebox { font-family: ui-monospace, monospace; font-size: 1.8rem; letter-spacing: .15em; font-weight: 700; text-align: center; padding: .5rem; }
.qr { display: flex; justify-content: center; margin: .5rem 0; }
.qr svg { width: 200px; height: 200px; background: #fff; padding: 8px; border-radius: 8px; }
.share-item { display: flex; gap: .75rem; align-items: center; padding: .6rem 0; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.deckpick { max-height: 300px; overflow: auto; border: 1px solid var(--border); border-radius: 6px; padding: .4rem; }

/* template editor */
.tmpl-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 1rem; }
.tmpl-grid textarea { min-height: 220px; }
.tmpl-grid iframe { width: 100%; height: 260px; border: 1px solid var(--border); border-radius: 6px; background: #fff; }
@media (max-width: 800px) { .tmpl-grid { grid-template-columns: 1fr; } }
.preview-frame { width: 100%; height: 320px; border: 1px solid var(--border); border-radius: 6px; background: #fff; }
.mapping td { padding: .25rem .4rem; }
.preview-table { font-size: .8rem; border-collapse: collapse; width: 100%; }
.preview-table td { border: 1px solid var(--border); padding: .2rem .35rem; max-width: 14rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.menu { position: absolute; background: var(--panel); border: 1px solid var(--border); border-radius: 6px; box-shadow: 0 6px 20px rgba(0,0,0,.25); z-index: 150; min-width: 180px; padding: .25rem 0; }
.menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: .45rem .9rem; cursor: pointer; }
.menu button:hover { background: var(--hover); }
.menu button.danger { color: var(--danger); }

/* image occlusion editor */
.io { margin-bottom: 1rem; }
.io-stage { display: flex; justify-content: center; min-height: 120px; align-items: center; border: 1px dashed var(--border); border-radius: 6px; padding: .5rem; }
.io-wrap { position: relative; display: inline-block; max-width: 100%; touch-action: none; user-select: none; }
.io-wrap img { display: block; max-width: 100%; max-height: 70vh; }
.io-wrap svg { position: absolute; inset: 0; width: 100%; height: 100%; cursor: crosshair; }
.io-shape { fill: rgba(255, 142, 142, .75); stroke: #212121; stroke-width: 1px; cursor: move; }
.io-shape.sel { fill: rgba(255, 200, 80, .8); stroke: #1e6bd6; stroke-width: 2px; }
.io-num { fill: #000; pointer-events: none; font-family: system-ui, sans-serif; }
/* heatmap */
.heat svg { width: 100%; height: auto; display: block; }
.heat .legend { justify-content: flex-end; }
.hm0 { fill: var(--hover); background: var(--hover); }
.hm1 { fill: #9be9a8; background: #9be9a8; } .hm2 { fill: #40c463; background: #40c463; }
.hm3 { fill: #30a14e; background: #30a14e; } .hm4 { fill: #216e39; background: #216e39; }
.heat .legend i { width: .7rem; height: .7rem; margin: 0 .1rem; }
table.mini { width: 100%; border-collapse: collapse; font-size: .9rem; }
table.mini td, table.mini th { padding: .2rem .4rem; border-bottom: 1px solid var(--border); }
table.mini td.num, table.mini th { text-align: right; font-variant-numeric: tabular-nums; }
table.mini th:first-child, table.mini td:first-child { text-align: left; }
.ext-item { display: grid; grid-template-columns: auto 1fr; gap: .25rem .75rem; padding: .8rem 0; border-bottom: 1px solid var(--border); align-items: start; }
.ext-item input[type=checkbox] { width: 1.15rem; height: 1.15rem; margin-top: .2rem; }
.ext-item .cfg { grid-column: 2; display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; font-size: .9rem; }
.ext-item .cfg input[type=number] { width: 5.5rem; }
