:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #334155;
  --border: #334155;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --green: #22c55e;
  --red: #ef4444;
  --english-bg: #1e293b;
  --bulgarian-bg: #2563eb;
  --radius: 16px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, "Apple Color Emoji", "Segoe UI Emoji", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  line-height: 1.4;
}

.app {
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 16px) 16px
           calc(env(safe-area-inset-bottom) + 32px);
  display: flex;
  flex-direction: column;
  gap: 20px;
}

a { color: var(--accent); }

/* ---------- Nav ---------- */
.nav {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}
.nav .brand {
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-right: auto;
  text-decoration: none;
  color: var(--text);
  font-size: 1.05rem;
}
.nav a.link {
  text-decoration: none;
  color: var(--muted);
  padding: 7px 12px;
  border-radius: 10px;
  font-size: 0.9rem;
  font-weight: 600;
}
.nav a.link:hover { color: var(--text); background: var(--surface); }
.nav a.link.active { color: #fff; background: var(--accent); }

/* ---------- Generic bits ---------- */
h1 { font-size: 1.4rem; margin: 0 0 4px; letter-spacing: -0.02em; }
.sub { color: var(--muted); margin: 0; font-size: 0.9rem; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px 16px;
  font-size: 0.95rem;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border: 1px solid transparent;
  border-radius: 12px;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s ease, transform 0.05s ease, border-color 0.15s ease;
}
.btn:hover { background: var(--accent-hover); }
.btn:active { transform: scale(0.99); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn.secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn.secondary:hover { background: var(--surface-2); }
.btn.danger { background: transparent; color: #fca5a5; border-color: rgba(239,68,68,0.4); }
.btn.danger:hover { background: rgba(239,68,68,0.12); }
.btn.small { padding: 7px 11px; font-size: 0.82rem; border-radius: 9px; }

.field {
  width: 100%;
  padding: 13px 15px;
  font-size: 1rem;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  outline: none;
  transition: border-color 0.15s ease;
}
.field::placeholder { color: var(--muted); }
.field:focus { border-color: var(--accent); }

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

.error {
  background: rgba(239, 68, 68, 0.12);
  border: 1px solid rgba(239, 68, 68, 0.4);
  color: #fca5a5;
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 0.9rem;
}
.muted { color: var(--muted); }
.center { text-align: center; }
[hidden] { display: none !important; }

/* ---------- Home ---------- */
.tiles { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
.tile {
  text-decoration: none;
  color: var(--text);
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
  text-align: center;
  padding: 22px 14px;
}
.tile .emoji { font-size: 1.8rem; }
.tile .label { font-weight: 700; }
.tile .desc { color: var(--muted); font-size: 0.78rem; }
.stats-row { display: flex; gap: 12px; flex-wrap: wrap; }
.stat { flex: 1; min-width: 90px; text-align: center; }
.stat .n { font-size: 1.6rem; font-weight: 700; }
.stat .k { color: var(--muted); font-size: 0.8rem; }
@media (max-width: 520px) {
  .tiles { grid-template-columns: 1fr; }
}

/* ---------- Forms ---------- */
.form-row { display: flex; flex-direction: column; gap: 10px; }
@media (min-width: 560px) {
  .form-row.inline { flex-direction: row; }
  .form-row.inline .btn { white-space: nowrap; }
}

/* Add-card form: a row of fields, with the actions on their own line below. */
.add-form { display: flex; flex-direction: column; gap: 14px; }
.add-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
@media (max-width: 520px) { .add-fields { grid-template-columns: 1fr; } }
.add-actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* Bulk import panel */
.import-panel { display: flex; flex-direction: column; gap: 12px; }
.import-panel code { background: var(--bg); padding: 1px 6px; border-radius: 6px; font-size: 0.85em; }
.textarea { width: 100%; resize: vertical; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; line-height: 1.5; }
.import-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.import-actions .muted { font-size: 0.85rem; }

/* Search + deck filter bar */
.filter-bar { display: flex; flex-wrap: wrap; gap: 10px; }
.filter-bar #search { flex: 1 1 220px; }
/* width:auto keeps the select content-sized so it sits beside the search box
   (the shared .field rule would otherwise stretch it to a full-width row). */
.filter-bar #deck-filter { flex: 0 0 auto; width: auto; min-width: 160px; }
/* Native dropdown arrow is removed by appearance:none, so draw a themed chevron. */
select.field {
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
  padding-right: 38px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M2 4l4 4 4-4' fill='none' stroke='%2394a3b8' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
  background-size: 12px;
}
select.field option { color: var(--text); background: var(--surface); }

/* ---------- Manage table ---------- */
.table-wrap { overflow-x: auto; border-radius: var(--radius); border: 1px solid var(--border); }
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th, td { padding: 11px 12px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.04em; background: var(--surface); }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text); }
.sort-arrow { color: var(--accent); }
tr:last-child td { border-bottom: none; }
/* Keep the actions cell a real table-cell (don't use display:flex on a <td>,
   it drops the cell out of the column layout). */
td.actions { white-space: nowrap; }
td.actions .btn { vertical-align: middle; }
td.actions .btn + .btn { margin-left: 6px; }
td .cell-input { width: 100%; min-width: 120px; padding: 7px 9px; font-size: 0.88rem; color: var(--text); background: var(--bg); border: 1px solid var(--accent); border-radius: 8px; }
.box-badge { display: inline-block; min-width: 22px; text-align: center; padding: 2px 7px; border-radius: 999px; font-size: 0.78rem; font-weight: 700; background: var(--surface-2); }
.box-1 { background: rgba(239,68,68,0.25); color: #fca5a5; }
.box-2 { background: rgba(249,115,22,0.22); color: #fdba74; }
.box-3 { background: rgba(234,179,8,0.20); color: #fde047; }
.box-4 { background: rgba(132,204,22,0.20); color: #bef264; }
.box-5 { background: rgba(34,197,94,0.22); color: #86efac; }

/* ---------- Play / quiz ---------- */
.quiz { display: flex; flex-direction: column; gap: 16px; }
.prompt {
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  padding: 30px 20px;
  text-align: center;
}
.prompt .dir { color: var(--muted); font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.08em; margin-bottom: 8px; }
.prompt .word { font-size: 2rem; font-weight: 700; letter-spacing: -0.01em; word-break: break-word; }
.prompt .ctx { color: var(--muted); font-size: 0.9rem; margin-top: 8px; font-style: italic; }
.feedback { padding: 14px 16px; border-radius: 12px; font-size: 0.95rem; }
.feedback.ok { background: rgba(34,197,94,0.14); border: 1px solid rgba(34,197,94,0.4); color: #86efac; }
.feedback.bad { background: rgba(239,68,68,0.14); border: 1px solid rgba(239,68,68,0.4); color: #fca5a5; }
.feedback .answer { color: var(--text); font-weight: 700; }
.scoreline { display: flex; justify-content: space-between; color: var(--muted); font-size: 0.85rem; }

/* Multiple-choice options */
.choices { display: grid; gap: 10px; }
.choice { justify-content: flex-start; text-align: left; gap: 10px; padding: 14px 16px; font-weight: 600; }
.choice .choice-num {
  display: inline-flex; align-items: center; justify-content: center;
  flex: 0 0 auto; min-width: 22px; height: 22px; border-radius: 6px;
  background: var(--surface-2); color: var(--muted); font-size: 0.8rem; font-weight: 700;
}
.choice .choice-label { word-break: break-word; }
/* Keep the result colours readable after answering (override .btn:disabled fade). */
.choice:disabled { opacity: 1; cursor: default; }
.choice.correct { background: rgba(34,197,94,0.18); border-color: rgba(34,197,94,0.5); color: #86efac; }
.choice.correct .choice-num { background: rgba(34,197,94,0.32); color: #bbf7d0; }
.choice.wrong { background: rgba(239,68,68,0.16); border-color: rgba(239,68,68,0.5); color: #fca5a5; }
.choice.wrong .choice-num { background: rgba(239,68,68,0.32); color: #fecaca; }

/* ---------- Flip ---------- */
.flip-stage { perspective: 1200px; }
.flip-card {
  position: relative;
  width: 100%;
  min-height: 230px;
  cursor: pointer;
  transform-style: preserve-3d;
  transition: transform 0.5s cubic-bezier(0.2, 0.7, 0.2, 1);
}
.flip-card.flipped { transform: rotateY(180deg); }
.flip-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: 20px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 28px 22px;
  text-align: center;
}
.flip-face .tag { position: absolute; top: 14px; left: 16px; font-size: 0.72rem; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; }
.flip-face .word { font-size: 2rem; font-weight: 700; word-break: break-word; }
.flip-face .ctx { margin-top: 8px; font-size: 0.9rem; font-style: italic; color: var(--muted); }
.flip-back .ctx { color: rgba(255, 255, 255, 0.8); }
.flip-front { background: var(--english-bg); border: 1px solid var(--border); color: var(--text); }
.flip-front .tag { color: var(--muted); }
.flip-back { background: var(--bulgarian-bg); color: #fff; transform: rotateY(180deg); }
.flip-back .tag { color: rgba(255,255,255,0.8); }
.flip-controls { display: flex; align-items: center; justify-content: center; gap: 10px; }
.flip-controls .pos { min-width: 64px; text-align: center; color: var(--muted); font-size: 0.9rem; }
.flip-nav { margin-top: 16px; }
.flip-actions { margin-top: 10px; }
.flip-hint { text-align: center; color: var(--muted); font-size: 0.8rem; }

/* ---------- Play / flip deck controls ---------- */
.play-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; }
.play-controls .field { width: auto; min-width: 150px; }
.play-controls .toggle { display: inline-flex; align-items: center; gap: 7px; color: var(--muted); font-size: 0.88rem; cursor: pointer; user-select: none; }
.play-controls .toggle input { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; }
.play-controls .due-count { margin-left: auto; font-size: 0.85rem; }
#flip-controls-top { justify-content: center; margin-bottom: 16px; }

.caught-up { text-align: center; display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 32px 20px; }
.caught-up .big { font-size: 2.6rem; }
.caught-up p { margin: 0; color: var(--muted); }

/* ---------- Home activity / streak ---------- */
.activity { display: flex; flex-direction: column; gap: 12px; }
.activity-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.activity-head .streak { font-weight: 600; }
.activity-head .streak-n { font-size: 1.5rem; font-weight: 700; color: var(--accent); }
.chart { display: flex; align-items: flex-end; gap: 4px; height: 64px; }
.chart .bar { flex: 1; height: 100%; display: flex; align-items: flex-end; background: var(--bg); border-radius: 4px; overflow: hidden; }
.chart .bar-fill { width: 100%; min-height: 2px; background: var(--accent); border-radius: 4px; transition: height 0.2s ease; }
.chart-caption { font-size: 0.75rem; text-align: right; }

/* ---------- Deck picker (Manage add/edit) ---------- */
.deck-picker { display: flex; flex-direction: column; gap: 8px; }
/* Compact the picker when it lives inside a table edit row. */
td .deck-picker .field { min-width: 130px; padding: 7px 9px; font-size: 0.88rem; }

/* ---------- Leech marker / filter (Manage) ---------- */
.leech-badge { margin-left: 6px; cursor: help; }
.leech-toggle {
  display: inline-flex; align-items: center; gap: 6px;
  color: var(--muted); font-size: 0.88rem; white-space: nowrap;
  cursor: pointer; user-select: none;
}
.leech-toggle input { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; }

/* ---------- SM-2 grade buttons (Play, type mode) ---------- */
.grades { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.grade {
  flex-direction: column; gap: 3px; padding: 12px 6px;
  background: var(--surface); color: var(--text); border-color: var(--border);
}
.grade:hover { background: var(--surface-2); }
.grade .grade-key { font-size: 0.68rem; font-weight: 700; color: var(--muted); }
.grade-again { color: #fca5a5; border-color: rgba(239, 68, 68, 0.45); }
.grade-hard  { color: #fdba74; border-color: rgba(249, 115, 22, 0.45); }
.grade-good  { color: #93c5fd; border-color: rgba(59, 130, 246, 0.5); }
.grade-easy  { color: #86efac; border-color: rgba(34, 197, 94, 0.45); }
/* Highlight the default (Enter) choice with a ring in its own colour. */
.grade.default { box-shadow: inset 0 0 0 2px currentColor; }
@media (max-width: 420px) { .grades { grid-template-columns: repeat(2, 1fr); } }

/* ---------- PWA install (Home) ---------- */
.install-row { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.install-hint { font-size: 0.85rem; max-width: 440px; }

/* ---------- Reduced motion ---------- */
/* Honour the OS "reduce motion" setting: keep the 3D flip and other state
   changes instant rather than animated. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}

/* ---------- Light theme ---------- */
/* Follows the OS colour scheme. Only the palette changes; layout is shared. */
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f8fafc;
    --surface: #ffffff;
    --surface-2: #e2e8f0;
    --border: #cbd5e1;
    --text: #0f172a;
    --muted: #64748b;
    --accent: #2563eb;
    --accent-hover: #1d4ed8;
    --green: #16a34a;
    --red: #dc2626;
    --english-bg: #ffffff;
    --bulgarian-bg: #2563eb;
    --shadow: 0 10px 30px rgba(15, 23, 42, 0.12);
  }
  /* Darken the pastel status text so it stays legible on light surfaces.
     (The answer word keeps var(--text) via .feedback .answer, so it adapts.) */
  .error, .feedback.bad, .btn.danger { color: #b91c1c; }
  .btn.danger { border-color: rgba(220,38,38,0.4); }
  .feedback.ok { color: #15803d; }
  .choice.correct { color: #15803d; }
  .choice.wrong { color: #b91c1c; }
  /* Grade buttons: darker text so the colour reads on light surfaces. */
  .grade-again { color: #b91c1c; }
  .grade-hard  { color: #c2410c; }
  .grade-good  { color: #1d4ed8; }
  .grade-easy  { color: #15803d; }
  .box-1 { color: #b91c1c; }
  .box-2 { color: #c2410c; }
  .box-3 { color: #a16207; }
  .box-4 { color: #4d7c0f; }
  .box-5 { color: #15803d; }
}
