:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #eff6ff;
  --success: #16a34a;
  --success-soft: #f0fdf4;
  --warn: #d97706;
  --warn-soft: #fffbeb;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --radius: 14px;
  --shadow: 0 4px 18px rgba(15, 23, 42, 0.06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --surface: #111827;
    --surface-alt: #1e293b;
    --text: #f1f5f9;
    --muted: #94a3b8;
    --border: #1f2937;
    --primary: #3b82f6;
    --primary-hover: #2563eb;
    --primary-soft: rgba(59, 130, 246, 0.16);
    --success: #22c55e;
    --success-soft: rgba(34, 197, 94, 0.14);
    --warn: #f59e0b;
    --warn-soft: rgba(245, 158, 11, 0.14);
    --danger: #ef4444;
    --danger-soft: rgba(239, 68, 68, 0.14);
    --shadow: 0 4px 20px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}

.app-shell {
  max-width: 760px;
  margin: 0 auto;
  padding: 12px 14px 48px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand-badge {
  width: 40px; height: 40px; border-radius: 12px;
  background: linear-gradient(135deg, #2563eb, #1e40af);
  color: #fff; font-weight: 800; font-size: 16px;
  display: grid; place-items: center;
}
.brand h1 { font-size: 18px; margin: 0; }
.subtitle { margin: 0; font-size: 12px; color: var(--muted); }

.streak-pill {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 6px 12px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 14px;
}

.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  background: var(--surface);
  padding: 5px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  margin-bottom: 14px;
}

.tab {
  border: none;
  background: transparent;
  color: var(--muted);
  font-weight: 600;
  font-size: 14px;
  padding: 10px 8px;
  border-radius: 10px;
  cursor: pointer;
}
.tab.active {
  background: var(--primary);
  color: #fff;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

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

.card h2 { margin: 0 0 8px; font-size: 17px; }
.muted { color: var(--muted); font-size: 13px; margin: 0; }
.hidden { display: none !important; }

.controls-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-bottom: 12px;
}

.segmented {
  display: inline-flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 3px;
}
.seg-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  padding: 7px 10px;
  border-radius: 7px;
  cursor: pointer;
}
.seg-btn.active {
  background: var(--primary-soft);
  color: var(--primary);
}

.filters-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 12px;
}
.filter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 10px;
}
.filter-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.filter-grid .checkbox-row { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-size: 13px; padding-top: 6px; }

select, input[type="text"], input[type="search"], textarea {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--primary);
  border-color: transparent;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 11px 16px;
  font-weight: 600;
  font-size: 15px;
  cursor: pointer;
}
.btn-primary:hover { background: var(--primary-hover); }

.btn-ghost {
  background: var(--surface-alt);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
}
.btn-sm { padding: 7px 10px; font-size: 13px; }

.quiz-card { padding: 18px; }
.quiz-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
}
.case-badge {
  background: var(--primary-soft);
  color: var(--primary);
  padding: 5px 10px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 13px;
}
.pattern-badge {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}

.q-trigger {
  font-size: 18px;
  font-weight: 700;
  color: var(--primary);
  text-align: center;
  margin-bottom: 6px;
}
.q-prompt-box {
  text-align: center;
  padding: 14px 10px;
  background: var(--surface-alt);
  border-radius: 12px;
  margin-bottom: 14px;
}
.q-base { font-size: 26px; font-weight: 800; letter-spacing: -0.01em; }
.q-ru { font-size: 14px; color: var(--muted); margin-top: 2px; }

.diacritic-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-bottom: 10px;
  justify-content: center;
}
.diacritic-bar button {
  min-width: 34px;
  height: 36px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}
.diacritic-bar button:active { background: var(--primary-soft); color: var(--primary); }

#answer-form {
  display: flex;
  gap: 8px;
}
#answer-form input { flex: 1; font-size: 17px; font-weight: 600; }

.choice-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.choice-btn {
  padding: 14px 10px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  min-height: 52px;
}
.choice-btn.correct { background: var(--success-soft); border-color: var(--success); color: var(--success); }
.choice-btn.wrong { background: var(--danger-soft); border-color: var(--danger); color: var(--danger); }

.feedback {
  margin-top: 14px;
  padding: 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
}
.feedback.ok { background: var(--success-soft); border-color: var(--success); }
.feedback.warn { background: var(--warn-soft); border-color: var(--warn); }
.feedback.bad { background: var(--danger-soft); border-color: var(--danger); }
.feedback-head { font-weight: 800; font-size: 15px; margin-bottom: 4px; }
.feedback-answer { font-size: 18px; font-weight: 800; margin-bottom: 4px; }
.feedback-note { font-size: 13px; color: var(--muted); margin-bottom: 10px; }
.feedback-actions { display: flex; justify-content: space-between; gap: 8px; }

.cases-list, .patterns-list, .tips-list { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.case-row, .tip-item {
  background: var(--surface-alt);
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 14px;
}

.pattern-item {
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}
.pattern-summary {
  width: 100%;
  text-align: left;
  background: var(--surface-alt);
  border: none;
  color: var(--text);
  padding: 12px 14px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}
.pattern-title { font-weight: 800; font-size: 15px; }
.pattern-rule { font-size: 13px; color: var(--muted); margin-top: 2px; }
.pattern-ex { font-size: 13px; color: var(--primary); font-weight: 600; margin-top: 2px; }
.pattern-table-wrap { padding: 10px 12px; overflow-x: auto; }

table.decl-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
table.decl-table th, table.decl-table td {
  text-align: left;
  padding: 7px 8px;
  border-bottom: 1px solid var(--border);
}
table.decl-table th { color: var(--muted); font-size: 12px; font-weight: 700; }

.stats-header, .dict-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.stats-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 10px;
}
.stat-box {
  background: var(--surface-alt);
  padding: 10px;
  border-radius: 10px;
  text-align: center;
}
.stat-val { font-size: 20px; font-weight: 800; }
.stat-lbl { font-size: 11px; color: var(--muted); }
.weak-spots { font-size: 13px; color: var(--muted); }

.dict-controls { display: grid; grid-template-columns: 1fr 160px; gap: 8px; margin-bottom: 12px; }
.dict-list { display: flex; flex-direction: column; gap: 6px; max-height: 460px; overflow-y: auto; }
.dict-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  background: var(--surface-alt);
  border-radius: 10px;
  cursor: pointer;
}
.dict-row:hover { border: 1px solid var(--primary); padding: 9px 11px; }

.modal { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 16px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.55); }
.modal-content {
  position: relative;
  width: 100%;
  max-width: 520px;
  max-height: 85vh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.3);
}
.modal-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 12px; }
.modal-header h3 { margin: 0; }
.modal-actions { display: flex; justify-content: space-between; gap: 8px; margin-top: 10px; }

@media (max-width: 520px) {
  .dict-controls { grid-template-columns: 1fr; }
  .choice-grid { grid-template-columns: 1fr; }
  #answer-form { flex-direction: column; }
}