/* ── Reset & tokens ──────────────────────────────────────────────────────────── */
:root {
  --bg:       #0f1724;
  --accent:   #7c3aed;
  --muted:    #94a3b8;
  --glass:    rgba(255, 255, 255, 0.03);
  --radius:   16px;

  /* Safe area insets for Android PWA gesture bar / iOS home indicator */
  --safe-top:    env(safe-area-inset-top,    0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left:   env(safe-area-inset-left,   0px);
  --safe-right:  env(safe-area-inset-right,  0px);

  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto,
               "Helvetica Neue", Arial, sans-serif;
}

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

html, body {
  height: 100%;
  margin: 0;
  background: linear-gradient(180deg, #071024 0%, #071933 100%);
  color: #e6eef8;
  /* Prevent overscroll bounce on iOS */
  overscroll-behavior: none;
}

/* ── App shell ───────────────────────────────────────────────────────────────── */

.app {
  min-height: 100vh;
  min-height: 100dvh;   /* dynamic viewport height — accounts for mobile browser chrome */
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.panel {
  width: 100%;
  max-width: 520px;
  min-height: min(720px, 100dvh);
  background: linear-gradient(180deg, rgba(255,255,255,0.10), transparent);
  border-radius: 18px;
  padding: 18px;
  backdrop-filter: blur(6px);
  box-shadow: 0 8px 30px rgba(2,6,23,0.6);
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: relative;
}

/* ── Landing ─────────────────────────────────────────────────────────────────── */

.vertical {
  display: flex;
  flex-direction: column;
  flex: 1;
  align-items: center;
  justify-content: center;
  gap: 20px;
  padding: 20px 0;
}

.icon {
  width: 96px; height: 96px;
  border-radius: 24px;
  background: linear-gradient(135deg, var(--accent), #4f46e5);
  display: flex; align-items: center; justify-content: center;
  box-shadow: inset 0 -6px 14px rgba(255,255,255,0.06);
  flex-shrink: 0;
}

.title { font-size: 28px; font-weight: 700; letter-spacing: 0.4px; }

.bottom-50 {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 100%;
  align-items: center;
}

.gear-btn {
  position: absolute;
  bottom: calc(16px + var(--safe-bottom));
  right: calc(16px + var(--safe-right));
  background: none; border: none;
  color: rgba(255,255,255,0.18);
  font-size: 18px; cursor: pointer;
  padding: 8px; line-height: 1;
  transition: color 0.2s;
  /* Ensure minimum touch target */
  min-width: 44px; min-height: 44px;
  display: flex; align-items: center; justify-content: center;
}
.gear-btn:hover { color: rgba(255,255,255,0.55); }

/* ── Buttons ─────────────────────────────────────────────────────────────────── */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 80%;
  padding: 14px;
  min-height: 44px;   /* WCAG touch target */
  border-radius: 12px;
  background: var(--glass);
  border: 1px solid rgba(255,255,255,0.04);
  font-family: inherit; font-size: 1rem; line-height: 1.4; font-weight: 600;
  cursor: pointer; user-select: none;
  color: #e6eef8;
  -webkit-tap-highlight-color: transparent;
}

.btn.primary {
  background: linear-gradient(90deg, var(--accent), #4f46e5);
  box-shadow: 0 6px 18px rgba(124,58,237,0.18);
  color: white;
}
.btn.primary:disabled { opacity: 0.35; cursor: not-allowed; box-shadow: none; }
.btn.secondary {
  background: linear-gradient(90deg, rgba(255,255,255,0.11), transparent);
  color: white;
}

.btn-tool {
  padding: 5px 12px;
  min-height: 36px;
  border-radius: 8px;
  border: 1px solid rgba(255,255,255,0.15);
  background: rgba(255,255,255,0.07);
  color: var(--muted);
  font-family: inherit; font-size: 12px; font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.btn-tool:hover { background: rgba(255,255,255,0.13); color: #e6eef8; }

.btn-nav {
  padding: 4px 10px;
  min-height: 36px;
  border-radius: 7px;
  border: 1px solid rgba(255,255,255,0.10);
  background: rgba(255,255,255,0.05);
  color: var(--muted);
  font-family: inherit; font-size: 12px; font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}
.btn-nav:hover { background: rgba(255,255,255,0.11); color: #e6eef8; }

/* Modal back chevron — no border, just the SVG icon */
.modal-back {
  background: none; border: none;
  padding: 2px 4px;
  min-width: 44px; min-height: 44px;
  display: flex; align-items: center;
  cursor: pointer;
  color: rgba(255,255,255,0.25);
  flex-shrink: 0;
  transition: color 0.15s;
  line-height: 0;
  margin-left: -8px;   /* optically align chevron with modal edge */
  -webkit-tap-highlight-color: transparent;
}
.modal-back:hover { color: rgba(255,255,255,0.75); }

.small { font-size: 13px; color: var(--muted); }
input[type=file] { display: none; }

/* ── Modal backdrop ──────────────────────────────────────────────────────────── */

.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(2,6,23,0.6);
  display: none;
  align-items: flex-end;   /* bottom-sheet default on mobile */
  justify-content: center;
  z-index: 100;
  padding: 0;
}

/* ── Modal base ──────────────────────────────────────────────────────────────── */

.modal {
  display: flex;
  flex-direction: column;
  padding: 22px;
  width: 100%;
  max-width: 560px;
  /* Full-height by default — sheet picker and options need this */
  height: calc(100dvh - 40px);
  max-height: calc(100dvh - 40px);
  background: linear-gradient(170deg, #12061a, #161b2b);
  box-shadow: 0 16px 48px rgba(2,6,23,0.8);
  gap: 12px;
  /* Rounded top corners — bottom-sheet style */
  border-radius: 18px 18px 0 0;
}

/* Modal header: back chevron + title */
.modal-header {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}
.modal-header h3 { margin: 0; font-size: 17px; }

/* Sheet picker — needs full height */
.modal--sheet { max-width: 600px; }

/* Info and settings — auto height, grows from bottom */
.modal--info,
.modal--settings,
.modal--presets {
  height: auto;
  max-height: 80dvh;
}

/* ── Modal footer ────────────────────────────────────────────────────────────── */

.modal-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: flex-end;
  /* Push above Android gesture bar */
  padding-bottom: var(--safe-bottom);
  flex-shrink: 0;
}
.modal-footer .btn { width: auto; padding: 10px 22px; }
.modal-footer-count { margin-right: auto; font-weight: 400; color: var(--muted); }

/* ── Sheet picker ────────────────────────────────────────────────────────────── */

.search-input {
  width: 100%;
  padding: 9px 12px;
  border-radius: 8px;
  border: 1px solid rgba(255,255,255,0.08);
  background: rgba(255,255,255,0.07);
  color: inherit;
  font-family: inherit; font-size: 0.9rem;
  flex-shrink: 0;
  /* Prevent iOS zoom on focus (requires font-size ≥ 16px or this override) */
  font-size: max(16px, 0.9rem);
}
.search-input:focus { outline: none; border-color: rgba(124,58,237,0.5); }

.sheet-toolbar { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.modal-status  { margin-left: auto; }

.sheet-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
  mask-image: linear-gradient(to bottom,
    transparent 0%, black 16px, black calc(100% - 16px), transparent 100%);
}

.sheet-row {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px;   /* slightly taller for touch */
  border-radius: 8px;
  cursor: pointer; user-select: none;
  transition: background 0.12s;
  border: 1px solid transparent;
  flex-shrink: 0;
  min-height: 44px;
  -webkit-tap-highlight-color: transparent;
}
.sheet-row:hover    { background: rgba(255,255,255,0.06); }
.sheet-row.selected { background: rgba(124,58,237,0.15); border-color: rgba(124,58,237,0.3); }

.sheet-row input[type=checkbox] {
  width: 18px; height: 18px;   /* slightly larger on touch */
  accent-color: var(--accent);
  flex-shrink: 0; cursor: pointer;
}

.sheet-row-name { flex: 1; font-size: 0.95rem; }

.sheet-row-counts { display: flex; gap: 4px; flex-shrink: 0; align-items: center; }

.count-pill {
  display: inline-flex; align-items: center; gap: 3px;
  font-size: 11px; padding: 3px 8px;
  border-radius: 10px;
  cursor: pointer; user-select: none;
  border: 1px solid transparent;
  background: rgba(255,255,255,0.06); color: var(--muted);
  transition: background 0.12s, color 0.12s, border-color 0.12s;
  white-space: nowrap;
  min-height: 28px;
  -webkit-tap-highlight-color: transparent;
}
.count-pill--active { background: rgba(255,255,255,0.14); color: #e6eef8; border-color: rgba(255,255,255,0.18); }
.count-pill--tagged.count-pill--active { background: rgba(124,58,237,0.28); color: #c4b5fd; border-color: rgba(124,58,237,0.50); }

.pill-bookmark {
  width: 8px; height: 11px; flex-shrink: 0;
  clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 72%, 0 100%);
  background: currentColor; opacity: 0.85;
}

/* ── Options modal ───────────────────────────────────────────────────────────── */

.controls { display: flex; flex-direction: column; gap: 10px; flex-shrink: 0; padding-bottom: 4px; }

.sample-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.sample-row input[type=number] {
  width: 56px; padding: 3px 6px;
  border-radius: 6px;
  border: 1px solid rgba(255,255,255,0.08);
  background: rgba(255,255,255,0.09);
  color: inherit; font-family: inherit; font-size: inherit; text-align: center;
}

input[type=number], input[type=text], input[type=search] {
  font-family: inherit; font-size: inherit; color: inherit;
}

label { display: flex; gap: 8px; align-items: center; cursor: pointer; min-height: 36px; }

/* ── Tag filter scroll area ──────────────────────────────────────────────────── */

.tag-filters-scroll {
  flex: 1; overflow-y: auto; min-height: 0;
  display: flex; flex-direction: column;
  -webkit-overflow-scrolling: touch;
  mask-image: linear-gradient(to bottom,
    transparent 0%, black 16px, black calc(100% - 16px), transparent 100%);
}

.tag-group { margin-top: 10px; border: 1px solid rgba(255,255,255,0.07); border-radius: 10px; overflow: hidden; flex-shrink: 0; }

.tag-group-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px;
  cursor: pointer; background: rgba(255,255,255,0.04);
  user-select: none; transition: background 0.12s;
  min-height: 44px;
  -webkit-tap-highlight-color: transparent;
}
.tag-group-header:hover { background: rgba(255,255,255,0.08); }

.tag-group-title { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.6px; font-weight: 600; }
.tag-group-chevron { font-size: 10px; color: var(--muted); transition: transform 0.2s; }
.tag-group.open .tag-group-chevron { transform: rotate(180deg); }
.tag-group-body { display: none; padding: 10px 12px 12px; }
.tag-group.open .tag-group-body { display: block; }

.tag-chips { display: flex; flex-wrap: wrap; gap: 6px; }

.tag-chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 6px 12px;   /* larger touch target */
  border-radius: 20px;
  background: rgba(255,255,255,0.07); font-size: 12px;
  cursor: pointer; user-select: none;
  border: 1px solid transparent;
  transition: background 0.12s, border-color 0.12s;
  min-height: 36px;
  -webkit-tap-highlight-color: transparent;
}
.tag-chip input[type=checkbox] { display: none; }
.tag-chip.checked { background: rgba(124,58,237,0.22); border-color: rgba(124,58,237,0.5); }
.tag-chip-count { opacity: 0.55; font-size: 11px; }

/* ── Settings modal ──────────────────────────────────────────────────────────── */

.settings-section { display: flex; flex-direction: column; gap: 10px; }
.settings-section-title { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.6px; font-weight: 600; }
.settings-row { display: flex; gap: 8px; }

/* ── Quiz view ───────────────────────────────────────────────────────────────── */

#quiz {
  display: flex;
  flex-direction: column;
  flex: 1;
  justify-content: space-between;
  width: 100%;
  gap: 12px;
}

.quiz-box {
  position: relative;
  /* Responsive height: at least 220px, at most 340px, ideally 40dvh */
  height: clamp(220px, 40dvh, 340px);
  border-radius: 14px;
  border: 1px solid rgba(255,255,255,0.03);
  overflow: hidden;
  flex-shrink: 0;
}

/* ── Bookmark ribbon ─────────────────────────────────────────────────────────── */

.ribbon {
  position: absolute; top: 8px; right: 12px;
  width: 18px; height: 26px;
  clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 72%, 0 100%);
  background: rgba(255,255,255,0.15);
  cursor: pointer; z-index: 2;
  transition: background 0.18s;
  /* Extend tap area without changing visual size */
  padding: 8px;
  margin: -8px;
}
.ribbon:hover     { background: rgba(255,255,255,0.35); }
.ribbon--on       { background: #7c3aed; }
.ribbon--on:hover { background: #9155ff; }

/* ── Card inner ──────────────────────────────────────────────────────────────── */

.card-inner {
  width: 100%; height: 100%;
  display: grid; grid-template-rows: 1fr auto;
  gap: 6px;
  padding: 20px 48px 16px 20px;
  border-radius: 14px;
  transition: background 0.22s;
}
.quiz-box.front .card-inner { background: linear-gradient(135deg, #333333, #1b2030); }
.quiz-box.back  .card-inner { background: linear-gradient(135deg, #000000, #1b2030); }

@keyframes squish-out { from { transform: scaleX(1); } to { transform: scaleX(0); } }
@keyframes squish-in  { from { transform: scaleX(0); } to { transform: scaleX(1); } }
.card-inner.squish-out { animation: squish-out 0.15s ease-in  forwards; }
.card-inner.squish-in  { animation: squish-in  0.15s ease-out forwards; }

.card-text {
  /* Responsive font: 16px minimum, 20px ideal, scales with viewport */
  font-size: clamp(16px, 4vw, 20px);
  text-align: center;
  word-break: break-word;
  align-self: center;
  overflow-y: auto;
  max-height: 100%;
}

.card-context {
  font-size: clamp(11px, 2.5vw, 13px);
  color: var(--muted);
  text-align: center;
  word-break: break-word;
  align-self: start;
  min-height: 1em;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.info-btn {
  position: absolute; bottom: 8px; right: 10px;
  background: none; border: none;
  color: rgba(255,255,255,0.22);
  font-size: 15px; cursor: pointer;
  /* Large tap target */
  padding: 10px; margin: -6px;
  line-height: 1; transition: color 0.15s; z-index: 1;
  -webkit-tap-highlight-color: transparent;
}
.info-btn:hover { color: rgba(255,255,255,0.7); }

/* ── Card info modal ─────────────────────────────────────────────────────────── */

/* Meta row: File / Sheet / Line — horizontal chips */
.info-meta {
  display: flex;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 8px;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.07);
  flex-shrink: 0;
  flex-wrap: wrap;   /* wraps on narrow screens */
}
.info-meta-item { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.info-meta-label { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; }
.info-meta-value { font-size: 13px; color: #e6eef8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 200px; }

/* Info rows: key/value pairs */
.info-list {
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid rgba(255,255,255,0.07);
  border-radius: 8px;
  /* Shrinks to content; modal--info uses height:auto so this just takes what it needs */
  flex-shrink: 1;
  min-height: 0;
}

.info-row {
  display: grid;
  grid-template-columns: 110px 1fr;
  font-size: 14px;
  border-bottom: 1px solid rgba(255,255,255,0.06);
}
.info-row:last-child { border-bottom: none; }

.info-row-key {
  padding: 10px 12px;
  background: rgba(255,255,255,0.04);
  color: var(--muted); font-size: 11px;
  text-transform: uppercase; letter-spacing: 0.5px;
  display: flex; align-items: center;
  border-right: 1px solid rgba(255,255,255,0.06);
  word-break: break-word;
}
.info-row-val {
  padding: 10px 12px;
  word-break: break-word;
  display: flex; align-items: center;
}

/* ── Controls row ────────────────────────────────────────────────────────────── */

.controls-row {
  display: flex; gap: 8px;
  align-items: center; justify-content: center;
  flex-shrink: 0;
}
.controls-row .btn {
  width: auto; flex: 1;
  min-height: 48px;   /* larger touch target for primary quiz controls */
}

/* ── Top nav ────────────────────────────────────────────���────────────────────── */

.topnav {
  display: flex; align-items: center; gap: 8px;
  flex-shrink: 0;
  padding-top: var(--safe-top);   /* under notch / status bar in PWA */
}
.topnav-label {
  margin-left: auto;
  text-align: right;
  overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
  font-size: 12px; color: var(--muted);
}

/* ── Drag hover ──────────────────────────────────────────────────────────────── */

.dragover { outline: 3px dashed rgba(124,58,237,0.35); }

/* ── Desktop: centre the modal as a floating card ────────────────────────────── */

@media (min-width: 641px) {
  .modal-backdrop {
    align-items: center;
    padding: 16px;
  }

  .modal {
    height: calc(100vh - 60px);
    max-height: 720px;
    border-radius: 14px;   /* fully rounded on desktop */
  }

  .modal--info,
  .modal--settings,
  .modal--presets {
    height: auto;
    max-height: calc(100vh - 60px);
    max-width: 480px;
  }
}

/* ── Mobile / PWA ────────────────────────────────────────────────────────────── */

@media (max-width: 640px) {
  html { overflow: hidden; }

  .app {
    padding: 0;
    margin: 0;
    height: 100dvh;
    align-items: stretch;
  }

  .panel {
    height: 100dvh;
    max-width: 100%;
    border-radius: 0;
    padding: 16px;
    padding-top:    calc(16px + var(--safe-top));
    padding-bottom: calc(16px + var(--safe-bottom));
    padding-left:   calc(16px + var(--safe-left));
    padding-right:  calc(16px + var(--safe-right));
  }

  #quiz { justify-content: flex-start; }

  /* Full-height modals (sheet picker, options) stay full height */
  .modal {
    height: calc(100dvh - var(--safe-top));
    max-height: calc(100dvh - var(--safe-top));
    border-radius: 18px 18px 0 0;
    padding-bottom: calc(22px + var(--safe-bottom));
  }

  /* Auto-height modals (info, settings, presets) grow from bottom */
  .modal--info,
  .modal--settings,
  .modal--presets {
    height: auto;
    max-height: calc(85dvh - var(--safe-top));
    padding-bottom: calc(22px + var(--safe-bottom));
  }

  /* Slightly smaller card text on very small screens */
  .card-text { font-size: clamp(15px, 5vw, 20px); }

  /* Wider info key column would waste space — collapse a bit */
  .info-row { grid-template-columns: 80px 1fr; }
}

/* ── Landscape phone ─────────────────────────────────────────────────────────── */

@media (max-width: 900px) and (max-height: 500px) {
  .quiz-box { height: clamp(160px, 45dvh, 220px); }
  .vertical  { gap: 12px; }
  .icon      { width: 64px; height: 64px; border-radius: 16px; }
  .title     { font-size: 22px; }

  /* Modals become side-anchored in landscape */
  .modal-backdrop { align-items: center; padding: 8px; }
  .modal          { border-radius: 12px; height: 98dvh; }
  .modal--info, .modal--settings, .modal--presets {
    height: auto; max-height: 95dvh; border-radius: 12px;
  }
}