/* DVD Rebar — bản web thử nghiệm. Sáng / tối theo hệ thống. */
:root {
    color-scheme: light dark;
    --bg: #f3f4f6;
    --panel: #ffffff;
    --panel2: #f8f9fb;
    --line: #dde1e7;
    --text: #1d2330;
    --muted: #697386;
    --accent: #1f6feb;
    --accent-ink: #ffffff;
    --hover: #eef3fb;
    --sel: #dbe8fd;
    --bad: #c62828;
    --bad-bg: #fdecec;
    --ok: #1b7f3b;
    --paper: #ffffff;
    --ink: #111111;
    --rebar: #d00000;
    --hatch: #808080;
    --input: #ffffff;
    --shadow: 0 1px 2px rgba(0, 0, 0, .06);
    --font: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #14171c;
        --panel: #1c2027;
        --panel2: #222731;
        --line: #303643;
        --text: #e5e8ee;
        --muted: #9aa3b2;
        --accent: #4c8dff;
        --accent-ink: #0b1220;
        --hover: #262c38;
        --sel: #1f3358;
        --bad: #ff6b6b;
        --bad-bg: #3a1f22;
        --ok: #4cc27a;
        --paper: #101318;
        --ink: #d8dde6;
        --rebar: #ff5a5a;
        --hatch: #8a93a3;
        --input: #161a20;
        --shadow: none;
    }
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font: 13px/1.4 var(--font);
}

button, input, select { font: inherit; color: inherit; }

button {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 6px;
    padding: 4px 10px;
    cursor: pointer;
}
button:hover:not(:disabled) { background: var(--hover); }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
button.danger { color: var(--bad); }
button.mini { padding: 0 7px; min-width: 24px; height: 22px; line-height: 20px; }
button.lnk { border: none; background: none; padding: 0; color: var(--accent); }

input[type=text], select {
    width: 100%;
    background: var(--input);
    border: 1px solid var(--line);
    border-radius: 5px;
    padding: 3px 6px;
    min-height: 26px;
}
input[type=text]:focus, select:focus { outline: 2px solid color-mix(in srgb, var(--accent) 45%, transparent); outline-offset: -1px; }
input:disabled, select:disabled { opacity: .55; }
input.num { text-align: right; font-variant-numeric: tabular-nums; }
input.bar { font-family: Consolas, "Cascadia Mono", monospace; }

/* ------------------------------------------------------------------ bố cục */
.app {
    display: grid;
    grid-template-columns: 220px minmax(0, 1fr) 380px;
    grid-template-rows: 44px minmax(0, 1fr);
    grid-template-areas: "top top top" "left center right";
    height: 100vh;
}
.top {
    grid-area: top;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 12px;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    min-width: 0;
}
.brand { font-weight: 700; font-size: 15px; white-space: nowrap; }
.brand .logo { background: var(--accent); color: var(--accent-ink); border-radius: 4px; padding: 1px 5px; margin-right: 2px; }
.brand small { font-weight: 400; color: var(--muted); font-size: 12px; margin-left: 4px; }
.ptitle { max-width: 220px; }
.top .ptitle { width: 220px; }
.tools { display: flex; gap: 6px; }
.msg { flex: 1; min-width: 0; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: right; }
.msg.bad { color: var(--bad); }

.left, .right, .center { min-height: 0; overflow: auto; }
.left { grid-area: left; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 8px 6px; gap: 6px; }
.right { grid-area: right; background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column; }
.center { grid-area: center; display: flex; flex-direction: column; overflow: hidden; }

