:root {
  --bg: #23201c;
  --bg2: #2d2925;
  --panel: #2a2622;
  --line: #453e37;
  --text: #ece5da;
  --muted: #a89c8d;
  --accent: #e8973a;
  --accent-dim: #b06f22;
  --danger: #d05b4a;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font: 13px/1.45 "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

#app { display: flex; flex-direction: column; height: 100vh; }

/* ---- top bar ---- */
#topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 10px;
  background: var(--bg2);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.brand { font-size: 15px; white-space: nowrap; }
.brand b { color: var(--accent); letter-spacing: 0.2px; }
.brand .sub { color: var(--muted); font-size: 11px; margin-left: 7px; }
#project-name {
  background: var(--bg); color: var(--text);
  border: 1px solid var(--line); border-radius: 5px;
  padding: 5px 9px; width: 190px; font-size: 13px;
}
#project-name:focus { outline: 1px solid var(--accent-dim); }
.spacer { flex: 1; }
.menu-group, .view-group { display: flex; gap: 4px; align-items: center; }

button {
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 5px;
  padding: 5px 10px; cursor: pointer; font-size: 12.5px;
  font-family: inherit;
}
button:hover { border-color: var(--accent-dim); color: #fff; }
button:disabled { opacity: 0.4; cursor: default; }
button:disabled:hover { border-color: var(--line); }
button.primary { background: var(--accent-dim); border-color: var(--accent-dim); color: #fff; }
button.primary:hover { background: var(--accent); }
button.danger { border-color: #6b3a32; color: #f0b0a5; }
button.danger:hover { background: var(--danger); color: #fff; }

select {
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 5px;
  padding: 5px 6px; font-size: 12.5px; font-family: inherit;
}

/* dropdown */
.dropdown { position: relative; }
.dropdown-body {
  display: none; position: absolute; top: calc(100% + 4px); left: 0;
  min-width: 240px; max-height: 380px; overflow: auto;
  background: var(--bg2); border: 1px solid var(--line); border-radius: 7px;
  box-shadow: 0 10px 28px rgba(0,0,0,0.5);
  z-index: 40; padding: 5px;
}
.dropdown.open .dropdown-body { display: block; }
.dropdown-body button {
  display: block; width: 100%; text-align: left; border: none;
  background: none; padding: 7px 9px; border-radius: 5px;
}
.dropdown-body button:hover { background: var(--panel); }
.dropdown-body button b { display: block; }
.dropdown-body button span { color: var(--muted); font-size: 11px; }
.dd-row { display: flex; align-items: center; }
.dd-row > button:first-child { flex: 1; }
.dd-del { width: 26px; flex: none !important; color: var(--muted); text-align: center !important; }
.dd-del:hover { color: #fff; background: var(--danger) !important; }
.dd-empty { color: var(--muted); padding: 8px; }

/* ---- main layout ---- */
#main { flex: 1; display: flex; min-height: 0; }

#toolbar {
  display: flex; flex-direction: column; gap: 6px;
  padding: 8px 6px; background: var(--bg2);
  border-right: 1px solid var(--line);
}
#toolbar button {
  width: 40px; height: 40px; padding: 8px;
  display: grid; place-items: center;
}
#toolbar svg { width: 22px; height: 22px; }
#toolbar button.active { background: var(--accent-dim); border-color: var(--accent); color: #fff; }

#viewport { flex: 1; position: relative; min-width: 0; }
#viewport canvas { display: block; }

.measure-label {
  position: absolute; top: 0; left: 0;
  background: #c53321; color: #fff;
  font-size: 11.5px; font-weight: 600;
  padding: 2px 7px; border-radius: 4px;
  pointer-events: auto; white-space: nowrap;
  margin: -10px 0 0 8px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.4);
  z-index: 5;
}

/* ---- sidebar ---- */
#sidebar {
  width: 314px; background: var(--bg2);
  border-left: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
}
#tabs { display: flex; border-bottom: 1px solid var(--line); }
#tabs button {
  flex: 1; border: none; border-radius: 0; background: none;
  padding: 9px 2px; color: var(--muted); font-size: 12.5px;
  border-bottom: 2px solid transparent;
}
#tabs button.active { color: var(--text); border-bottom-color: var(--accent); }
#tab-content { flex: 1; overflow-y: auto; min-height: 0; }
.tab { display: none; }
.tab.active { display: block; }

