:root {
  --bg: #eef0f1;
  --panel: #ffffff;
  --panel-2: #f3f4f6;
  --ink: #1f2328;
  --muted: #69707a;
  --line: #dde1e5;
  --accent: #2f6fdf;
  --accent-soft: #e5eeff;
  --accent-ink: #ffffff;
  --danger: #c93c37;
  --warn-bg: #fdecea;
  --stage: #dfe6ea;
  --shadow: 0 8px 28px rgba(20, 30, 40, .16);
  --radius: 10px;
  --bar-h: 64px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15181c; --panel: #1d2126; --panel-2: #262b31; --ink: #e8eaed; --muted: #9aa3ad;
    --line: #333a42; --accent: #6b9cff; --accent-soft: #24324d; --accent-ink: #0d1117; --danger: #ff6b63;
    --warn-bg: #3a1f1d; --stage: #2a3036; --shadow: 0 8px 28px rgba(0, 0, 0, .45);
  }
}
:root[data-theme="dark"] {
  --bg: #15181c; --panel: #1d2126; --panel-2: #262b31; --ink: #e8eaed; --muted: #9aa3ad;
  --line: #333a42; --accent: #6b9cff; --accent-soft: #24324d; --accent-ink: #0d1117; --danger: #ff6b63;
  --warn-bg: #3a1f1d; --stage: #2a3036; --shadow: 0 8px 28px rgba(0, 0, 0, .45);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  background: var(--bg); color: var(--ink);
  font: 14px/1.45 "IBM Plex Sans Thai", system-ui, -apple-system, sans-serif;
  display: grid;
  grid-template-columns: 290px 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas: "top top" "cat stage" "status status";
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
button {
  border: 1px solid var(--line); background: var(--panel); border-radius: 8px;
  padding: 6px 11px; cursor: pointer; white-space: nowrap;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
button:hover { border-color: var(--accent); }
button:focus-visible, input:focus-visible, select:focus-visible, summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.danger { color: var(--danger); }
button.toggle.on { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
svg { width: 18px; height: 18px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
#i-more circle { fill: currentColor; }
.icon-btn { padding: 6px; width: 36px; height: 36px; }
.hint { color: var(--muted); font-size: 12.5px; margin: 8px 0 0; }
.touch { display: none; }
@media (hover: none) { .desk { display: none !important; } .touch { display: block; } }

/* ------------------------------------------------------------ top bar */
#topbar {
  grid-area: top; display: flex; align-items: center; gap: 12px;
  padding: 8px 14px; background: var(--panel); border-bottom: 1px solid var(--line); z-index: 5;
}
.brand { display: flex; flex-direction: column; line-height: 1.25; margin-right: 4px; }
.brand .sub { color: var(--muted); font-size: 12px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; background: var(--panel); flex: none; }
.seg button { border: 0; border-radius: 0; background: transparent; padding: 6px 12px; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent); color: var(--accent-ink); }
.seg .sm { display: none; }
#more { display: flex; align-items: center; gap: 12px; flex: 1; min-width: 0; flex-wrap: wrap; }
#more .group { display: flex; align-items: center; gap: 6px; }
.glabel { font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
#lightSeg .lbl { display: none; }
#lightSeg button { padding: 6px 9px; }
.toggles { gap: 6px; }
.filemenu { position: relative; margin-left: auto; }
.filemenu summary {
  list-style: none; cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--line); border-radius: 8px; padding: 6px 11px; background: var(--panel);
}
.filemenu summary::-webkit-details-marker { display: none; }
.filemenu .menu {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 200px; z-index: 20;
  display: flex; flex-direction: column; gap: 4px; padding: 6px;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
}
.filemenu .menu button { justify-content: flex-start; border-color: transparent; }
.undo-redo { display: flex; gap: 4px; }

/* ------------------------------------------------------------ catalog / build panels */
#catalog {
  grid-area: cat; background: var(--panel); border-right: 1px solid var(--line);
  overflow-y: auto; padding: 12px; z-index: 4;
}
.tabs { display: flex; width: 100%; margin-bottom: 12px; }
.tabs button { flex: 1; flex-direction: column; gap: 2px; padding: 7px 4px; font-size: 12.5px; }
#search { width: 100%; padding: 9px 12px; border: 1px solid var(--line); border-radius: 9px; background: var(--panel-2); }
.cat { margin-top: 10px; }
.cat summary { cursor: pointer; font-size: 13px; font-weight: 600; padding: 6px 2px; color: var(--ink); }
.cat summary .count { font-weight: 400; color: var(--muted); }
.cat[open] summary { margin-bottom: 4px; }
.item {
  display: flex; width: 100%; justify-content: flex-start; align-items: center; gap: 10px;
  text-align: left; margin-bottom: 5px; padding: 6px 8px; background: var(--panel-2); border-color: transparent; white-space: normal;
}
.item.on { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.item .thumb { width: 48px; height: 48px; flex: none; border-radius: 8px; background: var(--panel); }
.item .nm { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; font-weight: 500; }
.item .dim { color: var(--muted); font-size: 11.5px; align-self: flex-start; padding-top: 2px; }
.th { font-size: 11.5px; color: var(--muted); font-weight: 400; }
.badges { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 2px; }
.badge { font-size: 10.5px; line-height: 1.4; padding: 0 6px; border-radius: 99px; background: var(--line); color: var(--muted); font-weight: 400; }
.badge.b-wall { background: #dbe8ff; color: #2456b3; }
.badge.b-surface { background: #e3f3e1; color: #2f6b2a; }
.badge.b-ceiling { background: #f6ecd9; color: #8a5a12; }
.custom { margin-top: 16px; border-top: 1px solid var(--line); padding-top: 10px; }
.custom summary { cursor: pointer; font-weight: 600; }
.custom .primary { width: 100%; margin-top: 10px; }
label { display: flex; flex-direction: column; font-size: 12px; color: var(--muted); gap: 3px; margin-top: 8px; }
.custom input, #inspector input, select {
  padding: 7px 9px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2); width: 100%; min-width: 0;
}
input[type=color] { padding: 2px; height: 36px; }
.row3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.row2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px; }

.paint-mode { display: flex; width: 100%; }
.paint-mode button { flex: 1; }
.paint-list { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; margin-top: 10px; }
.paint { justify-content: flex-start; gap: 8px; text-align: left; font-size: 12.5px; background: var(--panel-2); border-color: transparent; padding: 7px 8px; }
.paint .chip { width: 20px; height: 20px; border-radius: 6px; border: 1px solid rgba(0,0,0,.15); flex: none; }
.paint.on, .floor.on { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.custom-color { flex-direction: row; align-items: center; justify-content: space-between; gap: 8px; margin-top: 12px; font-size: 12.5px; }
.custom-color input { width: 64px; height: 32px; padding: 2px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2); }
.floor-list { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 10px; }
.floor { flex-direction: column; align-items: stretch; gap: 4px; padding: 5px; font-size: 11.5px; text-align: left; background: var(--panel-2); border-color: transparent; white-space: normal; }
.floor img, .floor .orig { width: 100%; aspect-ratio: 1.6; object-fit: cover; border-radius: 6px; display: grid; place-items: center; background: var(--line); font-size: 20px; }

/* ------------------------------------------------------------ stage */
#stage { grid-area: stage; position: relative; background: var(--stage); min-width: 0; min-height: 0; }
#stage canvas { display: block; width: 100%; height: 100%; touch-action: none; }
#loading { position: absolute; inset: 0; display: grid; place-items: center; align-content: center; gap: 12px; color: var(--muted); font-size: 15px; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
#walkHint, #toolPill {
  position: absolute; left: 50%; top: 14px; transform: translateX(-50%); z-index: 3;
  background: rgba(20, 24, 30, .82); color: #fff; padding: 7px 8px 7px 14px; border-radius: 99px; font-size: 13px;
  display: flex; align-items: center; gap: 10px; box-shadow: var(--shadow); max-width: calc(100% - 24px);
}
#walkHint { padding: 8px 14px; }
#toolPillText { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
#walkHint[hidden], #toolPill[hidden] { display: none; }
#toolPill button { background: #fff; color: #111; border: 0; border-radius: 99px; padding: 4px 12px; font-weight: 600; }
#toolPill .dot { width: 14px; height: 14px; border-radius: 50%; border: 1px solid rgba(255,255,255,.6); flex: none; display: inline-block; vertical-align: -2px; margin-right: 6px; }
#labels { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.rlabel {
  position: absolute; transform: translate(-50%, -50%); white-space: nowrap; text-align: center;
  font-size: 12px; color: #2b3138; background: rgba(255, 255, 255, .8); padding: 2px 8px; border-radius: 7px;
}
.rlabel small { display: block; color: #69707a; font-size: 10.5px; }
.dlabel {
  position: absolute; transform: translate(-50%, -140%); white-space: nowrap;
  font-size: 12px; background: var(--accent); color: var(--accent-ink); padding: 2px 8px; border-radius: 7px;
}
.dlabel.bad { background: var(--danger); }
.mlabel { position: absolute; transform: translate(-50%, -50%); background: #111; color: #fff; font-size: 12px; padding: 2px 8px; border-radius: 7px; white-space: nowrap; }

/* ------------------------------------------------------------ inspector */
#inspector {
  position: fixed; right: 14px; top: 72px; width: 300px; z-index: 6;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 12px; box-shadow: var(--shadow);
  max-height: calc(100vh - 120px); overflow-y: auto;
}
.ins-head { display: flex; gap: 6px; align-items: flex-start; }
.name-wrap { flex: 1; min-width: 0; }
.name-wrap .name { width: 100%; font-weight: 600; font-size: 15px; border-color: transparent; background: transparent; padding: 4px 6px; }
.name-wrap .name:hover, .name-wrap .name:focus { border-color: var(--line); background: var(--panel-2); }
.name-wrap .th { margin: 0 6px; }
.quick { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 10px; }
.quick button { flex-direction: column; gap: 2px; padding: 8px 4px; font-size: 11.5px; }
.quick svg { width: 20px; height: 20px; }
.details { margin-top: 10px; border-top: 1px solid var(--line); padding-top: 6px; }
.details summary { cursor: pointer; font-size: 13px; font-weight: 600; padding: 4px 0; }
.swatches { display: grid; grid-template-columns: repeat(12, 1fr); gap: 4px; margin-top: 10px; }
.details .swatches { display: none; }
.sw { padding: 0; height: 22px; border-radius: 6px; border: 1px solid rgba(0,0,0,.15); }
.warn { background: var(--warn-bg); color: var(--danger); padding: 6px 9px; border-radius: 8px; margin: 10px 0 0; font-size: 12.5px; }
.warn::before { content: "⚠ "; }

/* ------------------------------------------------------------ status */
#status {
  grid-area: status; display: flex; gap: 16px; padding: 6px 14px; font-size: 12.5px;
  background: var(--panel); border-top: 1px solid var(--line); color: var(--muted);
}
#stRoom { color: var(--ink); font-weight: 500; min-width: 160px; }
#stCount { margin-left: auto; }

