/* Kota Lapis — tokens first (see DESIGN.md), then components. */
:root {
  --font-display: 'Bricolage Grotesque', 'IBM Plex Sans', system-ui, sans-serif;
  --font-text: 'IBM Plex Sans', system-ui, -apple-system, 'Segoe UI', sans-serif;

  --bg: #ECEAE4;
  --surface: rgba(255, 255, 255, 0.88);
  --surface-solid: #FFFFFF;
  --ink: #1B1A17;
  --muted: #5A564F;
  --line: rgba(27, 26, 23, 0.12);
  --track: rgba(27, 26, 23, 0.07);
  --primary: #1B1A17;
  --on-primary: #FFFFFF;
  --accent: #C8352B;
  --error: #A3261C;
  --shadow: 0 8px 28px rgba(20, 18, 14, 0.12), 0 1px 2px rgba(20, 18, 14, 0.08);

  --r-sm: 6px;
  --r-md: 10px;
  --r-lg: 16px;
  --r-full: 9999px;

  --s-xs: 4px;
  --s-sm: 8px;
  --s-md: 16px;
  --s-lg: 24px;
  --s-xl: 40px;

  --fs-display: 30px;
  --fs-h-sm: 20px;
  --fs-body: 15px;
  --fs-label: 13px;
  --fs-small: 12px;
  --fs-micro: 11px;

  --t-fast: 150ms;
  --t-base: 240ms;
  --hit: 36px;

  /* layer bands, set from the active theme */
  --b1: #D3C6AA; --b2: #7EA1C9; --b3: #8EB97F; --b4: #E4A955; --b5: #8F897F; --b6: #C8352B;
  --scene: #ECEAE4;
}

