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

:root {
  /* ── Raw palette (never referenced by components) ── */
  --ink: #1a1208;
  --paper: #f5efe3;
  --red: #c0392b;
  --gold: #b8960c;
  --muted: #7a6e5f;
  --border: #d4c8b0;
  --card-bg: #faf6ee;

  /* ── Semantic tokens (components use only these) ── */
  --text: var(--ink);
  --text-2: #665a4c;                 /* secondary copy: meanings, subtitles, labels (≈6:1 on card) */
  --icon: var(--muted);              /* resting icons and control glyphs */
  --surface: var(--paper);
  --surface-2: var(--card-bg);       /* cards, toolbar controls, popovers */
  --surface-highlight: transparent;  /* inset top edge on cards (dark mode only) */
  --line: var(--border);
  --accent: var(--red);
  --accent-soft: rgba(192,57,43,0.12);
  --accent-2: var(--gold);
  --accent-2-soft: rgba(184,150,12,0.1);
  --focus-ring: var(--accent-soft);

  --shadow: rgba(26,18,8,0.09);
  --overlay: rgba(26,18,8,0.85);
  --speaking-bg: #fff8e6;
  --hint-bg: #fff4da;
  --gradient-red: rgba(192,57,43,0.06);
  --gradient-gold: rgba(184,150,12,0.07);
  --intro-gradient: linear-gradient(135deg, rgba(192,57,43,0.08) 0%, rgba(184,150,12,0.06) 100%);
  --lang-shadow: rgba(26,18,8,0.08);

  /* System font stacks - no external fonts needed */
  --font-hanzi: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Heiti SC", system-ui, sans-serif;
  --font-mono: ui-monospace, "SF Mono", "Menlo", "Monaco", monospace;
  --font-serif: "New York", "Iowan Old Style", Georgia, serif;
  --font-sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
}

/* the hidden attribute must beat any component display rule */
[hidden] { display: none !important; }

/* ── Dark mode ────────────────────────────────────────────── */
[data-theme="dark"] {
  --ink: #f4ece0;           /* warm white */
  --paper: #12100d;         /* deeper page so cards lift */
  --red: #f07060;
  --gold: #d9b544;          /* less neon than #e0b828 */
  --muted: #b8a890;
  --border: #463b2f;        /* card edges read without the shadow */
  --card-bg: #1e1a15;

  --text-2: #b8a890;        /* ≈8.5:1 on card */
  --surface-highlight: rgba(255,255,255,0.035);
  --accent-soft: rgba(240,112,96,0.3);
  --accent-2-soft: rgba(217,181,68,0.14);

  --shadow: rgba(0,0,0,0.45);
  --overlay: rgba(0,0,0,0.88);
  --speaking-bg: #2c2416;
  --hint-bg: #261e10;
  --gradient-red: rgba(240,112,96,0.07);
  --gradient-gold: rgba(217,181,68,0.05);
  --intro-gradient: linear-gradient(135deg, rgba(240,112,96,0.1) 0%, rgba(217,181,68,0.08) 100%);
  --lang-shadow: rgba(0,0,0,0.35);
}

body {
  background: var(--surface);
  color: var(--text);
  font-family: var(--font-sans);
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
  padding: calc(env(safe-area-inset-top) + 1.25rem) calc(env(safe-area-inset-right) + 1rem) calc(env(safe-area-inset-bottom) + 4rem) calc(env(safe-area-inset-left) + 1rem);
  background-image:
    radial-gradient(ellipse at 10% 0%, var(--gradient-red) 0%, transparent 50%),
    radial-gradient(ellipse at 90% 100%, var(--gradient-gold) 0%, transparent 50%);
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}

header {
  text-align: center;
  margin-bottom: 1.5rem;
  position: relative;
  padding-top: 0.5rem;
}

.header-rule {
  width: 60px;
  height: 3px;
  background: var(--accent);
  margin: 0 auto 0.9rem;
}

h1 {
  font-family: var(--font-hanzi);
  font-size: clamp(2.2rem, 6vw, 3.5rem);
  font-weight: 700;
  letter-spacing: 0.15em;
  color: var(--text);
  line-height: 1.1;
}

