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

:root {
  --bg: #0e0e0e;
  --surface: #161616;
  --border: #2a2a2a;
  --accent: #e8d5a3;
  --accent2: #c9a96e;
  --text: #f0ece3;
  --muted: #7a7570;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'IBM Plex Mono', monospace;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 48px 16px;
  padding-bottom: max(48px, env(safe-area-inset-bottom));
  overflow-x: hidden;
}

.grain {
  position: fixed; inset: 0;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)' opacity='0.04'/%3E%3C/svg%3E");
  pointer-events: none; z-index: 100; opacity: 0.4;
}

/* ── Install Banner ── */
.install-banner {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 14px 20px;
  padding-bottom: max(14px, env(safe-area-inset-bottom));
  display: none;
  align-items: center;
  gap: 12px;
  z-index: 200;
  animation: slideUp 0.3s ease;
}
.install-banner.visible { display: flex; }

@keyframes slideUp {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

.install-text { font-size: 11px; color: var(--muted); flex: 1; letter-spacing: 0.05em; }

.install-btn {
  background: var(--accent); color: #0e0e0e;
  border: none; padding: 8px 18px;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px; font-weight: 500;
  letter-spacing: 0.1em; cursor: pointer;
  transition: background 0.2s;
}
.install-btn:hover { background: var(--accent2); }

.install-dismiss {
  background: transparent; border: none;
  color: var(--muted); cursor: pointer;
  font-size: 14px; padding: 4px 8px;
  transition: color 0.2s;
}
.install-dismiss:hover { color: var(--text); }

/* ── Header ── */
header { text-align: center; margin-bottom: 48px; width: 100%; }

.eyebrow {
  font-size: 10px; letter-spacing: 0.3em;
  text-transform: uppercase; color: var(--muted); margin-bottom: 12px;
}

h1 {
  font-family: 'Playfair Display', serif;
  font-size: clamp(40px, 10vw, 88px);
  font-weight: 900; line-height: 0.95;
  letter-spacing: -0.02em; color: var(--text);
}
h1 em { font-style: italic; color: var(--accent); }

.subtitle { margin-top: 16px; font-size: 11px; color: var(--muted); letter-spacing: 0.15em; }

/* ── Spin Button ── */
.spin-btn {
  position: relative;
  display: inline-flex; align-items: center; justify-content: center; gap: 12px;
  background: var(--accent); color: #0e0e0e; border: none;
  padding: 18px 48px;
  font-family: 'IBM Plex Mono', monospace; font-size: 13px;
  font-weight: 500; letter-spacing: 0.15em; text-transform: uppercase;
  cursor: pointer; transition: all 0.2s ease;
  margin-bottom: 40px; overflow: hidden;
  width: 100%; max-width: 360px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.spin-btn::before {
  content: ''; position: absolute; inset: 0;
  background: var(--accent2); transform: translateX(-100%); transition: transform 0.3s ease;
}
.spin-btn:hover::before { transform: translateX(0); }
.spin-btn:hover { transform: translateY(-2px); box-shadow: 0 8px 32px rgba(232,213,163,0.2); }
.spin-btn:active { transform: scale(0.98); }
.spin-btn span { position: relative; z-index: 1; }
.spin-btn.loading { pointer-events: none; opacity: 0.7; }

.dice { font-size: 18px; display: inline-block; }
.spin-btn.loading .dice { animation: spin 0.6s linear infinite; }

@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }

/* ── Error ── */
.error-msg {
  background: #1e0e0e; border: 1px solid #3a1a1a; color: #e07070;
  padding: 14px 20px; font-size: 11px; max-width: 720px;
  width: 100%; display: none; margin-bottom: 24px; letter-spacing: 0.05em;
}
.error-msg.visible { display: block; }

/* ── Card Wrapper ── */
.card-wrap {
  width: 100%; max-width: 720px;
  opacity: 0; transform: translateY(24px);
  transition: opacity 0.5s ease, transform 0.5s ease;
}
.card-wrap.visible { opacity: 1; transform: translateY(0); }

/* ── Empty State ── */
.empty { text-align: center; padding: 60px 24px; border: 1px dashed var(--border); }
.empty-icon { font-size: 36px; margin-bottom: 16px; opacity: 0.4; }
.empty-text { font-size: 12px; color: var(--muted); line-height: 1.8; letter-spacing: 0.05em; }

/* ── Card shell ── */
.card { background: var(--surface); border: 1px solid var(--border); position: relative; }
.card::before {
  content: ''; position: absolute; top: 0; left: 0;
  width: 3px; height: 100%; background: var(--accent);
}

/* ── Settings Modal ── */
.settings-btn {
  position: absolute; top: 20px; right: 20px;
  background: transparent; border: none; color: var(--muted);
  font-size: 20px; cursor: pointer; transition: color 0.2s, transform 0.3s;
  z-index: 10;
}
.settings-btn:hover { color: var(--accent); transform: rotate(90deg); }

.modal-overlay {
  position: fixed; inset: 0; background: rgba(14, 14, 14, 0.85);
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
  z-index: 1000; display: none; align-items: center; justify-content: center;
  opacity: 0; transition: opacity 0.3s ease;
}
.modal-overlay.visible { display: flex; opacity: 1; }

.modal {
  background: var(--surface); border: 1px solid var(--border);
  width: 90%; max-width: 400px; padding: 24px;
  transform: translateY(20px); transition: transform 0.3s ease;
}
.modal-overlay.visible .modal { transform: translateY(0); }

.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px; border-bottom: 1px solid var(--border); padding-bottom: 12px; }
.modal-header h2 { font-family: 'IBM Plex Mono', monospace; font-size: 14px; font-weight: 500; letter-spacing: 0.1em; text-transform: uppercase; color: var(--accent); }
.modal-close { background: transparent; border: none; color: var(--muted); cursor: pointer; font-size: 16px; padding: 4px; }
.modal-close:hover { color: var(--text); }