.panel-block { padding: 12px 14px; border-bottom: 1px solid var(--line); }
.panel-block h3 { font-size: 13.5px; margin-bottom: 8px; color: var(--accent); }
.panel-block h4 { font-size: 12px; margin: 12px 0 6px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.4px; }
.muted { color: var(--muted); }
.small { font-size: 11.5px; }
.warn { color: #f0b06a; }

.field { display: flex; flex-direction: column; gap: 3px; margin-bottom: 8px; }
.field span { font-size: 11px; color: var(--muted); }
.field input, .field select {
  background: var(--bg); color: var(--text);
  border: 1px solid var(--line); border-radius: 5px;
  padding: 6px 8px; font-size: 12.5px; width: 100%;
  font-family: inherit;
}
.field input:focus, .field select:focus { outline: 1px solid var(--accent-dim); }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 7px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; }
.btn-row { display: flex; gap: 6px; margin: 8px 0; flex-wrap: wrap; }
.btn-row button { flex: 1; white-space: nowrap; }
.check { display: flex; gap: 7px; align-items: center; margin: 8px 0; cursor: pointer; }

#stock-list { display: flex; flex-direction: column; gap: 5px; }
.stock-item { text-align: left; padding: 7px 10px; }
.stock-item b { display: block; font-size: 12.5px; }
.stock-item span { color: var(--muted); font-size: 11px; }

