/* Mobile-first base styles. Colors as tokens; dark mode follows the system unless data-theme is set. */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #1b1f24;
  --muted: #5f6b7a;
  --border: #dde1e6;
  --primary: #2f6fed;
  --primary-text: #ffffff;
  --danger: #c62828;
  --good: #2e7d32;
  --good-text: #ffffff;
  --hard: #b26a00;
  --again: #c62828;
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111418;
    --surface: #1b2027;
    --text: #e8ebef;
    --muted: #9aa5b1;
    --border: #2c333c;
    --primary: #5b8cff;
    --primary-text: #0b0f14;
    --danger: #ef7070;
    --good: #66bb6a;
    --good-text: #0b0f14;
    --hard: #ffb74d;
    --again: #ef7070;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #111418;
  --surface: #1b2027;
  --text: #e8ebef;
  --muted: #9aa5b1;
  --border: #2c333c;
  --primary: #5b8cff;
  --primary-text: #0b0f14;
  --danger: #ef7070;
  --good: #66bb6a;
  --good-text: #0b0f14;
  --hard: #ffb74d;
  --again: #ef7070;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.brand { display: inline-flex; align-items: center; gap: 8px; font-weight: 700; color: var(--text); text-decoration: none; }
.brand img { border-radius: 7px; }
.login-logo { display: block; margin: 0 auto 8px; border-radius: 16px; }
.narrow h1 { text-align: center; }


