:root {
  --bg: #f4f2ee;
  --panel: #ffffff;
  --ink: #16181d;
  --muted: #6b6f78;
  --line: #e3e0da;
  --field: #f7f6f3;
  --accent: #e8463a;
  --accent-ink: #ffffff;
  --stage: #dcd8d0;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121316; --panel: #1b1d22; --ink: #eceef2; --muted: #9399a4;
    --line: #2c2f36; --field: #23262c; --stage: #0b0c0e; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121316; --panel: #1b1d22; --ink: #eceef2; --muted: #9399a4;
  --line: #2c2f36; --field: #23262c; --stage: #0b0c0e; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }

.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 800; font-size: 20px; letter-spacing: -0.02em; display: flex; align-items: center; gap: 8px; }
.logo {
  display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 8px;
  background: var(--accent); color: var(--accent-ink); font-size: 16px; transform: rotate(-20deg);
}
.title-input {
  flex: 1; max-width: 420px; border: 1px solid transparent; background: var(--field);
  border-radius: 8px; padding: 7px 10px;
}
.title-input:focus { border-color: var(--accent); outline: none; }
.top-actions { margin-left: auto; display: flex; gap: 6px; }
.ghost {
  background: none; border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; cursor: pointer;
}
.ghost:hover { background: var(--field); }
.file-btn { cursor: pointer; }

.layout { flex: 1; display: flex; min-height: 0; }
.sidebar {
  width: 340px; flex: none; overflow-y: auto; background: var(--panel);
  border-right: 1px solid var(--line); padding: 14px 16px 24px;
}
.sidebar section, .sidebar details { border-bottom: 1px solid var(--line); padding-bottom: 14px; margin-bottom: 14px; }
h2, summary { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 0 0 10px; }
summary { cursor: pointer; }
details[open] summary { margin-bottom: 10px; }

.search { position: relative; }
.search input {
  width: 100%; padding: 9px 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--field);
}
.search input:focus { outline: none; border-color: var(--accent); }
.results {
  position: absolute; z-index: 10; left: 0; right: 0; top: calc(100% + 4px); list-style: none; margin: 0; padding: 4px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.15);
}
.results li { padding: 7px 9px; border-radius: 6px; cursor: pointer; display: flex; flex-direction: column; }
.results li:hover, .results li:focus { background: var(--field); outline: none; }
.results small { color: var(--muted); }

.row-between { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin: 8px 0; flex-wrap: wrap; }
.check { display: flex; align-items: center; gap: 6px; cursor: pointer; }
.link { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }
.muted { color: var(--muted); }

.places { list-style: none; margin: 10px 0 0; padding: 0; }
.place { display: flex; align-items: center; gap: 4px; }
.place .num {
  flex: none; width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink); font-size: 12px; font-weight: 700; cursor: pointer;
}
.place input {
  flex: 1; min-width: 0; border: 1px solid transparent; background: none; border-radius: 6px; padding: 5px 6px; font-weight: 600;
}
.place input:hover { background: var(--field); }
.place input:focus { border-color: var(--accent); outline: none; background: var(--field); }
.icon-btn { background: none; border: 0; width: 26px; height: 26px; border-radius: 6px; color: var(--muted); }
.icon-btn:hover:not(:disabled) { background: var(--field); color: var(--ink); }
.icon-btn:disabled { opacity: .3; cursor: default; }
.leg {
  margin: 2px 0 2px 11px; padding: 4px 0 4px 20px; border-left: 2px dashed var(--line);
  display: flex; align-items: center; gap: 6px;
}
.leg select { padding: 3px 6px; border-radius: 6px; border: 1px solid var(--line); background: var(--field); font-size: 13px; }
.chip { font-size: 11px; border: 0; border-radius: 999px; padding: 2px 8px; background: var(--field); color: var(--muted); }