.subtitle {
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 1rem;
  color: var(--text-2);
  margin-top: 0.4rem;
  letter-spacing: 0.03em;
}

.section {
  max-width: 860px;
  margin: 0 auto 2.8rem;
}

.subsection-title {
  max-width: 860px;
  margin: 0 auto 1.5rem;
  font-family: var(--font-hanzi);
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--text);
  padding: 0 0 0.6rem;
  border-bottom: 1px solid var(--text);
  position: relative;
}
.subsection-title::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 60px;
  height: 3px;
  background: var(--accent);
}
.subsection-title + .section { margin-top: 0; }
.subsection:not(:first-child) .subsection-title {
  margin-top: 1rem;
}

.section-title {
  font-family: var(--font-hanzi);
  font-size: 0.7rem;
  letter-spacing: 0.25em;
  text-transform: uppercase;
  color: var(--accent);
  border-left: 3px solid var(--accent);
  padding-left: 0.75rem;
  margin-bottom: 1rem;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 0.75rem;
}

.card {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0.9rem 2.9rem 0.9rem 1.1rem; /* right gutter holds speaker + star */
  position: relative;
  transition: transform 0.15s, box-shadow 0.15s;
  cursor: pointer;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  box-shadow: inset 0 1px 0 var(--surface-highlight), 0 1px 2px var(--shadow);
}

.card::before {
  content: '';
  position: absolute;
  top: 0; left: 0;
  width: 3px; height: 100%;
  background: var(--accent-2);
  opacity: 0;
  transition: opacity 0.2s;
}

.card:hover {
  transform: translateY(-2px);
  box-shadow: inset 0 1px 0 var(--surface-highlight), 0 6px 20px var(--shadow);
}
.card:hover::before { opacity: 1; }

.card:active,
.phrase-card:active {
  transform: scale(0.98);
  transition: transform 0.08s ease;
}

.card.speaking {
  background: var(--speaking-bg);
  border-color: var(--accent-2);
}
.card.speaking::before { opacity: 1; }

.card:focus-visible,
.phrase-card:focus-visible,
.tab:focus-visible,
.favorite-btn:focus-visible,
.toolbar button:focus-visible,
.theme-btn:focus-visible,
.lang-switch button:focus-visible,
.fav-close:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Card actions: speaker top-right, star bottom-right, each with a 36px hit area */
.speaker,
.favorite-btn {
  position: absolute;
  right: 0.2rem;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  padding: 0;
  margin: 0;
  color: var(--icon);
  opacity: 0.55;
  transition: opacity 0.15s, color 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.speaker { top: 0.2rem; pointer-events: none; }
.favorite-btn { bottom: 0.2rem; cursor: pointer; }
.card:hover .speaker,
.phrase-card:hover .speaker,
.card:hover .favorite-btn,
.phrase-card:hover .favorite-btn { opacity: 0.85; }
.card.speaking .speaker,
.phrase-card.speaking .speaker { opacity: 1; color: var(--accent); animation: speakPulse 0.9s ease-in-out infinite; }
.speaker svg { width: 16px; height: 16px; }
.favorite-btn svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 2; }
.favorite-btn.active { opacity: 1; color: var(--accent-2); }
.favorite-btn.active svg { fill: var(--accent-2); stroke: var(--accent-2); }

/* Pleco lookup (rendered on iOS / Android only): mid-right, between speaker and star */
.pleco-btn {
  position: absolute;
  right: 0.2rem;
  top: 50%;
  transform: translateY(-50%);
  width: 36px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--icon);
  opacity: 0.55;
  -webkit-tap-highlight-color: transparent;
  transition: opacity 0.15s, color 0.15s;
}
.pleco-btn:hover, .pleco-btn:active { opacity: 1; color: var(--accent); }
.pleco-btn svg { width: 16px; height: 16px; }
.pleco-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

@keyframes speakPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.18); }
}

.hanzi {
  font-family: var(--font-hanzi);
  font-size: 2rem;
  font-weight: 700;
  color: var(--text);
  line-height: 1;
  margin-bottom: 0.35rem;
}

.pinyin {
  font-family: var(--font-mono);
  font-size: 0.74rem;
  color: var(--accent);
  letter-spacing: 0.08em;
  margin-bottom: 0.3rem;
  font-weight: 500;
}

