/* ==========================================================================
   DSE Vocab King — design tokens, glass header, cards, sheet, skeletons
   Apple / Swiss minimalism. No external stylesheet dependencies.
   ========================================================================== */

:root {
  --canvas: #fafafc;
  --surface: #ffffff;
  --hair: #e5e5ea;
  --ink: #1d1d1f;
  --muted: #86868b;
  --pill: #f2f2f7;
  --pill-ink: #3a3a3c;
  --danger: #b3261e;
  --accent: #000000;

  --radius: 12px;
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
  --header-h: 132px;

  --font: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text",
          "Helvetica Neue", "PingFang HK", "PingFang TC", "Noto Sans HK",
          "Microsoft JhengHei", system-ui, sans-serif;
}

/* Dark mode — deep charcoal, same hierarchy, no pure-black OLED burn-in */
@media (prefers-color-scheme: dark) {
  :root {
    --canvas: #1d1d1f;
    --surface: #2c2c2e;
    --hair: #3a3a3c;
    --ink: #f5f5f7;
    --muted: #98989d;
    --pill: #3a3a3c;
    --pill-ink: #e5e5ea;
    --danger: #ff6b60;
    --accent: #ffffff;
  }
  .glass { background: rgba(29, 29, 31, 0.72); }
  .sheet { background: var(--canvas); }
  .gate-card { background: var(--surface); }
  .skeleton-card { background: linear-gradient(100deg, #2c2c2e 30%, #3a3a3c 50%, #2c2c2e 70%); }
  .card { background: var(--surface); }
  #search, #gateInput { background: var(--surface); }
  .upgrade-box { background: var(--pill); }
  .chip { background: var(--pill); color: var(--pill-ink); }
  .on-white { background: var(--surface); }
  .sheet-backdrop { background: rgba(0, 0, 0, 0.5); }
}

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

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
  background: var(--canvas);
}

body {
  font-family: var(--font);
  background: var(--canvas);
  color: var(--ink);
  margin: 0;
  min-height: 100svh;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

/* iOS: kill the native search-widget chrome */
input[type="search"]::-webkit-search-cancel-button,
input[type="search"]::-webkit-search-decoration { -webkit-appearance: none; display: none; }

button { font-family: inherit; }

/* --------------------------- Scrollbars --------------------------- */
.scrollbar-none { scrollbar-width: none; -ms-overflow-style: none; }
.scrollbar-none::-webkit-scrollbar { display: none; }

.sheet-scroll, body { scrollbar-width: thin; scrollbar-color: var(--hair) transparent; }
.sheet-scroll::-webkit-scrollbar,
body::-webkit-scrollbar { width: 8px; height: 8px; }
.sheet-scroll::-webkit-scrollbar-thumb,
body::-webkit-scrollbar-thumb {
  background: var(--hair); border-radius: 99px; border: 2px solid transparent; background-clip: content-box;
}
.sheet-scroll::-webkit-scrollbar-track, body::-webkit-scrollbar-track { background: transparent; }

/* --------------------------- Glass header --------------------------- */
.glass {
  background: rgba(250, 250, 252, 0.78);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--hair);
  transition: transform 0.3s var(--ease-out), opacity 0.3s var(--ease-out);
}
.glass.is-hidden { transform: translateY(-102%); opacity: 0; pointer-events: none; }

/* Sync indicator */
.sync-dot {
  width: 6px; height: 6px; border-radius: 99px;
  background: var(--muted); opacity: 0.45;
  transition: background-color 0.3s, opacity 0.3s, transform 0.3s;
}
.sync-dot[data-state="live"] { background: #30a46c; opacity: 1; }
.sync-dot[data-state="busy"] { background: var(--muted); opacity: 1; animation: pulse 1.1s ease-in-out infinite; }
.sync-dot[data-state="error"] { background: var(--danger); opacity: 1; }
.sync-dot.is-bumped { transform: scale(1.9); }
@keyframes pulse { 0%, 100% { opacity: 0.3; } 50% { opacity: 1; } }

/* --------------------------- Segmented pills --------------------------- */
.seg {
  display: inline-flex; align-items: center; gap: 2px;
  background: var(--pill); border-radius: 99px; padding: 3px;
  flex-shrink: 0;
}
.seg-btn {
  appearance: none; border: 0; cursor: pointer;
  padding: 6px 12px; border-radius: 99px;
  font-size: 12.5px; line-height: 1; font-weight: 500; white-space: nowrap;
  color: var(--muted); background: transparent;
  transition: color 0.2s, background-color 0.2s, box-shadow 0.2s, transform 0.15s var(--ease-out);
  -webkit-tap-highlight-color: transparent;
}
.seg-btn:hover { color: var(--ink); }
.seg-btn:active { transform: scale(0.96); }
.seg-btn.is-on {
  color: var(--ink); background: var(--surface);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1), 0 0 0 0.5px rgba(0, 0, 0, 0.04);
}

.pill {
  display: inline-flex; align-items: center; gap: 6px; flex-shrink: 0;
  padding: 8px 13px; border-radius: 99px; cursor: pointer;
  font-size: 12.5px; line-height: 1; font-weight: 500; white-space: nowrap;
  color: var(--muted); background: var(--pill);
  border: 1px solid transparent;
  transition: color 0.2s, background-color 0.2s, border-color 0.2s, transform 0.15s var(--ease-out);
  -webkit-tap-highlight-color: transparent;
}
.pill:hover { color: var(--ink); }
.pill:active { transform: scale(0.96); }
.pill[aria-pressed="true"] {
  color: var(--canvas); background: var(--ink); border-color: var(--ink);
}

/* --------------------------- Cards --------------------------- */
/* Height is FIXED, not content-driven: the virtual list maps scroll offset to
   record index by division, so a variable row height would make neighbouring
   cards overlap. Long content truncates instead. */
.card {
  height: 116px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--hair);
  border-radius: var(--radius);
  padding: 14px 16px;
  cursor: pointer;
  user-select: none; -webkit-user-select: none;
  transition: transform 0.18s var(--ease-out), box-shadow 0.18s var(--ease-out), border-color 0.18s;
  contain: layout paint style;
}
.card:active { transform: scale(0.982); }
@media (hover: hover) {
  .card:hover { box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 8px 24px rgba(0, 0, 0, 0.05); border-color: rgba(0, 0, 0, 0.1); }
}
.card-word {
  font-size: 16px; font-weight: 600; letter-spacing: -0.015em; line-height: 1.3;
  color: var(--ink);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.card-cn {
  flex: 0 0 auto; height: 38px; margin-top: 3px;
  display: flex; align-items: center; overflow: hidden;
  font-size: 13px; line-height: 1.45; color: var(--muted);
}
.card-cn > span {
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  white-space: pre-line; word-break: break-word;
}
.chip {
  display: inline-block; background: var(--pill); color: var(--pill-ink);
  border-radius: 5px; padding: 2.5px 6px;
  font-size: 10px; font-weight: 600; letter-spacing: 0.05em; text-transform: uppercase;
  line-height: 1; white-space: nowrap;
}
.chip-cefr { border: 0.5px solid var(--hair); background: transparent; }
.chip-p1 { background: #eef2ff; color: #3730a3; }
.chip-p3 { background: #ecfdf5; color: #065f46; }
@media (prefers-color-scheme: dark) {
  .chip-p1 { background: #312e5e; color: #c7d2fe; }
  .chip-p3 { background: #14403a; color: #a7f3d0; }
}
.upgrade-dot {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 10px; font-weight: 600; letter-spacing: 0.05em; text-transform: uppercase;
  color: var(--muted);
}
.upgrade-dot::before {
  content: ""; width: 3px; height: 3px; border-radius: 99px; background: currentColor; opacity: 0.6;
}
mark.hit { background: rgba(0, 0, 0, 0.07); color: inherit; border-radius: 3px; padding: 0 1px; }
@media (prefers-color-scheme: dark) { mark.hit { background: rgba(255, 255, 255, 0.14); } }

/* --------------------------- Skeletons --------------------------- */
.skeleton-card {
  height: 116px; border-radius: var(--radius); border: 1px solid var(--hair);
  background: linear-gradient(100deg, var(--surface) 30%, var(--pill) 50%, var(--surface) 70%);
  background-size: 220% 100%;
  animation: shimmer 1.4s linear infinite;
}
@keyframes shimmer { from { background-position: 180% 0; } to { background-position: -80% 0; } }

/* --------------------------- Bottom sheet --------------------------- */
#sheetRoot.hidden { display: none; }
.sheet-backdrop {
  position: absolute; inset: 0; background: rgba(0, 0, 0, 0.28);
  -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px);
  opacity: 0; transition: opacity 0.32s var(--ease-out);
}
.sheet-backdrop.is-open { opacity: 1; }
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 10;
  max-height: 92svh;
  display: flex; flex-direction: column;
  background: var(--canvas);
  border-radius: 20px 20px 0 0;
  border-top: 1px solid var(--hair);
  box-shadow: 0 -8px 40px rgba(0, 0, 0, 0.12);
  transform: translateY(100%);
  transition: transform 0.42s var(--ease-out);
  will-change: transform;
  padding-bottom: env(safe-area-inset-bottom);
  overscroll-behavior: contain;
}
.sheet.is-open { transform: translateY(0); }
.sheet.is-dragging { transition: none; }
.sheet-scroll {
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding-bottom: 24px;
}

/* Sheet content primitives */
.sheet h3 {
  font-size: 11px; font-weight: 600; letter-spacing: 0.14em; text-transform: uppercase;
  color: var(--muted); margin: 0 0 8px;
}
.sheet h4 { font-size: 12px; font-weight: 600; color: var(--muted); margin: 0 0 6px; letter-spacing: 0.02em; }
.sheet-body-text { font-size: 15px; line-height: 1.6; color: var(--ink); white-space: pre-line; }
.sec { margin-top: 26px; }
.sec-rule { height: 1px; background: var(--hair); margin-top: 26px; }
.def-block {
  background: var(--surface); border: 1px solid var(--hair); border-radius: var(--radius);
  padding: 14px 16px; font-size: 15px; line-height: 1.55; white-space: pre-line; color: var(--ink);
}
.term-list { margin: 0; padding: 0; list-style: none; display: flex; flex-wrap: wrap; gap: 6px; }
.term-list li {
  background: var(--pill); color: var(--pill-ink); border-radius: 7px;
  padding: 6px 10px; font-size: 13.5px; line-height: 1.2;
}
.example {
  background: var(--surface); border: 1px solid var(--hair); border-radius: var(--radius);
  padding: 16px; font-size: 16px; line-height: 1.62; color: var(--ink);
}
.example b { font-weight: 700; border-bottom: 1.5px solid var(--ink); padding-bottom: 1px; }
.upgrade-box {
  background: #f5f5f7; border-left: 2px solid var(--accent); border-radius: 0 var(--radius) var(--radius) 0;
  padding: 14px 16px;
}
@media (prefers-color-scheme: dark) { .upgrade-box { border-left-color: #ffffff; } }
.compare { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 10px; }
.compare .from, .compare .to { min-width: 0; word-break: break-word; }
.compare .from { font-size: 15px; color: var(--muted); text-decoration: line-through; text-decoration-color: rgba(0, 0, 0, 0.28); }
.compare .to { font-size: 19px; font-weight: 700; letter-spacing: -0.02em; color: var(--ink); }
.compare .arrow { color: var(--muted); flex-shrink: 0; }
.trap {
  background: var(--surface); border: 1px solid var(--hair); border-left: 2px solid var(--danger);
  border-radius: 0 var(--radius) var(--radius) 0; padding: 14px 16px;
  font-size: 14.5px; line-height: 1.6; color: var(--ink); white-space: pre-line;
}
.meta-row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }

/* --------------------------- Passcode gate --------------------------- */
.gate {
  position: fixed; inset: 0; z-index: 60;
  display: grid; place-items: center;
  padding: 24px;
  padding-top: calc(env(safe-area-inset-top) + 24px);
  padding-bottom: calc(env(safe-area-inset-bottom) + 24px);
  background: rgba(250, 250, 252, 0.86);
  -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
  transition: opacity 0.5s var(--ease-out), backdrop-filter 0.5s, -webkit-backdrop-filter 0.5s, filter 0.5s;
  overflow-y: auto;
}
.gate.is-unlocking { opacity: 0; filter: blur(14px); pointer-events: none; }
.gate.is-gone { display: none; }
.gate-card {
  width: 100%; max-width: 24rem;
  background: var(--surface); border: 1px solid var(--hair); border-radius: 20px;
  padding: 28px 24px 24px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 24px 60px rgba(0, 0, 0, 0.08);
  animation: rise 0.6s var(--ease-out) both;
}
@keyframes rise { from { opacity: 0; transform: translateY(14px) scale(0.985); } to { opacity: 1; transform: none; } }
.gate.is-shaking .gate-card { animation: shake 0.4s cubic-bezier(0.36, 0.07, 0.19, 0.97); }
@keyframes shake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(4px); }
  30%, 50%, 70% { transform: translateX(-7px); }
  40%, 60% { transform: translateX(7px); }
}
@media (prefers-color-scheme: dark) {
  .gate { background: rgba(29, 29, 31, 0.86); }
  .gate-card { box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5); }
  .compare .from { text-decoration-color: rgba(255, 255, 255, 0.35); }
}

/* --------------------------- Toast --------------------------- */
.toast {
  position: fixed; z-index: 70; left: 50%;
  /* Clears the 52px tab bar; the toast was landing on top of the nav labels. */
  bottom: calc(env(safe-area-inset-bottom) + 64px);
  transform: translate(-50%, 12px);
  background: rgba(29, 29, 31, 0.92); color: #fff;
  font-size: 13px; font-weight: 500; letter-spacing: -0.01em;
  padding: 10px 16px; border-radius: 99px;
  opacity: 0; pointer-events: none;
  transition: opacity 0.25s, transform 0.25s var(--ease-out);
  max-width: min(90vw, 24rem); text-align: center;
}
.toast.is-on { opacity: 1; transform: translate(-50%, 0); }
@media (prefers-color-scheme: dark) { .toast { background: rgba(245, 245, 247, 0.94); color: #1d1d1f; } }

/* --------------------------- Accessibility --------------------------- */
:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; border-radius: 6px; }
.gate :focus-visible { outline-offset: 3px; }

@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;
  }
  .sheet { transition: none; }
  .gate.is-unlocking { transition: opacity 0.15s; }
}