[data-chrome="dark"] {
  --surface-solid: var(--chrome, #151E2E);
  --surface: color-mix(in srgb, var(--surface-solid) 88%, transparent);
  --ink: #F2EEE6;
  --muted: #A9B0BD;
  --line: rgba(242, 238, 230, 0.16);
  --track: rgba(242, 238, 230, 0.08);
  --primary: #F2EEE6;
  --on-primary: #14171C;
  --accent: #F3C25B;
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
}

@media (pointer: coarse) { :root { --hit: 44px; } }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--scene); color: var(--ink); }
body { font: 400 var(--fs-body)/1.5 var(--font-text); -webkit-font-smoothing: antialiased; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
.num { font-variant-numeric: tabular-nums; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
svg { width: 18px; height: 18px; flex: none; }

/* ---------- stages ---------- */
.stage { position: fixed; inset: 0; }
.stage-scene { touch-action: none; }
body[data-mode="lapis"] .stage-map { visibility: hidden; }
body[data-mode="peta"] .stage-scene { visibility: hidden; }
.scene-canvas { display: block; }
.scene-labels { position: absolute; inset: 0; pointer-events: none; }

/* ---------- glass + controls ---------- */
.top, .top-right, .legend, .dock, .credit { position: fixed; z-index: 5; }
.city, .legend, .dock, .chip, .menu, .progress-card, .notice, .credit {
  background: var(--surface);
  -webkit-backdrop-filter: blur(14px) saturate(1.2);
  backdrop-filter: blur(14px) saturate(1.2);
  box-shadow: var(--shadow);
}

.seg { display: inline-flex; gap: 2px; padding: 3px; border-radius: var(--r-full); background: var(--track); }
.top-right .seg { background: var(--surface); box-shadow: var(--shadow); -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px); }
.seg button {
  min-height: var(--hit); padding: 0 14px; border: 0; border-radius: var(--r-full);
  background: transparent; color: var(--muted); font-size: var(--fs-label); font-weight: 500;
  transition: background var(--t-fast), color var(--t-fast);
}
.seg button:hover { color: var(--ink); }
.seg button[aria-pressed="true"] { background: var(--primary); color: var(--on-primary); }

.chip {
  display: inline-flex; align-items: center; gap: 6px; min-height: var(--hit); padding: 0 14px;
  border: 0; border-radius: var(--r-full); font-size: var(--fs-label); font-weight: 500; color: var(--ink);
}
.dock .chip { box-shadow: none; background: var(--track); -webkit-backdrop-filter: none; backdrop-filter: none; }
.chip[aria-pressed="true"] { background: var(--primary); color: var(--on-primary); }

.primary {
  display: inline-flex; align-items: center; gap: 8px; min-height: var(--hit); padding: 0 16px;
  border: 0; border-radius: var(--r-full); background: var(--primary); color: var(--on-primary);
  font-size: var(--fs-label); font-weight: 600; white-space: nowrap;
  transition: transform var(--t-fast);
}
.primary:active { transform: scale(0.97); }
.primary[disabled] { opacity: 0.45; cursor: default; }

.icon-btn {
  display: inline-grid; place-items: center; width: var(--hit); height: var(--hit); padding: 0;
  border: 0; border-radius: var(--r-full); background: var(--track); color: var(--ink);
}

/* ---------- header ---------- */
.top { top: max(var(--s-md), env(safe-area-inset-top)); left: var(--s-md); display: flex; flex-direction: column; align-items: flex-start; gap: var(--s-sm); max-width: min(460px, calc(100vw - 32px)); }
.brand { display: flex; align-items: center; gap: var(--s-sm); padding-left: 2px; }
.brand-name { font: 700 15px/1 var(--font-display); letter-spacing: -0.01em; }
.mark {
  width: 18px; height: 18px; border-radius: 4px; flex: none;
  background: linear-gradient(to top, var(--b1) 0 16.66%, var(--b2) 0 33.33%, var(--b3) 0 50%, var(--b4) 0 66.66%, var(--b5) 0 83.33%, var(--b6) 0);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.city {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px; text-align: left;
  border: 0; border-radius: var(--r-md); padding: 10px 14px 10px;
}
.city-name { font: 700 var(--fs-display)/1.05 var(--font-display); letter-spacing: -0.02em; }
.city-meta { display: flex; align-items: center; gap: 6px; font-size: var(--fs-small); color: var(--muted); }
.city-meta .chev { width: 14px; height: 14px; }
.city:hover .chev { color: var(--ink); }
.caption {
  margin: 0; padding-left: 2px; font: 600 var(--fs-label)/1.3 var(--font-text); color: var(--muted);
  opacity: 0; transform: translateY(-4px); transition: opacity var(--t-base), transform var(--t-base);
}
.caption.is-on { opacity: 1; transform: none; }

.top-right { top: max(var(--s-md), env(safe-area-inset-top)); right: var(--s-md); display: flex; gap: var(--s-sm); align-items: center; }

/* ---------- legend ---------- */
.legend { left: var(--s-md); top: 50%; transform: translateY(-38%); width: 236px; padding: 12px 8px 8px; border-radius: var(--r-lg); }
body[data-mode="peta"] .legend { display: none; }
.legend h2 { margin: 0 8px 6px; font: 600 var(--fs-small)/1.2 var(--font-text); color: var(--muted); letter-spacing: 0.02em; }
.legend ol { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column-reverse; gap: 2px; }
.layer-row { display: flex; align-items: center; gap: 2px; border-radius: var(--r-md); }
.layer-row:hover, .layer-row.is-hover { background: var(--track); }
.layer-open {
  flex: 1; display: grid; grid-template-columns: 10px 22px 1fr; align-items: center; column-gap: 8px;
  min-height: var(--hit); padding: 4px 6px; border: 0; background: none; text-align: left; border-radius: var(--r-md);
}
.layer-band { width: 10px; height: 22px; border-radius: 3px; box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.1); }
.layer-no { font-size: var(--fs-small); color: var(--muted); font-variant-numeric: tabular-nums; }
.layer-name { font-size: var(--fs-label); font-weight: 600; line-height: 1.2; }
.layer-en { display: block; font-size: var(--fs-micro); font-weight: 400; color: var(--muted); }
.layer-eye { width: var(--hit); height: var(--hit); display: grid; place-items: center; border: 0; background: none; color: var(--muted); border-radius: var(--r-full); }
.layer-eye:hover { color: var(--ink); }
.layer-row.is-off .layer-open { opacity: 0.45; }

/* ---------- dock ---------- */
.dock {
  left: 50%; bottom: calc(26px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  display: flex; align-items: center; gap: var(--s-sm); padding: 6px; border-radius: var(--r-full);
  max-width: calc(100vw - 16px);
}
.dock-lapis, .dock-peta { display: flex; align-items: center; gap: var(--s-sm); }
body[data-mode="lapis"] .dock-peta { display: none; }
body[data-mode="peta"] .dock-lapis { display: none; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.theme-wrap { position: relative; display: flex; align-items: center; }
.theme-btn { display: none; }
.theme-dot, .swatch .sw {
  width: 22px; height: 22px; border-radius: 50%; flex: none;
  background: conic-gradient(from 200deg, var(--sw-a) 0 50%, var(--sw-b) 0 75%, var(--sw-c) 0);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.18);
}
.sw-name { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.swatches { display: flex; gap: 2px; padding: 0 4px; }
.swatch { width: var(--hit); height: var(--hit); display: grid; place-items: center; border: 0; padding: 0; background: none; border-radius: var(--r-full); }
.swatch .sw { transition: transform var(--t-fast); }
.swatch[aria-pressed="true"] .sw { box-shadow: 0 0 0 2px var(--surface-solid), 0 0 0 4px var(--ink); }
.swatch:hover .sw { transform: scale(1.08); }
.save-wrap { position: relative; }
.menu {
  position: absolute; right: 0; bottom: calc(100% + 10px); min-width: 250px; padding: 6px;
  border-radius: var(--r-md); display: flex; flex-direction: column; background: var(--surface-solid);
}
.menu[hidden] { display: none; }
.menu button { min-height: var(--hit); padding: 8px 12px; border: 0; background: none; text-align: left; border-radius: var(--r-sm); font-size: var(--fs-label); }
.menu button:hover, .menu button:focus-visible { background: var(--track); }

.credit {
  right: var(--s-sm); bottom: max(4px, env(safe-area-inset-bottom)); margin: 0; padding: 1px 8px;
  border-radius: var(--r-full); font-size: var(--fs-micro); color: var(--muted); box-shadow: none;
}
.credit a { color: inherit; }

/* ---------- 3D labels ---------- */
.tag {
  pointer-events: auto; display: inline-flex; align-items: center; gap: 6px; height: 26px; padding: 0 10px 0 4px;
  border: 0; border-radius: var(--r-full); background: var(--surface); color: var(--ink);
  -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); font-size: var(--fs-small); font-weight: 600; white-space: nowrap;
  transition: background var(--t-fast), color var(--t-fast);
}
.tag-no { display: inline-grid; place-items: center; min-width: 20px; height: 18px; padding: 0 4px; border-radius: var(--r-full); background: var(--track); font-variant-numeric: tabular-nums; font-size: var(--fs-micro); }
.tag.is-hover { background: var(--primary); color: var(--on-primary); }
.tag.is-hover .tag-no { background: rgba(127, 127, 127, 0.3); }

/* ---------- progress ---------- */
.progress { position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; pointer-events: none; }
.progress[hidden] { display: none; }
.progress-card { display: flex; align-items: center; gap: var(--s-md); padding: 16px 20px; border-radius: var(--r-lg); background: var(--surface-solid); min-width: 280px; }
.cake { display: flex; flex-direction: column-reverse; width: 44px; height: 48px; border-radius: 6px; overflow: hidden; box-shadow: inset 0 0 0 1px var(--line); }
.cake i { flex: 1; background: var(--band); opacity: 0.15; transition: opacity var(--t-base); }
.cake i.is-done { opacity: 1; }
.cake i.is-now { opacity: 0.55; animation: pulse 900ms ease-in-out infinite alternate; }
@keyframes pulse { to { opacity: 0.25; } }
.progress-title { margin: 0; font: 700 var(--fs-h-sm)/1.15 var(--font-display); }
.progress-step { margin: 4px 0 0; font-size: var(--fs-label); color: var(--muted); }

.notice { position: fixed; z-index: 25; left: 50%; top: 50%; transform: translate(-50%, -50%); max-width: 360px; padding: 20px; border-radius: var(--r-lg); background: var(--surface-solid); text-align: center; }
.notice[hidden] { display: none; }
.notice p { margin: 0 0 12px; }

/* ---------- city dialog ---------- */
.city-dlg {
  width: min(560px, calc(100vw - 24px)); max-height: min(720px, calc(100dvh - 48px)); padding: 0;
  border: 0; border-radius: var(--r-lg); background: var(--surface-solid); color: var(--ink); box-shadow: var(--shadow);
  overflow: auto;
}
.city-dlg[open] { display: flex; flex-direction: column; }
.city-dlg::backdrop { background: rgba(10, 10, 10, 0.35); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 16px 8px 20px; margin: 0; }
.dlg-head h2 { margin: 0; font: 700 var(--fs-h-sm)/1.1 var(--font-display); }
.search { display: flex; align-items: center; gap: 8px; margin: 0 16px; padding: 4px 4px 4px 12px; border-radius: var(--r-full); background: var(--track); }
.search svg { color: var(--muted); }
.search input { flex: 1; min-width: 0; min-height: var(--hit); border: 0; background: none; outline: none; font-size: var(--fs-body); }
.search .chip { background: var(--primary); color: var(--on-primary); box-shadow: none; }
.search-state { margin: 6px 20px 0; min-height: 0; font-size: var(--fs-label); color: var(--muted); }
.search-state:empty { display: none; }
.results, .cities { list-style: none; margin: 0; padding: 6px 8px 8px; }
.results:empty { display: none; }
.results li button, .cities li button {
  width: 100%; display: flex; align-items: baseline; justify-content: space-between; gap: 12px; min-height: var(--hit);
  padding: 8px 12px; border: 0; background: none; text-align: left; border-radius: var(--r-md);
}
.results li button:hover, .cities li button:hover, .results li button:focus-visible, .cities li button:focus-visible { background: var(--track); }
.place-name { font-weight: 600; }
.place-state { font-size: var(--fs-small); color: var(--muted); text-align: right; }
.results .place-state { max-width: 60%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dlg-sub { display: flex; align-items: center; justify-content: space-between; padding: 8px 16px 0 20px; border-top: 1px solid var(--line); margin-top: 6px; }
.dlg-sub h3 { margin: 0; font-size: var(--fs-small); font-weight: 600; color: var(--muted); }
.cities { display: grid; grid-template-columns: 1fr 1fr; }
.cities li button { flex-direction: column; align-items: flex-start; gap: 0; }
.cities .place-state { text-align: left; }

/* ---------- focus viewer ---------- */
.focus { position: fixed; inset: 0; z-index: 30; display: flex; flex-direction: column; background: var(--scene); }
.focus[hidden] { display: none; }
.focus-bar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: max(12px, env(safe-area-inset-top)) 16px 12px; }
.focus-bar h2 { margin: 0; font: 700 var(--fs-h-sm)/1.1 var(--font-display); }
.focus-bar h2 .num { color: var(--muted); margin-right: 4px; }
.focus-en { font: 400 var(--fs-label) var(--font-text); color: var(--muted); margin-left: 6px; }
.focus-actions { display: flex; gap: 6px; }
.focus-actions .chip { box-shadow: none; background: var(--track); }
.focus-stage { flex: 1; display: grid; place-items: center; padding: 8px 16px 32px; min-height: 0; }
.focus-stage img { max-width: 100%; max-height: 100%; width: min(82vh, 92vw); aspect-ratio: 1; object-fit: contain; filter: drop-shadow(0 10px 30px rgba(0, 0, 0, 0.18)); }

/* ---------- phone ---------- */
@media (max-width: 760px) {
  :root { --fs-display: 23px; }
  .top { top: max(10px, env(safe-area-inset-top)); left: 12px; gap: 6px; max-width: calc(100vw - 24px); }
  .brand { height: var(--hit); }
  .city { padding: 8px 12px; max-width: 100%; }
  .city-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
  .top-right { top: max(10px, env(safe-area-inset-top)); right: 12px; }
  .top-right .seg button { padding: 0 11px; }
  .caption { display: none; }
  body[data-view="potong"] .scene-labels { display: none; }
  body[data-view="bentang"] .legend { display: none; }

  .legend {
    left: 8px; right: 8px; top: auto; bottom: calc(130px + env(safe-area-inset-bottom)); transform: none; width: auto;
    padding: 4px; border-radius: var(--r-md);
  }
  .legend h2 { display: none; }
  .legend ol { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0; }
  .layer-row { min-width: 0; }
  .layer-open { grid-template-columns: 8px 1fr; padding: 2px 8px; column-gap: 8px; min-height: 36px; }
  .layer-open .layer-no { display: none; }
  .layer-band { width: 8px; height: 18px; }
  .layer-name { font-size: var(--fs-small); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .layer-en, .layer-eye { display: none; }

  .dock {
    left: 8px; right: 8px; transform: none; bottom: calc(22px + env(safe-area-inset-bottom));
    display: grid; grid-template-columns: 1fr auto auto; gap: 6px; border-radius: var(--r-lg);
    grid-template-areas: "views views save" "shapes spin theme";
  }
  .dock > .dock-lapis:not(.save-wrap) { display: contents; }
  #viewSeg { grid-area: views; }
  #shapeSeg { grid-area: shapes; }
  #spinBtn { grid-area: spin; }
  .theme-wrap { grid-area: theme; }
  .save-wrap { grid-area: save; position: static; }
  .dock-peta { grid-area: views; }
  body[data-mode="peta"] .dock { grid-template-areas: "views views theme"; }
  #viewSeg button, #shapeSeg button { flex: 1; padding: 0 6px; }
  .icon-chip, #saveBtn { width: var(--hit); padding: 0; justify-content: center; }
  .icon-chip span, #saveBtn span { display: none; }
  .theme-btn { display: inline-flex; width: var(--hit); padding: 0; justify-content: center; }
  .swatches {
    display: none; position: fixed; left: 8px; right: 8px; bottom: calc(136px + env(safe-area-inset-bottom));
    grid-template-columns: repeat(3, 1fr); gap: 4px; padding: 8px; border-radius: var(--r-md);
    background: var(--surface-solid); box-shadow: var(--shadow); z-index: 8;
  }
  .themes-open .swatches { display: grid; }
  .swatch { width: auto; justify-content: start; display: flex; gap: 8px; padding: 0 8px; border-radius: var(--r-sm); font-size: var(--fs-label); }
  .swatch .sw-name { position: static; width: auto; height: auto; clip: auto; }
  .swatch[aria-pressed="true"] { background: var(--track); }
  .menu { position: fixed; left: 8px; right: 8px; bottom: calc(136px + env(safe-area-inset-bottom)); z-index: 8; }
  .cities { grid-template-columns: 1fr; }
  .focus-bar { flex-direction: column; align-items: flex-start; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 1ms !important; animation: none !important; }
}
