:root {
  --ink: #1c1915;
  --muted: #5e574e;
  --paper: #e6dfd2;
  --panel: #f7f3eb;
  --white: #fffdf8;
  --line: #ddd4c4;
  --spruce: #1c3b33;
  --spruce-2: #2c5649;
  --copper: #b4532a;
  --copper-deep: #8a3d1c;
  --ok: #1d6b45;
  --ok-bg: #e7f3ec;
  --warn: #7a5410;
  --warn-bg: #f8ead0;
  --bad: #7a2e2e;
  --bad-bg: #f8e4e0;
  --grok: #5b35c5;
  --shadow: 0 16px 40px rgba(48, 36, 22, 0.08);
  font-family: "Source Sans 3", "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--paper);
  font-size: 18px;
  line-height: 1.4;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
button, input, select, textarea { font: inherit; color: inherit; font-size: 16px; }
.gate-form { display: flex; flex-direction: column; gap: 12px; max-width: 520px; padding: 8px 48px 48px; }
.gate-form input { min-height: 48px; border-radius: 12px; border: 1px solid var(--line); padding: 8px 12px; width: 100%; }
button, a.btn { cursor: pointer; touch-action: manipulation; }
h1, h2, h3 { font-weight: 650; letter-spacing: -0.02em; margin: 0; }
.hidden { display: none !important; }
.short-label { display: none; }

.btn, .tool, .choice, .quick-types button, .sheet-card, .item-card, .suggest-actions .btn {
  min-height: 48px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--white);
  padding: 0 16px;
  font-weight: 650;
}
.btn:hover, .tool:hover { border-color: #c8bba6; }
.btn-primary { background: var(--spruce); color: #f6f1e6; border-color: var(--spruce); }
.btn-primary:hover { background: var(--spruce-2); }
.btn-quiet { background: transparent; }
.btn-xl { min-height: 64px; padding: 0 28px; font-size: 22px; }
.tool.is-on { background: var(--copper); color: white; border-color: var(--copper-deep); }
.text-btn { border: 0; background: transparent; color: var(--spruce); font-weight: 700; text-decoration: underline; min-height: 44px; }

.screen { min-height: 100%; }
.home-hero { padding: 48px 48px 12px; }
.brand { display: flex; gap: 18px; align-items: center; }
.brand-mark {
  font-family: Fraunces, Georgia, serif;
  background: var(--spruce);
  color: #f4ecdf;
  width: 92px;
  height: 92px;
  display: grid;
  place-items: center;
  border-radius: 18px;
  font-size: 34px;
}
.brand-mark-sm { width: 52px; height: 52px; border-radius: 12px; font-size: 18px; }
.brand h1 { font-family: Fraunces, Georgia, serif; font-size: 64px; font-weight: 600; line-height: 0.9; }
.brand p { margin: 6px 0 0; color: var(--muted); font-size: 20px; }
.home-actions { display: flex; gap: 12px; padding: 12px 48px; flex-wrap: wrap; }
.home-note { padding: 0 48px 8px; color: var(--muted); max-width: 720px; }
.job-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; padding: 20px 48px 48px; }
.job-card {
  background: var(--white);
  border-radius: 18px;
  padding: 18px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 160px;
}
.job-card h3 { font-size: 26px; }
.job-card p { margin: 0; color: var(--muted); }
.job-card .btn { margin-top: auto; }

#workspace {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto auto auto auto auto minmax(0, 1fr);
  grid-template-areas: "top" "rules" "tools" "note" "mode" "alert" "work";
}
#mode-banner { grid-area: mode; }
.topbar { grid-area: top; }
.rule-row { grid-area: rules; }
.toolstrip { grid-area: tools; }
.tool-help { grid-area: note; }
#banner { grid-area: alert; }
.workspace-body { grid-area: work; min-height: 0; }
.topbar {
  display: flex;
  gap: 12px;
  align-items: center;
  background: var(--spruce);
  color: #f6f1e6;
  padding: 10px 16px;
}
.topbar .btn, .topbar .btn-quiet { color: #f6f1e6; border-color: transparent; background: transparent; }
.topbar a.btn { color: var(--ink); background: #f6f1e6; border-color: #f6f1e6; text-decoration: none; display: grid; place-items: center; }
.job-title { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.job-title input {
  background: transparent;
  border: 0;
  color: #f6f1e6;
  font-weight: 700;
  font-size: 22px;
  width: 100%;
}
.job-title input::placeholder { color: #d5cbbd; }
#job-client { font-size: 16px; font-weight: 500; }
.save-state { margin: 0; color: #d7e4de; font-size: 15px; white-space: nowrap; }
.rule-row {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 16px;
  background: #efe8dc;
}
.rule-row span {
  background: var(--white);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 15px;
  font-weight: 650;
}
.toolstrip {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.tools { display: flex; gap: 8px; flex-wrap: wrap; flex: 1; }
.tool { min-width: 108px; }
.tool-help {
  margin: 0;
  padding: 4px 16px;
  height: 32px;
  line-height: 24px;
  color: var(--muted);
  background: var(--panel);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.banner { margin: 0; padding: 12px 16px; background: var(--warn-bg); color: var(--warn); font-weight: 700; }
.banner.bad { background: var(--bad-bg); color: var(--bad); }
.banner.ok { background: var(--ok-bg); color: var(--ok); }

.workspace-body { display: grid; grid-template-columns: 300px 1fr 400px; min-height: 0; }
.rail { background: var(--panel); min-height: 0; border-right: 1px solid var(--line); display: flex; flex-direction: column; }
.rail-right { border-right: 0; border-left: 1px solid var(--line); }
.rail-scroll { min-height: 0; }
.rail-scroll { overflow: auto; padding-bottom: 12px; }
.rail-head, .scale-panel, .sheet-list, .editor, .suggestions, .quick-types { padding: 12px; }
.rail-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.rail h2 { font-size: 15px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
.sheet-list { display: flex; flex-direction: column; gap: 8px; overflow: auto; }
.sheet-card, .item-card, .suggest-card {
  display: grid;
  grid-template-columns: 64px 1fr;
  gap: 8px;
  padding: 8px;
  border-radius: 14px;
  background: var(--white);
  border: 2px solid transparent;
  text-align: left;
  width: 100%;
}
.sheet-card.is-on, .item-card.is-on { border-color: var(--copper); }
.sheet-card img { width: 64px; height: 48px; object-fit: cover; background: #ddd; border-radius: 6px; }
.sheet-card strong, .item-card strong { display: block; }
.sheet-meta, .muted { color: var(--muted); font-size: 14px; }
.scale-dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; background: var(--warn); margin-right: 6px; }
.scale-dot.ok { background: var(--ok); }
.scale-panel { border-top: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; }
.scale-panel select, .editor input, .editor select, dialog input, dialog select {
  width: 100%;
  min-height: 48px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--white);
  padding: 8px 12px;
}
.viewer-column { display: grid; grid-template-rows: minmax(180px, 1fr) auto auto; min-width: 0; min-height: 0; background: #cfc6b6; }
.command-dock {
  background: var(--panel);
  border-top: 1px solid var(--line);
  padding: 10px 12px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 280px;
  overflow: auto;
}
.command-head, .command-row, .tag-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.command-head h2 { font-size: 15px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); margin-right: auto; }
.command-row textarea {
  flex: 1;
  min-width: 180px;
  min-height: 64px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--white);
  padding: 8px 12px;
  resize: vertical;
}
.command-diff {
  background: #efe9ff;
  border: 2px solid var(--grok);
  border-radius: 14px;
  padding: 10px 12px;
}
.command-diff h3 { color: var(--grok); font-size: 18px; margin-bottom: 4px; }
.command-diff li { margin: 4px 0 4px 18px; }
.flag-card {
  background: #fff4e8;
  border-left: 4px solid #c2410c;
  border-radius: 12px;
  padding: 8px 10px;
  margin-top: 6px;
}
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.viewport { position: relative; overflow: hidden; min-height: 0; touch-action: none; cursor: grab; }
.viewport[data-panning="true"] { cursor: grabbing; }
.viewport[data-tool="wall"], .viewport[data-tool="area"], .viewport[data-tool="length"], .viewport[data-tool="count"], .viewport[data-tool="scale"] { cursor: crosshair; }
.stage { position: absolute; top: 0; left: 0; transform-origin: 0 0; }
.sheet-img { display: block; max-width: none; user-select: none; box-shadow: 0 12px 40px rgba(40, 30, 18, 0.18); background: white; }
.overlay { position: absolute; inset: 0; overflow: visible; }
.empty-plan { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 24px; padding: 24px; text-align: center; }
.viewer-bar {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 8px 12px;
  background: var(--panel);
  border-top: 1px solid var(--line);
}
.hud { margin-left: 8px; font-weight: 700; font-variant-numeric: tabular-nums; }
.viewer-bar .btn { min-width: 48px; }

.quick-types { display: flex; flex-wrap: wrap; gap: 6px; }
.quick-types button { border-radius: 999px; border: 1px solid var(--line); background: var(--white); padding: 0 14px; font-weight: 650; }
.item-list { display: flex; flex-direction: column; gap: 8px; padding: 0 12px; }
.item-card { grid-template-columns: 18px 1fr auto; align-items: center; }
.swatch { width: 18px; height: 18px; border-radius: 50%; display: inline-block; }
.pill { font-size: 12px; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; border-radius: 999px; padding: 4px 8px; background: var(--ok-bg); color: var(--ok); }
.pill.assumed { background: var(--warn-bg); color: var(--warn); }
.pill.excluded { background: #eee; color: var(--muted); }
.pill.unmeasured { background: transparent; color: var(--bad); border: 1px dashed var(--bad); }
.pill.proposed { background: #efe9ff; color: var(--grok); }
.editor { display: flex; flex-direction: column; gap: 10px; }
.editor h3 { font-size: 22px; }
.choice-row, .feet-row, .dialog-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.choice-row button, .feet-row label { flex: 1; }
.choice { min-height: 48px; border-radius: 12px; border: 1px solid var(--line); background: var(--white); font-weight: 700; }
.choice.is-on { background: var(--spruce); color: white; border-color: var(--spruce); }
.editor label, dialog label { display: flex; flex-direction: column; gap: 4px; font-size: 15px; font-weight: 700; color: var(--muted); }
.help { margin: 0; color: var(--muted); }
.totals-dock {
  order: -1;
  flex: 0 0 auto;
  background: var(--white);
  border-bottom: 1px solid var(--line);
  padding: 10px 12px 8px;
  max-height: 42vh;
  overflow: auto;
}
.rail-scroll { flex: 1; }
.bucket { margin-bottom: 8px; }
.bucket h3 { font-size: 12px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); margin-bottom: 2px; }
.bucket.measured h3 { color: var(--ok); }
.bucket.assumed h3 { color: var(--warn); }
.bucket.unmeasured h3 { color: var(--bad); }
.total-row, .line-note { display: flex; justify-content: space-between; gap: 12px; padding: 1px 0; align-items: baseline; }
.total-row strong { font-variant-numeric: tabular-nums; font-size: 18px; white-space: nowrap; }
.sheet-math { margin: 0 0 6px; color: var(--spruce); font-weight: 650; font-size: 14px; }
.suggestions { border-top: 1px solid var(--line); }
.suggest-card { grid-template-columns: 1fr; border-left: 4px solid var(--grok); }
.suggest-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.suggest-actions .btn { flex: 1; min-width: 88px; }
.suggest-actions .btn-primary,
.command-diff .btn-primary,
.heights-needed .btn-primary { background: var(--spruce); color: #f6f1e6; border-color: var(--spruce); }
.heights-needed, .scale-log { display: flex; flex-direction: column; gap: 8px; }
.heights-needed { padding: 12px; border-bottom: 1px solid var(--line); }
.height-row, .band-row { background: var(--white); border-radius: 12px; padding: 8px; }
.height-row { display: flex; flex-direction: column; gap: 6px; }
.band { margin: 0 0 8px; }
.band h3 { font-size: 16px; margin-bottom: 4px; }
.band h4 { font-size: 14px; color: var(--muted); font-weight: 700; margin: 6px 0 2px; }
.scale-entry { background: var(--white); border-radius: 12px; padding: 8px; }
.scale-entry p { margin: 2px 0; }
.log-line { margin: 4px 0; color: var(--muted); font-size: 15px; }

dialog {
  border: 0;
  border-radius: 20px;
  padding: 24px;
  width: min(640px, calc(100vw - 32px));
  background: var(--white);
  color: var(--ink);
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(28, 25, 21, 0.45); }
dialog h2 { font-family: Fraunces, Georgia, serif; font-size: 36px; margin-bottom: 8px; }
dialog p { margin-top: 0; }
.dialog-actions { justify-content: flex-end; margin-top: 16px; }
.dialog-note { color: var(--muted); }
.type-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 8px 0 16px; }
.type-grid button {
  min-height: 56px;
  text-align: left;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--panel);
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 8px 12px;
  font-weight: 700;
}
#rules-list { padding-left: 20px; }
#rules-list li { margin: 8px 0; }
.toast {
  position: fixed;
  top: 72px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: white;
  padding: 14px 18px;
  border-radius: 14px;
  max-width: min(560px, calc(100vw - 24px));
  z-index: 5;
}
.check { display: flex; gap: 8px; align-items: center; font-weight: 650; color: var(--ink); }
.check input { width: 22px; height: 22px; }

@media (max-width: 1366px) {
  .workspace-body { grid-template-columns: 260px minmax(0, 1fr) 340px; }
  .home-hero, .home-actions, .home-note, .job-grid { padding-left: 24px; padding-right: 24px; }
}

@media (max-width: 1100px) {
  .topbar { padding: 4px 10px; min-height: 52px; }
  .job-title input { font-size: 18px; }
  .brand-mark-sm { width: 44px; height: 44px; }
  .save-state { display: none; }
  .rule-row { flex-wrap: nowrap; overflow-x: auto; padding: 4px 10px; }
  .rule-row span, #open-rules { min-height: 44px; display: inline-flex; align-items: center; }
  .toolstrip, .tools { flex-wrap: nowrap; overflow-x: auto; min-width: 0; }
  .tools { flex: 1; }
  .tool-help { display: none; }
  .short-label { display: inline; }
  .wide-label { display: none; }
  .tool { min-width: 0; flex: 0 0 auto; padding: 0 12px; }
  .toolstrip, .tools { gap: 6px; }
  .workspace-body {
    grid-template-columns: minmax(0, 1fr) 300px;
    grid-template-rows: 96px minmax(0, 1fr);
    grid-template-areas: "plans totals" "viewer totals";
  }
  .rail-left { grid-area: plans; }
  .viewer-column { grid-area: viewer; }
  .rail-right { grid-area: totals; }
  .rail-left {
    flex-direction: row;
    height: 96px;
    max-height: 96px;
    min-height: 0;
    overflow: auto;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  .scale-panel { max-height: 96px; overflow: auto; min-width: 200px; border-top: 0; border-left: 1px solid var(--line); }
  .scale-panel .help { display: none; }
  .sheet-list { flex-direction: row; overflow-x: auto; }
  .sheet-card { width: 210px; flex: 0 0 auto; }
  .viewer-column { min-height: 0; }
  .rail-right {
    border-left: 1px solid var(--line);
    border-top: 0;
    height: auto;
    max-height: none;
    min-height: 0;
    overflow: auto;
  }
  .home-hero { padding-top: 24px; }
  .brand h1 { font-size: 48px; }
  .brand-mark { width: 72px; height: 72px; }
}

@media (orientation: portrait), (max-width: 900px) {
  .workspace-body {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(280px, 1fr) minmax(160px, 38vh);
    grid-template-areas: "plans" "viewer" "totals";
  }
  .rail-left {
    grid-area: plans;
    flex-direction: row;
    max-height: 132px;
    overflow: auto;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  .viewer-column { grid-area: viewer; min-height: 280px; }
  .rail-right {
    grid-area: totals;
    max-height: 38vh;
    overflow: auto;
    border-left: 0;
    border-top: 1px solid var(--line);
  }
  .btn, .tool, .choice, .quick-types button { min-height: 48px; }
  .command-dock { max-height: none; }
}

@media (max-width: 1280px) and (max-height: 900px) {
  .tool-help { display: none; }
  .home-hero { padding-top: 16px; }
  .command-dock { max-height: 220px; }
}
