:root {
  --bg: #efe9df;
  --panel: #f6f2eb;
  --card: #fffdf9;
  --line: #e4dccf;
  --line-2: #d6ccbc;
  --ink: #26221c;
  --ink-2: #5d564b;
  --mute: #8d8577;
  --unit: #2552c8;
  --unit-soft: rgba(37, 82, 200, 0.1);
  --gold: #c99a2e;
  --radius: 12px;
  --shadow: 0 1px 0 rgba(255, 255, 255, 0.8) inset, 0 1px 2px rgba(60, 45, 20, 0.06), 0 6px 18px -10px rgba(60, 45, 20, 0.18);
  --font: "Inter", "SF Pro Text", -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --panel-w: 368px;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink); font-family: var(--font); -webkit-font-smoothing: antialiased; }
body { overflow: hidden; }
button, select, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--unit); outline-offset: 2px; }

.app { display: grid; grid-template-columns: minmax(0, 1fr) var(--panel-w); height: 100vh; height: 100dvh; }

/* ---------------------------------------------------------------- viewport */
.viewport { position: relative; overflow: hidden; min-width: 0; background: #e9e2d6; }
#scene { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; outline: none; }
#scene.dragging { cursor: grabbing; }

.viewport { --hud: #2a2620; --hud-2: rgba(42, 38, 32, 0.62); --hud-bg: rgba(255, 253, 249, 0.72); --hud-line: rgba(42, 38, 32, 0.12); }
.viewport[data-ui="dark"] { --hud: #f2eee8; --hud-2: rgba(242, 238, 232, 0.66); --hud-bg: rgba(20, 20, 24, 0.55); --hud-line: rgba(255, 255, 255, 0.14); }

.plate { position: absolute; left: 22px; top: 18px; pointer-events: none; color: var(--hud); transition: color .6s; }
.plate-code { font: 700 12px/1 var(--mono); letter-spacing: .16em; color: var(--unit); display: inline-flex; align-items: center; gap: 8px; }
.plate-code::before { content: ""; width: 18px; height: 3px; background: var(--unit); border-radius: 2px; }
.plate-name { font-size: clamp(22px, 2.6vw, 34px); font-weight: 750; letter-spacing: -0.02em; margin-top: 6px; line-height: 1.05; }
.plate-meta { display: flex; align-items: center; gap: 8px; margin-top: 6px; font-size: 12.5px; color: var(--hud-2); font-weight: 550; letter-spacing: .02em; }
.plate-meta .dot { width: 3px; height: 3px; border-radius: 50%; background: currentColor; }
.plate.swap .plate-name { animation: plateIn .55s cubic-bezier(.2, .8, .2, 1); }
@keyframes plateIn { from { opacity: 0; transform: translateY(6px); filter: blur(3px); } }

.hud-tr { position: absolute; right: 16px; top: 16px; display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; pointer-events: none; }
.chip { display: inline-flex; align-items: center; gap: 7px; height: 28px; padding: 0 11px; border-radius: 999px; background: var(--hud-bg); border: 1px solid var(--hud-line); color: var(--hud); font-size: 12px; font-weight: 600; backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); transition: background .6s, color .6s; }
.chip-k { font: 600 10px/1 var(--mono); letter-spacing: .12em; text-transform: uppercase; color: var(--hud-2); }
.chip.flash { animation: chipFlash .7s ease-out; }
@keyframes chipFlash { 0% { box-shadow: 0 0 0 0 var(--unit); } 100% { box-shadow: 0 0 0 8px transparent; } }
.compass svg { width: 16px; height: 16px; }
.compass circle { fill: none; stroke: var(--hud-2); stroke-width: 1.4; }
.compass path { fill: var(--unit); transform-origin: 10px 10px; }
#chipHeading { font-family: var(--mono); min-width: 3.2ch; text-align: right; }

.toast { position: absolute; left: 50%; top: 18px; transform: translate(-50%, -8px); opacity: 0; pointer-events: none; display: flex; align-items: center; gap: 10px; padding: 8px 14px 8px 10px; border-radius: 10px; background: #26221c; color: #f7f3ec; font-size: 13px; font-weight: 550; box-shadow: 0 10px 30px -12px rgba(0, 0, 0, .5); transition: opacity .25s, transform .35s cubic-bezier(.2, .8, .2, 1); white-space: nowrap; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast .slot { font: 700 10px/1 var(--mono); letter-spacing: .14em; text-transform: uppercase; background: var(--unit); color: #fff; padding: 5px 7px; border-radius: 6px; }
.toast .delta { font-family: var(--mono); font-size: 11.5px; color: #cfc6b7; }

.cam-dock { position: absolute; left: 50%; bottom: 18px; transform: translateX(-50%); display: flex; gap: 8px; align-items: center; padding: 6px; border-radius: 14px; background: rgba(255, 253, 249, 0.86); border: 1px solid rgba(42, 38, 32, 0.1); box-shadow: 0 12px 30px -16px rgba(40, 30, 10, .45); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); max-width: calc(100% - 24px); }
.seg { display: inline-flex; background: #efe9df; border: 1px solid var(--line); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; height: 34px; padding: 0 12px; border-radius: 7px; font-size: 13px; font-weight: 600; color: var(--ink-2); display: inline-flex; align-items: center; gap: 7px; transition: background .2s, color .2s, box-shadow .2s; white-space: nowrap; }
.seg button:hover { color: var(--ink); background: rgba(255, 255, 255, .6); }
.seg button.on { background: var(--card); color: var(--ink); box-shadow: 0 1px 2px rgba(60, 45, 20, .14), 0 0 0 1px rgba(60, 45, 20, .06); }
.seg button.on .kbd { background: var(--unit); color: #fff; border-color: transparent; }
.kbd { font: 700 10px/1 var(--mono); min-width: 17px; height: 17px; display: inline-grid; place-items: center; border-radius: 4px; border: 1px solid var(--line-2); color: var(--mute); background: #fff; }
.pad { display: inline-flex; gap: 3px; align-items: center; }
.pad button { width: 36px; height: 36px; border-radius: 9px; border: 1px solid var(--line); background: var(--card); display: grid; place-items: center; color: var(--ink-2); transition: background .15s, transform .1s, color .15s; touch-action: none; -webkit-user-select: none; user-select: none; }
.pad button:hover { color: var(--ink); border-color: var(--line-2); }
.pad button.active, .pad button:active { background: var(--unit); color: #fff; border-color: var(--unit); transform: scale(.96); }
.pad button[aria-pressed="true"] { background: var(--unit-soft); color: var(--unit); border-color: var(--unit); }
.pad svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.pad-sep { width: 1px; height: 22px; background: var(--line-2); margin: 0 3px; }

.keyhint { position: absolute; left: 22px; bottom: 22px; display: flex; flex-direction: column; gap: 3px; font-size: 11px; color: var(--hud-2); pointer-events: none; transition: color .6s; }
.keyhint b { font: 600 10.5px var(--mono); color: var(--hud); margin-right: 5px; letter-spacing: .02em; }

/* ---------------------------------------------------------------- panel */
.panel { background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto; overflow-x: hidden; padding: 14px 14px 18px; display: flex; flex-direction: column; gap: 10px; scrollbar-width: thin; scrollbar-color: var(--line-2) transparent; overscroll-behavior: contain; }
.panel-head { display: flex; align-items: center; gap: 11px; padding: 4px 4px 8px; }
.mark { width: 34px; height: 34px; border-radius: 9px; background: #26221c; display: grid; place-items: center; flex: none; }
.mark svg { width: 20px; height: 20px; fill: none; stroke: #f3c53a; stroke-width: 2.6; stroke-linejoin: round; stroke-linecap: round; }
.title { min-width: 0; flex: 1; }
.title h1 { margin: 0; font-size: 15.5px; font-weight: 750; letter-spacing: -0.01em; line-height: 1.2; }
.title p { margin: 2px 0 0; font-size: 11.5px; color: var(--mute); }
.live { display: inline-flex; align-items: center; gap: 6px; font: 600 10.5px var(--mono); color: var(--ink-2); padding: 5px 8px; border-radius: 7px; background: var(--card); border: 1px solid var(--line); flex: none; }
.live-dot { width: 7px; height: 7px; border-radius: 50%; background: #2fae62; box-shadow: 0 0 0 0 rgba(47, 174, 98, .6); animation: live 1.6s infinite; }
@keyframes live { 70% { box-shadow: 0 0 0 6px rgba(47, 174, 98, 0); } 100% { box-shadow: 0 0 0 0 rgba(47, 174, 98, 0); } }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; box-shadow: var(--shadow); }
.card-h { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; min-height: 22px; }
.card-h h2 { margin: 0; font: 700 10.5px/1 var(--mono); letter-spacing: .16em; text-transform: uppercase; color: var(--ink-2); }
.card-sub { font: 600 10.5px var(--mono); color: var(--mute); }
.card-actions { display: flex; gap: 4px; }
.ghost { border: 1px solid var(--line); background: transparent; height: 26px; padding: 0 9px; border-radius: 7px; font-size: 11.5px; font-weight: 600; color: var(--ink-2); }
.ghost:hover { background: #f5f0e7; color: var(--ink); }

/* roster */
.roster { display: grid; gap: 6px; }
.unit { display: grid; grid-template-columns: 38px 1fr auto; align-items: center; gap: 10px; text-align: left; width: 100%; padding: 8px 10px 8px 8px; border-radius: 10px; border: 1px solid var(--line); background: #fbf8f3; transition: border-color .2s, background .2s, box-shadow .2s; position: relative; }
.unit:hover { border-color: var(--line-2); background: #fff; }
.unit[aria-pressed="true"] { border-color: var(--u); background: #fff; box-shadow: 0 0 0 3px color-mix(in srgb, var(--u) 14%, transparent); }
.unit-badge { width: 38px; height: 38px; border-radius: 9px; display: grid; place-items: center; background: #f1ece3; border: 1px solid var(--line); position: relative; overflow: hidden; }
.unit-badge svg { width: 30px; height: 30px; }
.unit-txt { min-width: 0; }
.unit-code, .unit-name, .unit-frame { display: block; }
.unit-code { font: 700 10px/1 var(--mono); letter-spacing: .12em; color: var(--u); }
.unit-name { font-size: 13.5px; font-weight: 700; margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.unit-frame { font-size: 11px; color: var(--mute); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.unit-sw { display: flex; gap: 3px; }
.unit-sw i { width: 9px; height: 18px; border-radius: 3px; border: 1px solid rgba(0, 0, 0, .08); }
.unit-specs { display: none; grid-column: 1 / -1; gap: 10px; font: 600 10.5px var(--mono); color: var(--ink-2); padding-top: 6px; margin-top: 2px; border-top: 1px dashed var(--line); }
.unit[aria-pressed="true"] .unit-specs { display: flex; }
.unit-specs span b { color: var(--mute); font-weight: 600; margin-right: 4px; }

/* fields */
.field { display: grid; gap: 6px; }
.field + .field { margin-top: 10px; }
.field label, .lbl { font-size: 11.5px; font-weight: 650; color: var(--ink-2); }
.sel-row { display: grid; grid-template-columns: 30px 1fr 30px; gap: 4px; align-items: center; }
.sel { position: relative; }
.sel::after { content: ""; position: absolute; right: 11px; top: 50%; width: 7px; height: 7px; border-right: 1.6px solid var(--ink-2); border-bottom: 1.6px solid var(--ink-2); transform: translateY(-70%) rotate(45deg); pointer-events: none; }
select { width: 100%; height: 34px; appearance: none; -webkit-appearance: none; border: 1px solid var(--line-2); background: #fff; border-radius: 8px; padding: 0 30px 0 10px; font-size: 13px; font-weight: 600; transition: border-color .2s, box-shadow .2s; }
select:hover { border-color: #c5b9a5; }
select:focus-visible { outline: none; border-color: var(--unit); box-shadow: 0 0 0 3px var(--unit-soft); }
.step { height: 34px; border: 1px solid var(--line); background: #f8f4ee; border-radius: 8px; font-size: 17px; line-height: 1; color: var(--ink-2); font-weight: 600; }
.step:hover { background: #fff; color: var(--ink); }
.step:active { transform: scale(.95); }
.hint { margin: 0; font-size: 11.5px; color: var(--mute); line-height: 1.4; min-height: 1.4em; }

/* loadout rows */
.loadout { display: grid; gap: 6px; }
.slot-row { display: grid; grid-template-columns: 62px 1fr; gap: 8px; align-items: center; padding: 6px 6px 6px 8px; border-radius: 9px; border: 1px solid transparent; background: #faf7f1; transition: background .3s, border-color .3s; position: relative; }
.slot-row.pulse { animation: rowPulse .9s ease-out; }
@keyframes rowPulse { 0% { background: color-mix(in srgb, var(--unit) 16%, #fff); border-color: var(--unit); } 100% { background: #faf7f1; border-color: transparent; } }
.slot-l { display: grid; gap: 2px; }
.slot-name { font-size: 12px; font-weight: 700; }
.slot-count { font: 600 10px var(--mono); color: var(--mute); }
.slot-row .sel-row { grid-template-columns: 28px 1fr 28px; }
.slot-row .step { height: 32px; }
.slot-row select { height: 32px; font-size: 12.5px; }

/* segmented small & toggles */
.seg.small { width: 100%; }
.seg.small button { flex: 1; justify-content: center; height: 30px; font-size: 12px; padding: 0 8px; }
.toggles { display: flex; gap: 14px; margin-top: 12px; flex-wrap: wrap; }
.tog { display: inline-flex; align-items: center; gap: 8px; font-size: 12px; font-weight: 600; color: var(--ink-2); cursor: pointer; min-height: 28px; }
.tog input { position: absolute; opacity: 0; pointer-events: none; }
.sw { width: 30px; height: 18px; border-radius: 99px; background: #ddd4c6; position: relative; transition: background .2s; flex: none; }
.sw::after { content: ""; position: absolute; left: 2px; top: 2px; width: 14px; height: 14px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0, 0, 0, .2); transition: transform .2s cubic-bezier(.3, .8, .3, 1); }
.tog input:checked + .sw { background: var(--unit); }
.tog input:checked + .sw::after { transform: translateX(12px); }
.tog input:focus-visible + .sw { outline: 2px solid var(--unit); outline-offset: 2px; }

/* stats */
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.stat { background: #faf7f1; border: 1px solid #efe8dc; border-radius: 8px; padding: 7px 9px; display: grid; gap: 3px; }
.stat .k { font-size: 10.5px; color: var(--mute); font-weight: 600; }
.stat .v { font: 700 16px/1.1 var(--mono); font-variant-numeric: tabular-nums; letter-spacing: -0.02em; transition: color .3s; }
.stat .v.bump { color: var(--unit); }
.spark { width: 100%; height: 36px; margin-top: 8px; display: block; border-radius: 6px; background: #faf7f1; border: 1px solid #efe8dc; }
.build-id { display: flex; gap: 8px; align-items: center; margin-top: 8px; font-size: 10.5px; color: var(--mute); font-weight: 600; }
.build-id code { font: 600 10.5px var(--mono); color: var(--ink-2); background: #f3eee6; padding: 3px 6px; border-radius: 5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.log { list-style: none; margin: 8px 0 0; padding: 0; display: grid; gap: 3px; }
.log li { display: grid; grid-template-columns: 54px 60px 1fr auto; gap: 6px; align-items: baseline; font: 500 10.5px var(--mono); color: var(--ink-2); padding: 3px 2px; border-top: 1px dashed #efe8dc; }
.log li:first-child { animation: logIn .4s ease-out; }
.log li .t { color: var(--mute); }
.log li .s { font-weight: 700; color: var(--unit); text-transform: uppercase; letter-spacing: .08em; }
.log li .d { color: var(--mute); }
@keyframes logIn { from { opacity: 0; transform: translateX(-4px); } }

.panel-foot { font-size: 10.5px; color: var(--mute); text-align: center; padding: 4px 0 0; }
.nojs { position: fixed; inset: 0; display: grid; place-items: center; background: var(--bg); margin: 0; }

/* ---------------------------------------------------------------- responsive */
@media (max-width: 1100px) {
  :root { --panel-w: 340px; }
  .keyhint { display: none; }
}

@media (max-width: 860px) {
  body { overflow: auto; }
  .app { display: block; height: auto; }
  .viewport { position: sticky; top: 0; z-index: 2; height: 60vh; height: 60svh; min-height: 300px; box-shadow: 0 8px 20px -14px rgba(40, 30, 10, .45); }
  .panel { border-left: 0; overflow: visible; padding: 12px 12px calc(20px + env(safe-area-inset-bottom)); display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); align-items: start; }
  .panel-head, .panel-foot { grid-column: 1 / -1; }
  .plate { left: 14px; top: 12px; }
  .plate-name { font-size: 21px; }
  .plate-meta { font-size: 11.5px; }
  .hud-tr { right: 10px; top: 10px; }
  .hud-tr #chipLight, .hud-tr #chipPose { display: none; }
  .toast { top: auto; bottom: 118px; font-size: 12px; }
  .cam-dock { bottom: 10px; flex-direction: column; gap: 6px; padding: 6px; width: calc(100% - 20px); max-width: 520px; }
  .cam-dock .seg { width: 100%; }
  .cam-dock .seg button { flex: 1; justify-content: center; padding: 0 6px; height: 40px; }
  .cam-dock .kbd { display: none; }
  .pad { width: 100%; justify-content: space-between; }
  .pad button { width: auto; flex: 1; height: 40px; }
  .step, select { height: 44px !important; }
  .slot-row .sel-row, .sel-row { grid-template-columns: 44px 1fr 44px; }
  .seg.small button { height: 40px; }
  .ghost { height: 40px; padding: 0 14px; }
  .tog { min-height: 40px; }
  .unit { padding: 10px; }
}

@media (max-width: 860px) and (max-height: 520px) and (orientation: landscape) {
  body { overflow: hidden; }
  .app { display: grid; grid-template-columns: minmax(0, 1fr) 300px; height: 100dvh; }
  .viewport { position: relative; height: 100%; min-height: 0; }
  .panel { display: flex; overflow-y: auto; }
  .cam-dock { flex-direction: row; width: auto; }
  .cam-dock .seg { width: auto; }
  .pad { width: auto; }
  .pad button { width: 36px; flex: none; }
  .toast { bottom: 64px; }
}

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