.meaning {
  font-size: 0.85rem;
  line-height: 1.35;
  color: var(--text-2);
}

.tip-box {
  max-width: 860px;
  margin: 0 auto;
  background: var(--text);
  color: var(--surface);
  border-radius: 4px;
  padding: 1.2rem 1.5rem;
  display: flex;
  gap: 1rem;
  align-items: flex-start;
  font-size: 0.78rem;
  line-height: 1.7;
}
.tip-box .icon { font-size: 1.3rem; flex-shrink: 0; }
.tip-box strong { color: var(--accent-2); }

@media (max-width: 500px) {
  .grid { grid-template-columns: 1fr 1fr; gap: 0.6rem; }
  .phrase-grid { grid-template-columns: 1fr; }

  /* two-column vocab cards are ~170px wide: tighten the icon gutter and type
     so a typical meaning ("Home / Homepage") stays on one line */
  .card { padding: 0.8rem 2.1rem 0.8rem 0.9rem; }
  .card .speaker,
  .card .favorite-btn { width: 32px; height: 32px; right: 0.05rem; }
  .card .speaker { top: 0.15rem; }
  .card .favorite-btn { bottom: 0.15rem; }
  .card .pleco-btn { width: 32px; height: 28px; right: 0.05rem; }
  [data-quiz="on"] .card:not(.revealed)::after { left: 0.9rem; right: 2.1rem; bottom: 0.8rem; }
  .hanzi { font-size: 1.8rem; }
  .pinyin { font-size: 0.7rem; letter-spacing: 0.05em; }
  .meaning { font-size: 0.8rem; }
}

/* Phrase cards - wider, different layout */
.phrase-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 0.75rem;
}

.phrase-card {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-left: 3px solid var(--accent-2);
  border-radius: 4px;
  padding: 0.95rem 2.9rem 0.95rem 1.2rem;
  position: relative;
  transition: transform 0.15s, box-shadow 0.15s;
  cursor: pointer;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  box-shadow: inset 0 1px 0 var(--surface-highlight), 0 1px 2px var(--shadow);
}

.phrase-card:hover {
  transform: translateY(-2px);
  box-shadow: inset 0 1px 0 var(--surface-highlight), 0 6px 20px var(--shadow);
  border-left-color: var(--accent);
}
.phrase-card.speaking {
  background: var(--speaking-bg);
  border-left-color: var(--accent);
}

.phrase-hanzi {
  font-family: var(--font-hanzi);
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--text);
  line-height: 1.25;
  margin-bottom: 0.35rem;
}

.phrase-pinyin {
  font-family: var(--font-mono);
  font-size: 0.74rem;
  color: var(--accent);
  letter-spacing: 0.06em;
  margin-bottom: 0.4rem;
  font-weight: 500;
}

.phrase-meaning {
  font-size: 0.88rem;
  color: var(--text-2);
  line-height: 1.4;
}

/* Tabs */
.tabs {
  max-width: 860px;
  margin: 0 auto 2rem;
  display: flex;
  gap: 0.3rem;
  padding: 0.3rem;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.tab {
  flex: 1 1 auto;
  min-width: 0;
  background: transparent;
  border: none;
  padding: 0.5rem 0.4rem;
  font-family: var(--font-sans);
  font-size: 0.65rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-2);
  cursor: pointer;
  border-radius: 4px;
  transition: all 0.2s;
  font-weight: 500;
  text-align: center;
  white-space: nowrap;
  position: relative;
}

.tab:hover { color: var(--text); }

/* active = ink text + 2px red underline; the mobile bar overrides this */
.tab.active {
  color: var(--text);
  font-weight: 700;
}
.tab.active .tab-hanzi { color: var(--accent); }
.tab.active::after {
  content: '';
  position: absolute;
  left: 0.5rem;
  right: 0.5rem;
  bottom: 0.2rem;
  height: 2px;
  border-radius: 1px;
  background: var(--accent);
}

