/* Phone first: the stage (3D model or 2D drawing) fills the screen above a docked panel
   that expands into a sheet. Wide or landscape screens get a side panel instead. */
:root {
  --bg: #EDF0F2;
  --bg-deep: #DCE2E6;
  --ink: #1F2D36;
  --ink-soft: #55656F;
  --line: #D3DADF;
  --card: #FFFFFF;
  --accent: #2F4858;
  --accent-ink: #FFFFFF;
  --warm: #B8683A;
  --paper: #FCFCFA;
  --estimated: #1F5FBF;   /* same blue as the estimated dimensions on the drawings */
  --radius: 16px;
  --dock: 172px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; background: var(--bg); overflow: hidden; overscroll-behavior: none; }
body { -webkit-text-size-adjust: 100%; }
button, input { font: inherit; color: inherit; }
button { -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

#app { position: fixed; inset: 0; }

/* ---------- Stage ---------- */
#stage {
  position: absolute; left: 0; right: 0; top: 0; bottom: calc(var(--dock) - var(--radius)); overflow: hidden;
  background: radial-gradient(120% 90% at 50% 35%, #F8FAFB 0%, var(--bg) 55%, var(--bg-deep) 100%);
  touch-action: none; user-select: none; -webkit-user-select: none;
}
#scene { position: absolute; inset: 0; width: 100%; height: 100%; display: block; outline: none; }
#labels { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
#labels.off { display: none; }

.label {
  position: absolute; left: 0; top: 0; will-change: transform; white-space: nowrap; text-align: center;
  padding: 3px 8px 4px; border-radius: 8px; background: rgba(255, 255, 255, .93);
  box-shadow: 0 1px 2px rgba(31, 45, 54, .2), 0 0 0 1px rgba(31, 45, 54, .06);
  line-height: 1.15;
}
.label b { display: block; font-size: 11px; font-weight: 700; letter-spacing: .02em; color: var(--ink); }
.label i { display: block; font-size: 10px; font-style: normal; color: var(--ink-soft); margin-top: 1px; }
.label.compact i { display: none; }
.label.hide { visibility: hidden; }
.label.tag { background: var(--accent); }
.label.tag b { color: #fff; }
.label.tag i { color: rgba(255, 255, 255, .8); }

/* The toolbar wraps: on a narrow phone the mode switch takes the first row and the tools the second. */
#tools {
  position: absolute; top: calc(10px + var(--safe-t)); left: 10px; right: 10px;
  display: flex; flex-wrap: wrap; gap: 6px; align-items: flex-start; pointer-events: none;
}
.toolset { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px; margin-left: auto; }
#mode {
  pointer-events: auto; display: inline-flex; height: 38px; padding: 3px; border: 1px solid var(--line); border-radius: 999px;
  background: rgba(255, 255, 255, .95); box-shadow: 0 1px 3px rgba(31, 45, 54, .12);
}
#mode button {
  border: 0; border-radius: 999px; background: transparent; padding: 0 13px; font-size: 13px; font-weight: 700;
  color: var(--ink-soft); cursor: pointer; white-space: nowrap;
}
#mode button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }
.tool {
  pointer-events: auto; display: inline-flex; align-items: center; gap: 6px; height: 38px; padding: 0 13px 0 10px;
  border: 1px solid var(--line); border-radius: 999px; background: rgba(255, 255, 255, .95); color: var(--ink);
  font-size: 13px; font-weight: 600; cursor: pointer; box-shadow: 0 1px 3px rgba(31, 45, 54, .12); white-space: nowrap;
}
.tool svg { width: 17px; height: 17px; flex: 0 0 auto; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tool[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.tool[aria-pressed="false"] { color: var(--ink-soft); }
.tool.icon { width: 38px; padding: 0; justify-content: center; }
.tool.icon span { display: none; }
.tool.text { padding: 0 11px; }
:is(.tool, .chip, #btn-more, #btn-explode, #mode button, #room-close, #notes .pick, #notes .links a):focus-visible { outline: 2px solid var(--warm); outline-offset: 2px; }

#explode {
  position: absolute; left: 12px; right: 12px; bottom: calc(12px + var(--radius)); display: flex; align-items: center; gap: 10px;
  padding: 5px 14px 5px 5px; border-radius: 999px; background: rgba(255, 255, 255, .95); border: 1px solid var(--line);
  box-shadow: 0 1px 3px rgba(31, 45, 54, .12);
}
#btn-explode {
  flex: 0 0 auto; height: 32px; padding: 0 13px; border: 0; border-radius: 999px; background: var(--accent); color: #fff;
  font-size: 13px; font-weight: 600; cursor: pointer;
}
#explode input { flex: 1; min-width: 0; accent-color: var(--accent); height: 28px; margin: 0; }

#hint {
  position: absolute; left: 50%; bottom: calc(14px + var(--radius)); transform: translateX(-50%); margin: 0; padding: 6px 12px;
  border-radius: 999px; background: rgba(31, 45, 54, .78); color: #fff; font-size: 12px; white-space: nowrap;
  max-width: calc(100% - 24px); overflow: hidden; text-overflow: ellipsis; pointer-events: none;
  transition: opacity .5s ease; opacity: 1;
}
#hint.gone { opacity: 0; }
#explode:not([hidden]) ~ #hint { bottom: calc(66px + var(--radius)); }