.only-mobile { display: none !important; }
#scrim[hidden] { display: none; }

/* ------------------------------------------------------------ medium screens: tighter bar */
@media (max-width: 1280px) {
  .brand .sub { display: none; }
  .glabel { display: none; }
}

/* ------------------------------------------------------------ phones & small tablets */
@media (max-width: 820px) {
  body { grid-template-columns: 1fr; grid-template-areas: "top" "stage"; grid-template-rows: auto 1fr; }
  .only-mobile { display: block !important; }
  button.only-mobile { display: inline-flex !important; }
  #status { display: none; }
  #topbar { gap: 8px; padding: 8px 10px; z-index: auto; } /* no stacking context: the ⋯ sheet must sit above the bottom bar */
  .brand { flex: 1; min-width: 0; }
  .brand strong { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .seg .lg { display: none; }
  .seg .sm { display: inline; }
  .undo-redo { display: none; }

  /* ⋯ settings sheet */
  #more {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    flex-direction: column; align-items: stretch; gap: 14px; flex-wrap: nowrap;
    background: var(--panel); border-radius: 18px 18px 0 0; box-shadow: var(--shadow);
    padding: 14px 16px calc(18px + env(safe-area-inset-bottom));
    transform: translateY(105%); transition: transform .22s ease;
  }
  #more.open { transform: none; }
  .more-head { display: flex !important; align-items: center; justify-content: space-between; }
  #more .group { justify-content: space-between; }
  .glabel { display: inline; font-size: 13px; text-transform: none; letter-spacing: 0; color: var(--ink); }
  #more .seg button { padding: 9px 14px; }
  #lightSeg .lbl { display: inline; }
  .toggles { justify-content: flex-start !important; flex-wrap: wrap; }
  .toggles button { padding: 9px 14px; }
  .filemenu { margin-left: 0; }
  .filemenu summary { display: none; }
  .filemenu .menu { position: static; display: grid; grid-template-columns: 1fr 1fr; box-shadow: none; border: 0; padding: 0; min-width: 0; }
  .filemenu .menu button { justify-content: center; border-color: var(--line); padding: 10px; white-space: normal; }
  .count-line { margin: 0; color: var(--muted); font-size: 12.5px; text-align: center; }

  /* catalog drawer */
  #catalog {
    position: fixed; left: 0; right: 0; bottom: 0; top: auto; z-index: 25; height: 78vh;
    border-radius: 18px 18px 0 0; border: 0; box-shadow: var(--shadow);
    transform: translateY(105%); transition: transform .22s ease; padding: 10px 14px calc(14px + env(safe-area-inset-bottom));
  }
  #catalog.open { transform: none; }
  .drawer-head { display: flex !important; align-items: center; justify-content: space-between; margin-bottom: 8px; }
  #toolTabs { display: none; }
  #scrim { position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, .35); }

  /* stage leaves room for the bottom bar */
  #stage { margin-bottom: calc(var(--bar-h) + env(safe-area-inset-bottom)); }
  .rlabel { font-size: 10.5px; padding: 1px 6px; }
  .rlabel small { display: none; }

  /* bottom navigation */
  #mobileBar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 15; display: grid !important;
    grid-template-columns: repeat(5, 1fr); height: calc(var(--bar-h) + env(safe-area-inset-bottom));
    padding: 6px 6px env(safe-area-inset-bottom); background: var(--panel); border-top: 1px solid var(--line);
  }
  #mobileBar button { flex-direction: column; gap: 3px; border: 0; background: transparent; font-size: 11.5px; color: var(--muted); padding: 4px 0; }
  #mobileBar button svg { width: 22px; height: 22px; }
  #mobileBar button.on { color: var(--accent); }

  /* item sheet above the bottom bar */
  #inspector {
    left: 8px; right: 8px; width: auto; top: auto;
    bottom: calc(var(--bar-h) + env(safe-area-inset-bottom) + 8px);
    max-height: 52vh; padding: 10px 12px; border-radius: 16px;
  }
  .sw { height: 26px; }
  #inspector > .hint { display: none; }
  #inspector > .swatches { display: none; }
  .details .swatches { display: grid; }
}