.tab .tab-hanzi {
  font-family: var(--font-hanzi);
  margin-right: 0.25rem;
  font-size: 0.85rem;
  letter-spacing: 0;
  display: block;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; animation: fadeIn 0.25s ease-out; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Install hint banner (shown on iOS/Android, not when standalone or dismissed) */
.install-hint .install-emoji { font-size: 1.1rem; }
.install-hint {
  max-width: 860px;
  margin: 0 auto 1.5rem;
  background: var(--hint-bg);
  border: 1px solid var(--accent-2);
  border-radius: 6px;
  padding: 0.85rem 1rem;
  font-size: 0.78rem;
  color: var(--text);
  line-height: 1.5;
  display: flex;
  gap: 0.75rem;
  align-items: flex-start;
  position: relative;
}
.install-hint strong { color: var(--accent); }
.install-hint .close-hint {
  position: absolute;
  top: 0.5rem; right: 0.6rem;
  background: transparent;
  border: none;
  font-size: 1rem;
  color: var(--icon);
  cursor: pointer;
  padding: 0.2rem 0.4rem;
}
.install-btn {
  flex-shrink: 0;
  align-self: center;
  background: var(--text);
  color: var(--surface);
  border: none;
  border-radius: 4px;
  padding: 0.35rem 0.9rem;
  font-family: var(--font-sans);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  cursor: pointer;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}
.install-btn:hover { background: var(--accent); }
/* Hide when in standalone mode */
@media (display-mode: standalone) {
  .install-hint { display: none !important; }
}

/* Migration notice — same shape as .install-hint but red-accented, and it
   deliberately stays visible in standalone (installed users must see it). */
.migrate-hint .migrate-emoji { font-size: 1.1rem; }
.migrate-hint {
  max-width: 860px;
  margin: 0 auto 1.5rem;
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 0.85rem 1rem;
  font-size: 0.78rem;
  color: var(--text);
  line-height: 1.5;
  display: flex;
  gap: 0.75rem;
  align-items: flex-start;
  position: relative;
}
.migrate-hint strong { color: var(--accent); }
.migrate-hint a { color: var(--accent); }
.migrate-hint .close-hint {
  position: absolute;
  top: 0.5rem; right: 0.6rem;
  background: transparent;
  border: none;
  font-size: 1rem;
  color: var(--icon);
  cursor: pointer;
  padding: 0.2rem 0.4rem;
}

.favorites-empty {
  max-width: 860px;
  margin: 0 auto;
  padding: 3rem 1rem;
  text-align: center;
  color: var(--text-2);
  font-size: 0.95rem;
  line-height: 1.6;
}

.load-error {
  max-width: 860px;
  margin: 0 auto;
  padding: 1rem;
  color: var(--accent);
  font-size: 0.85rem;
  text-align: center;
}

.lang-switch {
  position: absolute;
  top: 0;
  right: 0;
  z-index: 10;
  display: flex;
  gap: 0.1rem;
  padding: 0.2rem;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  box-shadow: 0 2px 8px var(--lang-shadow);
}
.lang-switch button {
  background: transparent;
  border: none;
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  font-family: var(--font-sans);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  color: var(--icon);
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
  -webkit-tap-highlight-color: transparent;
}
.lang-switch button:hover { color: var(--text); }
.lang-switch button.active {
  background: var(--text);
  color: var(--surface);
}

.app-version {
  position: fixed;
  bottom: calc(env(safe-area-inset-bottom) + 0.6rem);
  right: calc(env(safe-area-inset-right) + 0.9rem);
  z-index: 100;
  font-family: var(--font-mono);
  font-size: 0.65rem;
  color: var(--text-2);
  letter-spacing: 0.1em;
  opacity: 0.55;
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
}

.search-wrap {
  position: relative;
  max-width: 860px;
  margin: 0 auto 1.2rem;
  display: flex;
  justify-content: center;
}

.toolbar {
  position: sticky;
  top: 0;
  z-index: 120;
  max-width: calc(860px + 2rem);
  margin: 0 auto 1.2rem;
  padding: 0.6rem 1rem;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
/* bleed to the viewport edges on narrow screens (body has 1rem side padding) */
@media (max-width: 899px) {
  .toolbar {
    margin-left: -1rem;
    margin-right: -1rem;
    max-width: none;
  }
}

.search-wrap { flex: 1; min-width: 0; }
.search-field { width: 100%; }

.search-input {
  flex: 1;
  width: 100%;
  min-width: 0;
  padding: 0.45rem 0.9rem;
  font-family: var(--font-sans);
  font-size: 0.85rem;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
  -webkit-appearance: none;
}

.search-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--focus-ring);
}