/* ---------- Drawing mode: the plan as SVG ---------- */
#stage.plan { background: var(--paper); }
#stage.plan :is(#scene, #labels) { visibility: hidden; }
#plan { position: absolute; inset: 0; cursor: grab; }
#plan:active { cursor: grabbing; }
#plan svg { display: block; width: 100%; height: 100%; }
#plan .failed { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; margin: 0; padding: 24px; text-align: center; font-size: 14px; color: #9B2C2C; }
/* Only the room outlines take the pointer; everything drawn above them lets it through. */
#plan svg g:not(.rooms) { pointer-events: none; }
#plan .rooms path { pointer-events: all; cursor: pointer; transition: fill .15s ease; }
#plan .rooms path.on { fill: rgba(199, 125, 79, .24); }
@media (hover: hover) { #plan .rooms path:not(.on):hover { fill: rgba(47, 72, 88, .07); } }
#plan.no-dims [data-layer^="A-ANNO-DIMS"],
#plan.no-names :is([data-layer="A-AREA-IDEN"], [data-layer="A-DOOR-IDEN"], [data-layer="A-GLAZ-IDEN"], [data-layer="A-ANNO-SYMB"]),
#plan.no-furniture :is([data-layer="A-FURN"], [data-layer="L-SITE-VEHC"]),
#plan.no-grid [data-layer^="S-GRID"] { display: none; }

#plan-key {
  position: absolute; left: 10px; bottom: calc(10px + var(--radius)); margin: 0; padding: 6px 10px; display: flex; flex-direction: column; gap: 2px;
  border-radius: 10px; background: rgba(255, 255, 255, .94); border: 1px solid var(--line); font-size: 11.5px; line-height: 1.35; color: var(--ink-soft);
  pointer-events: none;
}
#plan-key b { display: inline-block; min-width: 46px; color: #161616; font-weight: 600; }
#plan-key b.estimated { color: var(--estimated); }

#room-card {
  position: absolute; left: 10px; right: 10px; bottom: calc(10px + var(--radius)); padding: 12px 14px 12px;
  border-radius: 14px; background: var(--card); border: 1px solid var(--line); box-shadow: 0 6px 22px rgba(31, 45, 54, .16);
  user-select: text; -webkit-user-select: text;
}
#room-card .kicker { padding-right: 40px; }
#room-name { margin: 0 0 8px; font-size: 17px; line-height: 1.2; padding-right: 40px; }
#room-close {
  position: absolute; top: 6px; right: 6px; width: 36px; height: 36px; display: grid; place-items: center; border: 0; border-radius: 50%;
  background: transparent; color: var(--ink-soft); cursor: pointer;
}
#room-close svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
#room-facts { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; font-size: 14px; line-height: 1.35; }
#room-facts dt { color: var(--ink-soft); }
#room-facts dd { margin: 0; font-weight: 600; }
#room-note { margin: 9px 0 0; font-size: 12px; line-height: 1.4; color: var(--estimated); }
/* The card takes the place of the legend and of the hint. */
#room-card:not([hidden]) ~ :is(#plan-key, #hint) { display: none; }
#stage.plan #hint { bottom: calc(66px + var(--radius)); }

