:root {
    --bg-app: #17181b;
    --bg-panel: #1f2125;
    --bg-raised: #272a2f;
    --bg-hover: #30343a;
    --border: #33363c;
    --text-main: #e4e6ea;
    --text-muted: #9097a3;
    --accent: #2f7cf6;
    --accent-hover: #1f68dc;
    --danger: #e5484d;
    --radius: 7px;
    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
    font-family: var(--font);
    background: var(--bg-app);
    color: var(--text-main);
    font-size: 13px;
    display: grid;
    grid-template-rows: 50px 1fr;
    overflow: hidden;
}

svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- top bar ---------- */
.topbar {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 0 14px;
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border);
    min-width: 0;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 14px; white-space: nowrap; }
.brand-logo { height: 24px; width: auto; display: block; }
.btn-group { display: flex; gap: 4px; }
.spacer { flex: 1; }

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    height: 30px; padding: 0 12px;
    background: var(--bg-raised); color: var(--text-main);
    border: 1px solid var(--border); border-radius: var(--radius);
    font: inherit; font-weight: 550; cursor: pointer; white-space: nowrap;
    transition: background 0.12s, border-color 0.12s;
}
.btn:hover:not(:disabled) { background: var(--bg-hover); }
.btn[hidden] { display: none; }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover:not(:disabled) { background: var(--bg-hover); }
.btn.icon { width: 30px; padding: 0; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn.danger { color: #ff8a8d; }
.btn.danger:hover { background: rgba(229, 72, 77, 0.15); border-color: rgba(229, 72, 77, 0.4); }
.btn.wide { flex: 1; }

.segmented { display: flex; background: var(--bg-app); border: 1px solid var(--border); border-radius: var(--radius); padding: 2px; }
.segmented button {
    background: transparent; border: 0; color: var(--text-muted); font: inherit; font-weight: 600;
    padding: 5px 14px; border-radius: 5px; cursor: pointer;
}
.segmented button.active { background: var(--bg-hover); color: var(--text-main); }

/* ---------- workspace ---------- */
.workspace {
    display: grid;
    grid-template-columns: 64px 1fr 300px;
    min-height: 0;
}

.toolbar {
    display: flex; flex-direction: column; align-items: center; gap: 4px;
    padding: 10px 0;
    background: var(--bg-panel);
    border-right: 1px solid var(--border);
}
.tool {
    width: 52px; height: 50px;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    background: transparent; border: 1px solid transparent; border-radius: var(--radius);
    color: var(--text-muted); cursor: pointer; font: inherit; font-size: 10px; font-weight: 600;
}
.tool:hover { background: var(--bg-hover); color: var(--text-main); }
.tool.active { background: rgba(47, 124, 246, 0.16); border-color: rgba(47, 124, 246, 0.5); color: #8fb8ff; }
.tool-sep { width: 32px; height: 1px; background: var(--border); margin: 6px 0; }

/* ---------- views ---------- */
.views { display: flex; min-width: 0; min-height: 0; }
.pane { position: relative; flex: 1; min-width: 0; min-height: 0; overflow: hidden; }
.views[data-mode="2d"] .pane-3d, .views[data-mode="3d"] .pane-2d { display: none; }
.views[data-mode="split"] .pane-2d { border-right: 1px solid var(--border); }

#planCanvas { display: block; touch-action: none; }
#view3d { position: absolute; inset: 0; }
#view3d canvas { display: block; touch-action: none; }

.statusbar {
    position: absolute; left: 12px; right: 90px; top: 10px;
    color: #59606c; font-size: 12px; pointer-events: none;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.zoom-controls { position: absolute; right: 12px; bottom: 12px; display: flex; flex-direction: column; gap: 4px; }
.zoom-controls button {
    width: 30px; height: 30px; border-radius: var(--radius); border: 1px solid #d6d3cc;
    background: #fff; color: #333; font-size: 16px; cursor: pointer; box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}
.zoom-controls button:hover { background: #f0efec; }

.view-brand {
    position: absolute; right: 12px; bottom: 12px;
    display: flex; align-items: center;
    padding: 5px 10px 5px 5px; border-radius: 14px;
    background: rgba(20, 22, 26, 0.5); color: #fff;
    font-weight: 650; font-size: 12px; pointer-events: none; user-select: none;
}
.view-brand img { height: 18px; width: auto; display: block; }
.view-brand.light { right: 54px; background: rgba(255, 255, 255, 0.85); color: #2d3139; border: 1px solid #e2dfd8; }
.overlay-3d { position: absolute; top: 10px; left: 10px; right: 10px; display: flex; flex-wrap: wrap; gap: 6px; pointer-events: none; }
.chip {
    pointer-events: auto;
    height: 28px; padding: 0 11px; border-radius: 14px;
    background: rgba(255, 255, 255, 0.92); color: #23262b;
    border: 1px solid rgba(0, 0, 0, 0.08); font: inherit; font-weight: 600; font-size: 12px; cursor: pointer;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}
.chip:hover { background: #fff; }
.chip.on { background: #23262b; color: #fff; }
.chip.accent { background: var(--accent); color: #fff; border-color: var(--accent); }
.hint-3d {
    position: absolute; bottom: 14px; left: 12px; max-width: calc(100% - 170px);
    overflow: hidden; text-overflow: ellipsis;
    background: rgba(20, 22, 26, 0.55); color: #fff; font-size: 11.5px; padding: 5px 10px; border-radius: 12px;
    pointer-events: none; white-space: nowrap;
}

/* ---------- sidebar ---------- */
.sidebar {
    background: var(--bg-panel);
    border-left: 1px solid var(--border);
    display: flex; flex-direction: column; min-height: 0;
}
.tabs { display: flex; border-bottom: 1px solid var(--border); }
.tabs button {
    flex: 1; padding: 12px 0; background: transparent; border: 0; border-bottom: 2px solid transparent;
    color: var(--text-muted); font: inherit; font-weight: 600; cursor: pointer;
}
.tabs button.active { color: var(--text-main); border-bottom-color: var(--accent); }
.tab-body { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.tab-body[hidden] { display: none; }

.tab-body h3 { font-size: 15px; font-weight: 650; margin-bottom: 2px; }
.tab-body h4 { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-muted); margin-top: 10px; }

.field { display: flex; flex-direction: column; gap: 5px; }
.field > span { color: var(--text-muted); font-size: 12px; }
.field input[type="number"], .field input[type="text"], .field select {
    width: 100%; height: 32px; padding: 0 10px;
    background: var(--bg-app); color: var(--text-main);
    border: 1px solid var(--border); border-radius: var(--radius); font: inherit; outline: none;
}
.field input:focus, .field select:focus { border-color: var(--accent); }
.field input[type="color"] { width: 100%; height: 32px; padding: 2px; background: var(--bg-app); border: 1px solid var(--border); border-radius: var(--radius); cursor: pointer; }
.field input[type="range"] { width: 100%; accent-color: var(--accent); }
.input-unit { position: relative; }
.input-unit em { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); font-style: normal; color: var(--text-muted); font-size: 11px; pointer-events: none; }
.input-unit input { padding-right: 36px !important; }

.check { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.check input { accent-color: var(--accent); width: 15px; height: 15px; }

.btn-row { display: flex; gap: 6px; flex-wrap: wrap; }
.btn-row .btn { flex: 1; }

.hint { color: var(--text-muted); font-size: 12px; line-height: 1.5; }
.error { color: #ff8a8d; }
.center { text-align: center; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.stat { background: var(--bg-raised); border-radius: var(--radius); padding: 8px; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.stat span { color: var(--text-muted); font-size: 11px; }
.stat strong { font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.room-list { display: flex; flex-direction: column; gap: 2px; }
.room-row {
    display: flex; align-items: center; gap: 8px; padding: 7px 8px; border-radius: 6px;
    background: transparent; border: 0; color: var(--text-main); font: inherit; cursor: pointer; text-align: left;
}
.room-row:hover { background: var(--bg-hover); }
.room-row i { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.room-row span { flex: 1; }
.room-row em { font-style: normal; color: var(--text-muted); font-size: 12px; }

.shortcuts { display: grid; grid-template-columns: 1fr 1fr; gap: 5px 10px; color: var(--text-muted); font-size: 12px; }
.shortcuts div { display: flex; align-items: center; gap: 6px; }
kbd { font-family: inherit; font-size: 10.5px; background: var(--bg-raised); border: 1px solid var(--border); border-bottom-width: 2px; padding: 1px 5px; border-radius: 4px; color: var(--text-main); min-width: 20px; text-align: center; }

.lib-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.lib-item {
    display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 4px 7px;
    background: var(--bg-raised); border: 1px solid var(--border); border-radius: var(--radius);
    color: var(--text-main); font: inherit; font-size: 12px; font-weight: 550; cursor: pointer;
}
.lib-item:hover { border-color: #4a4f57; background: var(--bg-hover); }
.lib-item.active { border-color: var(--accent); background: rgba(47, 124, 246, 0.14); }
.lib-item canvas { width: 100%; max-width: 128px; height: auto; aspect-ratio: 128 / 88; background: #f7f6f3; border-radius: 4px; }
.lib-item em { font-style: normal; color: var(--text-muted); font-size: 10.5px; }

/* ---------- toast & dialog ---------- */
.toast {
    position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
    background: #2b2e34; color: #fff; border: 1px solid #3d4148; padding: 10px 16px; border-radius: 10px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35); opacity: 0; pointer-events: none;
    transition: opacity 0.2s, transform 0.2s; max-width: min(560px, calc(100vw - 32px)); text-align: center; z-index: 50;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

dialog {
    margin: auto; width: min(860px, calc(100vw - 32px)); max-height: calc(100vh - 48px);
    background: var(--bg-panel); color: var(--text-main); border: 1px solid var(--border); border-radius: 12px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.55); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.dialog-head h3 { font-size: 15px; }
.dialog-body { padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.dialog-body pre, .dialog-body code { font-family: ui-monospace, Menlo, monospace; font-size: 12px; background: var(--bg-app); border-radius: 5px; }
.dialog-body pre { padding: 10px 12px; }
.dialog-body code { padding: 1px 5px; }
.render-img { max-width: 100%; max-height: calc(100vh - 220px); object-fit: contain; border-radius: 8px; display: block; margin: 0 auto; }
.spinner {
    width: 36px; height: 36px; margin: 20px auto 0; border-radius: 50%;
    border: 3px solid var(--border); border-top-color: var(--accent); animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- narrow screens ---------- */
@media (max-width: 1100px) {
    .workspace { grid-template-columns: 60px 1fr 260px; }
    .brand { display: none; }
}
@media (max-width: 860px) {
    body { grid-template-rows: auto 1fr; }
    .topbar { flex-wrap: wrap; padding: 8px 12px; row-gap: 6px; }
    .workspace { grid-template-columns: 56px 1fr; grid-template-rows: 1fr 40%; }
    .sidebar { grid-column: 1 / -1; border-left: 0; border-top: 1px solid var(--border); }
    .views[data-mode="split"] { flex-direction: column; }
    .views[data-mode="split"] .pane-2d { border-right: 0; border-bottom: 1px solid var(--border); }
    .tool { width: 46px; height: 44px; }
    .hint-3d { display: none; }
}

/* ---------- plan export ---------- */
dialog.wide { width: min(1180px, calc(100vw - 32px)); }
.export-layout { display: grid; grid-template-columns: 1fr 320px; gap: 0; min-height: 0; }
.export-preview { background: #2a2c31; padding: 20px; display: flex; align-items: center; justify-content: center; min-height: 420px; }
.export-preview img { max-width: 100%; max-height: calc(100vh - 170px); box-shadow: 0 6px 24px rgba(0,0,0,0.4); background: #fff; }
.export-options { padding: 16px; display: flex; flex-direction: column; gap: 10px; overflow-y: auto; max-height: calc(100vh - 110px); }
.export-options .two { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.export-options .checks { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; padding: 4px 0; }
.export-actions .btn { flex: 1; }
.export-actions .btn.primary { flex: 2; }
@media (max-width: 860px) {
    .export-layout { grid-template-columns: 1fr; }
    .export-preview { min-height: 240px; }
}

/* ---------- walkthrough HUD ---------- */
body.walking .overlay-3d, body.walking .hint-3d, body.walking .view-brand { display: none; }
.walk-hud { position: absolute; inset: 0; pointer-events: none; z-index: 5; }
.walk-hud[hidden] { display: none; }
.walk-top { position: absolute; top: 12px; left: 12px; right: 12px; display: flex; gap: 10px; align-items: flex-start; }
.walk-rooms { display: flex; flex-wrap: wrap; gap: 6px; flex: 1; }
.walk-rooms button, .walk-exit {
    pointer-events: auto; height: 30px; padding: 0 12px; border-radius: 15px; border: 1px solid rgba(255,255,255,0.18);
    background: rgba(18, 20, 24, 0.62); color: #fff; font: 600 12px var(--font); cursor: pointer;
    backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.walk-rooms button:hover, .walk-exit:hover { background: rgba(47, 124, 246, 0.85); }
.walk-exit kbd { background: rgba(255,255,255,0.15); border-color: transparent; color: #fff; margin-left: 4px; }
.walk-map {
    position: absolute; right: 12px; bottom: 12px; width: 180px; height: 180px; pointer-events: auto; cursor: pointer;
    background: rgba(18, 20, 24, 0.55); border-radius: 12px; border: 1px solid rgba(255,255,255,0.15);
    backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.walk-help {
    position: absolute; left: 50%; bottom: 14px; transform: translateX(-50%); max-width: calc(100% - 240px);
    background: rgba(18, 20, 24, 0.55); color: #fff; font-size: 12px; padding: 6px 12px; border-radius: 14px; text-align: center;
    animation: walkHelp 9s forwards;
}
@keyframes walkHelp { 0%, 80% { opacity: 1; } 100% { opacity: 0; } }
.walk-stick { position: absolute; width: 100px; height: 100px; border-radius: 50%; border: 2px solid rgba(255,255,255,0.5); background: rgba(0,0,0,0.15); display: none; }
.walk-stick.on { display: block; }
.walk-stick i { position: absolute; left: 30px; top: 30px; width: 40px; height: 40px; border-radius: 50%; background: rgba(255,255,255,0.8); }
.walk-fade { position: absolute; inset: 0; background: #000; opacity: 0; transition: opacity 0.2s; }
.walk-fade.on { opacity: 1; }
@media (max-width: 860px) {
    .walk-map { width: 120px; height: 120px; }
    .walk-help { display: none; }
}

.notice { background: rgba(245, 159, 0, 0.12); border: 1px solid rgba(245, 159, 0, 0.35); color: #ffd58a; padding: 10px 12px; border-radius: 8px; font-size: 12.5px; line-height: 1.5; }