.search-input::placeholder { color: var(--icon); opacity: 0.7; }

/* remove native clear button on webkit */
.search-input::-webkit-search-cancel-button { -webkit-appearance: none; }

.search-field {
  position: relative;
  display: flex;
  align-items: center;
}

.search-input { padding-right: 1.6rem; }

.search-clear {
  position: absolute;
  right: 0.55rem;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  color: var(--icon);
  font-size: 0.8rem;
  line-height: 1;
  padding: 0.2rem 0.3rem;
  border-radius: 50%;
  transition: color 0.15s;
}
.search-clear:hover { color: var(--text); }

.fav-btn {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 50%;
  cursor: pointer;
  color: var(--icon);
  transition: all 0.2s;
}

.fav-btn:hover, .fav-btn.active {
  color: var(--accent-2);
  border-color: var(--accent-2);
}

.fav-btn svg {
  width: 18px;
  height: 18px;
}

.favorites-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--overlay);
  z-index: 1000;
  display: none;
  overflow-y: auto;
  padding: 2rem 1rem;
}

.favorites-overlay.active {
  display: block;
  animation: fadeIn 0.2s ease-out;
}

body.fav-open {
  overflow: hidden;
}

.favorites-panel {
  max-width: 860px;
  margin: 0 auto;
  background: var(--surface);
  border-radius: 12px;
  min-height: 60vh;
}

.favorites-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.2rem;
  border-bottom: 1px solid var(--line);
}

.favorites-header h2 {
  font-family: var(--font-hanzi);
  font-size: 1.4rem;
  font-weight: 700;
  margin: 0;
}

.fav-close {
  background: none;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  color: var(--icon);
  padding: 0.3rem;
}

.fav-close:hover {
  color: var(--text);
}

.favorites-content {
  padding: 1rem;
}

/* App cards */
.app-intro {
  max-width: 860px;
  margin: 0 auto 1.5rem;
  background: var(--intro-gradient);
  border-left: 3px solid var(--accent);
  padding: 0.9rem 1rem;
  font-size: 0.82rem;
  line-height: 1.5;
  color: var(--text);
  border-radius: 0 8px 8px 0;
}

.app-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.9rem;
}

.app-card {
  display: flex;
  gap: 0.9rem;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 1rem;
  user-select: none;
  -webkit-user-select: none;
  transition: box-shadow 0.2s, transform 0.15s;
  box-shadow: inset 0 1px 0 var(--surface-highlight);
}

.app-card:hover {
  box-shadow: 0 4px 12px var(--shadow);
  transform: translateY(-1px);
}

.app-icon {
  font-size: 2.2rem;
  flex-shrink: 0;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  border-radius: 10px;
  overflow: hidden;
}

.app-icon img {
  width: 100%;
  height: 100%;
  display: block;
}

.app-content {
  flex: 1;
  min-width: 0;
}

.app-name-row {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.app-name {
  font-weight: 600;
  font-size: 1rem;
  color: var(--text);
}

.app-hanzi {
  font-family: var(--font-hanzi);
  font-size: 0.9rem;
  color: var(--accent);
  font-weight: 500;
}

.app-pinyin {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--text-2);
  margin-top: 0.15rem;
}

.app-desc {
  font-size: 0.8rem;
  line-height: 1.45;
  color: var(--text);
  margin-top: 0.4rem;
  opacity: 0.85;
}

/* ── Collapsible groups ───────────────────────────────────── */
.subsection-title { cursor: pointer; user-select: none; display: flex; align-items: center; justify-content: space-between; }
.section-title    { cursor: pointer; user-select: none; display: flex; align-items: center; justify-content: space-between; }
.collapse-chevron { width: 16px; height: 16px; flex-shrink: 0; transition: transform 0.2s ease; }
.is-collapsed .collapse-chevron { transform: rotate(-90deg); }
.subsection.is-collapsed > .section { display: none; }
.section.is-collapsed > .grid,
.section.is-collapsed > .phrase-grid { display: none; }
.search-active .subsection.is-collapsed > .section,
.search-active .section.is-collapsed > .grid,
.search-active .section.is-collapsed > .phrase-grid { display: block; }