#loading {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; gap: 10px;
  background: var(--bg); font-size: 14px; color: var(--ink-soft); transition: opacity .35s ease;
}
#loading.done { opacity: 0; pointer-events: none; }
#loading.error { color: #9B2C2C; padding: 24px; text-align: center; }
.spinner { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
#loading.error .spinner { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Panel (phone: docked sheet) ---------- */
#panel {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 2; display: flex; flex-direction: column; max-height: 74dvh;
  background: var(--card); border-radius: var(--radius) var(--radius) 0 0; box-shadow: 0 -6px 22px rgba(31, 45, 54, .1);
  padding-bottom: var(--safe-b);
}
#views {
  display: flex; gap: 8px; overflow-x: auto; padding: 12px 14px 10px; scrollbar-width: none; flex: 0 0 auto;
  scroll-snap-type: x proximity; -webkit-overflow-scrolling: touch;
}
#views::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto; scroll-snap-align: center; height: 40px; padding: 0 15px; border-radius: 999px;
  border: 1px solid var(--line); background: #F5F7F8; font-size: 14px; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.chip[aria-current="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.chip.variant { border-color: #DDB89F; background: #FBF3EC; color: #7A4322; }
.chip.variant[aria-current="true"] { background: var(--warm); border-color: var(--warm); color: #fff; }

#heading { position: relative; padding: 2px 16px 12px; flex: 0 0 auto; }
.kicker { margin: 0 0 3px; font-size: 11px; font-weight: 700; letter-spacing: .08em; color: var(--ink-soft); padding-right: 118px; }
.kicker.variant { color: var(--warm); }
#view-title { margin: 0; font-size: 19px; line-height: 1.2; letter-spacing: -.01em; }
#view-sub {
  margin: 5px 0 0; font-size: 14px; line-height: 1.45; color: var(--ink-soft);
  display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; overflow: hidden; min-height: 2.9em;
}
#panel.open #view-sub { display: block; min-height: 0; }
#btn-more {
  position: absolute; top: -4px; right: 12px; display: inline-flex; align-items: center; gap: 2px; height: 30px; padding: 0 6px 0 10px;
  border: 0; border-radius: 999px; background: transparent; color: var(--accent); font-size: 12.5px; font-weight: 700; cursor: pointer;
}
#btn-more svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; transition: transform .2s ease; }
#panel.open #btn-more svg { transform: rotate(180deg); }

#notes { display: none; overflow-y: auto; padding: 0 16px 14px; min-height: 0; -webkit-overflow-scrolling: touch; border-top: 1px solid var(--line); }
#panel.open #notes { display: block; }
#notes h2 { margin: 14px 0 6px; font-size: 12.5px; letter-spacing: .05em; text-transform: uppercase; color: var(--accent); }
#notes p { margin: 0 0 8px; font-size: 14px; line-height: 1.5; }
#notes ul { margin: 0; padding: 0 0 0 18px; }
#notes li { margin: 0 0 6px; font-size: 14px; line-height: 1.5; }
#notes .scroll-x { overflow-x: auto; margin: 0 -2px; }
#notes table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
#notes th, #notes td { text-align: left; padding: 7px 10px 7px 0; border-bottom: 1px solid var(--line); vertical-align: top; line-height: 1.35; }
#notes thead th { font-size: 12px; color: var(--ink-soft); font-weight: 700; white-space: nowrap; }
#notes tbody th { font-weight: 600; color: var(--ink-soft); }
#notes td:last-child, #notes th:last-child { padding-right: 0; }
#notes td.best { font-weight: 700; color: #1E6B4A; }
#notes td.estimated { color: var(--estimated); }
#notes td.figure { white-space: nowrap; }
#notes p.small { margin: 8px 0 0; font-size: 12.5px; line-height: 1.45; color: var(--ink-soft); }
#notes .pick { padding: 0; border: 0; background: none; font-weight: 600; color: var(--accent); text-align: left; cursor: pointer; text-decoration: underline dotted; text-underline-offset: 3px; }
#notes tr.on :is(th, td) { background: rgba(199, 125, 79, .16); }
#notes .links { list-style: none; padding: 0; display: grid; gap: 6px; }
#notes .links li { margin: 0; }
#notes .links a {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 42px; padding: 6px 12px; border: 1px solid var(--line);
  border-radius: 10px; background: #F8FAFB; color: var(--ink); font-size: 14px; font-weight: 600; line-height: 1.3; text-decoration: none;
}
#notes .links a b { flex: 0 0 auto; padding: 2px 7px; border-radius: 6px; background: #E6ECEF; color: var(--accent); font-size: 11px; letter-spacing: .05em; }
#foot { display: none; padding: 10px 16px 12px; font-size: 11.5px; line-height: 1.4; color: var(--ink-soft); flex: 0 0 auto; }
#panel.open #foot { display: block; border-top: 1px solid var(--line); }

