/* ─────────────────────────────────────────────────────────────
   Traffic Scenario Editor — editor shell
   Neutral dark theme. Layout via CSS grid (PRD §18).
   ───────────────────────────────────────────────────────────── */

:root {
  --bg:        #1e2127;
  --panel:     #262a32;
  --panel-2:   #2d323b;
  --line:      #3a404d;
  --text:      #e6e9ef;
  --muted:     #8b93a3;
  --accent:    #4c9aff;
  --accent-dim:#2f5fa0;
  --danger:    #ff5c5c;
  --toolbar-h: 44px;
  --status-h:  26px;
  --left-w:    240px;
  --right-w:   288px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 system-ui, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

#app {
  height: 100vh;
  display: grid;
  grid-template-columns: var(--left-w) 1fr var(--right-w);
  grid-template-rows: var(--toolbar-h) 1fr var(--status-h);
  grid-template-areas:
    "toolbar toolbar toolbar"
    "library viewport rightbar"
    "status  status   status";
}

/* ── Toolbar ─────────────────────────────────────────────── */
#toolbar {
  grid-area: toolbar;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 10px;
  background: linear-gradient(#2b303a, #23272f);
  border-bottom: 1px solid var(--line);
  user-select: none;
}
.brand { font-weight: 600; letter-spacing: .2px; white-space: nowrap; }
.tb-group { display: flex; gap: 4px; }
.tb-sep { width: 1px; height: 22px; background: var(--line); margin: 0 4px; }
.tb-spacer { flex: 1; }
.tb-phase { color: var(--muted); font-size: 12px; white-space: nowrap; }

.tb-btn {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 12px;
  cursor: pointer;
  transition: background .12s, border-color .12s;
}
.tb-btn:hover:not(:disabled) { background: #363c47; }
.tb-btn.is-active { background: var(--accent-dim); border-color: var(--accent); }
.tb-btn:disabled { opacity: .4; cursor: not-allowed; }

#toolbar { overflow-x: auto; }
#toolbar::-webkit-scrollbar { height: 0; }
.tb-group, .tb-sep, .tb-btn, .tb-select { flex: 0 0 auto; }
.tb-select {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 6px; padding: 5px 6px; font-size: 12px; cursor: pointer;
}
.tb-check {
  display: inline-flex; align-items: center; gap: 4px; flex: 0 0 auto;
  font-size: 12px; color: var(--muted); background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 6px; padding: 4px 7px; cursor: pointer;
}
.tb-check input { accent-color: var(--accent); margin: 0; }
.sb-link { color: var(--accent); cursor: pointer; text-decoration: none; }
.sb-link:hover { text-decoration: underline; }

/* ── Panels (library / hierarchy / inspector) ────────────── */
.panel {
  background: var(--panel);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
#library  { grid-area: library;  border-right: 1px solid var(--line); }
#rightbar { grid-area: rightbar; border-left:  1px solid var(--line); }

.panel-title {
  padding: 8px 12px;
  font-size: 11px;
  letter-spacing: .8px;
  text-transform: uppercase;
  color: var(--muted);
  background: var(--panel-2);
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
}
.panel-body { padding: 12px; }
.panel-placeholder p { margin: 0 0 8px; }
.muted { color: var(--muted); }

/* ── Viewport ────────────────────────────────────────────── */
#viewport {
  grid-area: viewport;
  position: relative;
  overflow: hidden;
  background: #0e1116;
}
#scene-canvas { display: block; width: 100%; height: 100%; }

#viewport-hint {
  position: absolute;
  left: 12px;
  bottom: 12px;
  padding: 5px 10px;
  font-size: 11px;
  color: var(--muted);
  background: rgba(20, 23, 29, .6);
  border: 1px solid var(--line);
  border-radius: 6px;
  pointer-events: none;
  backdrop-filter: blur(2px);
}

/* ── Status bar ──────────────────────────────────────────── */
#statusbar {
  grid-area: status;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  font-size: 12px;
  color: var(--muted);
  background: var(--panel-2);
  border-top: 1px solid var(--line);
}
.sb-sep { width: 1px; height: 14px; background: var(--line); }
.sb-spacer { flex: 1; }
#status-fps { color: var(--text); min-width: 58px; text-align: right; }

