/* ═══════════════════════════════════════════════════════════
   DS Flashcards — Style Sheet
   ═══════════════════════════════════════════════════════════ */

:root {
  --bg: #0f1117;
  --bg2: #161b27;
  --bg3: #1e2535;
  --surface: #222a3d;
  --surface-hover: #2a3450;
  --border: #2e3a55;
  --primary: #4f8ef7;
  --primary-dark: #3a70d4;
  --success: #34c98b;
  --success-bg: #0d3326;
  --warning: #f9a825;
  --warning-bg: #332900;
  --text: #e8eaf0;
  --text-muted: #7a8aaa;
  --text-dim: #4a5570;
  --card-front: #1a2035;
  --card-back: #1a2c20;
  --radius: 14px;
  --radius-sm: 8px;
  --shadow: 0 8px 32px rgba(0,0,0,0.45);
  --sidebar-w: 260px;
  --header-h: 56px;
  --transition: 0.22s ease;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 16px; scroll-behavior: smooth; }

body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.6;
}

/* ── HEADER ──────────────────────────────────────────────── */
.header {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--header-h);
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  z-index: 100;
  backdrop-filter: blur(8px);
}

.header-inner {
  max-width: 1400px;
  margin: 0 auto;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
}

.logo-icon { font-size: 1.4rem; }

.logo-text {
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--text);
}

.logo-sub {
  font-size: 0.72rem;
  font-weight: 500;
  color: var(--primary);
  background: rgba(79,142,247,0.12);
  border: 1px solid rgba(79,142,247,0.25);
  padding: 2px 8px;
  border-radius: 20px;
  margin-left: 4px;
}

.header-controls {
  display: flex;
  gap: 8px;
}

/* ── LAYOUT ──────────────────────────────────────────────── */
.layout {
  display: flex;
  margin-top: var(--header-h);
  min-height: calc(100vh - var(--header-h));
}

/* ── SIDEBAR ─────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: var(--header-h);
  height: calc(100vh - var(--header-h));
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
  transition: width var(--transition), min-width var(--transition);
  z-index: 50;
}

.sidebar.collapsed {
  width: 48px;
  min-width: 48px;
}

.sidebar.collapsed .chapter-nav,
.sidebar.collapsed .sidebar-footer { display: none; }

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg2);
  z-index: 1;
}

.sidebar.collapsed .sidebar-header span { display: none; }

.sidebar-toggle {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 1rem;
  padding: 2px 4px;
  border-radius: 4px;
  transition: background var(--transition);
}

.sidebar-toggle:hover { background: var(--surface); color: var(--text); }

.chapter-nav {
  padding: 8px 0;
  flex: 1;
}

.chapter-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 16px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.82rem;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
  transition: background var(--transition), color var(--transition);
  position: relative;
  border-left: 3px solid transparent;
}

.chapter-btn:hover {
  background: var(--surface);
  color: var(--text);
}

.chapter-btn.active {
  background: rgba(79,142,247,0.1);
  color: var(--primary);
  border-left-color: var(--primary);
  font-weight: 600;
}

.chapter-btn .ch-badge {
  margin-left: auto;
  font-size: 0.68rem;
  background: var(--surface);
  color: var(--text-dim);
  padding: 1px 7px;
  border-radius: 10px;
  flex-shrink: 0;
}

.chapter-btn.active .ch-badge {
  background: rgba(79,142,247,0.15);
  color: var(--primary);
}

.chapter-btn .ch-done {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--success);
  flex-shrink: 0;
  opacity: 0;
  transition: opacity var(--transition);
}

.chapter-btn.chapter-complete .ch-done { opacity: 1; }

.sidebar-footer {
  padding: 16px;
  border-top: 1px solid var(--border);
}

.overall-progress {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 6px;
}

#overall-label { color: var(--text); font-weight: 600; }

/* ── MAIN ────────────────────────────────────────────────── */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 40px 24px 80px;
  min-width: 0;
}

.chapter-heading {
  width: 100%;
  max-width: 680px;
  margin-bottom: 28px;
}

.chapter-heading h1 {
  font-size: 1.55rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 8px;
}

.chapter-meta {
  display: flex;
  align-items: center;
  gap: 12px;
}

#chapter-progress-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
}