/* --------------------------- Sort selector --------------------------- */
#sorterWrap { position: relative; }
.sort-list {
  position: fixed; z-index: 45;                 /* fixed: escapes the sticky header's overflow */
  background: var(--surface); border: 1px solid var(--hair); border-radius: var(--radius);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.04);
  min-width: 190px; max-width: min(78vw, 260px);
  overflow: hidden; opacity: 0; transform: translateY(-6px) scale(0.98);
  pointer-events: none; transform-origin: top left;
  transition: opacity 0.16s var(--ease-out), transform 0.16s var(--ease-out);
}
.sort-list.is-open { opacity: 1; transform: none; pointer-events: auto; }
.sort-list-inner { padding: 4px; max-height: 60svh; overflow-y: auto; }
.sort-item {
  display: block; width: 100%; text-align: left;
  padding: 7px 10px; border-radius: 8px; cursor: pointer;
  font-size: 13px; line-height: 1.2; color: var(--muted);
  transition: background-color 0.12s, color 0.12s;
  white-space: nowrap; -webkit-tap-highlight-color: transparent;
}
.sort-item:hover { background: var(--pill); color: var(--ink); }
.sort-item.is-on { background: var(--pill); color: var(--ink); font-weight: 500; }
.sort-item:focus-visible { outline: 2px solid var(--ink); outline-offset: -2px; }
#relevanceBadge { color: var(--muted); }