@media (max-width: 389px) {
  /* The hint wraps instead of being cut short. */
  #hint { white-space: normal; width: max-content; text-align: center; border-radius: 14px; }
  .tool.text { padding: 0 9px; }
}
@media (max-width: 359px) {
  .tool:not(.icon, .text) span { display: none; }
  .tool:not(.icon, .text) { width: 38px; padding: 0; justify-content: center; }
  .tool.text { padding: 0 8px; font-size: 12.5px; }
  .toolset { gap: 4px; }
  .label b { font-size: 10px; }
  .label i { font-size: 9px; }
}

/* ---------- Side panel: desktop, tablet, phone held sideways ---------- */
@media (min-width: 900px), (orientation: landscape) and (min-width: 600px) {
  #stage { left: var(--side); bottom: 0; }
  #panel {
    --pad: 24px;
    top: 0; right: auto; width: var(--side); max-height: none; border-radius: 0; box-shadow: none;
    border-right: 1px solid var(--line); padding: 20px 0 0;
  }
  :root { --side: 372px; --radius: 0px; }
  #heading { order: 1; padding: 0 var(--pad) 2px; }
  #views { order: 2; flex-wrap: wrap; overflow: visible; padding: 14px var(--pad) 8px; }
  #notes { order: 3; display: block; flex: 1 1 auto; padding: 0 var(--pad) 12px; border-top: 0; }
  #foot { order: 4; display: block; padding: 12px var(--pad) 16px; border-top: 1px solid var(--line); }
  #btn-more { display: none; }
  .kicker { padding-right: 0; }
  #view-title { font-size: 24px; }
  #view-sub { display: block; min-height: 0; }
  .chip { height: 36px; font-size: 13.5px; padding: 0 13px; }
  .label b { font-size: 12px; }
  .label i { font-size: 11px; }
  .tool.icon { width: auto; padding: 0 13px 0 10px; }
  .tool.icon span { display: inline; }
  #tools { top: 16px; right: 16px; }
  #tools { left: 16px; }
  #explode { left: auto; right: 16px; bottom: 16px; width: 320px; }
  #hint { bottom: 20px; left: 24px; transform: none; }
  #explode:not([hidden]) ~ #hint { bottom: 20px; }
  #plan-key { left: 16px; bottom: 16px; }
  #stage.plan #hint { left: 16px; bottom: 72px; }
  #room-card { left: 16px; right: auto; bottom: 16px; width: 340px; }
}
/* Phone held sideways: a narrower side panel with tighter type. */
@media (orientation: landscape) and (min-width: 600px) and (max-height: 520px) {
  :root { --side: 300px; }
  #panel { --pad: 16px; padding-top: 12px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
  #notes { overflow: visible; flex: 0 0 auto; }
  #view-title { font-size: 19px; }
  #view-sub { font-size: 13px; }
  .chip { height: 34px; font-size: 13px; padding: 0 11px; }
  #views { gap: 6px; padding-top: 10px; }
  #foot { display: none; }
  .tool.icon { width: 38px; padding: 0; }
  .tool.icon span { display: none; }
  #explode { width: 260px; }
  #hint { display: none; }
  #tools { left: 10px; right: 10px; top: 10px; }
  #plan-key { left: 10px; bottom: 10px; font-size: 11px; }
  #room-card { left: 10px; bottom: 10px; width: 300px; padding: 10px 12px; }
  #room-facts { font-size: 13px; gap: 2px 12px; }
  #room-note { margin-top: 6px; }
  .label b { font-size: 11px; }
  .label i { font-size: 10px; }
}