/* ── Quiz mode ────────────────────────────────────────────── */
[data-quiz="on"] .card:not(.revealed) .pinyin,
[data-quiz="on"] .card:not(.revealed) .meaning,
[data-quiz="on"] .phrase-card:not(.revealed) .phrase-pinyin,
[data-quiz="on"] .phrase-card:not(.revealed) .phrase-meaning { visibility: hidden; }

[data-quiz="on"] .card:not(.revealed)::after,
[data-quiz="on"] .phrase-card:not(.revealed)::after {
  content: var(--quiz-hint, "tap to reveal");
  position: absolute;
  left: 1.1rem;
  right: 2.9rem;
  bottom: 0.9rem;
  padding-top: 0.3rem;
  border-top: 1px dashed var(--line);
  font-family: var(--font-sans);
  font-size: 0.7rem;
  color: var(--text-2);
  opacity: 0.8;
  pointer-events: none;
}
[data-quiz="on"] .phrase-card:not(.revealed)::after {
  left: 1.2rem;
  bottom: 0.95rem;
}

.quiz-btn {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 50%;
  cursor: pointer;
  font-size: 1.1rem;
  font-weight: 600;
  line-height: 1;
  color: var(--icon);
  transition: color 0.2s, border-color 0.2s, background 0.2s;
  -webkit-tap-highlight-color: transparent;
}
.quiz-btn:hover { color: var(--text); }
.quiz-btn.active {
  color: var(--accent-2);
  border-color: var(--accent-2);
  background: var(--accent-2-soft);
}

/* ── Hide pinyin mode ─────────────────────────────────────── */
[data-pinyin="hidden"] .pinyin,
[data-pinyin="hidden"] .phrase-pinyin,
[data-pinyin="hidden"] .app-pinyin { display: none; }

.pinyin-btn {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 50%;
  cursor: pointer;
  font-family: var(--font-hanzi);
  font-size: 1rem;
  color: var(--icon);
  transition: color 0.2s, border-color 0.2s, background 0.2s;
  -webkit-tap-highlight-color: transparent;
}
.pinyin-btn:hover:not(:disabled) { color: var(--text); }
.pinyin-btn:disabled { opacity: 0.3; cursor: not-allowed; }
.pinyin-btn.active {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--focus-ring);
}

/* ── About / tips popover ─────────────────────────────────── */
.about-btn {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 50%;
  cursor: pointer;
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 1.1rem;
  font-weight: 700;
  line-height: 1;
  color: var(--icon);
  transition: color 0.2s, border-color 0.2s, background 0.2s;
  -webkit-tap-highlight-color: transparent;
}
.about-btn:hover { color: var(--text); }
.about-btn.active {
  color: var(--surface);
  background: var(--text);
  border-color: var(--text);
}

.about-panel {
  position: absolute;
  top: 100%;
  right: 1rem;
  margin-top: 0.5rem;
  width: min(340px, calc(100vw - 2rem));
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 8px 28px var(--shadow);
  padding: 1rem 1.1rem;
  text-align: left;
  animation: fadeIn 0.15s ease-out;
}
.about-name {
  font-family: var(--font-hanzi);
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text);
  letter-spacing: 0.04em;
}
.about-name .about-hanzi { color: var(--accent); margin: 0 0.15em; }
.about-tagline {
  font-size: 0.8rem;
  color: var(--text-2);
  margin: 0.15rem 0 0.8rem;
}
.about-tips {
  list-style: none;
  font-size: 0.8rem;
  line-height: 1.5;
  color: var(--text);
}
.about-tips li {
  padding: 0.3rem 0;
  border-top: 1px solid var(--line);
}
.about-tips strong { color: var(--accent); font-weight: 600; }

/* ── Global search: show all panels stacked ──────────────── */
#panels.global-search .tab-panel          { display: block; }
#panels.global-search .tab-panel.no-match { display: none; }
/* non-matching cards / sections / subsections (set by filterCards in js/search.js) */
#panels .search-hidden { display: none; }

