:root {
    --bg: #101014;
    --bg-2: #14141a;
    --panel: #18181f;
    --panel-2: #1e1e27;
    --line: #2a2a36;
    --line-strong: #3a3a4a;
    --fg: #f3f3f7;
    --muted: #9a9aab;
    --muted-2: #6f6f80;
    --accent: #7c6af7;
    --accent-2: #9b8cff;
    --accent-ink: #0e0e14;
    --accent-soft: rgba(124, 106, 247, 0.18);
    --ok: #3ddc97;
    --danger: #ff6b81;
    --warn: #f5c14a;
    --radius: 10px;
    --font: "Segoe UI", Inter, system-ui, sans-serif;
    --mono: "Cascadia Code", "JetBrains Mono", ui-monospace, monospace;
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.35);
}

html[data-theme="light"] {
    --bg: #ececf2;
    --bg-2: #f6f6fa;
    --panel: #ffffff;
    --panel-2: #f0f0f6;
    --line: #d8d8e4;
    --line-strong: #c4c4d4;
    --fg: #16161e;
    --muted: #5c5c6e;
    --muted-2: #808094;
    --accent-ink: #ffffff;
    --shadow: 0 8px 24px rgba(40, 30, 80, 0.08);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
    background: var(--bg);
    color: var(--fg);
    font-family: var(--font);
    font-size: 13px;
    display: grid;
    grid-template-rows: 52px 1fr 28px;
    overflow: hidden;
}

.topbar, .footer, .rail, .stage-tabs, .stage-toolbar, .timeline-head {
    border-color: var(--line);
}

.topbar {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 0 14px;
    border-bottom: 1px solid var(--line);
    background: var(--panel);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 210px; }
