:root {
  --bg: #10151c; --panel: #171e28; --panel2: #1e2733; --line: #2a3544; --text: #e6edf5; --muted: #8a9bb0;
  --accent: #4cc2ff; --accent-ink: #04202e; --danger: #ff6b6b; --ok: #4ade80; --warn: #fbbf24;
  --r: 12px;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #eef2f7; --panel: #fff; --panel2: #f1f5fa; --line: #d5dde8; --text: #14202e; --muted: #5d6f84;
    --accent: #0a84d0; --accent-ink: #fff; --danger: #d63b3b; --ok: #14a04c; --warn: #b7791f;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif; }
[hidden] { display: none !important; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--line); background: var(--panel2); border-radius: 10px; padding: 8px 12px; min-height: 40px; }
button:hover { border-color: var(--accent); }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
button.danger { color: var(--danger); }
input { background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; min-height: 40px; width: 100%; }
.muted { color: var(--muted); } .small { font-size: 13px; }
.error { color: var(--danger); min-height: 1.4em; margin: 0; }

.screen { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 24px; width: min(380px, 100%); display: grid; gap: 12px; }
.card h1 { margin: 0; font-size: 22px; }

#app { height: 100dvh; display: flex; flex-direction: column; }
header { display: flex; align-items: center; gap: 12px; padding: 8px 16px; padding-top: max(8px, env(safe-area-inset-top)); border-bottom: 1px solid var(--line); background: var(--panel); flex-wrap: wrap; }
.brand { font-weight: 700; letter-spacing: .2px; }
.seg { display: inline-flex; background: var(--panel2); border: 1px solid var(--line); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { border: 0; background: transparent; min-height: 34px; padding: 4px 14px; }
.seg button.active { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.status { margin-left: auto; display: flex; align-items: center; gap: 10px; min-width: 0; }
.chip { background: var(--panel2); border: 1px solid var(--line); padding: 3px 10px; border-radius: 999px; font-size: 13px; max-width: 46vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conn { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); display: inline-block; }
.conn.on .dot { background: var(--ok); } .conn.wait .dot { background: var(--warn); }

main { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 12px; padding: 12px 16px; padding-bottom: max(12px, env(safe-area-inset-bottom)); }
.stage-wrap { min-height: 0; display: flex; align-items: flex-start; }
.stage { position: relative; width: 100%; aspect-ratio: 16 / 9; max-height: 100%; background: #000; border-radius: var(--r); overflow: hidden; border: 1px solid var(--line); touch-action: none; user-select: none; -webkit-user-select: none; }
.stage.armed { cursor: crosshair; } .stage.orbit { cursor: grab; }
#main { width: 100%; height: 100%; display: block; }
#pip { position: absolute; right: 8px; bottom: 8px; width: 28%; max-width: 240px; border: 2px solid rgba(255,255,255,.7); border-radius: 8px; background: #000; pointer-events: none; }
.tools { position: absolute; left: 8px; top: 8px; display: flex; gap: 6px; flex-wrap: wrap; }
.tool { background: rgba(16,21,28,.72); color: #fff; border-color: rgba(255,255,255,.25); backdrop-filter: blur(4px); padding: 4px 10px; min-height: 34px; font-size: 13px; }
.tool[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.fps { position: absolute; left: 8px; bottom: 6px; font-size: 11px; color: rgba(255,255,255,.7); pointer-events: none; }
.hint { position: absolute; left: 50%; top: 52px; transform: translateX(-50%); background: rgba(16,21,28,.85); color: #fff; padding: 6px 12px; border-radius: 999px; font-size: 13px; pointer-events: none; text-align: center; max-width: 90%; }
.marker { position: absolute; width: 22px; height: 22px; margin: -11px 0 0 -11px; border: 3px solid var(--accent); border-radius: 50%; pointer-events: none; animation: ping .8s ease-out forwards; }
@keyframes ping { to { transform: scale(2); opacity: 0; } }
.overlay { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 12px; background: rgba(8,12,18,.78); color: #fff; text-align: center; padding: 16px; }
.overlay.gone { display: none; }

.side { min-height: 0; display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); overflow: hidden; }
.tabs { display: none; }
.panel { min-height: 0; padding: 12px; }
.chat { flex: 1; display: flex; flex-direction: column; gap: 8px; }
.log { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; padding-right: 2px; }
.msg { max-width: 88%; padding: 7px 11px; border-radius: 14px; background: var(--panel2); overflow-wrap: anywhere; white-space: pre-wrap; }
.msg.me { align-self: flex-end; background: var(--accent); color: var(--accent-ink); }
.msg.system { align-self: center; background: transparent; color: var(--muted); font-size: 13px; text-align: center; }
.msg.error { align-self: center; color: var(--danger); background: transparent; font-size: 13px; }
.chatform { display: flex; gap: 8px; }
.world { border-top: 1px solid var(--line); overflow-y: auto; max-height: 46%; }
.world h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); margin: 8px 0 8px; }
.kinds, .colors { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.kinds button.active { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
.sw { width: 34px; height: 34px; min-height: 34px; padding: 0; border-radius: 50%; border: 2px solid var(--line); }
.sw.active { outline: 3px solid var(--accent); outline-offset: 2px; }
.objlist { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.objlist li { display: flex; align-items: center; gap: 8px; background: var(--panel2); border-radius: 10px; padding: 4px 6px 4px 10px; }
.objlist li .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.objlist li .c { width: 12px; height: 12px; border-radius: 50%; border: 1px solid var(--line); flex: none; }
.objlist button { min-height: 32px; padding: 2px 10px; }
.objlist li.armed { outline: 2px solid var(--accent); }

@media (max-width: 860px) {
  main { grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr); padding: 8px 12px; gap: 8px; }
  .stage-wrap { display: block; }
  .tabs { display: flex; border-bottom: 1px solid var(--line); }
  .tabs button { flex: 1; border: 0; border-radius: 0; background: transparent; }
  .tabs button.active { background: var(--panel2); font-weight: 600; box-shadow: inset 0 -2px var(--accent); }
  .panel { display: none; } .panel.active { display: flex; }
  .world.active { display: block; max-height: none; border-top: 0; flex: 1; }
  .brand { display: none; }
  header { padding: 6px 12px; }
  #pip { width: 32%; }
}