.modal-body { display: flex; flex-direction: column; gap: 12px; margin-bottom: 16px; }
.checkbox-label {
  display: flex; align-items: center; gap: 12px; cursor: pointer; font-size: 13px; color: var(--text);
  padding: 8px 12px; background: #1a1a1a; border: 1px solid var(--border); transition: border-color 0.2s;
  user-select: none;
}
.checkbox-label:hover { border-color: #3aa; } /* Slight highlight */
.checkbox-label input { appearance: none; width: 16px; height: 16px; border: 1px solid var(--muted); background: var(--bg); position: relative; cursor: pointer;}
.checkbox-label input:checked { background: var(--accent); border-color: var(--accent); }
.checkbox-label input:checked::after { content: "✓"; position: absolute; color: #000; font-size: 12px; top: -1px; left: 3px; }

/* ── Lists (History & Bookmarks) ── */
.lists-wrap {
  width: 100%; max-width: 720px; margin-top: 40px; padding-bottom: 80px;
  display: grid; grid-template-columns: 1fr; gap: 40px;
  opacity: 0; transition: opacity 0.4s ease;
}
.lists-wrap.visible { opacity: 1; }

@media (min-width: 600px) {
  .lists-wrap { grid-template-columns: 1fr 1fr; }
}

.list-label {
  font-size: 10px; color: var(--muted); letter-spacing: 0.25em;
  text-transform: uppercase; margin-bottom: 16px;
  padding-bottom: 8px; border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
}
.list-items { display: flex; flex-direction: column; }

.history-item {
  display: flex; align-items: flex-start; gap: 12px;
  font-size: 12px; color: var(--muted);
  padding: 10px 0; border-bottom: 1px solid #1a1a1a;
  text-decoration: none; transition: color 0.2s;
  animation: fadeIn 0.3s ease;
  -webkit-tap-highlight-color: transparent; position: relative;
}
.history-item:hover { color: var(--text); }

.history-item-num { font-size: 10px; color: #3a3a3a; width: 22px; flex-shrink: 0; padding-top: 2px; }
.history-item-text { display: flex; flex-direction: column; gap: 3px; min-width: 0; flex: 1; }
.history-item-title { word-break: break-word; }
.history-item-cat { font-size: 9px; color: var(--accent); letter-spacing: 0.12em; opacity: 0.75; text-transform: uppercase; }

.delete-btn {
  background: transparent; border: none; color: #5a3a3a; cursor: pointer; font-size: 12px; padding: 6px;
  margin-left: auto; transition: color 0.2s;
}
.delete-btn:hover { color: #e07070; }

.empty-list { font-size: 12px; color: var(--muted); font-style: italic; opacity: 0.7; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateX(-8px); }
  to   { opacity: 1; transform: translateX(0); }
}

/* ═══════════════════════════════════
   FULL WIKIPEDIA PAGE — DARK THEME
═══════════════════════════════════ */
.wiki-page {
  background: var(--surface);
  color: var(--text);
  font-family: 'Playfair Display', 'Georgia', serif;
  font-size: 15px; line-height: 1.75;
  padding: 28px 24px; overflow-x: hidden;
  border-left: 3px solid var(--accent);
}

.wiki-category-tag {
  display: inline-block;
  background: transparent;
  color: var(--accent);
  font-size: 9px; font-family: 'IBM Plex Mono', monospace;
  padding: 3px 10px; margin-bottom: 14px;
  border: 1px solid var(--accent2);
  letter-spacing: 0.2em; text-transform: uppercase;
}

.wiki-title {
  font-family: 'Playfair Display', serif;
  font-size: clamp(22px, 5vw, 34px);
  font-weight: 700; line-height: 1.15;
  border-bottom: 1px solid var(--border);
  padding-bottom: 10px; margin-bottom: 6px;
  color: var(--text); word-break: break-word;
}

.wiki-from {
  font-size: 11px; font-family: 'IBM Plex Mono', monospace;
  color: var(--muted); margin-bottom: 20px; letter-spacing: 0.05em;
}

.wiki-hr {
  border: none; border-top: 1px solid var(--border); margin-bottom: 20px;
}

.wiki-content { overflow-x: hidden; }

.wiki-content h2 {
  font-family: 'Playfair Display', serif;
  font-size: clamp(18px, 4vw, 22px); font-weight: 700;
  border-bottom: 1px solid var(--border);
  margin: 32px 0 12px; padding-bottom: 6px;
  color: var(--accent);
}

.wiki-content h3 {
  font-family: 'IBM Plex Mono', monospace;
  font-size: clamp(12px, 3vw, 14px); font-weight: 500;
  margin: 20px 0 8px; color: var(--accent2);
  letter-spacing: 0.08em; text-transform: uppercase;
}

.wiki-content h4 {
  font-size: 13px; font-weight: bold;
  margin: 14px 0 6px; color: var(--muted);
}

.wiki-content p {
  margin-bottom: 14px; color: #c8c0b4;
  word-break: break-word; overflow-wrap: break-word;
}

.wiki-content a { color: var(--accent2); text-decoration: none; word-break: break-word; }
.wiki-content a:hover { color: var(--accent); text-decoration: underline; }

/* Infobox */
.wiki-content table.infobox, .wiki-content .infobox {
  float: right; clear: right; margin: 0 0 20px 20px;
  border: 1px solid var(--border);
  background: #1e1e1e;
  font-size: 12px; line-height: 1.5; border-collapse: collapse;
  max-width: 240px; width: 240px;
  font-family: 'IBM Plex Mono', monospace;
}

.wiki-content .infobox th,
.wiki-content .infobox td {
  padding: 5px 10px; vertical-align: top;
  border-top: 1px solid var(--border);
  color: #c8c0b4;
}

.wiki-content .infobox th {
  font-weight: 500; background: #1a1a1a;
  color: var(--accent); text-align: left;
  white-space: nowrap; letter-spacing: 0.05em;
}

.wiki-content .infobox caption,
.wiki-content .infobox-above,
.wiki-content .infobox .infobox-title {
  background: #1a1a1a;
  color: var(--accent);
  font-weight: 700; text-align: center;
  padding: 8px; font-size: 13px;
  border-bottom: 1px solid var(--border);
  font-family: 'Playfair Display', serif;
}

/* General tables */
.wiki-content table {
  border-collapse: collapse; margin: 16px 0; font-size: 12px;
  max-width: 100%; display: block;
  overflow-x: auto; -webkit-overflow-scrolling: touch;
  font-family: 'IBM Plex Mono', monospace;
}

.wiki-content table td,
.wiki-content table th {
  border: 1px solid var(--border);
  padding: 6px 10px; vertical-align: top;
  color: #c8c0b4;
}

.wiki-content table th {
  background: #1a1a1a;
  color: var(--accent); font-weight: 500;
  letter-spacing: 0.05em;
}

/* Images */
.wiki-content img { max-width: 100%; height: auto; display: block; filter: brightness(0.9); }

.wiki-content figure, .wiki-content .thumb {
  margin: 8px 16px 8px 0; padding: 6px;
  border: 1px solid var(--border);
  background: #1a1a1a;
  display: inline-block; font-size: 11px;
  color: var(--muted); max-width: min(220px, 45%);
}

.wiki-content .thumbcaption,
.wiki-content figcaption {
  font-size: 11px; color: var(--muted);
  margin-top: 6px; text-align: center;
  line-height: 1.4; font-family: 'IBM Plex Mono', monospace;
  letter-spacing: 0.03em;
}

/* Lists */
.wiki-content ul, .wiki-content ol { margin: 8px 0 14px 24px; }
.wiki-content li { margin-bottom: 5px; word-break: break-word; color: #c8c0b4; }

/* Blockquote */
.wiki-content blockquote {
  border-left: 3px solid var(--accent);
  margin: 16px 0 16px 0;
  padding: 8px 16px;
  color: var(--muted);
  background: #1a1a1a;
  font-style: italic;
}

/* Footer */
.wiki-footer {
  margin-top: 36px; padding-top: 16px;
  border-top: 1px solid var(--border); text-align: right;
}
.wiki-footer a {
  color: var(--accent); font-size: 11px; text-decoration: none;
  font-family: 'IBM Plex Mono', monospace; letter-spacing: 0.1em;
}
.wiki-footer a:hover { color: var(--accent2); text-decoration: underline; }

.wiki-content::after { content: ''; display: table; clear: both; }

/* ═══════════════════════════════════
   RESPONSIVE
═══════════════════════════════════ */
@media (max-width: 768px) {
  body { padding: 36px 14px; }
  .wiki-page { padding: 20px 16px; }
  .wiki-content table.infobox, .wiki-content .infobox { max-width: 200px; width: 200px; font-size: 11px; }
  .wiki-content figure, .wiki-content .thumb { max-width: min(180px, 42%); }
}

@media (max-width: 560px) {
  body { padding: 28px 12px; }
  header { margin-bottom: 32px; }
  .spin-btn { width: 100%; max-width: 100%; padding: 16px 20px; margin-bottom: 28px; }
  .wiki-page { padding: 16px 14px; }
  .wiki-content table.infobox, .wiki-content .infobox {
    float: none; width: 100%; max-width: 100%; margin: 0 0 20px 0; display: block;
  }
  .wiki-content figure, .wiki-content .thumb { float: none; display: block; max-width: 100%; margin: 12px 0; }
  .wiki-content img { width: 100%; }
  .wiki-content blockquote { margin-left: 0; }
  .wiki-content ul, .wiki-content ol { margin-left: 18px; }
  .wiki-footer { text-align: left; }
  .empty { padding: 48px 16px; }
}

@media (max-width: 360px) {
  h1 { font-size: 36px; }
  .wiki-title { font-size: 18px; }
  .wiki-content h2 { font-size: 16px; }
}

/* Safe area for notched phones */
@supports (padding: env(safe-area-inset-bottom)) {
  body { padding-bottom: max(48px, calc(env(safe-area-inset-bottom) + 24px)); }
}

/* ── Card Actions (Bookmark, Listen, Share) ── */
.card-actions {
  display: flex; gap: 12px; margin: 16px 0 24px 0;
  border-bottom: 1px solid var(--border); padding-bottom: 16px;
}
.action-btn {
  background: #1a1a1a; border: 1px solid var(--border);
  color: var(--text); padding: 8px 14px;
  font-family: 'IBM Plex Mono', monospace; font-size: 11px;
  border-radius: 4px; cursor: pointer; transition: all 0.2s;
  display: flex; align-items: center; gap: 6px;
  letter-spacing: 0.05em;
}
.action-btn:hover { border-color: var(--accent); color: var(--accent); }
.action-btn.active { background: var(--accent); color: #0e0e0e; border-color: var(--accent); }
.action-btn.audio-playing { animation: pulseBg 1.5s infinite alternate; border-color: var(--accent); }

@keyframes pulseBg {
  from { box-shadow: 0 0 0 rgba(232, 213, 163, 0); }
  to { box-shadow: 0 0 8px rgba(232, 213, 163, 0.4); }
}