.brand-mark {
    width: 28px; height: 28px; border-radius: 6px;
    background:
        linear-gradient(#c08a5a 0 8px, #5a3d2b 8px 12px, #c08a5a 12px 20px, #5a3d2b 20px) 6px 4px / 16px 20px no-repeat,
        linear-gradient(var(--accent), #5b4ad6);
}
.brand-copy { display: flex; flex-direction: column; line-height: 1.15; }
.brand-copy strong { font-size: 13px; }
.brand-copy span { font-size: 11px; color: var(--muted); }
.top-center { display: flex; align-items: center; gap: 8px; flex: 1; }
.save-state { font-size: 12px; color: var(--ok); }
.save-state.dirty { color: var(--warn); }
.top-actions { display: flex; align-items: center; gap: 6px; }

.workspace {
    display: grid;
    grid-template-columns: 250px minmax(0, 1fr) 276px;
    min-height: 0;
}

.rail {
    background: var(--panel);
    border-right: 1px solid var(--line);
    padding: 12px;
    overflow: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.rail-right { border-right: none; border-left: 1px solid var(--line); }
.rail h3, .rail-title, .elements-panel h3 {
    margin: 0;
    font-size: 11px;
    letter-spacing: 0.6px;
    text-transform: uppercase;
    color: var(--muted);
}
.project-name { display: flex; flex-direction: column; gap: 6px; font-size: 11px; color: var(--muted); }
.add-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.add-grid button, .presets button, .example, .library-item, .saved-item {
    background: var(--panel-2);
    border: 1px solid var(--line);
    color: var(--fg);
    border-radius: 8px;
    padding: 8px;
    cursor: pointer;
    font: inherit;
}
.add-grid button:hover, .presets button:hover, .example:hover, .library-item:hover { border-color: var(--accent); }

.quick-heads, .presets, .library-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
}
.quick-heads canvas, .example canvas, .library-item canvas {
    width: 100%;
    image-rendering: pixelated;
    background: repeating-conic-gradient(#2a2a36 0 25%, #18181f 0 50%) 0 0 / 8px 8px;
    border-radius: 4px;
    display: block;
}
.example, .library-item { display: flex; flex-direction: column; gap: 4px; align-items: center; font-size: 11px; }
.saved-list { display: flex; flex-direction: column; gap: 6px; }
.saved-item { display: flex; justify-content: space-between; align-items: center; font-size: 12px; }
.saved-item .x { border: none; background: none; color: var(--danger); cursor: pointer; }

.stage {
    min-width: 0;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) 210px;
    background: var(--bg-2);
}
.stage-tabs, .stage-toolbar, .timeline-head {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
}
.tab {
    background: none; border: none; color: var(--muted);
    font: inherit; font-weight: 700; padding: 6px 10px; cursor: pointer; border-radius: 6px;
}
.tab.active { color: var(--fg); background: var(--accent-soft); }
.view { min-height: 0; overflow: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.view.hidden, .hidden { display: none !important; }
.canvas-stack { display: flex; flex-direction: column; gap: 10px; min-height: 0; flex: 1; }

.tag-strip {
    min-height: 84px;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 14px;
    overflow-x: auto;
    background:
        linear-gradient(var(--line) 1px, transparent 1px) 0 0 / 16px 16px,
        linear-gradient(90deg, var(--line) 1px, transparent 1px) 0 0 / 16px 16px,
        var(--bg);
    border: 1px solid var(--line);
    border-radius: var(--radius);
}
.chip {
    display: flex; flex-direction: column; align-items: center; gap: 4px;
    padding: 6px; border-radius: 8px; border: 1px solid transparent;
    background: rgba(20, 20, 28, 0.72); cursor: pointer; min-width: 52px;
}
.chip.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.chip canvas { image-rendering: pixelated; }
.chip .label { font-size: 10px; color: var(--muted); max-width: 90px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip-text, .chip-symbol, .chip-name {
    font-weight: 700; padding: 8px 10px; white-space: nowrap;
}
.chip-spacer { width: 18px; height: 36px; border-left: 1px dashed var(--muted); border-right: 1px dashed var(--muted); }

.inspector {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 12px;
    min-height: 220px;
}
.inspector-head { display: flex; gap: 16px; flex-wrap: wrap; }
.pixel-wrap { display: flex; justify-content: center; padding: 12px; background: var(--bg); border-radius: 8px; }
#grid {
    image-rendering: pixelated;
    background: repeating-conic-gradient(#2a2a36 0 25%, #14141a 0 50%) 0 0 / 16px 16px;
    cursor: crosshair; border-radius: 4px;
}
.tool-row { display: flex; gap: 6px; flex-wrap: wrap; }
.empty-hint { color: var(--muted); max-width: 520px; margin: 24px auto; text-align: center; }

.stage-bottom {
    display: grid;
    grid-template-columns: 220px 1fr;
    border-top: 1px solid var(--line);
    background: var(--panel);
    min-height: 0;
}
.elements-panel { border-right: 1px solid var(--line); padding: 8px; overflow: auto; }
.elements { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; }
.el-row {
    display: grid; grid-template-columns: 18px 1fr auto; gap: 6px; align-items: center;
    padding: 6px 8px; border-radius: 6px; cursor: pointer; border: 1px solid transparent;
}
.el-row.selected { background: var(--accent-soft); border-color: var(--accent); }
.el-row.hidden-el { opacity: 0.4; }
.eye { border: none; background: none; color: var(--muted); cursor: pointer; }

.timeline-panel { display: flex; flex-direction: column; min-width: 0; }
.timeline { flex: 1; overflow: auto; padding: 8px 12px 12px; display: flex; flex-direction: column; gap: 6px; }
.track { display: grid; grid-template-columns: 90px 1fr; gap: 8px; align-items: center; }
.track-name { font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.track-bar {
    position: relative; height: 22px; background: var(--panel-2);
    border: 1px solid var(--line); border-radius: 4px; overflow: hidden;
}
.frame-block {
    position: absolute; top: 1px; bottom: 1px; background: var(--accent);
    color: var(--accent-ink); font-size: 10px; font-weight: 700;
    display: grid; place-items: center; border-radius: 3px;
}
.playhead {
    position: absolute; top: 0; bottom: 0; width: 2px; background: #fff; pointer-events: none;
}

.preview-box {
    background: repeating-conic-gradient(#2a2a36 0 25%, #14141a 0 50%) 0 0 / 12px 12px;
    border: 1px solid var(--line); border-radius: 8px; padding: 10px;
}
#preview { width: 100%; height: 72px; image-rendering: pixelated; display: block; }
.code-block {
    background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
    padding: 8px; font-family: var(--mono); font-size: 11px; white-space: pre-wrap; word-break: break-all;
    min-height: 64px; max-height: 120px; overflow: auto; margin: 0;
}
#code-out {
    width: 100%; min-height: 220px; background: var(--bg); color: var(--fg);
    border: 1px solid var(--line); border-radius: 8px; padding: 12px;
    font-family: var(--mono); font-size: 12px; resize: vertical;
}
.server-row { display: flex; justify-content: space-between; align-items: center; }
.send-card { margin-top: auto; }

.btn, .icon-btn, .seg-btn {
    font: inherit; cursor: pointer; border-radius: 8px;
}
.btn {
    background: var(--panel-2); border: 1px solid var(--line-strong);
    color: var(--fg); padding: 7px 10px; font-weight: 650;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn-accent { background: var(--accent); border-color: transparent; color: var(--accent-ink); }
.btn-block { width: 100%; }
.btn-caret { padding: 7px 8px; }
.icon-btn {
    width: 32px; height: 32px; border: 1px solid var(--line);
    background: var(--panel-2); color: var(--fg);
}
.seg { display: inline-flex; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 3px; gap: 3px; }
.seg.full { display: flex; }
.seg-btn { flex: 1; border: none; background: transparent; color: var(--muted); padding: 6px 10px; font-weight: 700; }
.seg-btn.active { background: var(--accent); color: var(--accent-ink); }

input[type=text], input[type=number], input[type=search], select, textarea {
    background: var(--panel-2); border: 1px solid var(--line); color: var(--fg);
    border-radius: 8px; padding: 8px 10px; font: inherit; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--muted); }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.hint { color: var(--muted); font-size: 11px; margin: 0; }
.hint code { font-family: var(--mono); color: var(--accent-2); }
.resbadge, .time-label { font-size: 11px; color: var(--muted); margin-left: auto; }
.spacer { flex: 1; }
.split { position: relative; display: flex; }
.menu {
    position: absolute; top: 110%; right: 0; background: var(--panel);
    border: 1px solid var(--line); border-radius: 8px; min-width: 160px;
    box-shadow: var(--shadow); z-index: 30; padding: 4px;
}
.menu button { display: block; width: 100%; text-align: left; background: none; border: none; color: var(--fg); padding: 8px 10px; cursor: pointer; border-radius: 6px; }
.menu button:hover { background: var(--accent-soft); }

.status {
    font-size: 11px; font-weight: 700; color: var(--muted);
    display: inline-flex; align-items: center; gap: 6px;
}
.status::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--muted-2); }
.status.online { color: var(--ok); }
.status.online::before { background: var(--ok); }
.player-label { color: var(--muted); font-size: 12px; margin: 0; }
.log { min-height: 16px; font-size: 12px; color: var(--muted); white-space: pre-wrap; }
.log.ok { color: var(--ok); }
.log.err { color: var(--danger); }
.swatches { display: flex; flex-wrap: wrap; gap: 4px; }
.swatch { width: 18px; height: 18px; border-radius: 4px; border: 1px solid var(--line-strong); cursor: pointer; padding: 0; }
.symbol-grid { display: flex; flex-wrap: wrap; gap: 4px; }
.symbol-grid button { width: 32px; height: 32px; }

.footer {
    display: flex; align-items: center; justify-content: space-between;
    padding: 0 14px; border-top: 1px solid var(--line);
    background: var(--panel); color: var(--muted); font-size: 11px;
}

.settings-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; max-width: 720px; }

@media (max-width: 1180px) {
    .workspace { grid-template-columns: 220px minmax(0, 1fr); }
    .rail-right { display: none; }
}
@media (max-width: 820px) {
    body { overflow: auto; height: auto; grid-template-rows: auto auto auto; }
    .workspace { grid-template-columns: 1fr; }
    .stage { grid-template-rows: auto auto auto; }
    .stage-bottom { grid-template-columns: 1fr; }
}