.container { max-width: 640px; margin: 0 auto; padding: 16px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.narrow { max-width: 380px; margin: 10vh auto 0; }

.stack { display: flex; flex-direction: column; gap: 10px; }

h1 { margin: 0 0 12px; font-size: 1.5rem; }

input[type="password"], input[type="text"], input[type="search"], input[type="number"], select, textarea {
  font-size: 1rem;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

.btn {
  min-height: 48px;
  padding: 12px 18px;
  border-radius: 12px;
  border: 1px solid var(--border);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  background: var(--surface);
  color: var(--text);
}

.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }

.link-button { background: none; border: none; color: var(--muted); font-size: 0.95rem; cursor: pointer; padding: 8px; }

.big-number { display: block; font-size: 3rem; font-weight: 700; margin: 0; line-height: 1.1; }

.btn-large { min-height: 56px; font-size: 1.1rem; width: 100%; text-align: center; text-decoration: none; display: flex; align-items: center; justify-content: center; }
a.btn { text-decoration: none; display: flex; align-items: center; justify-content: center; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.stats-row { display: flex; gap: 24px; flex-wrap: wrap; }

.notice { background: var(--bg); border-radius: 10px; padding: 12px; margin: 0; }

/* Segmented radio control */
.segmented { border: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 8px; }
.segmented legend { width: 100%; margin-bottom: 6px; color: var(--muted); font-size: 0.9rem; }
.segmented label { flex: 1 1 auto; }
.segmented input { position: absolute; opacity: 0; }
.segmented span { display: block; text-align: center; padding: 12px; border: 1px solid var(--border); border-radius: 10px; cursor: pointer; }
.segmented input:checked + span { border-color: var(--primary); background: var(--primary); color: var(--primary-text); }
.segmented input:focus-visible + span { outline: 2px solid var(--primary); outline-offset: 2px; }

/* Study screen */
.study { display: flex; flex-direction: column; gap: 14px; min-height: calc(100dvh - 120px); }
.study-meta { display: flex; justify-content: space-between; font-size: 0.9rem; }
.flashcard { flex: 1; display: flex; flex-direction: column; justify-content: center; text-align: center; min-height: 240px; }
.term { font-size: 1.8rem; font-weight: 600; margin: 0; overflow-wrap: anywhere; }
.example { font-style: italic; color: var(--muted); }
.flashcard hr { border: none; border-top: 1px solid var(--border); margin: 20px 0; width: 100%; }

.study[data-flipped="false"] .answer,
.study[data-flipped="false"] .rate-buttons,
.study[data-flipped="true"] .flip-button { display: none; }

.rate-form { display: flex; flex-direction: column; gap: 10px; }
.rate-buttons { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.rate-buttons .btn { min-height: 64px; padding: 8px; }
.btn-again { border-color: var(--again); color: var(--again); }
.btn-hard { border-color: var(--hard); color: var(--hard); }
.btn-good { background: var(--good); border-color: var(--good); color: var(--good-text); }

.summary-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 6px; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; }
.dot-good { background: var(--good); }
.dot-hard { background: var(--hard); }
.dot-again { background: var(--again); }

.muted { color: var(--muted); }

.error { color: var(--danger); margin: 0; }

code { font-size: 0.9em; }

h2 { margin: 0 0 8px; font-size: 1.2rem; }
h3 { margin: 8px 0 0; font-size: 1rem; }
label { font-weight: 600; font-size: 0.95rem; }
textarea { font: inherit; resize: vertical; }
select { min-height: 46px; }
.small { font-size: 0.85rem; }

.page-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; }
.page-head h1 { margin: 0; }
.container > * + * { margin-top: 12px; }

.btn-small { min-height: 38px; padding: 6px 12px; font-size: 0.9rem; font-weight: 500; }
.btn-danger { background: var(--danger); border-color: var(--danger); color: var(--primary-text); }
.btn-danger-outline { border-color: var(--danger); color: var(--danger); }

.filters { display: flex; flex-direction: column; gap: 10px; padding: 14px; }
.search { width: 100%; }
.filter-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.filter-row > select, .filter-row > input { flex: 1 1 140px; min-width: 0; }

.list-head { display: flex; justify-content: space-between; font-size: 0.9rem; }
.list-head a { color: var(--primary); }

.card-row { display: flex; flex-wrap: wrap; gap: 10px; justify-content: space-between; padding: 14px; }
.card-row-main { flex: 1 1 260px; min-width: 0; }
.card-row-main p { margin: 0; }
.card-terms { overflow-wrap: anywhere; }
.card-meta { font-size: 0.85rem; margin-top: 4px !important; }
.card-row-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.card-row-actions form { margin: 0; }
.is-suspended { opacity: 0.6; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-block; font-size: 0.8rem; padding: 2px 8px; border-radius: 999px; background: var(--bg); border: 1px solid var(--border); margin-left: 4px; color: var(--text); }
.chip-warn { border-color: var(--hard); color: var(--hard); }
.chip-button { cursor: pointer; margin: 0; padding: 6px 10px; font-size: 0.85rem; }

.warning { border: 1px solid var(--hard); border-radius: 10px; padding: 12px; }
.warning p, .warning ul { margin: 0 0 8px; }
.notice-good { color: var(--good); }

.pager { display: flex; gap: 8px; justify-content: space-between; }

.manage-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--border); }
.inline-form { display: flex; gap: 6px; flex: 1 1 220px; margin: 0; }
.inline-form input { flex: 1; min-width: 0; padding: 8px; }
.manage-row form { margin: 0; }
.new-deck { margin-top: 8px; }

.secondary-actions { display: flex; justify-content: space-around; flex-wrap: wrap; }
.secondary-actions a.link-button { text-decoration: none; }

.checkbox { display: flex; gap: 8px; align-items: center; font-weight: 500; }
.checkbox input { width: 20px; height: 20px; }
input[type="file"] { font-size: 0.95rem; }

.import-row { display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; }
.import-check input { width: 24px; height: 24px; margin-top: 10px; }
.import-fields { flex: 1; display: grid; grid-template-columns: 1fr 1fr; gap: 6px; min-width: 0; }
.import-fields input[type="text"] { padding: 8px; min-width: 0; }
.import-fields p { grid-column: 1 / -1; margin: 0; }
.import-columns { display: grid; grid-template-columns: 34px 1fr 1fr; gap: 6px; padding: 0 12px; font-weight: 600; }
@media (max-width: 480px) { .import-columns { display: none; } }
.status-duplicate { opacity: 0.65; }
.status-duplicate .import-hint { color: var(--again); }
.status-similar .import-hint { color: var(--hard); }
@media (max-width: 480px) { .import-fields { grid-template-columns: 1fr; } }