/* ── Object Library ──────────────────────────────────────── */
.lib-cat {
  font-size: 10px; letter-spacing: .8px; text-transform: uppercase;
  color: var(--muted); margin: 10px 2px 6px;
}
.lib-cat:first-child { margin-top: 2px; }
.lib-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.lib-item {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 10px 6px; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; cursor: pointer;
  transition: background .12s, border-color .12s, transform .05s;
}
.lib-item:hover { background: #363c47; }
.lib-item:active { transform: translateY(1px); }
.lib-item.is-armed { border-color: var(--accent); background: var(--accent-dim); }
.lib-icon { font-size: 20px; line-height: 1; }
.lib-label { font-size: 11px; }

/* ── Inspector / fields ──────────────────────────────────── */
.field { display: grid; grid-template-columns: 84px 1fr; align-items: center; gap: 8px; margin-bottom: 8px; }
.field > label { color: var(--muted); font-size: 11px; }
.vec { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 4px; }
input.num, input.txt {
  width: 100%; background: #1b1e24; color: var(--text);
  border: 1px solid var(--line); border-radius: 5px; padding: 4px 6px; font-size: 12px;
}
input.num::-webkit-inner-spin-button { opacity: .4; }
input.color { width: 100%; height: 26px; background: none; border: 1px solid var(--line); border-radius: 5px; cursor: pointer; }
.ro { color: var(--muted); font-size: 11px; font-family: ui-monospace, monospace; }
.insp-actions { display: flex; gap: 6px; margin-top: 10px; }

.btn {
  flex: 1; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 6px; padding: 6px 10px;
  font-size: 12px; cursor: pointer; transition: background .12s;
}
.btn:hover { background: #363c47; }
.btn.is-active { background: var(--accent-dim); border-color: var(--accent); }
.btn.danger:hover { background: #5a2a2a; border-color: var(--danger); }

/* ── Scene Hierarchy ─────────────────────────────────────── */
.tree { display: flex; flex-direction: column; gap: 2px; }
.tree-row {
  display: flex; align-items: center; gap: 8px; padding: 5px 8px;
  border-radius: 6px; cursor: pointer; font-size: 12px;
}
.tree-row:hover { background: var(--panel-2); }
.tree-row.is-sel { background: var(--accent-dim); }
.tree-icon { width: 18px; text-align: center; }
.tree-name { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tree-lock { opacity: .8; }

/* ── Driver overlay ──────────────────────────────────────── */
#driver-overlay { position: absolute; inset: 0; pointer-events: none; }
#driver-overlay .dv-badge {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%);
  padding: 5px 14px; background: rgba(20,23,29,.65); border: 1px solid var(--line);
  border-radius: 999px; font-size: 12px; letter-spacing: .5px;
}
#exit-driver {
  position: absolute; top: 12px; right: 12px; flex: none;
  pointer-events: auto; background: rgba(20,23,29,.75);
}

/* ── Rearview mirror (top center) ────────────────────────── */
#rearview {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%);
  width: min(42%, 360px); aspect-ratio: 400 / 96;
  border-radius: 12px; overflow: hidden; pointer-events: none;
  border: 3px solid #14161a;
  box-shadow: 0 4px 14px rgba(0,0,0,.5), inset 0 0 0 1px rgba(255,255,255,.06);
  background: #0e1013;
}
#rearview canvas { width: 100%; height: 100%; display: block; }

/* ── Dashboard (bottom) ──────────────────────────────────── */
#dashboard {
  position: absolute; left: 0; right: 0; bottom: 0;
  height: 30%; min-height: 180px;
  display: flex; align-items: stretch; gap: 12px; padding: 12px 18px;
  pointer-events: auto;
  background: linear-gradient(#2b2f36, #141619 70%);
  border-top: 1px solid rgba(255,255,255,.09);
  box-shadow: 0 -6px 20px rgba(0,0,0,.45);
  border-top-left-radius: 18px; border-top-right-radius: 18px;
}
.dvmirror {
  flex: 0 0 20%; max-width: 220px; align-self: center; aspect-ratio: 240 / 150;
  border-radius: 10px; overflow: hidden; background: #0e1013;
  border: 3px solid #0c0e11; box-shadow: inset 0 0 0 1px rgba(255,255,255,.06);
}
.dvmirror.left  { clip-path: polygon(0 0, 100% 6%, 100% 94%, 0 100%); }
.dvmirror.right { clip-path: polygon(0 6%, 100% 0, 100% 100%, 0 94%); }
.dvmirror canvas { width: 100%; height: 100%; display: block; }

.dv-console { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; }
.dv-gauges { display: flex; align-items: center; gap: 14px; }
.speedo { width: 132px; height: 132px; filter: drop-shadow(0 2px 6px rgba(0,0,0,.5)); }
.sp-rim { fill: #14161a; stroke: #3a4350; stroke-width: 2; }
.sp-face { fill: #0e1013; }
.sp-tick { stroke: #8b95a2; stroke-width: 1.5; }
.sp-tick.major { stroke: #fff; stroke-width: 2.5; }
.sp-label, .sp-digital, .sp-unit { text-anchor: middle; fill: #cfd6df; font-family: system-ui, sans-serif; }
.sp-label { font-size: 12px; }
.sp-digital { font-size: 26px; font-weight: 700; fill: #fff; }
.sp-unit { font-size: 11px; fill: #8b93a3; }
.sp-needle { stroke: #ff4444; stroke-width: 3; stroke-linecap: round; }
.sp-hub { fill: #cfd6df; }

.turn { width: 42px; height: 42px; }
.turn polygon { fill: #29402c; transition: fill .1s; }
.turn { cursor: pointer; }
.turn.on polygon { fill: #37d24a; animation: blink .6s steps(1) infinite; }
@keyframes blink { 50% { fill: #14351a; } }

.dv-controls { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: center; }
.dv-speed-label { color: var(--muted); font-size: 11px; }
.sp-slider { width: 160px; accent-color: var(--accent); cursor: pointer; }
.dv-toggles { display: flex; gap: 4px; }
.dv-toggle {
  background: var(--panel-2); color: var(--muted); border: 1px solid var(--line);
  border-radius: 6px; padding: 4px 8px; font-size: 11px; cursor: pointer;
}
.dv-toggle.is-active { background: var(--accent-dim); border-color: var(--accent); color: var(--text); }

/* ── Toast ───────────────────────────────────────────────── */
#toast {
  position: fixed; bottom: 42px; left: 50%; transform: translateX(-50%) translateY(10px);
  background: #33383f; color: var(--text); border: 1px solid var(--line);
  padding: 8px 16px; border-radius: 8px; font-size: 12px; z-index: 50;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ── Collapsible panels ──────────────────────────────────── */
#app.hide-left  { grid-template-columns: 0 1fr var(--right-w); }
#app.hide-right { grid-template-columns: var(--left-w) 1fr 0; }
#app.hide-left.hide-right { grid-template-columns: 0 1fr 0; }
#app.hide-left  #library  { display: none; }
#app.hide-right #rightbar { display: none; }

/* ── Empty-state hint ────────────────────────────────────── */
#empty-hint {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  max-width: 320px; text-align: center; line-height: 1.6;
  padding: 14px 18px; color: var(--muted); font-size: 13px;
  background: rgba(20, 23, 29, .5); border: 1px solid var(--line);
  border-radius: 10px; pointer-events: none; backdrop-filter: blur(2px);
}
#empty-hint b { color: var(--text); }

/* ── Responsive ──────────────────────────────────────────── */
@media (max-width: 1024px) { :root { --left-w: 200px; --right-w: 232px; } }
@media (max-width: 860px)  { :root { --left-w: 176px; --right-w: 208px; } .lib-grid { grid-template-columns: 1fr; } }

/* Keep the cockpit usable on short / narrow viewports */
#dashboard { height: clamp(150px, 28%, 220px); }
@media (max-height: 620px) { #dashboard { min-height: 140px; } .speedo { width: 108px; height: 108px; } }
@media (max-width: 720px) {
  .dvmirror { flex-basis: 26%; }
  #rearview { width: min(52%, 300px); }
}