/* --------------------------- Bottom tab bar --------------------------- */
.tab-bar {
  background: rgba(250, 250, 252, 0.88);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid var(--hair);
  padding-bottom: env(safe-area-inset-bottom);
  display: flex; height: calc(52px + env(safe-area-inset-bottom));
}
@media (prefers-color-scheme: dark) {
  .tab-bar { background: rgba(29, 29, 31, 0.88); }
}
.tab-bar.hidden { display: none; }
.tab-btn {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; background: none; border: 0; cursor: pointer; padding: 0;
  color: var(--muted); transition: color 0.18s;
  -webkit-tap-highlight-color: transparent; position: relative;
}
.tab-btn.is-on { color: var(--ink); }
.tab-btn.is-on::after {
  content: ''; position: absolute; top: 0; left: 20%; right: 20%; height: 2px;
  background: var(--ink); border-radius: 0 0 2px 2px;
}
.tab-icon { width: 20px; height: 20px; }
.tab-label { font-size: 10px; font-weight: 500; letter-spacing: 0.04em; }
.tab-badge {
  position: absolute; top: 8px; right: calc(50% - 22px);
  min-width: 16px; height: 16px; border-radius: 99px;
  background: var(--danger); color: #fff; font-size: 10px; font-weight: 600;
  display: flex; align-items: center; justify-content: center; padding: 0 4px;
}
.tab-badge.hidden { display: none; }