.mapping-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.mapping-col { display: flex; flex-direction: column; gap: 4px; font-weight: 500; }
.rejected-list { margin: 0; padding-left: 18px; display: flex; flex-direction: column; gap: 6px; }
.rejected-list code { overflow-wrap: anywhere; }
details summary { cursor: pointer; font-weight: 600; }
.sticky-submit { position: sticky; bottom: calc(8px + env(safe-area-inset-bottom)); box-shadow: 0 4px 16px rgba(0,0,0,0.15); }

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.center { text-align: center; margin: 0; }
.hints { margin-top: 12px; }
.hints p { margin: 6px 0 0; }
.hint-level:not(.example) { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 0.05em; }
.study[data-mode="type"] .answer, .study[data-mode="choice"] .answer { display: none; }
.study[data-flipped="true"] .answer { display: block; }

.answer-input { font-size: 1.2rem; padding: 14px; text-align: center; }
.answer-buttons { display: grid; grid-template-columns: 1fr 2fr; gap: 8px; }
.answer-buttons .btn { min-height: 56px; }
.choice-buttons { display: grid; grid-template-columns: 1fr; gap: 8px; }
.choice { min-height: 56px; text-align: left; overflow-wrap: anywhere; }
@media (min-width: 560px) { .choice-buttons { grid-template-columns: 1fr 1fr; } }

.verdict { font-weight: 700; font-size: 1.1rem; margin: 14px 0 4px; }
.verdict-correct { color: var(--good); }
.verdict-almost { color: var(--hard); }
.verdict-wrong { color: var(--again); }
.given { text-decoration: line-through; text-decoration-color: var(--again); }
.is-suggested { outline: 3px solid var(--primary); outline-offset: 2px; }
.secondary-actions { margin: 0; }

/* Dashboard */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.tile { background: var(--bg); border-radius: 12px; padding: 12px 14px; display: flex; flex-direction: column; gap: 2px; }
.tile-value { font-size: 2.2rem; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.tile-unit { font-size: 1rem; font-weight: 600; color: var(--muted); }
.tile-label { font-weight: 600; }
.tile-sub { font-size: 0.8rem; color: var(--muted); }
@media (max-width: 520px) {
  .tiles { grid-template-columns: 1fr 1fr; }
  .tile:first-child { grid-column: 1 / -1; }  /* due today is the headline number */
}

/* Vertical bars: one hue, 4px rounded top, 2px gaps, recessive baseline */
.columns { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 2px; height: 150px; align-items: end;
           border-bottom: 1px solid var(--border); padding-top: 18px; }
.column { position: relative; height: 100%; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; outline: none; }
.column-bar { width: 70%; max-width: 36px; min-height: 2px; background: var(--primary); border-radius: 4px 4px 0 0; }
.column-value { font-size: 0.8rem; color: var(--muted); font-variant-numeric: tabular-nums; margin-bottom: 2px; }
.column-label { position: absolute; bottom: -22px; font-size: 0.75rem; color: var(--muted); }
.columns { margin-bottom: 22px; }
.columns-dense .column-bar { width: 80%; }
.column:hover .column-bar, .column:focus-visible .column-bar { opacity: 0.8; }

/* Horizontal bars for boxes (small multiples, one per direction) */
.box-charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 16px; }
.box-chart h3 { font-size: 0.9rem; font-weight: 600; margin: 0 0 6px; }
.hbar { display: grid; grid-template-columns: 34px 1fr 40px; align-items: center; gap: 8px; padding: 2px 0; position: relative; outline: none; }
.hbar-label { font-size: 0.8rem; color: var(--muted); text-align: right; }
.hbar-track { height: 14px; }
.hbar-fill { display: block; height: 100%; min-width: 2px; background: var(--primary); border-radius: 0 4px 4px 0; }
.hbar-value { font-size: 0.85rem; font-variant-numeric: tabular-nums; }