/* ── PROGRESS BARS ───────────────────────────────────────── */
.progress-bar-wrap {
  background: var(--surface);
  border-radius: 8px;
  height: 6px;
  overflow: hidden;
  width: 100%;
}

.progress-bar-wrap.inline { flex: 1; }

.progress-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--primary), var(--success));
  border-radius: 8px;
  transition: width 0.4s ease;
}

/* ── CARD PLACEHOLDER ────────────────────────────────────── */
.card-area {
  width: 100%;
  max-width: 680px;
  perspective: 1200px;
  margin-bottom: 24px;
  min-height: 340px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.card-placeholder {
  text-align: center;
  color: var(--text-muted);
  padding: 60px 40px;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  width: 100%;
}

.card-placeholder .hint {
  margin-top: 8px;
  font-size: 0.78rem;
  color: var(--text-dim);
}

/* ── FLASHCARD ───────────────────────────────────────────── */
.flashcard {
  width: 100%;
  min-height: 340px;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 0.55s cubic-bezier(0.4, 0, 0.2, 1);
  cursor: pointer;
}

.flashcard.flipped { transform: rotateY(180deg); }

.card-face {
  position: absolute;
  inset: 0;
  border-radius: var(--radius);
  padding: 36px 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  min-height: 340px;
}

.card-front {
  background: var(--card-front);
}

.card-back {
  background: var(--card-back);
  transform: rotateY(180deg);
  overflow-y: auto;
  justify-content: flex-start;
}

.card-label {
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  margin-bottom: 20px;
  opacity: 0.5;
}

.card-front .card-label { color: var(--primary); }
.card-back .card-label { color: var(--success); }

.card-question {
  font-size: 1.22rem;
  font-weight: 600;
  color: var(--text);
  line-height: 1.55;
  flex: 1;
  display: flex;
  align-items: center;
}

.card-answer {
  font-size: 0.93rem;
  color: var(--text);
  line-height: 1.75;
}

.card-answer b { color: var(--primary); }
.card-back .card-answer b { color: #6de5b0; }

.card-answer pre {
  background: rgba(0,0,0,0.3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  margin: 10px 0;
  font-size: 0.8rem;
  overflow-x: auto;
  color: #b8e0c8;
  white-space: pre;
  font-family: 'Menlo', 'Consolas', monospace;
}

.card-answer table {
  border-collapse: collapse;
  margin: 10px 0;
  font-size: 0.82rem;
}

.card-answer table td {
  border: 1px solid var(--border);
  padding: 5px 10px;
}

.card-flip-hint {
  margin-top: 20px;
  font-size: 0.72rem;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 6px;
}

/* Known overlay */
.card-known-badge {
  position: absolute;
  top: 14px;
  right: 16px;
  background: var(--success-bg);
  color: var(--success);
  font-size: 0.7rem;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 20px;
  border: 1px solid rgba(52,201,139,0.3);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

/* ── CONTROLS ────────────────────────────────────────────── */
.card-controls {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-bottom: 14px;
}

.card-counter {
  font-size: 0.9rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  min-width: 60px;
  text-align: center;
}

#card-index { color: var(--text); font-weight: 600; }

.known-controls {
  display: flex;
  gap: 12px;
  margin-bottom: 16px;
}

/* ── BUTTONS ─────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  font-family: inherit;
}

.btn-ghost {
  background: var(--surface);
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.btn-ghost:hover { background: var(--surface-hover); color: var(--text); }

.btn-nav {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  width: 40px;
  height: 40px;
  padding: 0;
  justify-content: center;
  border-radius: 50%;
}

.btn-nav:hover { background: var(--surface-hover); border-color: var(--primary); }
.btn-nav:disabled { opacity: 0.3; cursor: not-allowed; }

.btn-success {
  background: var(--success-bg);
  color: var(--success);
  border: 1px solid rgba(52,201,139,0.3);
}

.btn-success:hover { background: rgba(52,201,139,0.15); }

.btn-warning {
  background: var(--warning-bg);
  color: var(--warning);
  border: 1px solid rgba(249,168,37,0.3);
}

.btn-warning:hover { background: rgba(249,168,37,0.15); }

/* ── KBD HINT ────────────────────────────────────────────── */
.kbd-hint {
  font-size: 0.72rem;
  color: var(--text-dim);
}

kbd {
  display: inline-block;
  padding: 1px 6px;
  font-size: 0.7rem;
  font-family: inherit;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text-muted);
}

/* ── MOBILE HAMBURGER BUTTON ─────────────────────────────── */
.btn-menu {
  display: none;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  cursor: pointer;
  flex-shrink: 0;
  margin-right: 4px;
  -webkit-tap-highlight-color: transparent;
}

/* ── SIDEBAR OVERLAY ─────────────────────────────────────── */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 150;
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  opacity: 0;
  transition: opacity var(--transition);
  pointer-events: none;
}

.sidebar-overlay.visible {
  opacity: 1;
  pointer-events: all;
}

/* ── VISIBILITY HELPERS ──────────────────────────────────── */
.mobile-only { display: none; }
.desktop-only { display: block; }
.placeholder-mobile { display: none; }

/* ── ANIMATIONS ──────────────────────────────────────────── */
@keyframes slideIn {
  from { opacity: 0; transform: translateY(12px); }
  to   { opacity: 1; transform: translateY(0); }
}

.flashcard { animation: slideIn 0.3s ease; }

/* ── RESPONSIVE: TABLET (≤900px) ────────────────────────── */
@media (max-width: 900px) {
  :root { --sidebar-w: 230px; }

  .main { padding: 32px 20px 80px; }

  .card-question { font-size: 1.1rem; }
}

/* ── RESPONSIVE: MOBILE (≤768px) ────────────────────────── */
@media (max-width: 768px) {
  :root { --header-h: 52px; }

  /* Show hamburger, hide sidebar toggle */
  .btn-menu { display: flex; }
  .sidebar-toggle { display: none; }
  .sidebar-overlay { display: block; }

  /* Sidebar: slide in from left as drawer */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100dvh;
    width: var(--sidebar-w) !important;
    min-width: var(--sidebar-w) !important;
    transform: translateX(-100%);
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 200;
    box-shadow: var(--shadow);
    padding-top: var(--header-h);
  }

  .sidebar.mobile-open {
    transform: translateX(0);
  }

  /* Header */
  .header-inner { padding: 0 14px; }
  .logo-sub { display: none; }

  /* Shrink header buttons to icon only */
  .btn-label { display: none; }
  .btn-ghost { padding: 8px 10px; }

  /* Main */
  .main { padding: 20px 14px 100px; }

  .chapter-heading { margin-bottom: 16px; }
  .chapter-heading h1 { font-size: 1.2rem; }

  /* Card area — fill viewport width */
  .card-area {
    min-height: 0;
    max-width: 100%;
    margin-bottom: 16px;
  }

  .flashcard { min-height: 300px; }

  .card-face {
    padding: 22px 20px;
    min-height: 300px;
  }

  .card-question { font-size: 1.05rem; }
  .card-answer { font-size: 0.87rem; line-height: 1.65; }

  .card-answer pre { font-size: 0.75rem; padding: 8px 10px; }

  /* Controls: larger tap targets */
  .card-controls { gap: 16px; margin-bottom: 12px; }

  .btn-nav {
    width: 48px;
    height: 48px;
  }

  /* Known/Again buttons: full width */
  .known-controls {
    width: 100%;
    max-width: 100%;
    gap: 10px;
  }

  .known-controls .btn {
    flex: 1;
    justify-content: center;
    padding: 12px 16px;
    font-size: 0.88rem;
  }

  /* Swipe indicator on card */
  .card-flip-hint { margin-top: 14px; }

  /* Visibility helpers */
  .mobile-only { display: block; }
  .desktop-only { display: none; }
  .placeholder-mobile { display: block; }
  .placeholder-desktop { display: none; }

  .kbd-hint.mobile-only { display: block; }
}

/* ── RESPONSIVE: SMALL MOBILE (≤480px) ──────────────────── */
@media (max-width: 480px) {
  :root { --sidebar-w: 85vw; }

  .card-face { padding: 18px 16px; }
  .card-question { font-size: 1rem; }
  .card-answer { font-size: 0.84rem; }

  .chapter-heading h1 { font-size: 1.05rem; }

  /* Make card back scrollable with momentum */
  .card-back {
    -webkit-overflow-scrolling: touch;
  }
}

/* ── TOUCH: active states ────────────────────────────────── */
@media (hover: none) {
  .btn:active { opacity: 0.7; }
  .flashcard:active { opacity: 0.95; }
  .chapter-btn:active { background: var(--surface-hover); }
}