/* --------------------------- SRS dot on cards --------------------------- */
.srs-dot {
  width: 6px; height: 6px; border-radius: 99px; flex-shrink: 0;
  border: 1.5px solid var(--muted); background: transparent;
  transition: background-color 0.2s, border-color 0.2s;
}
.srs-dot.is-due { background: var(--ink); border-color: var(--ink); }
.srs-dot.is-learning { background: var(--surface); border-color: var(--muted); }
.srs-dot.hidden { display: none; }

/* --------------------------- Flip card --------------------------- */
.flip-stage { perspective: 1000px; height: min(46svh, 380px); min-height: 300px; margin: 0 auto; max-width: 380px; }
.flipper {
  position: relative; width: 100%; height: 100%;
  transform-style: preserve-3d;
  -webkit-transform-style: preserve-3d;
  transition: transform 0.42s var(--ease-out);
  cursor: pointer;
}
.flipper.is-flipped { transform: rotateY(180deg); -webkit-transform: rotateY(180deg); }
.flipper.is-dragging { transition: none; }
.face {
  position: absolute; inset: 0; backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  background: var(--surface); border: 1px solid var(--hair); border-radius: 16px;
  padding: 24px; display: flex; flex-direction: column;
  box-shadow: 0 1px 3px rgba(0,0,0,0.04), 0 8px 24px rgba(0,0,0,0.05);
}
.face.back { transform: rotateY(180deg); -webkit-transform: rotateY(180deg); overflow-y: auto; }
.card-head { font-size: 26px; font-weight: 700; letter-spacing: -0.025em; line-height: 1.1; word-break: break-word; }
.reveal-hint { margin-top: auto; font-size: 12px; color: var(--muted); text-align: center; letter-spacing: 0.05em; text-transform: uppercase; }
.cn-block { font-size: 15px; line-height: 1.55; color: var(--ink); white-space: pre-line; margin-bottom: 12px; }
.sec-block { margin-top: 14px; }
.sec-block h4 { font-size: 10px; font-weight: 600; letter-spacing: 0.14em; text-transform: uppercase; color: var(--muted); margin: 0 0 6px; }
.en-block { font-size: 13px; line-height: 1.55; color: var(--muted); white-space: pre-line; }
.ex-block { font-size: 14px; line-height: 1.6; color: var(--ink); white-space: pre-line; }
.ex-block b { font-weight: 700; border-bottom: 1.5px solid var(--ink); padding-bottom: 1px; }
.chips { display: flex; flex-wrap: wrap; gap: 5px; }
.chips li { list-style: none; }