.grid { display: grid; gap: 10px; }
.grid label:not(.check) { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.grid label { position: relative; }
.grid label span[data-out] { position: absolute; right: 0; top: 0; color: var(--ink); font-variant-numeric: tabular-nums; }
.grid select, .grid input[type="file"] {
  padding: 6px 8px; border-radius: 6px; border: 1px solid var(--line); background: var(--field); color: var(--ink);
}
.grid input[type="color"] { width: 100%; height: 32px; padding: 2px; border: 1px solid var(--line); border-radius: 6px; background: var(--field); }
input[type="range"] { accent-color: var(--accent); width: 100%; }
input[type="checkbox"] { accent-color: var(--accent); }

.primary {
  margin-top: 14px; width: 100%; padding: 11px; border: 0; border-radius: 9px;
  background: var(--accent); color: var(--accent-ink); font-weight: 700; font-size: 15px;
}
.primary:hover { filter: brightness(1.06); }
.progress-wrap { display: grid; gap: 4px; margin-top: 8px; }
progress { width: 100%; accent-color: var(--accent); }
.status { color: var(--muted); font-size: 13px; min-height: 1em; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--stage); }
.stage { flex: 1; min-height: 0; display: grid; place-items: center; padding: 16px; }
/* The frame is laid out at the export's reference size and scaled into .frame-box (see layoutFrame). */
.frame-box { position: relative; flex: none; overflow: hidden; border-radius: 6px; box-shadow: 0 10px 40px rgba(0,0,0,.25); background: #0b1020; }
.frame { position: absolute; left: 0; top: 0; overflow: hidden; transform-origin: 0 0; background: #0b1020; }
#map { position: absolute; inset: 0; }
.overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.transport {
  display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--panel); border-top: 1px solid var(--line);
}
.play {
  width: 38px; height: 38px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink); font-size: 14px; flex: none;
}
.time { font-variant-numeric: tabular-nums; color: var(--muted); min-width: 88px; text-align: right; }

body.locked .sidebar, body.locked .transport, body.locked .topbar { pointer-events: none; }
body.locked .sidebar > :not(details:last-of-type) { opacity: .5; }
body.locked .frame { pointer-events: none; }
body.locked .maplibregl-ctrl { display: none; }