/* ── Per-panel tab label (visible in global search only) ─── */
.panel-search-label {
  display: none;
  font-family: var(--font-sans);
  font-size: 0.65rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-2);
  padding: 1.8rem 0 0.5rem;
  margin-bottom: 0.4rem;
  border-top: 1px solid var(--line);
}
#panels.global-search .tab-panel:first-child .panel-search-label {
  padding-top: 0;
  border-top: none;
}
#panels.global-search .panel-search-label { display: block; }

/* Theme toggle button */
.theme-btn {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 10;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  width: 2.25rem;
  height: 2.25rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 8px var(--lang-shadow);
  color: var(--text);
  transition: background 0.2s, border-color 0.2s, color 0.2s;
  -webkit-tap-highlight-color: transparent;
  font-size: 1rem;
  line-height: 1;
}

/* Toast (copy feedback) */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 2.2rem);
  transform: translateX(-50%) translateY(8px);
  z-index: 200;
  background: var(--text);
  color: var(--surface);
  font-family: var(--font-hanzi);
  font-size: 0.9rem;
  padding: 0.55rem 1rem;
  border-radius: 999px;
  box-shadow: 0 4px 16px var(--shadow);
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none;
  max-width: calc(100vw - 2rem);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
/* longer messages (e.g. the missing-voice hint) wrap instead of truncating */
.toast.toast-long {
  white-space: normal;
  text-align: center;
  line-height: 1.4;
  font-family: var(--font-sans);
  font-size: 0.8rem;
  border-radius: 12px;
  max-width: min(26rem, calc(100vw - 2rem));
}

/* Update prompt: same pill, but interactive and stacked above the copy toast */
.toast-update {
  pointer-events: auto;
  cursor: pointer;
  border: none;
  font-family: var(--font-sans);
  font-size: 0.85rem;
  font-weight: 600;
  background: var(--accent);
  color: var(--surface);
  bottom: calc(env(safe-area-inset-bottom) + 5.2rem);
  -webkit-tap-highlight-color: transparent;
}
.toast-update:hover { filter: brightness(1.08); }

.card.copy-armed, .phrase-card.copy-armed, .app-card.copy-armed,
.card.copied, .phrase-card.copied, .app-card.copied {
  transform: scale(0.97);
  transition: transform 0.15s ease;
}
.card.copy-armed, .phrase-card.copy-armed, .app-card.copy-armed {
  border-color: var(--accent-2);
}

/* ── Custom phrases (我的 tab) ─────────────────────────────── */
/* small pill buttons: action bar + import sheet */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.4rem 0.9rem;
  font-family: var(--font-sans);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.btn:hover, .btn.active { border-color: var(--accent); color: var(--accent); }
.btn-primary { background: var(--text); color: var(--surface); border-color: var(--text); }
.btn-primary:hover { background: var(--accent); border-color: var(--accent); color: var(--surface); }
.btn-danger { color: var(--accent); border-color: var(--accent); }
.btn-danger:hover { background: var(--accent-soft); }

.custom-bar {
  max-width: 860px;
  margin: 0 auto 1.4rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}
#panels.global-search .custom-bar { display: none; } /* results only while searching */

.custom-empty {
  max-width: 860px;
  margin: 0 auto;
  padding: 2.5rem 1rem;
  text-align: center;
  color: var(--text-2);
  font-size: 0.9rem;
  line-height: 1.7;
}
.custom-empty strong { color: var(--accent); font-weight: 600; }

/* optional note line on custom entries */
.phrase-note {
  margin-top: 0.4rem;
  font-size: 0.78rem;
  line-height: 1.4;
  color: var(--text-2);
  border-top: 1px dashed var(--line);
  padding-top: 0.35rem;
}
[data-quiz="on"] .phrase-card:not(.revealed) .phrase-note { visibility: hidden; }

/* per-card remove button: only while the panel is in edit mode */
.remove-btn { display: none; }
.tab-panel.editing .phrase-card { padding-left: 2.7rem; }
.tab-panel.editing .remove-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  left: 0.35rem;
  top: 50%;
  transform: translateY(-50%);
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--accent);
  font-size: 1rem;
  cursor: pointer;
}
.tab-panel.editing .remove-btn:hover { background: var(--accent-soft); }