/* ------------------------------------------------------------------ danh sách cấu kiện */
.grp-head { display: flex; align-items: center; gap: 6px; font-weight: 600; padding: 4px 6px; color: var(--muted); text-transform: uppercase; font-size: 11px; letter-spacing: .04em; }
.grp-head .cnt { background: var(--panel2); border: 1px solid var(--line); border-radius: 9px; padding: 0 6px; font-weight: 500; }
.grp-head button { margin-left: auto; }
.mem {
    display: flex; align-items: center; gap: 6px; width: 100%; text-align: left; border: none; border-radius: 5px; background: none;
    padding: 4px 10px; margin: 1px 0;
}
.mem.on { background: var(--sel); font-weight: 600; }
.mem .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mem.dis { color: var(--muted); cursor: default; }
.mem small { color: var(--muted); }
.dot { color: var(--accent); font-size: 10px; }
.none { color: var(--muted); padding: 2px 10px; font-size: 12px; }
.acts { display: flex; gap: 4px; padding: 6px 4px; border-top: 1px solid var(--line); flex-wrap: wrap; }
.foot { margin-top: auto; color: var(--muted); font-size: 11px; padding: 6px 4px; border-top: 1px solid var(--line); }

/* ------------------------------------------------------------------ giữa */
.ctabs { display: flex; align-items: center; gap: 4px; padding: 6px 10px; background: var(--panel); border-bottom: 1px solid var(--line); }
.ctabs > button:not(.mini) { border-color: transparent; background: none; }
.ctabs > button.on { background: var(--sel); border-color: var(--sel); font-weight: 600; }
.sp { flex: 1; }
.empty { margin: auto; color: var(--muted); padding: 24px; text-align: center; }
.empty.bad { color: var(--bad); }
.status { padding: 4px 10px; color: var(--muted); font-size: 12px; border-top: 1px solid var(--line); background: var(--panel); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.svgbox { flex: 1; min-height: 0; background: var(--paper); position: relative; overflow: hidden; cursor: grab; touch-action: none; user-select: none; }
.svgbox.dragging { cursor: grabbing; }
.svgbox > svg { width: 100%; height: 100%; display: block; }
/* nét in bằng 1 px dù phóng to / nhỏ; màu theo chủ đề */
.svgbox svg polyline, .svgbox svg polygon, .svgbox svg path, .svgbox svg circle, .svgbox svg line { vector-effect: non-scaling-stroke; stroke-width: 1px; }
.svgbox svg [stroke="#000000"] { stroke: var(--ink); }
.svgbox svg [fill="#000000"] { fill: var(--ink); }
.svgbox svg [stroke="#d00000"] { stroke: var(--rebar); stroke-width: 1.8px; }
.svgbox svg [fill="#d00000"] { fill: var(--rebar); }
.svgbox svg [fill="#808080"] { fill: var(--hatch); }
.svgbox svg text { font-family: var(--font); }

/* ------------------------------------------------------------------ thống kê */
.tk-bar { display: flex; align-items: center; gap: 14px; padding: 6px 10px; background: var(--panel2); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.tk-bar label { display: inline-flex; align-items: center; gap: 4px; }
.tk-wrap { flex: 1; overflow: auto; padding: 10px; }
.tk-wrap .svgbox.table { height: 360px; flex: none; border: 1px solid var(--line); border-radius: 6px; margin-bottom: 10px; }
.tk-side { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; margin-top: 12px; align-items: start; }
.tk-side > div { background: var(--panel); border: 1px solid var(--line); border-radius: 7px; padding: 8px 10px; }
table.tk { border-collapse: collapse; width: 100%; background: var(--panel); box-shadow: var(--shadow); font-variant-numeric: tabular-nums; font-size: 12px; }
table.tk th, table.tk td { border: 1px solid var(--line); padding: 2px 5px; vertical-align: middle; }
table.tk td.nw { white-space: nowrap; }
table.tk th { font-size: 11px; }
table.tk th { background: var(--panel2); font-weight: 600; text-align: left; white-space: nowrap; }
table.tk td.n { text-align: right; white-space: nowrap; }
table.tk td.c { text-align: center; }
table.tk tr.sub td { background: var(--panel2); color: var(--muted); }
table.tk tfoot td { font-weight: 700; background: var(--panel2); }
table.tk.small { font-size: 12px; }
table.tk td.ok { color: var(--ok); font-weight: 600; }
table.tk td.bad { color: var(--bad); font-weight: 600; }
.sk { display: block; margin: auto; color: var(--rebar); }
.tk-side h4 { margin: 0 0 6px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.note { font-size: 12px; margin: 2px 0; }
.note.muted { color: var(--muted); }
.ck { padding: 14px 16px; overflow: auto; }
.ck table th { width: 170px; }

/* ------------------------------------------------------------------ bảng thông số */
.rbar { display: flex; align-items: center; gap: 6px; padding: 8px 10px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); z-index: 2; }
.rbar .rt { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.valid { margin: 8px 10px 0; padding: 6px 8px; border-radius: 6px; background: var(--bad-bg); color: var(--bad); }
.sheet { padding: 6px 8px 24px; }
.ps-empty { color: var(--muted); padding: 6px; font-size: 12px; }
.ps-tabs { display: flex; flex-wrap: wrap; gap: 2px; margin: 4px 0 6px; }
.ps-tab { border-radius: 5px 5px 0 0; border-bottom: none; background: var(--panel2); padding: 3px 9px; }
.ps-tab.on { background: var(--sel); font-weight: 600; }
.ps-card { border: 1px solid var(--line); border-radius: 7px; margin: 6px 0; background: var(--panel); box-shadow: var(--shadow); }
.ps-card.sub { box-shadow: none; margin: 4px 0; }
.ps-head { display: flex; align-items: center; gap: 4px; padding: 4px 6px; background: var(--panel2); border-bottom: 1px solid var(--line); border-radius: 7px 7px 0 0; }
.ps-fold { border: none; background: none; padding: 0 4px; color: var(--muted); }
.ps-title { flex: 1; font-weight: 600; }
.ps-body { padding: 4px 6px; }
.ps-row { display: grid; grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr) 34px; align-items: center; gap: 2px 6px; padding: 2px 0; }
.ps-row.off .ps-label { color: var(--muted); }
.ps-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ps-unit { color: var(--muted); font-size: 11px; }
.ps-err { grid-column: 1 / -1; color: var(--bad); font-size: 12px; }
.ps-row.bad input { border-color: var(--bad); }
.ps-opt { display: flex; align-items: center; gap: 4px; }
.ps-list { display: flex; align-items: center; gap: 6px; }
.ps-nest { margin: 2px 0 6px 10px; padding-left: 6px; border-left: 2px solid var(--line); }
.ps-item-head { display: flex; align-items: center; justify-content: space-between; color: var(--muted); font-size: 12px; margin-top: 4px; }

.switch { position: relative; display: inline-block; width: 32px; height: 18px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch span { position: absolute; inset: 0; background: var(--line); border-radius: 9px; transition: background .15s; cursor: pointer; }
.switch span::before { content: ""; position: absolute; width: 14px; height: 14px; left: 2px; top: 2px; background: #fff; border-radius: 50%; transition: transform .15s; }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::before { transform: translateX(14px); }
.switch input:disabled + span { opacity: .45; cursor: default; }
.switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ------------------------------------------------------------------ khởi động / lỗi */
.boot { height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; }
.boot-title { font-size: 22px; font-weight: 700; }
.boot-sub { color: var(--muted); }
#blazor-error-ui { display: none; position: fixed; bottom: 0; left: 0; right: 0; padding: 8px 14px; background: var(--bad-bg); color: var(--bad); z-index: 100; }
#blazor-error-ui .dismiss { float: right; cursor: pointer; }

/* màn hẹp: bảng thông số xuống dưới */
@media (max-width: 1100px) {
    .app { grid-template-columns: 190px minmax(0, 1fr) 320px; }
}
@media (max-width: 760px) {
    .app { grid-template-columns: 1fr; grid-template-rows: auto auto minmax(360px, 1fr) auto; grid-template-areas: "top" "left" "center" "right"; height: auto; min-height: 100vh; }
    .top { flex-wrap: wrap; padding: 6px 16px; height: auto; }
    .left { max-height: 220px; }
    .center { min-height: 420px; }
}

/* ------------------------------------------------------------------ chế độ nhúng trong DVDCAD (?nhung=dvdcad): bảng hẹp ~420–520 px */
.app.embed {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(240px, 46vh) auto;
    grid-template-areas: "top" "center" "right";
    height: auto; min-height: 100%;
}
.app.embed .left { display: none; }
.app.embed .top { flex-wrap: wrap; gap: 6px; padding: 6px 8px; height: auto; }
.app.embed .brand { font-size: 13px; }
.app.embed .emsel { flex: 1; min-width: 90px; }
.app.embed .eminsert { white-space: nowrap; }
.app.embed .msg { flex-basis: 100%; text-align: left; white-space: normal; font-size: 12px; }
.app.embed .center { min-height: 240px; }
.app.embed .ctabs { flex-wrap: wrap; }
.app.embed .right { border-left: none; border-top: 1px solid var(--line); overflow: visible; }
.shell.embed { height: auto; min-height: 100vh; }
.shell-body > .app.embed { height: auto; }
.shell.embed .shell-body { overflow: visible; }

/* ------------------------------------------------------------------ vòng 4: thẻ Mặt bằng (Components/PlanEditor.razor) */
.pe { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr) minmax(300px, 420px); }
.pe-plan { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); }
.pe-bar { display: flex; align-items: center; gap: 8px; padding: 5px 10px; background: var(--panel2); border-bottom: 1px solid var(--line); }
.pe-bar .sp { flex: 1; }
.pe-bar .muted { font-size: 12px; }
.pe-svg { flex: 1; min-height: 200px; }
.pe-svg svg.plan text { font-family: var(--font); }
.pe-side { overflow: auto; padding: 8px; display: flex; flex-direction: column; gap: 8px; background: var(--panel); }
.pe-card { border: 1px solid var(--line); border-radius: 7px; padding: 6px 8px; background: var(--panel2); }
.pe-card h4 { margin: 2px 0 6px; font-size: 13px; display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
.pe-card h4 small { font-weight: 400; font-size: 11px; }
.pe-tab { width: 100%; border-collapse: collapse; font-size: 12px; }
.pe-tab th { text-align: left; font-weight: 500; color: var(--muted); padding: 2px 3px; white-space: nowrap; }
.pe-tab td { padding: 1px 2px; }
.pe-tab td.c, .pe-tab td.n { text-align: center; color: var(--muted); }
.pe-tab input, .pe-tab select { width: 100%; min-width: 0; min-height: 24px; padding: 1px 4px; background: var(--input); border: 1px solid var(--line); border-radius: 4px; }
.pe-tab input.nm { width: 54px; }
.pe-acts { display: flex; align-items: center; gap: 5px; flex-wrap: wrap; margin-top: 6px; font-size: 12px; }
.pe-acts select.sm { width: auto; min-width: 48px; }
.pe-acts label { display: inline-flex; align-items: center; gap: 4px; }
.pe-acts input.nm { width: 48px; }
.pe-gen { margin-top: 8px; font-size: 12px; color: var(--muted); display: flex; flex-direction: column; gap: 3px; }
.pe-msg { margin-top: 5px; font-size: 12px; color: var(--ok); }
.pe-msg.bad { color: var(--bad); }
.pe-hint { font-size: 11px; margin: 0; }
/* vòng 5: mặt bằng tương tác */
.pe-svg { outline: none; }
.pe-svg svg.plan g.pm { cursor: move; }
.pe-svg svg.plan g.pm.fixed { cursor: pointer; }
.pe-svg svg.plan g.pm.dragging { opacity: .75; }
.pe-svg svg.plan g.snapmk { pointer-events: none; }
.pe-svg:focus-visible { box-shadow: inset 0 0 0 2px var(--accent, #1f6feb); }
.pe-plan .status { padding: 3px 10px; font-size: 11px; color: var(--muted); border-top: 1px solid var(--line); }
.pe-tab tr.on td { background: color-mix(in srgb, #ff8f00 18%, transparent); }
.pe-tab td.pick { cursor: pointer; text-decoration: underline dotted; }
.pe-sup { margin-top: 8px; padding-top: 6px; border-top: 1px dashed var(--line); }
.pe-prev th { width: 26%; }
.pe-prev td { font-size: 12px; color: var(--text); padding: 2px 4px; }
.pe-prev tr.chg td:last-child { color: #c25e00; font-weight: 600; }
.pe-srcs { font-size: 11px; margin-top: 4px; }
.pe-bar select.sm { width: auto; max-width: 170px; flex: 0 0 auto; min-height: 24px; padding: 1px 4px; background: var(--input); border: 1px solid var(--line); border-radius: 4px; color: inherit; }
.pe-bar b, .pe-bar button { white-space: nowrap; }
.pe-bar .muted { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.muted { color: var(--muted); }
@media (max-width: 1100px) { .pe { grid-template-columns: 1fr; grid-template-rows: minmax(260px, 45%) minmax(0, 1fr); } .pe-plan { border-right: none; border-bottom: 1px solid var(--line); } }

/* Vòng 6: công tắc xem trước kiểu CAD trên thanh thẻ */
.ctabs label.mini { display: inline-flex; align-items: center; gap: 3px; white-space: nowrap; font-size: 12px; margin-left: 6px; }

/* vòng 8: bảng ô sàn (cấu kiện sàn) dưới hình xem trước */
.slabtbl { flex: none; max-height: 38%; overflow: auto; border-top: 1px solid var(--line); background: var(--panel); padding: 6px 10px; font-size: 12.5px; }
.slabtbl .st-head { font-weight: 600; margin-bottom: 4px; }
.slabtbl .st-head small { font-weight: 400; color: var(--muted); }
.slabtbl table { border-collapse: collapse; width: 100%; }
.slabtbl th, .slabtbl td { border: 1px solid var(--line); padding: 2px 4px; text-align: left; white-space: nowrap; }
.slabtbl th { background: var(--panel2); font-weight: 600; }
.slabtbl td.ro { color: var(--muted); }
.slabtbl td.ok { color: var(--ok); }
.slabtbl td.warn { color: var(--bad); white-space: normal; }
.slabtbl .st-in { width: 100%; min-width: 64px; box-sizing: border-box; border: 1px solid transparent; background: transparent; color: var(--text); font: inherit; padding: 1px 2px; }
.slabtbl .st-in:focus { border-color: var(--accent); background: var(--input); outline: none; }
.slabtbl .st-foot { margin-top: 4px; color: var(--muted); }
.slabtbl .st-foot.bad { color: var(--bad); }

/* vòng 8: dự án đã lưu (trình duyệt / máy chủ) */
.projpanel { position: absolute; z-index: 30; right: 12px; top: 52px; width: min(560px, calc(100vw - 32px)); max-height: 70vh; overflow: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 6px 24px rgba(0,0,0,.18); padding: 8px 10px; font-size: 13px; }
.projpanel .pp-head { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; font-weight: 600; }
.projpanel .pp-head .sp { flex: 1; }
.projpanel .pp-list { list-style: none; margin: 6px 0; padding: 0; }
.projpanel .pp-list li { display: flex; gap: 6px; align-items: center; padding: 3px 4px; border-bottom: 1px solid var(--line); }
.projpanel .pp-list li.on { background: var(--sel); }
.projpanel .pp-list .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.projpanel .pp-list small { color: var(--muted); white-space: nowrap; }
.projpanel .pp-note { color: var(--muted); margin-top: 4px; }
.projpanel .pp-note.bad, .projpanel .pp-conflict { color: var(--bad); }
.projpanel .pp-note.ok { color: var(--ok); }
.projpanel .pp-conflict { background: var(--bad-bg); padding: 6px; border-radius: 6px; margin-top: 6px; display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