body { font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
optgroup { font-weight: 700; }
.dialog {
  width: min(520px, calc(100vw - 32px)); border: 1px solid var(--line); border-radius: 14px;
  background: var(--panel); color: var(--ink); padding: 20px 22px; box-shadow: 0 24px 64px rgba(0,0,0,.35);
}
.dialog::backdrop { background: rgba(10, 12, 16, .45); }
.dialog h2 { font-size: 16px; text-transform: none; letter-spacing: 0; color: var(--ink); }
.keys-fields { display: grid; gap: 14px; margin: 16px 0; }
.key-field { display: grid; gap: 4px; }
.key-field span { display: flex; justify-content: space-between; }
.key-field a { color: var(--accent); font-size: 12px; }
.key-field input { padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--field); }
.key-field input:focus { outline: none; border-color: var(--accent); }
.key-field small { color: var(--muted); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.primary.small { width: auto; margin: 0; padding: 7px 16px; font-size: 14px; }
#cancel-export { justify-self: start; }
body.locked #cancel-export { pointer-events: auto; }

.export-rig { position: fixed; left: -100000px; top: 0; pointer-events: none; }
.export-rig canvas:not(.maplibregl-canvas) { display: none; }
.primary:disabled { opacity: .55; cursor: progress; filter: none; }

.places { position: relative; }
.place { border-radius: 8px; cursor: grab; touch-action: none; user-select: none; background: var(--panel); }
.place input { cursor: text; user-select: text; }
.grip { color: var(--muted); opacity: .45; font-size: 13px; width: 12px; flex: none; text-align: center; }
.place:hover .grip { opacity: 1; }
.place.dragging { position: relative; z-index: 5; cursor: grabbing; box-shadow: 0 6px 18px rgba(0,0,0,.18); opacity: .95; }
body.sorting, body.sorting * { cursor: grabbing !important; }
.drop-indicator { position: absolute; left: 0; right: 0; height: 3px; border-radius: 2px; background: var(--accent); pointer-events: none; }

/* ---- trip editor ---- */
.section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.section-head h2 { margin: 0; }
.head-actions { display: flex; gap: 2px; }
.links { display: flex; gap: 12px; }
.results li.active { background: var(--field); }
.results li.fav strong { color: var(--accent); }
.fav-bar { margin: 8px 0 2px; font-size: 13px; display: grid; gap: 6px; }
.fav-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.fav-chip { display: inline-flex; align-items: center; border: 1px solid var(--line); border-radius: 999px; background: var(--field); overflow: hidden; }
.fav-chip.is-home { border-color: var(--accent); }
.fav-add { border: 0; background: none; padding: 3px 4px 3px 10px; font-size: 13px; font-weight: 600; max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fav-add:hover { color: var(--accent); }
.fav-mini { border: 0; background: none; padding: 3px 5px; color: var(--muted); font-size: 13px; line-height: 1; }
.fav-mini:last-child { padding-right: 9px; }
.fav-mini:hover { color: var(--ink); }
.home-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.chip-btn { border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 3px 10px; font-size: 12px; }
.chip-btn:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.chip-btn:disabled { opacity: .4; cursor: default; }
.icon-btn.fav-on { color: var(--accent); }
.leg-dist { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.insert-btn { margin-left: auto; opacity: .55; }
.leg:hover .insert-btn, .insert-btn:focus-visible { opacity: 1; color: var(--accent); }
.leg.inserting { padding-right: 0; }
.search.inline { flex: 1; }
.search.inline input { padding: 6px 10px; font-size: 13px; }
.insert-first { display: block; width: 100%; margin: 8px 0 0; padding: 4px; border: 1px dashed var(--line); border-radius: 8px; background: none; color: var(--muted); font-size: 12px; }
.insert-first:hover { color: var(--accent); border-color: var(--accent); }
#paste-text { width: 100%; margin: 12px 0; padding: 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--field); font: inherit; resize: vertical; }
#paste-text:focus { outline: none; border-color: var(--accent); }
code { font-size: 12px; background: var(--field); padding: 1px 4px; border-radius: 4px; }
@media (hover: none) { .insert-btn { opacity: 1; } }

/* ---- selection / group moves ---- */
.place.selected { background: color-mix(in srgb, var(--accent) 10%, var(--panel)); }
.place.selected .num { background: var(--ink); color: var(--panel); }
.selection-bar {
  position: sticky; top: -14px; z-index: 6; display: flex; flex-wrap: wrap; align-items: center; gap: 6px;
  margin: 8px 0 0; padding: 8px; border-radius: 10px; background: var(--panel); border: 1px solid var(--accent);
  box-shadow: 0 4px 14px rgba(0,0,0,.1);
}
.selection-bar[hidden] { display: none; }
.sel-count { font-size: 12px; font-weight: 700; margin-right: 4px; }
.hint { font-size: 12px; margin: 8px 0 0; }

/* ---- mobile ---- */
body { height: 100dvh; }
.menu-btn, .tabs, .expand-preview { display: none; }
.share-video { width: 100%; margin-top: 8px; }
.share-video[hidden] { display: none; }

@media (max-width: 800px) {
  .topbar {
    position: relative; z-index: 20; gap: 10px;
    padding: max(8px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  }
  .brand { font-size: 0; gap: 0; }
  .brand .logo { font-size: 16px; }
  .title-input { display: block; min-width: 0; max-width: none; font-size: 16px; padding: 8px 10px; }
  .menu-btn { display: inline-grid; place-items: center; flex: none; width: 42px; height: 42px; padding: 0; font-size: 20px; line-height: 1; }
  .top-actions {
    display: none; position: absolute; top: calc(100% - 2px); right: max(12px, env(safe-area-inset-right));
    flex-direction: column; gap: 2px; min-width: 190px; padding: 6px;
    background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 12px 32px rgba(0,0,0,.2);
  }
  .top-actions.open { display: flex; }
  .top-actions .ghost { border: 0; border-radius: 8px; text-align: left; padding: 12px 14px; font-size: 16px; }

  .layout { flex-direction: column; }
  .main { order: -1; flex: none; }
  .stage { flex: none; padding: 8px; }
  .transport { gap: 10px; padding: 6px max(12px, env(safe-area-inset-right)) 6px max(12px, env(safe-area-inset-left)); }
  .play { width: 44px; height: 44px; }
  .time { min-width: 0; font-size: 12px; white-space: nowrap; }
  .section-head h2 { visibility: hidden; }
  .expand-preview { display: inline-grid; place-items: center; flex: none; font-size: 18px; }
  .expand-preview[aria-pressed="true"] { color: var(--accent); }

  .sidebar {
    flex: 1; min-height: 0; width: 100%; border-right: 0; border-top: 1px solid var(--line);
    padding: 0 max(16px, env(safe-area-inset-right)) calc(24px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
    overscroll-behavior: contain;
  }
  .tabs {
    display: flex; gap: 4px; position: sticky; top: 0; z-index: 8;
    margin: 0 -16px 12px; padding: 8px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
  }
  .tabs button {
    flex: 1; border: 0; background: none; border-radius: 8px; padding: 9px 8px;
    font-size: 15px; font-weight: 600; color: var(--muted);
  }
  .tabs button[aria-selected="true"] { background: var(--field); color: var(--ink); box-shadow: inset 0 -2px 0 var(--accent); }
  .sidebar [data-panel] { display: none; }
  .sidebar[data-tab="trip"] [data-panel="trip"],
  .sidebar[data-tab="style"] [data-panel="style"],
  .sidebar[data-tab="export"] [data-panel="export"] { display: block; }
  .sidebar section, .sidebar details { border-bottom: 0; }
  .selection-bar { top: 58px; }
  .sidebar input:not([type="checkbox"]):not([type="range"]), .sidebar select, .dialog input, .dialog textarea { font-size: 16px; }
  .status:not(:empty) {
    position: sticky; bottom: calc(-24px - env(safe-area-inset-bottom)); z-index: 7; margin: 0 -16px; padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
    background: var(--panel); border-top: 1px solid var(--line);
  }
  .dialog { max-height: 88dvh; overflow: auto; padding: 18px; }
}

@media (pointer: coarse) {
  .icon-btn { width: 40px; height: 40px; font-size: 16px; flex: none; }
  .place { min-height: 48px; touch-action: pan-y; }
  .place .num { width: 32px; height: 32px; font-size: 14px; }
  .grip { display: grid; place-items: center; width: 30px; height: 44px; font-size: 18px; opacity: .75; touch-action: none; cursor: grab; }
  .place input { padding: 8px 6px; }
  .leg { margin-left: 49px; padding: 6px 0 6px 14px; }
  .leg select { padding: 8px; font-size: 15px; }
  .leg-dist { font-size: 13px; }
  .results li { padding: 12px 10px; }
  .chip-btn { padding: 8px 14px; font-size: 14px; }
  .fav-add { padding: 8px 6px 8px 14px; font-size: 15px; }
  .fav-mini { padding: 8px; font-size: 15px; }
  .insert-first { padding: 10px; font-size: 14px; }
  .links { gap: 16px; }
  .link { padding: 8px 0; }
  .check { min-height: 40px; }
  input[type="checkbox"] { width: 20px; height: 20px; }
  .grid select { padding: 10px; }
  input[type="range"] { height: 32px; }
  .ghost { padding: 10px 14px; }
  .selection-bar .chip-btn { padding: 8px 12px; }
  body.touch .maplibregl-ctrl-group { display: none; }
}
#scrub { flex: 1; min-width: 0; }
