:root {
  --bg: #f4efe7;
  --panel: #fffdf9;
  --panel-2: #f9f4ec;
  --text: #2c2520;
  --muted: #8b7f74;
  --line: #e4dacb;
  --accent: #b5643c;
  --accent-ink: #fff;
  --accent-soft: rgba(181, 100, 60, 0.35);
  --clay-fill: rgba(181, 140, 100, 0.32);
  --clay-line: #7a5a40;
  --wet: #4f7f86;
  --grid: rgba(60, 40, 20, 0.05);
  --grid-major: rgba(60, 40, 20, 0.12);
  --liquid: rgba(90, 150, 200, 0.28);
  --liquid-line: #3d7fae;
  --rib-fill: rgba(196, 160, 110, 0.55);
  --rib-line: #8a6a40;
  --warn: #b8432f;
  --viewer-bg: radial-gradient(ellipse at 50% 35%, #fbf7f1 0%, #ece3d6 100%);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1d1a17;
    --panel: #26221e;
    --panel-2: #2d2823;
    --text: #ece4da;
    --muted: #9c9085;
    --line: #3b342d;
    --accent: #d98457;
    --accent-ink: #1d1a17;
    --accent-soft: rgba(217, 132, 87, 0.4);
    --clay-fill: rgba(200, 160, 120, 0.25);
    --clay-line: #cfa684;
    --wet: #7fb3bb;
    --grid: rgba(255, 240, 220, 0.04);
    --grid-major: rgba(255, 240, 220, 0.1);
    --liquid: rgba(110, 170, 220, 0.25);
    --liquid-line: #7fb8e0;
    --rib-fill: rgba(196, 160, 110, 0.35);
    --rib-line: #d4b285;
    --warn: #ec8a72;
    --viewer-bg: radial-gradient(ellipse at 50% 35%, #342e28 0%, #1f1b18 100%);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 13px/1.45 var(--sans);
  -webkit-font-smoothing: antialiased;
}

.app {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  grid-template-rows: auto 1fr;
  height: 100vh;
}

/* ---------- top bar ---------- */
.topbar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.brand { display: flex; align-items: center; gap: 8px; font: 600 17px var(--serif); white-space: nowrap; }
.brand svg { width: 22px; height: 22px; fill: var(--accent); }
.name-input {
  flex: 1;
  min-width: 120px;
  max-width: 360px;
  font: 500 15px var(--serif);
  border: 1px solid transparent;
  background: transparent;
  padding: 5px 8px;
  border-radius: 6px;
  color: var(--text);
}
.name-input:hover, .name-input:focus { border-color: var(--line); background: var(--panel-2); outline: none; }
.topbar-actions { display: flex; align-items: center; gap: 6px; margin-left: auto; flex-wrap: wrap; justify-content: flex-end; min-width: 0; }
.sep { width: 1px; height: 20px; background: var(--line); margin: 0 4px; }

/* ---------- controls ---------- */
button, .button, select, input[type="number"], input[type="text"] {
  font: inherit;
  color: var(--text);
}
button, .button {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 10px;
  cursor: pointer;
  white-space: nowrap;
  line-height: 1.3;
}
button:hover, .button:hover { border-color: var(--muted); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
select {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 4px 6px;
}
input[type="number"] {
  width: 74px;
  padding: 4px 6px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: var(--panel);
  text-align: right;
  font-variant-numeric: tabular-nums;
}
input[type="number"]:focus, select:focus { outline: 2px solid var(--accent-soft); outline-offset: 0; border-color: var(--accent); }
input[type="color"] { width: 44px; height: 26px; border: 1px solid var(--line); border-radius: 5px; padding: 1px; background: var(--panel); }
input[type="range"] { width: 100%; accent-color: var(--accent); margin: 4px 0 2px; }
input[type="checkbox"] { accent-color: var(--accent); }

/* ---------- sidebar ---------- */
.sidebar {
  overflow-y: auto;
  border-right: 1px solid var(--line);
  background: var(--panel);
  padding: 4px 0 24px;
}
.sidebar section { padding: 12px 16px 14px; border-bottom: 1px solid var(--line); }
h2 { font: 600 13px var(--serif); letter-spacing: 0.01em; margin: 0 0 8px; }
.sidebar h2 { font-size: 14px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 6px 0; }
.row.check { justify-content: flex-start; }
.with-suffix { display: inline-flex; align-items: center; gap: 5px; }
.with-suffix span { color: var(--muted); min-width: 22px; }
.hint { color: var(--muted); font-size: 12px; margin: 6px 0; }
.hint strong { color: var(--text); }
details { margin-top: 8px; }
summary { cursor: pointer; color: var(--accent); font-weight: 500; }

.presets { display: flex; flex-wrap: wrap; gap: 6px; }
.presets button { flex: 1 0 auto; }

.dims { width: 100%; border-collapse: collapse; }
.dims th { text-align: left; font-weight: 500; padding: 3px 4px 3px 0; }
.dims thead th { color: var(--muted); font-weight: 500; font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; text-align: right; padding-right: 6px; }
.dims td { padding: 2px 0 2px 4px; text-align: right; }
.dims td input { width: 78px; }
.dims td input[data-col="wet"] { border-color: color-mix(in srgb, var(--wet) 45%, var(--line)); }

#point-panel.is-empty .point-fields { display: none; }
#point-panel:not(.is-empty) .empty-msg { display: none; }
#foot-fields.off { opacity: 0.4; pointer-events: none; }

.stats { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; margin: 0 0 10px; }
.stats dt { color: var(--muted); }
.stats dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; font-weight: 500; }
.stats dd.big { font: 600 15px var(--serif); color: var(--accent); }

/* ---------- workspace ---------- */
.workspace {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 12px;
  min-height: 0;
}
.pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
}
.pane-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.pane-bar h2 { margin: 0 6px 0 2px; font-size: 14px; }
.pane-bar .check { display: inline-flex; align-items: center; gap: 4px; }
.grow { flex: 1; }
.legend { color: var(--muted); font-size: 12px; display: inline-flex; align-items: center; gap: 5px; }
.sw { display: inline-block; width: 14px; height: 10px; border-radius: 2px; }
.sw.fired { background: var(--clay-fill); border: 1.5px solid var(--clay-line); }
.sw.wet { border: 1.5px dashed var(--wet); margin-left: 6px; }
.sw.liquid { background: var(--liquid); border-top: 1.5px solid var(--liquid-line); margin-left: 6px; }
.sw.rib { background: var(--rib-fill); border: 1.5px solid var(--rib-line); }
.sw.edge { height: 3px; background: var(--accent); margin-left: 6px; }
.seg.small button { padding: 3px 9px; font-size: 12px; }
.range-labels { display: flex; justify-content: space-between; color: var(--muted); font-size: 11px; margin-top: -2px; }
.range-labels span:nth-child(2) { color: var(--text); font-weight: 500; }
.target-row { min-height: 28px; }
#target-status.ok { color: #3d7f4e; }
#target-status.short { color: var(--warn); }
.canvas-wrap { flex: 1; position: relative; min-height: 0; }
#profile { position: absolute; inset: 0; width: 100%; height: 100%; display: block; outline: none; cursor: crosshair; touch-action: none; }
#viewer { background: var(--viewer-bg); }
#viewer canvas { position: absolute; inset: 0; width: 100% !important; height: 100% !important; display: block; }
.pane-foot { margin: 0; padding: 6px 12px; color: var(--muted); font-size: 11.5px; border-top: 1px solid var(--line); }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; background: var(--panel-2); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent); color: var(--accent-ink); }

#toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translate(-50%, 20px);
  background: var(--text);
  color: var(--bg);
  padding: 8px 14px;
  border-radius: 8px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 1100px) {
  .workspace { grid-template-columns: 1fr; grid-template-rows: 1fr 1fr; }
}
@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; padding: 10px 16px; }
  .name-input { order: 3; flex-basis: 100%; max-width: none; }
  .topbar-actions { margin-left: 0; justify-content: flex-start; }
  .app { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto auto auto; height: auto; }
  .sidebar { border-right: 0; }
  .workspace { grid-template-rows: 70vh 70vh; padding: 12px 16px; }
}

/* ---------- print template ---------- */
#print-sheet { display: none; }
@media print {
  @page { margin: 10mm; }
  body { background: #fff; color: #000; }
  .app, #toast { display: none !important; }
  #print-sheet { display: block; font: 10pt/1.4 var(--sans); }
  #print-sheet h1 { font: 600 16pt var(--serif); margin: 0 0 2mm; }
  #print-sheet table { border-collapse: collapse; margin: 3mm 0; }
  #print-sheet td, #print-sheet th { border: 0.3mm solid #999; padding: 1mm 3mm; text-align: right; }
  #print-sheet th { text-align: left; }
  #print-sheet .tile { break-inside: avoid; margin-top: 4mm; }
}

#account, #account-menu { display: inline-flex; align-items: center; gap: 6px; }
.account-email { color: var(--muted); max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

dialog {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  padding: 20px;
  width: min(380px, calc(100vw - 32px));
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
}
dialog::backdrop { background: rgba(30, 20, 10, 0.35); }
dialog h2 { font-size: 17px; }
dialog input[type="email"] {
  width: 100%;
  font: inherit;
  color: var(--text);
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-2);
  margin: 6px 0 2px;
}
dialog input[type="email"]:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 10px; }

[hidden] { display: none !important; }