/* Tooltip on hover / focus (tap on touch devices focuses the bar) */
[data-tip]:hover::after, [data-tip]:focus-visible::after, [data-tip]:focus::after {
  content: attr(data-tip); position: absolute; bottom: calc(100% + 4px); left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--surface); font-size: 0.8rem; padding: 4px 8px; border-radius: 6px;
  white-space: nowrap; pointer-events: none; z-index: 5;
}

.plain-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 6px; }
.plain-list a { color: var(--text); }
.table-wrap { overflow-x: auto; margin-top: 10px; }
.table-wrap table { border-collapse: collapse; margin-bottom: 14px; font-size: 0.85rem; font-variant-numeric: tabular-nums; }
.table-wrap caption { text-align: left; font-weight: 600; padding-bottom: 4px; }
.table-wrap th, .table-wrap td { border: 1px solid var(--border); padding: 4px 8px; text-align: right; }
.table-wrap th { text-align: left; font-weight: 500; }

/* Navigation: top bar on wide screens, bottom tab bar on phones (thumb reach). */
.topbar { position: sticky; top: 0; z-index: 10; gap: 12px; }
.nav { display: flex; gap: 4px; margin-left: auto; }
.nav-item { display: flex; align-items: center; gap: 6px; padding: 8px 10px; border-radius: 10px;
            color: var(--muted); text-decoration: none; font-size: 0.95rem; }
.nav-item svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8;
                stroke-linecap: round; stroke-linejoin: round; }
.nav-item:hover { color: var(--text); background: var(--bg); }
.nav-item.is-active { color: var(--primary); font-weight: 600; }
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

@media (max-width: 720px) {
  .topbar { justify-content: center; padding-top: calc(10px + env(safe-area-inset-top)); }
  body { padding-top: 0; padding-bottom: calc(72px + env(safe-area-inset-bottom)); }
  .nav {
    position: fixed; left: 0; right: 0; bottom: 0; margin: 0; z-index: 20;
    justify-content: space-around; gap: 0;
    background: var(--surface); border-top: 1px solid var(--border);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  }
  .nav-item { flex-direction: column; gap: 2px; font-size: 0.7rem; padding: 6px 4px; flex: 1; text-align: center; }
  .nav-item svg { width: 24px; height: 24px; }
  .nav-desktop-only { display: none; }
  .page-head { flex-wrap: wrap; }
}

/* Focus mode while learning: no navigation, card fills the screen, buttons at the bottom. */
.focus-mode .topbar, .focus-mode .nav { display: none; }
.focus-mode { padding-bottom: env(safe-area-inset-bottom); }
.focus-mode .container { padding-top: calc(12px + env(safe-area-inset-top)); }
.focus-mode .study { min-height: calc(100dvh - 40px - env(safe-area-inset-top) - env(safe-area-inset-bottom)); }
.exit-link { color: var(--muted); text-decoration: none; font-size: 1.1rem; padding: 0 6px; }
.study-meta { align-items: center; gap: 8px; }

/* Touch: no double-tap zoom delay on controls; inputs >= 16px so iOS does not zoom in. */
button, .btn, a, input, select, label { touch-action: manipulation; }
input, select, textarea { font-size: max(16px, 1rem); }
.btn:active { transform: scale(0.98); }

/* Keyboard hints only where a keyboard is likely (mouse/trackpad). */
.shortcuts { display: none; text-align: center; margin: 0; }
@media (hover: hover) and (pointer: fine) { .shortcuts { display: block; } }


.example-button { align-self: center; margin-top: 14px; padding: 8px 16px; border-radius: 999px;
                  border: 1px solid var(--primary); background: none; color: var(--primary);
                  font-size: 0.95rem; font-weight: 600; cursor: pointer; }
.examples { margin-top: 12px; }
.examples p { margin: 6px 0 0; }
