:root {
  --bg: #e9edf2;
  --surface: #ffffff;
  --surface-2: #f1f4f7;
  --text: #17202a;
  --muted: #5d6b79;
  --line: #d5dbe2;
  --accent: #1273a6;
  --accent-text: #ffffff;
  --warn: #9a5b00;
  --warn-bg: #fff1d6;
  --shadow: 0 6px 24px rgba(15, 25, 40, 0.16);
  --bar: 58px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b2027;
    --surface: #242b34;
    --surface-2: #2d3540;
    --text: #e8edf2;
    --muted: #9aa7b4;
    --line: #3a4450;
    --accent: #4db4e6;
    --accent-text: #08202e;
    --warn: #ffcf7a;
    --warn-bg: #4a3a16;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

[hidden] { display: none !important; }

#stage { position: fixed; inset: 0; }
#stage canvas { display: block; }

.labels { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.measure-label {
  position: absolute; left: 0; top: 0;
  padding: 2px 7px; border-radius: 6px;
  background: #ff5a1f; color: #fff;
  font-size: 13px; font-weight: 600; white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

/* Barra superior */
#topbar {
  position: fixed; left: 0; right: 0; top: 0;
  display: flex; align-items: center; gap: 8px;
  padding: calc(8px + var(--safe-t)) 12px 8px;
  pointer-events: none;
}
#topbar > * { pointer-events: auto; }
#topbar .title {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column;
  padding: 6px 12px; border-radius: 12px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(10px);
  box-shadow: var(--shadow);
}
#topbar .title strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#topbar .title span { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#topbar .title span:empty { display: none; }

.btn {
  appearance: none; border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); color: var(--text);
  padding: 10px 14px; font: inherit; font-weight: 600; cursor: pointer;
  min-height: 42px;
}
.btn:active { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.small { padding: 7px 12px; min-height: 36px; font-weight: 500; }
#topbar .btn { box-shadow: var(--shadow); }

.icon-btn {
  appearance: none; border: 0; background: var(--surface); color: var(--text);
  width: 42px; height: 42px; border-radius: 10px; cursor: pointer;
  display: grid; place-items: center; box-shadow: var(--shadow);
}
.sheet-head .icon-btn { box-shadow: none; background: transparent; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
#btn-settings svg { fill: currentColor; stroke: none; }

.chip {
  appearance: none; border: 0; border-radius: 999px; cursor: pointer;
  background: var(--warn-bg); color: var(--warn);
  padding: 8px 12px; font: inherit; font-size: 13px; font-weight: 600; white-space: nowrap;
  box-shadow: var(--shadow);
}

/* Bienvenida */
#welcome {
  position: fixed; inset: 0; display: grid; place-items: center; padding: 24px;
  background: var(--bg);
}
#welcome .card { max-width: 440px; text-align: center; }
#welcome .logo { width: 64px; height: 64px; color: var(--accent); stroke-width: 3; }
#welcome h1 { font-size: 22px; margin: 14px 0 8px; }
#welcome p { color: var(--muted); margin: 0 0 18px; }
#welcome .btn { min-width: 200px; }
.hint { color: var(--muted); font-size: 13px; margin: 8px 0; }
#welcome .hint { margin-top: 16px; }

/* Barra de herramientas */
#toolbar {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(10px + var(--safe-b));
  display: flex; gap: 2px; padding: 4px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(10px);
  border-radius: 16px; box-shadow: var(--shadow);
  max-width: calc(100vw - 16px);
}
#toolbar button {
  appearance: none; border: 0; background: transparent; color: var(--muted);
  width: 66px; height: var(--bar); border-radius: 12px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font: inherit; font-size: 11px;
}
#toolbar button.active { background: var(--accent); color: var(--accent-text); }
#toolbar button.on { color: var(--accent); }
#toolbar button.active.on { color: var(--accent-text); }

/* Panel inferior (teléfono) / lateral (pantallas anchas) */
#sheet {
  position: fixed; left: 8px; right: 8px;
  bottom: calc(var(--bar) + 26px + var(--safe-b));
  max-height: min(44vh, 420px);
  display: flex; flex-direction: column;
  background: var(--surface); border-radius: 16px; box-shadow: var(--shadow);
  overflow: hidden;
}
@media (min-width: 820px) {
  #sheet { left: 12px; right: auto; width: 340px; top: calc(70px + var(--safe-t)); bottom: auto; max-height: calc(100vh - 170px); }
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 6px 6px 0 16px; }
.sheet-head h2 { font-size: 16px; margin: 0; }
.panel { display: none; padding: 6px 16px 16px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.panel.active { display: block; }
.panel h3 { font-size: 14px; margin: 4px 0; }

.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 8px 0; }
.row.between { justify-content: space-between; }
.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 8px 0 12px; }
.mono { font-variant-numeric: tabular-nums; font-weight: 600; }

.switch { display: flex; align-items: center; gap: 10px; padding: 8px 0; cursor: pointer; }
.switch.compact { padding: 0; }
.switch input, .layer input { width: 20px; height: 20px; accent-color: var(--accent); flex: none; }

.segmented { display: flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; margin: 8px 0 12px; }
.segmented button {
  flex: 1; appearance: none; border: 0; background: transparent; color: var(--text);
  padding: 9px 4px; font: inherit; cursor: pointer;
}
.segmented button + button { border-left: 1px solid var(--line); }
.segmented button.active { background: var(--accent); color: var(--accent-text); font-weight: 600; }

input[type="range"] { width: 100%; height: 34px; accent-color: var(--accent); margin: 0; }

/* Árbol de colecciones */
#layer-tree { margin-top: 4px; }
.layer { display: flex; align-items: center; gap: 8px; min-height: 40px; }
.layer .twisty {
  appearance: none; border: 0; background: transparent; color: var(--muted);
  width: 26px; height: 34px; padding: 0; cursor: pointer; flex: none; display: grid; place-items: center;
}
.layer .twisty svg { width: 14px; height: 14px; transition: transform 0.12s; }
.layer .twisty.open svg { transform: rotate(90deg); }
.layer .twisty:disabled { visibility: hidden; }
.layer label { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; cursor: pointer; }
.layer .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layer.dim .name { color: var(--muted); }
.layer-children { margin-left: 22px; border-left: 1px solid var(--line); padding-left: 4px; }

#measure-list, #notes-list { list-style: none; padding: 0; margin: 8px 0; }
#measure-list li { padding: 8px 0; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
#measure-list li b { font-size: 16px; }
#measure-list li span { display: block; color: var(--muted); font-size: 12px; }
#notes-list li { padding: 8px 10px; margin-bottom: 6px; border-radius: 8px; background: var(--warn-bg); color: var(--warn); font-size: 13px; }

.field { display: block; margin: 10px 0; }
.field span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.field input {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface-2); color: var(--text); font: inherit; font-size: 16px;
}

/* Estados */
#busy {
  position: fixed; inset: 0; display: flex; flex-direction: column; gap: 14px;
  align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--bg) 82%, transparent); backdrop-filter: blur(3px);
}
.spinner { width: 36px; height: 36px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

#toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  top: calc(70px + var(--safe-t)); max-width: min(92vw, 520px);
  padding: 10px 14px; border-radius: 10px;
  background: #17202a; color: #fff; box-shadow: var(--shadow); font-size: 14px; text-align: center;
}
#drop {
  position: fixed; inset: 12px; border: 3px dashed var(--accent); border-radius: 18px;
  display: grid; place-items: center; font-size: 20px; font-weight: 600; color: var(--accent);
  background: color-mix(in srgb, var(--bg) 80%, transparent); pointer-events: none;
}
