/* Mobile-first. Base styles target phones; media queries enhance for tablets/desktop. */
:root {
  --bg: #1a1430;
  --bg2: #241b42;
  --panel: #2c2150;
  --card: #34275e;
  --ink: #f6f1ff;
  --muted: #c3b5e6;
  --gold: #ffd166;
  --gold-deep: #f4b740;
  --pink: #ff7eb6;
  --teal: #4fd1c5;
  --purple: #a78bfa;
  --green: #5fd68a;
  --line: #4a3d77;
  --high: #5fd68a;
  --medium: #ffd166;
  --low: #ff9d9d;
  --radius: 18px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  background:
    radial-gradient(900px 500px at 85% -10%, #3a2a6b 0%, transparent 60%),
    radial-gradient(700px 400px at 0% 0%, #45264f 0%, transparent 55%),
    var(--bg);
  color: var(--ink);
  font-family: "Inter", system-ui, sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Header ---------- */
.topbar {
  padding: 14px 16px 0;
  padding-top: max(14px, env(safe-area-inset-top));
  background: rgba(26, 20, 48, 0.92);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 10;
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 12px; }
.crown { font-size: 30px; line-height: 1; filter: drop-shadow(0 2px 8px rgba(255,209,102,.45)); }
.brand-text h1 {
  font-family: "Baloo 2", "Fredoka", cursive;
  font-weight: 700;
  font-size: 22px;
  margin: 0;
  background: linear-gradient(90deg, var(--gold), var(--pink));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.subtitle { margin: 1px 0 0; color: var(--muted); font-size: 12.5px; line-height: 1.3; }

.tabs {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  overflow-x: auto;
  padding-bottom: 10px;
  scroll-snap-type: x proximity;
  -webkit-overflow-scrolling: touch;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs button {
  flex: 0 0 auto;
  scroll-snap-align: start;
  background: var(--card);
  border: 1.5px solid transparent;
  color: var(--muted);
  padding: 10px 15px;
  min-height: 42px;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  border-radius: 999px;
  transition: transform .12s, background .15s, color .15s, border-color .15s;
}
.tabs button:active { transform: scale(0.95); }
.tabs button.active {
  color: #2a1a05;
  background: linear-gradient(90deg, var(--gold), var(--gold-deep));
  border-color: var(--gold);
}
.tabs button.story-tab {
  background: linear-gradient(90deg, var(--pink), var(--purple));
  color: #fff;
}
.tabs button.story-tab.active { box-shadow: 0 0 0 2px var(--gold) inset; color: #fff; }
.tabs button .dq { font-size: 11px; opacity: .7; margin-left: 6px; }

/* ---------- Search ---------- */
.search { position: relative; padding-bottom: 12px; }
#searchInput {
  width: 100%;
  background: var(--card);
  border: 1.5px solid var(--line);
  border-radius: 999px;
  color: var(--ink);
  font-size: 16px; /* 16px avoids iOS zoom-on-focus */
  padding: 12px 18px;
  min-height: 46px;
  outline: none;
}
#searchInput::placeholder { color: var(--muted); }
#searchInput:focus { border-color: var(--gold); }
.search-results {
  position: absolute;
  left: 0; right: 0; top: calc(100% - 4px);
  background: var(--panel);
  border: 1.5px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 16px 40px rgba(0,0,0,.5);
  max-height: 60vh;
  overflow-y: auto;
  z-index: 20;
  padding: 6px;
  -webkit-overflow-scrolling: touch;
}
.search-row {
  display: flex; flex-direction: column; gap: 2px;
  width: 100%; text-align: left;
  background: none; border: none; cursor: pointer;
  padding: 11px 12px; border-radius: 10px; min-height: 46px;
}
.search-row:active { background: rgba(138,107,214,.18); }
.search-row .sr-name { color: var(--ink); font-weight: 600; font-size: 15px; }
.search-row .sr-sub { color: var(--muted); font-size: 12.5px; }
.search-empty { padding: 16px; color: var(--muted); font-size: 14px; text-align: center; }

main { flex: 1; padding: 16px; }

/* ---------- Story view ---------- */
.story { max-width: 760px; margin: 0 auto; }
.story-hero {
  text-align: center;
  background: linear-gradient(160deg, #3a2a6b, #2c2150);
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  padding: 28px 20px 24px;
  position: relative;
  overflow: hidden;
}
.story-hero::before {
  content: "✨";
  position: absolute; top: 10px; left: 14px; font-size: 20px; opacity: .5;
}
.story-hero::after {
  content: "✨"; position: absolute; bottom: 10px; right: 14px; font-size: 20px; opacity: .5;
}
.story-hero .big-emoji { font-size: 60px; line-height: 1; }
.story-hero h2 {
  font-family: "Baloo 2", cursive;
  font-size: 30px; margin: 8px 0 2px;
  background: linear-gradient(90deg, var(--gold), var(--pink));
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.story-hero .sub { font-family: "Fredoka", sans-serif; color: var(--gold); font-size: 17px; margin: 0 0 8px; }
.story-hero .place { color: var(--muted); font-size: 14px; }
.story-hero .tagline { color: var(--ink); font-size: 16px; margin: 12px auto 0; max-width: 46ch; line-height: 1.5; }

.story-section {
  background: var(--card);
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 18px;
  margin-top: 14px;
}
.story-section h3 {
  font-family: "Baloo 2", cursive;
  font-size: 20px; margin: 0 0 8px;
  display: flex; align-items: center; gap: 10px;
  color: var(--gold);
}
.story-section h3 .se { font-size: 26px; }
.story-section p { margin: 0; line-height: 1.65; font-size: 16px; color: #efe9ff; }
.story-section em { color: var(--pink); font-style: normal; font-weight: 600; }
.story-section strong { color: #fff; }

.facts {
  background: linear-gradient(160deg, #45264f, #2c2150);
  border: 1.5px solid var(--pink);
  border-radius: var(--radius);
  padding: 18px;
  margin-top: 14px;
}
.facts h3 { font-family: "Baloo 2", cursive; color: var(--pink); margin: 0 0 10px; font-size: 20px; }
.facts ul { margin: 0; padding: 0; list-style: none; display: grid; gap: 10px; }
.facts li {
  background: rgba(255,255,255,.05);
  border-radius: 12px;
  padding: 11px 13px;
  font-size: 15px; line-height: 1.45;
}
.story-cta {
  display: block; width: 100%;
  margin-top: 16px;
  background: linear-gradient(90deg, var(--gold), var(--gold-deep));
  color: #2a1a05; font-weight: 700; font-size: 16px;
  border: none; border-radius: 14px; padding: 16px;
  cursor: pointer; font-family: "Baloo 2", cursive;
}
.story-cta:active { transform: scale(0.98); }
.story-back {
  background: var(--card); color: var(--ink);
  border: 1.5px solid var(--line); border-radius: 999px;
  padding: 9px 16px; min-height: 42px; font-size: 14px; font-weight: 600;
  cursor: pointer; margin-bottom: 12px;
}
.story-back:active { transform: scale(0.97); }
.story .d-grid { display: grid; grid-template-columns: auto 1fr; gap: 8px 14px; margin: 0; font-size: 15px; }
.story .d-grid dt { color: var(--muted); }
.story .d-grid dd { margin: 0; }
.story .d-rel {
  background: none; border: none; padding: 0; font: inherit;
  color: var(--purple); cursor: pointer; font-weight: 600;
}
.story .d-rel:active { color: var(--gold); }
.story .d-grid + * { margin-top: 0; }

/* ---------- Kingdom intro ---------- */
.kingdom-intro {
  background: linear-gradient(160deg, var(--bg2), transparent);
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 18px;
  margin-bottom: 14px;
}
.kingdom-intro h2 {
  font-family: "Baloo 2", cursive; font-size: 23px; margin: 0 0 4px; color: var(--gold);
}
.kingdom-intro .meta { color: var(--muted); font-size: 12.5px; margin-bottom: 8px; line-height: 1.5; }
.kingdom-intro .meta b { color: var(--ink); }
.kingdom-intro p { margin: 6px 0 0; line-height: 1.55; color: #e7defb; font-size: 15px; }

.connections { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.conn-title { font-family: "Baloo 2", cursive; color: var(--teal); font-size: 15px; margin-bottom: 10px; }
.conn-row { display: flex; flex-wrap: wrap; gap: 8px; }
.conn-chip {
  text-align: left;
  background: rgba(79,209,197,.1);
  border: 1.5px solid rgba(79,209,197,.4);
  border-radius: 14px;
  padding: 9px 13px;
  color: var(--ink);
  cursor: pointer;
  display: flex; flex-direction: column; gap: 2px;
  max-width: 100%;
}
.conn-chip.static { cursor: default; border-color: var(--line); background: rgba(255,255,255,.04); }
.conn-chip:not(.static):active { transform: scale(0.97); background: rgba(79,209,197,.2); }
.conn-chip .cc-name { font-weight: 700; font-size: 14px; color: var(--teal); }
.conn-chip.static .cc-name { color: var(--muted); }
.conn-chip .cc-rel { font-size: 12px; color: var(--muted); line-height: 1.3; }

/* ---------- Tree ---------- */
.tree-wrap {
  background: var(--bg2);
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.tree-toolbar {
  display: flex; flex-direction: column; gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}
.tree-title { font-family: "Baloo 2", cursive; font-size: 18px; color: var(--gold); }
.toolbar-actions { display: flex; gap: 8px; }
.btn {
  flex: 1;
  background: var(--card); color: var(--ink);
  border: 1.5px solid var(--line);
  padding: 11px 10px; min-height: 44px;
  border-radius: 12px; font-size: 13.5px; font-weight: 600;
  cursor: pointer;
}
.btn:active { transform: scale(0.96); background: var(--panel); }

.tree { width: 100%; height: 65vh; min-height: 420px; touch-action: none; }
.tree svg { width: 100%; height: 100%; display: block; }
.link { fill: none; stroke: var(--line); stroke-width: 2px; }
.node circle { stroke-width: 2.5px; cursor: pointer; }
.node text { fill: var(--ink); font-size: 14px; font-weight: 600; cursor: pointer; }
.node .title-label { fill: var(--muted); font-size: 11px; font-weight: 500; }
.node.featured circle { stroke: var(--gold); }
.node.featured text { fill: var(--gold); }

.legend {
  display: flex; gap: 14px 16px; align-items: center; flex-wrap: wrap;
  padding: 12px 16px;
  border-top: 1px solid var(--line);
  color: var(--muted); font-size: 12px;
}
.dot { width: 11px; height: 11px; border-radius: 50%; display: inline-block; margin-right: 5px; vertical-align: middle; }
.dot-high { background: var(--high); } .dot-medium { background: var(--medium); } .dot-low { background: var(--low); }
.legend .hint { width: 100%; opacity: .75; font-style: italic; }

/* ---------- Detail: bottom sheet on mobile ---------- */
.detail {
  position: fixed; left: 0; right: 0; bottom: 0;
  max-height: 86vh;
  background: var(--panel);
  border-top: 1.5px solid var(--gold);
  border-radius: 22px 22px 0 0;
  box-shadow: 0 -12px 50px rgba(0,0,0,.55);
  padding: 8px 22px calc(26px + var(--safe-b));
  overflow-y: auto;
  transform: translateY(100%);
  transition: transform .28s cubic-bezier(.22,.61,.36,1);
  z-index: 30;
}
.detail.open { transform: translateY(0); }
.detail-grabber { width: 44px; height: 5px; border-radius: 3px; background: var(--line); margin: 6px auto 14px; }
.detail-scrim {
  position: fixed; inset: 0; background: rgba(0,0,0,.55);
  opacity: 0; pointer-events: none; transition: opacity .28s; z-index: 25;
}
.detail-scrim.open { opacity: 1; pointer-events: auto; }
.detail-close {
  position: absolute; top: 12px; right: 14px;
  width: 38px; height: 38px; border-radius: 50%;
  background: var(--card); border: none; color: var(--muted);
  font-size: 24px; cursor: pointer; line-height: 1;
}
.detail h3 { font-family: "Baloo 2", cursive; font-size: 25px; margin: 0 0 2px; color: var(--gold); }
.detail .d-title { color: var(--gold-deep); font-size: 14px; font-weight: 600; }
.detail .d-aka { color: var(--muted); font-size: 13px; font-style: italic; margin-top: 3px; }
.detail .badge { display: inline-block; font-size: 11px; font-weight: 700; padding: 4px 11px; border-radius: 999px; margin: 12px 0; }
.badge-high { background: rgba(95,214,138,.2); color: var(--high); }
.badge-medium { background: rgba(255,209,102,.2); color: var(--gold); }
.badge-low { background: rgba(255,157,157,.22); color: var(--low); }
.d-grid { display: grid; grid-template-columns: auto 1fr; gap: 8px 14px; margin: 8px 0 14px; font-size: 15px; }
.d-grid dt { color: var(--muted); }
.d-grid dd { margin: 0; }
.detail .d-notes { line-height: 1.6; color: #efe9ff; font-size: 15px; }
.detail .d-section-h { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--gold-deep); margin: 18px 0 6px; }
.detail .d-rel { color: var(--purple); cursor: pointer; font-weight: 600; }
.detail .d-rel:active { color: var(--gold); }
.detail .d-feature-btn {
  display: block; width: 100%; margin-top: 16px;
  background: linear-gradient(90deg, var(--pink), var(--purple));
  color: #fff; font-weight: 700; border: none; border-radius: 14px;
  padding: 14px; font-size: 15px; cursor: pointer; font-family: "Baloo 2", cursive;
}
.detail .verify-flag { font-size: 12px; color: var(--low); margin-top: 14px; }
.detail .d-sources { font-size: 12px; color: var(--muted); }

.footer {
  padding: 16px; padding-bottom: calc(16px + var(--safe-b));
  border-top: 1px solid var(--line);
  color: var(--muted); font-size: 12.5px; line-height: 1.5; text-align: center;
}
.footer strong { color: var(--gold); }
.empty-state { padding: 50px 20px; text-align: center; color: var(--muted); }
.empty-state .big { font-size: 44px; display: block; margin-bottom: 10px; }

/* ---------- Tablet / desktop enhancements ---------- */
@media (min-width: 760px) {
  .topbar { padding: 18px 28px 0; }
  .crown { font-size: 38px; }
  .brand-text h1 { font-size: 30px; }
  .subtitle { font-size: 14px; }
  .tabs { margin-top: 16px; }
  main { padding: 24px 28px; }
  .tree-toolbar { flex-direction: row; justify-content: space-between; align-items: center; }
  .toolbar-actions { flex: 0 0 auto; }
  .btn { flex: 0 0 auto; }
  .tree { height: 72vh; }
  .facts ul { grid-template-columns: 1fr 1fr; }

  /* Detail becomes a right-side panel */
  .detail {
    left: auto; top: 0; bottom: 0; width: 420px; max-height: none;
    border-radius: 0; border-top: none; border-left: 1.5px solid var(--gold);
    transform: translateX(100%);
    padding: 30px 28px;
  }
  .detail.open { transform: translateX(0); }
  .detail-grabber { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