.cut-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 6px; padding: 5px 8px; margin-bottom: 4px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 5px;
  font-size: 12px;
}
.cut-del { padding: 1px 8px; color: var(--muted); }
.cut-del:hover { background: var(--danger); color: #fff; }

/* cut list table */
table.cutlist { width: 100%; border-collapse: collapse; font-size: 12px; margin-bottom: 8px; }
table.cutlist th, table.cutlist td {
  text-align: left; padding: 5px 6px;
  border-bottom: 1px solid var(--line);
}
table.cutlist th { color: var(--muted); font-weight: 600; font-size: 11px; text-transform: uppercase; }
table.cutlist td.dims { white-space: nowrap; }
.totals { font-size: 12.5px; display: flex; flex-direction: column; gap: 3px; margin: 8px 0; }
.totals .grand { margin-top: 5px; padding-top: 7px; border-top: 1px solid var(--line); font-size: 13.5px; }
.field.price { flex-direction: row; align-items: center; justify-content: space-between; }
.field.price span { flex: 1; }
.field.price input { width: 90px; }

/* sheet + stick diagrams */
.sheet-wrap, .stick-wrap { margin: 8px 0; }
.sheet-svg { width: 100%; height: auto; display: block; border-radius: 4px; margin-top: 3px; }
.sheet-bg { fill: #d8c9a3; stroke: #8a7c5c; }
.sheet-part { fill: #b98d4f; stroke: #6f5426; stroke-width: 1; }
.sheet-label { fill: #2f2413; font-size: 10px; text-anchor: middle; dominant-baseline: middle; }
.stick-svg { height: 22px; display: block; margin-top: 3px; min-width: 40%; }
.stick-waste { fill: #6b6155; stroke: #8a7c5c; }
.stick-cut { fill: #b98d4f; stroke: #6f5426; stroke-width: 1; }
.stick-label { fill: #2f2413; font-size: 9px; text-anchor: middle; dominant-baseline: middle; }
.stick-waste-label { fill: #ded4c6; }

/* buy list + optimizer suggestions */
.buy-list { list-style: none; font-size: 12.5px; }
.buy-list li { padding: 4px 0 4px 14px; position: relative; border-bottom: 1px solid var(--line); }
.buy-list li::before { content: "▸"; position: absolute; left: 0; color: var(--accent); }
.sug-row {
  display: flex; gap: 8px; align-items: flex-start;
  padding: 7px 9px; margin-bottom: 6px;
  background: var(--bg); border: 1px solid var(--line);
  border-left: 3px solid var(--accent); border-radius: 5px;
}
.sug-text { flex: 1; font-size: 12px; line-height: 1.4; }
.sug-save { display: block; color: #86c98a; font-size: 11px; margin-top: 2px; }
.sug-apply { flex: none; align-self: center; }

/* dimensioned plan drawings */
.plan-svg { width: 100%; height: auto; display: block; margin: 2px 0 4px; }
.pl-outline { fill: #f0e6d2; stroke: #2a2118; stroke-width: 1.4; }
.pl-outline-thin { stroke: #2a2118; stroke-width: 0.7; fill: none; }
.pl-cut { stroke: #2a2118; stroke-width: 0.9; fill: none; }
.pl-hidden { stroke: #7a6a55; stroke-width: 0.8; fill: none; stroke-dasharray: 4 3; }
.pl-dim, .pl-ext { stroke: #b4462c; stroke-width: 0.7; fill: none; }
.pl-ext { stroke-dasharray: 2 2; }
.pl-fill { fill: #b4462c; }
.pl-txt { fill: #b4462c; font-size: 10px; }
.pl-note { fill: #6b5a44; font-size: 9.5px; }

/* ---- status bar ---- */
#statusbar {
  display: flex; gap: 18px; align-items: center;
  padding: 5px 12px; background: var(--bg2);
  border-top: 1px solid var(--line);
  font-size: 12px; color: var(--muted);
  min-height: 28px;
}
#status-coords { color: var(--accent); font-variant-numeric: tabular-nums; }
#status-selection { margin-left: auto; color: var(--text); }

/* ---- help modal ---- */
#help-modal {
  display: none; position: fixed; inset: 0;
  background: rgba(10, 8, 6, 0.6); z-index: 100;
  align-items: center; justify-content: center;
}
#help-modal.open { display: flex; }
.modal-card {
  width: min(620px, 92vw); max-height: 86vh; overflow: auto;
  background: var(--bg2); border: 1px solid var(--line); border-radius: 10px;
  padding: 22px 26px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.6);
}
.modal-card h2 { font-size: 17px; margin-bottom: 8px; color: var(--accent); }
.help-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 26px; margin: 14px 0; }
.help-cols dl { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; align-content: start; }
.help-cols dt { color: var(--accent); font-weight: 600; white-space: nowrap; }
.help-cols dd { color: var(--text); }
.modal-card code { background: var(--bg); padding: 1px 5px; border-radius: 4px; }
.modal-card .primary { margin-top: 10px; }

/* ---- print ---- */
#print-area { display: none; }
@media print {
  @page { margin: 0.5in; }
  #app, #help-modal { display: none !important; }
  body { background: #fff; color: #000; overflow: visible; }
  #print-area { display: block; }
  #print-area h1 { font-size: 21px; margin-bottom: 2px; }
  #print-area h2 {
    font-size: 15px; margin: 16px 0 8px;
    padding-bottom: 3px; border-bottom: 1.5px solid #333;
  }
  #print-area h2.page-break { page-break-before: always; }
  #print-area h4 { font-size: 12px; margin: 10px 0 4px; color: #333; }
  #print-area .plans-sub { font-size: 11px; color: #555; margin-bottom: 10px; }
  #print-area table.cutlist { font-size: 11px; }
  #print-area table.cutlist th, #print-area table.cutlist td {
    border-bottom: 1px solid #999; color: #000;
  }
  #print-area .totals, #print-area .buy-list { color: #000; font-size: 12px; }
  #print-area .buy-list li { border-bottom: 1px solid #ddd; }
  #print-area .buy-list li::before { color: #666; }
  #print-area .muted { color: #444; }
  #print-area .warn { color: #a5500f; }

  /* assembly views */
  .views-grid {
    display: grid; grid-template-columns: 1fr 1fr; gap: 10px;
    margin-bottom: 8px; page-break-inside: avoid;
  }
  .views-grid figure { border: 1px solid #bbb; padding: 4px; }
  .views-grid figure.views-iso { grid-column: 1 / -1; }
  .views-grid img { width: 100%; height: auto; display: block; }
  .views-grid figcaption { font-size: 10px; color: #555; text-align: center; margin-top: 3px; }

  /* per-part drawings */
  .plan-part { page-break-inside: avoid; margin-bottom: 12px; }
  .plan-part-head { font-size: 11.5px; margin-bottom: 2px; }
  .plan-cuts { display: block; font-size: 10px; color: #555; }
  #print-area .plan-svg { max-width: 6.9in; }
  #print-area .pl-outline { fill: #f7f2e8; }

  /* cutting diagrams */
  .sheet-wrap, .stick-wrap { page-break-inside: avoid; }
  #print-area .sheet-bg { fill: #f3ecd9; }
  #print-area .sheet-part { fill: #ded0ae; stroke: #6f5426; }
  #print-area .sheet-svg { max-width: 6.9in; }
  #print-area .stick-cut { fill: #ded0ae; }
  #print-area .stick-waste { fill: #e8e4dc; }
  #print-area .stick-waste-label { fill: #666; }
}

/* small screens */
@media (max-width: 900px) {
  #sidebar { width: 260px; }
  .brand .sub { display: none; }
}
