:root {
  --bg: #0f0d22;
  --panel: #1a1736;
  --panel-2: #241f4a;
  --line: #2f2a5c;
  --text: #f3f0ff;
  --muted: #a49ecf;
  --green: #52d68a;
  --red: #ff6b7a;
  --gold: #ffd27a;
  --accent: #8b6cff;
  --teal: #28c7b0;
  --pink: #ff8fb1;
  --radius: 14px;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.45 -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; }
body { min-height: 100vh; -webkit-tap-highlight-color: transparent; }
button { font: inherit; color: inherit; }
#app { max-width: 560px; margin: 0 auto; padding: 10px 12px calc(76px + env(safe-area-inset-bottom)); }

/* KPI board */
.board { background: linear-gradient(180deg, #1c1940, #161335); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px 12px; }
.board-top { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.board-title { font-size: 11px; letter-spacing: 1.2px; text-transform: uppercase; color: var(--muted); font-weight: 600; }
.chip { font-size: 10.5px; padding: 2px 8px; border-radius: 99px; background: var(--panel-2); color: var(--muted); font-weight: 600; letter-spacing: .4px; }
.chip.live-mode { background: rgba(82, 214, 138, .15); color: var(--green); }
.chip.sim-mode { background: rgba(255, 210, 122, .15); color: var(--gold); }
.chip.paused { background: rgba(255, 107, 122, .15); color: var(--red); }
.live { margin-left: auto; display: flex; align-items: center; gap: 6px; font-size: 11px; color: var(--green); font-variant-numeric: tabular-nums; }
.live i { width: 7px; height: 7px; border-radius: 50%; background: var(--green); box-shadow: 0 0 8px var(--green); animation: pulse 1.6s infinite; }
.kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px 8px; }
.kpi .k { font-size: 10.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi .v { font-size: 17px; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -.2px; }
.kpi .s { font-size: 10.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.pos { color: var(--green); }
.neg { color: var(--red); }

/* scene */
.tab { display: none; padding-top: 10px; }
.tab.active { display: block; }
.scene-wrap { position: relative; width: 100%; aspect-ratio: 4 / 3; border-radius: var(--radius); overflow: hidden; background: radial-gradient(120% 90% at 50% 20%, #2a2352, #14112c); border: 1px solid var(--line); }
#scene { position: absolute; inset: 0; width: 100%; height: 100%; }
#overlay { position: absolute; inset: 0; pointer-events: none; }
.hint { text-align: center; color: var(--muted); font-size: 12px; margin: 8px 0 0; }

.char { cursor: pointer; outline: none; }
.char .body { transform-box: fill-box; transform-origin: 50% 100%; animation: bob 3.2s ease-in-out infinite; }
.char:focus-visible .head circle:first-child { stroke: var(--gold); stroke-width: 3; }
.char.working .arm { transform-box: fill-box; transform-origin: 50% 0; animation: type .32s ease-in-out infinite alternate; }
.char.working .arm-r { animation-delay: .16s; }
.char.alert .body { animation: hop .55s ease-in-out infinite; }
.char.thinking .antenna { animation: blink .5s steps(2) infinite; }
.char .badge { fill: #8f89b8; }
.char.working .badge { fill: var(--green); }
.char.alert .badge { fill: var(--red); animation: pulse 1s infinite; }
.char .eye { transform-box: fill-box; transform-origin: center; animation: eyes 5s infinite; }
.led { animation: blink 2.4s infinite; }
.board-line { stroke-dasharray: 120; animation: draw 6s linear infinite; }
.neon { fill: var(--gold); filter: url(#glow); }
.zzz { fill: #cfc8f2; animation: zzz 3s ease-in-out infinite; }
.clock-hand { transform-box: view-box; transform-origin: 0 0; }

.bubble { position: absolute; transform: translate(-50%, -100%); max-width: 46%; background: rgba(20, 17, 44, .94); border: 1px solid var(--line); border-radius: 10px; padding: 5px 8px; font-size: 11px; line-height: 1.35; color: var(--text); box-shadow: 0 6px 18px rgba(0, 0, 0, .35); opacity: 0; transition: opacity .35s ease, transform .35s ease; }
.bubble.show { opacity: 1; transform: translate(-50%, calc(-100% - 4px)); }
.bubble b { display: block; font-size: 9.5px; letter-spacing: .8px; text-transform: uppercase; color: var(--gold); }
.bubble::after { content: ""; position: absolute; left: 50%; bottom: -5px; width: 8px; height: 8px; background: inherit; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); transform: translateX(-50%) rotate(45deg); }
.name { position: absolute; transform: translate(-50%, 2px); font-size: 9.5px; font-weight: 700; padding: 1px 7px; border-radius: 99px; background: rgba(15, 13, 34, .75); color: var(--text); white-space: nowrap; }

/* team list */
.team { display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; }
.member { display: flex; align-items: center; gap: 10px; width: 100%; min-width: 0; text-align: left; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px; cursor: pointer; }
.member .av { flex: 0 0 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; font-weight: 800; color: #14122b; }
.member .txt { flex: 1; min-width: 0; }
.member .top { display: flex; align-items: center; gap: 6px; font-weight: 700; }
.member .msg-line { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pill { font-size: 10px; font-weight: 700; padding: 1px 7px; border-radius: 99px; }
.pill.idle { background: var(--panel-2); color: var(--muted); }
.pill.working { background: rgba(82, 214, 138, .15); color: var(--green); }
.pill.alert { background: rgba(255, 107, 122, .15); color: var(--red); }
.member .chev { color: var(--muted); font-size: 18px; }

/* cards */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; margin-bottom: 10px; }
.card h3 { margin: 0; font-size: 15px; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.card h3 a { color: var(--text); text-decoration: none; }
.tag { font-size: 10.5px; font-weight: 600; padding: 2px 7px; border-radius: 99px; background: var(--panel-2); color: var(--muted); }
.tag.in { background: rgba(82, 214, 138, .14); color: var(--green); }
.tag.below { background: rgba(255, 107, 122, .14); color: var(--red); }
.tag.above { background: rgba(255, 210, 122, .14); color: var(--gold); }
.row { display: flex; justify-content: space-between; gap: 8px; margin-top: 8px; font-variant-numeric: tabular-nums; }
.row .big { font-size: 22px; font-weight: 800; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.range { position: relative; height: 34px; margin: 12px 2px 4px; }
.range .track { position: absolute; left: 0; right: 0; top: 14px; height: 6px; border-radius: 3px; background: var(--panel-2); }
.range .zone { position: absolute; top: 14px; height: 6px; border-radius: 3px; background: linear-gradient(90deg, #5d4fd1, var(--accent)); }
.range .mark { position: absolute; top: 9px; width: 2px; height: 16px; background: var(--red); }
.range .mark.fast { background: #b3263a; }
.range .mark span { position: absolute; top: 17px; left: 50%; transform: translateX(-50%); font-size: 9px; color: var(--muted); white-space: nowrap; }
.range .dot { position: absolute; top: 10px; width: 14px; height: 14px; margin-left: -7px; border-radius: 50%; background: var(--text); border: 3px solid var(--bg); box-shadow: 0 0 0 2px var(--text); transition: left .6s ease; }
.spark { width: 100%; height: 44px; margin-top: 6px; }
.actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.btn { flex: 1; min-height: 40px; border: 0; border-radius: 10px; padding: 9px 12px; background: var(--panel-2); font-weight: 600; cursor: pointer; }
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); color: #fff; }
.btn.danger { background: rgba(255, 107, 122, .16); color: var(--red); }
.btn.ok { background: rgba(82, 214, 138, .16); color: var(--green); }
.btn:disabled { opacity: .5; }
.empty { text-align: center; color: var(--muted); padding: 32px 12px; }
.empty svg { width: 44px; height: 44px; fill: var(--panel-2); margin-bottom: 6px; }

.feed { list-style: none; margin: 0; padding: 0; }
.feed li { display: flex; gap: 10px; padding: 10px 2px; border-bottom: 1px solid var(--line); }
.feed .av { flex: 0 0 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; font-size: 11px; font-weight: 800; color: #14122b; }
.feed .t { font-size: 11px; color: var(--muted); }

.kv { display: grid; grid-template-columns: 1fr auto; gap: 6px 10px; font-variant-numeric: tabular-nums; }
.kv .k { color: var(--muted); }
.addr { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; word-break: break-all; }

details.costs { margin-top: 10px; background: var(--panel-2); border-radius: 10px; padding: 8px 10px; }
details.costs summary { cursor: pointer; font-size: 12px; color: var(--muted); font-weight: 600; }
details.costs .kv { margin-top: 8px; }

/* history */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px 8px; }
.stats .kpi .v { font-size: 16px; }
.cmp { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; font-size: 12.5px; }
.cmp th { text-align: right; color: var(--muted); font-weight: 600; font-size: 11px; padding: 4px 0 6px; }
.cmp th:first-child, .cmp td:first-child { text-align: left; }
.cmp td { padding: 6px 0; border-top: 1px solid var(--line); text-align: right; }
.hist-row { display: flex; justify-content: space-between; gap: 10px; padding: 10px 2px; border-bottom: 1px solid var(--line); }
.hist-row .l { min-width: 0; }
.hist-row .l div:first-child { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hist-row .r { text-align: right; font-variant-numeric: tabular-nums; }
.tag.trail { background: rgba(255, 210, 122, .14); color: var(--gold); }

/* tab bar */
.tabs { position: fixed; left: 0; right: 0; bottom: 0; display: grid; grid-template-columns: repeat(5, 1fr); background: rgba(15, 13, 34, .96); border-top: 1px solid var(--line); padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); backdrop-filter: blur(8px); z-index: 5; }
.tabs button { border: 0; background: none; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 0; border-radius: 10px; color: var(--muted); font-size: 11px; cursor: pointer; }
.tabs button svg { width: 22px; height: 22px; fill: currentColor; stroke: currentColor; }
.tabs button.active { color: var(--text); background: var(--panel); }

/* bottom sheet */
.sheet { position: fixed; inset: 0; z-index: 10; }
.sheet[hidden] { display: none; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(5, 4, 15, .55); animation: fade .2s ease; }
.sheet-panel { position: absolute; left: 0; right: 0; bottom: 0; max-width: 560px; margin: 0 auto; max-height: 86vh; overflow: auto; background: var(--panel); border-radius: 18px 18px 0 0; border: 1px solid var(--line); padding: 8px 14px calc(16px + env(safe-area-inset-bottom)); animation: up .25s ease; }
.sheet-handle { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 10px; cursor: pointer; }
.agent-head { display: flex; align-items: center; gap: 10px; }
.agent-head .av { width: 42px; height: 42px; border-radius: 50%; display: grid; place-items: center; font-weight: 800; font-size: 16px; color: #14122b; }
.agent-head h2 { margin: 0; font-size: 18px; }
.quote { background: var(--panel-2); border-radius: 12px; padding: 10px 12px; margin: 12px 0; font-size: 13px; }
.section-title { font-size: 11px; letter-spacing: 1px; text-transform: uppercase; color: var(--muted); margin: 14px 0 6px; font-weight: 700; }
.chat { display: flex; flex-direction: column; gap: 8px; max-height: 34vh; overflow: auto; padding: 4px 0; }
.msg { max-width: 85%; padding: 8px 11px; border-radius: 14px; font-size: 13px; white-space: pre-wrap; }
.msg.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.msg.agent { align-self: flex-start; background: var(--panel-2); border-bottom-left-radius: 4px; }
.msg.typing { color: var(--muted); }
.chat-form { display: flex; gap: 8px; margin-top: 8px; }
.chat-form input { flex: 1; min-height: 42px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); color: var(--text); padding: 0 12px; font: inherit; }
.chat-form input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.chips button { border: 1px solid var(--line); background: none; border-radius: 99px; padding: 5px 10px; font-size: 12px; color: var(--muted); cursor: pointer; }
pre.report { white-space: pre-wrap; font: 12.5px/1.5 inherit; background: var(--panel-2); padding: 10px; border-radius: 10px; margin: 0; }

#toast { position: fixed; left: 50%; bottom: calc(86px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px); background: #2b2659; color: var(--text); padding: 9px 14px; border-radius: 12px; font-size: 13px; opacity: 0; transition: .25s ease; pointer-events: none; z-index: 20; max-width: 90vw; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.gate { position: fixed; inset: 0; background: var(--bg); display: grid; place-content: center; text-align: center; padding: 24px; z-index: 30; }
.gate[hidden] { display: none; }

@keyframes bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-1.6px); } }
@keyframes hop { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-5px); } }
@keyframes type { from { transform: rotate(-8deg); } to { transform: rotate(10deg); } }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .35; } }
@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: .2; } }
@keyframes eyes { 0%, 92%, 100% { transform: scaleY(1); } 95% { transform: scaleY(.1); } }
@keyframes draw { from { stroke-dashoffset: 120; } to { stroke-dashoffset: -120; } }
@keyframes zzz { 0% { opacity: 0; transform: translate(0, 0); } 30% { opacity: 1; } 100% { opacity: 0; transform: translate(4px, -10px); } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes up { from { transform: translateY(30px); opacity: .6; } to { transform: none; opacity: 1; } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }

a.btn { color: var(--text); text-decoration: none; text-align: center; display: inline-flex; align-items: center; justify-content: center; }