/* --------------------------- Rating bar --------------------------- */
.rate-btn {
  flex: 1; border: 1px solid var(--hair); border-radius: 12px;
  background: var(--surface); cursor: pointer; padding: 12px 0; text-align: center;
  font-size: 14px; font-weight: 500; color: var(--ink);
  transition: background-color 0.15s, border-color 0.15s, transform 0.12s var(--ease-out);
  -webkit-tap-highlight-color: transparent;
}
.rate-btn:active { transform: scale(0.96); }
.rate-btn.hard:hover, .rate-btn.hard.is-selected { border-color: var(--danger); color: var(--danger); }
.rate-btn.good:hover, .rate-btn.good.is-selected { border-color: var(--ink); }
.rate-btn.easy:hover, .rate-btn.easy.is-selected { border-color: #30a46c; color: #30a46c; }
.rate-sub { display: block; font-size: 11px; font-weight: 400; color: var(--muted); margin-top: 2px; }
.rate-btn.is-selected .rate-sub { color: currentColor; }

/* --------------------------- Practice view helpers --------------------------- */
#practiceView .sec-block { margin-top: 12px; }
#practiceView .sec-block h4 { font-size: 10px; }

/* ------------------- Sort listbox interaction states ------------------- */
.sort-item.is-focus { background: var(--pill); color: var(--ink); }
.sort-list.is-open { opacity: 1; transform: none; pointer-events: auto; }

/* Compact header (Practice mode hides search + filters) */
.glass.is-compact .pb-3 { padding-bottom: 0.75rem; }

/* Tab bar only exists in app mode; keep the list clear of it */
#scroller, #practiceView { padding-bottom: 0; }
.tab-bar { z-index: 35; }

/* Flashcard: prevent text selection during rapid tapping */
.flipper, .rate-btn { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }

/* Reduced motion: skip the 3D spin, just cross-fade */
@media (prefers-reduced-motion: reduce) {
  .flipper { transition: opacity 0.15s; }
  .flipper.is-flipped { transform: none; }
  .face.back { transform: none; opacity: 0; transition: opacity 0.15s; }
  .flipper.is-flipped .face.front { opacity: 0; }
  .flipper.is-flipped .face.back { opacity: 1; }
}

/* ---------------------------------------------------------------------------
 * These two rules exist so index.html needs no inline style attributes, which
 * is what lets the Content-Security-Policy in _headers use style-src 'self'
 * instead of the much weaker 'unsafe-inline'.
 * ------------------------------------------------------------------------- */

/* Virtual window: collapsed until app.js sizes it to the row count. */
.viewport-window { height: 0; }

/* Shown only when JavaScript is unavailable. */
.noscript-note { padding: 32px; text-align: center; }