/* import sheet (same layering / look as the favorites overlay) */
.sheet-overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay);
  z-index: 1000;
  overflow-y: auto;
  padding: 2rem 1rem;
  animation: fadeIn 0.2s ease-out;
}
body.sheet-open { overflow: hidden; }
.sheet {
  max-width: 560px;
  margin: 0 auto;
  background: var(--surface);
  border-radius: 12px;
}
.sheet-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.2rem;
  border-bottom: 1px solid var(--line);
}
.sheet-header h2 {
  font-family: var(--font-hanzi);
  font-size: 1.4rem;
  font-weight: 700;
}
.sheet-close {
  background: none;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  color: var(--icon);
  padding: 0.3rem;
}
.sheet-close:hover { color: var(--text); }
.sheet-body { padding: 1rem 1.2rem 1.2rem; }
.field { display: block; margin-bottom: 0.8rem; }
.field-label {
  display: block;
  font-size: 0.65rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-2);
  margin-bottom: 0.3rem;
}
.field-label [lang="zh-CN"] { font-family: var(--font-hanzi); letter-spacing: 0.05em; }
.field-opt { text-transform: none; letter-spacing: 0; color: var(--icon); }
.field-input {
  display: block;
  width: 100%;
  padding: 0.55rem 0.8rem;
  font-family: var(--font-sans);
  font-size: 1rem; /* ≥16px so iOS Safari does not zoom on focus */
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  outline: none;
  -webkit-appearance: none;
}
.field-hanzi { font-family: var(--font-hanzi); font-size: 1.3rem; }
.field-pinyin { font-family: var(--font-mono); letter-spacing: 0.04em; }
.field-input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--focus-ring); }
.field-input::placeholder { color: var(--icon); opacity: 0.6; }
.sheet-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.8rem;
}

/* ── Mobile: tabs become a fixed bottom bar ──────────────── */
/* Kept at the END of the file on purpose: these override .tabs, .toast,
   .app-version and .toast-update by source order (same specificity). */
@media (max-width: 699px) {
  body {
    /* clear the bottom bar (≈3.6rem) plus breathing room */
    padding-bottom: calc(env(safe-area-inset-bottom) + 5.5rem);
  }

  .tabs {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 150;
    max-width: none;
    margin: 0;
    gap: 0;
    padding: 0.3rem 0.25rem calc(env(safe-area-inset-bottom) + 0.3rem);
    background: var(--surface-2);
    border: none;
    border-top: 1px solid var(--line);
    border-radius: 0;
    box-shadow: 0 -2px 12px var(--shadow);
    overflow: visible;
  }

  .tab {
    flex: 1 1 0;
    min-width: 0;
    padding: 0.3rem 0.15rem 0.2rem;
    border-radius: 8px;
    font-size: 0.55rem;
    letter-spacing: 0.06em;
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--text-2);
    transition: color 0.2s;
  }
  .tab .tab-hanzi {
    font-size: 1.1rem;
    font-weight: 600;
    margin: 0 0 0.1rem;
    line-height: 1.2;
    transition: transform 0.2s;
  }
  .tab:hover { color: var(--text-2); }
  .tab.active {
    background: transparent;
    color: var(--text);
    font-weight: 700;
  }
  .tab.active::after { display: none; }
  .tab.active .tab-hanzi {
    color: var(--accent);
    transform: translateY(-1px);
  }
  .tab:active .tab-hanzi { transform: scale(0.92); }

  /* slide the bar away while typing so the iOS keyboard doesn't push it over results */
  .tabs { transition: transform 0.2s ease; }
  body:has(#searchInput:focus) .tabs { transform: translateY(110%); }

  /* keep fixed bottom-right / bottom-center chrome above the bar */
  .app-version { bottom: calc(env(safe-area-inset-bottom) + 4.2rem); }
  .toast { bottom: calc(env(safe-area-inset-bottom) + 5rem); }
  .toast-update { bottom: calc(env(safe-area-inset-bottom) + 8rem); }
}
