:root {
  --navy: #2c3e46;
  --navy-dark: #24333a;
  --blue: #2f7fd1;
  --blue-dark: #2568ab;
  --red: #d64545;
  --border: #dde3e7;
  --text: #2c3e46;
  --text-light: #7c8a92;
  --bg-sidebar: #ffffff;
  --bg-viewport: #eef1f3;
  --panel-fill: #e9dcef;
  --panel-fill2: #dcece5;
  --panel-fill3: #fbe9d0;
  --panel-fill4: #d7e6f2;
  --panel-fill5: #f2d7db;
  --waste-fill: #f6f7f8;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  font-size: 14px;
}

button { font-family: inherit; cursor: pointer; }
input, select { font-family: inherit; }

/* ---------- Top bar ---------- */
.topbar {
  height: 48px;
  background: var(--navy);
  color: #fff;
  display: flex;
  align-items: center;
  padding: 0 14px;
  gap: 12px;
  position: relative;
  z-index: 50;
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 8px; font-size: 17px; font-weight: 600; }
.brand-icon { color: var(--blue); font-size: 19px; }
.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }

.btn {
  border: none;
  border-radius: 4px;
  padding: 8px 14px;
  font-size: 14px;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.btn-primary { background: var(--blue); color: #fff; }
.btn-primary:hover { background: var(--blue-dark); }
.btn-green { background: #2e9e50; color: #fff; }
.btn-green:hover { background: #257f40; }
.btn-secondary { background: #eef1f3; color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: #e2e7ea; }
.icon-btn {
  background: transparent;
  border: none;
  color: inherit;
  font-size: 16px;
  padding: 6px 8px;
  border-radius: 4px;
  line-height: 1;
}
.topbar .icon-btn { color: #fff; }
.icon-btn:hover { background: rgba(255,255,255,0.12); }
.icon-btn.small { font-size: 13px; color: var(--text-light); }
.icon-btn.small:hover { background: rgba(0,0,0,0.06); }

.popover {
  position: absolute;
  top: 48px;
  right: 90px;
  background: #fff;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.18);
  padding: 10px;
  min-width: 160px;
  z-index: 100;
}
.popover-right { right: 44px; }
.popover.hidden { display: none; }
.popover-title { font-weight: 600; margin-bottom: 6px; font-size: 13px; color: var(--text-light); text-transform: uppercase; }
.radio-row { display: flex; align-items: center; gap: 6px; padding: 4px 2px; font-size: 14px; }
.popover-wide { width: 300px; max-height: 80vh; overflow-y: auto; right: 14px; }
.popover-section { padding-bottom: 10px; margin-bottom: 10px; border-bottom: 1px solid var(--border); }
.popover-section:last-child { border-bottom: none; margin-bottom: 0; padding-bottom: 0; }
.popover-item {
  display: block; width: 100%; text-align: left; background: none; border: none;
  padding: 8px 6px; font-size: 14px; border-radius: 4px; color: var(--text);
}
.popover-item:hover { background: #f2f4f5; }
.about-text { font-size: 13px; line-height: 1.5; color: var(--text); margin: 0 0 10px; }
.about-credit { font-size: 13px; color: var(--text-light); margin: 0; }
.about-credit a { color: var(--blue); text-decoration: none; }
.about-credit a:hover { text-decoration: underline; }
#aboutPopover { width: 260px; }

/* ---------- Layout ---------- */
.app-body {
  display: flex;
  height: calc(100% - 48px);
  overflow: hidden;
}

.sidebar {
  background: var(--bg-sidebar);
  overflow-y: auto;
  flex-shrink: 0;
  border-color: var(--border);
}
.sidebar-left { width: 320px; border-right: 1px solid var(--border); }
.sidebar-right { width: 340px; border-left: 1px solid var(--border); }

.panel-section { border-bottom: 1px solid var(--border); }
.section-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  cursor: pointer;
  user-select: none;
  background: #f7f8f9;
}
.section-icon { color: var(--text-light); }
.section-title { font-weight: 600; font-size: 14px; }
.section-spacer { flex: 1; }
.chevron { color: var(--text-light); transition: transform 0.15s; font-size: 12px; }
.panel-section.collapsed .chevron { transform: rotate(-90deg); }
.panel-section.collapsed .section-body { display: none; }
.section-body { padding: 10px 14px 14px; }

/* ---------- Tables ---------- */
.data-table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.data-table th {
  text-align: left;
  color: var(--text-light);
  font-weight: 600;
  padding: 4px 4px;
  border-bottom: 1px solid var(--border);
  font-size: 12.5px;
  text-transform: none;
}
.data-table td { padding: 2px 3px; vertical-align: middle; }
.data-table tbody tr:hover { background: #f7f9fa; }
.data-table input[type=number], .data-table input[type=text] {
  width: 100%;
  border: 1px solid transparent;
  background: transparent;
  padding: 5px 4px;
  border-radius: 3px;
  font-size: 13.5px;
  color: var(--text);
}
.data-table input:focus {
  border-color: var(--blue);
  background: #fff;
  outline: none;
}
.actions-col { width: 44px; }
.row-actions { display: flex; gap: 2px; align-items: center; white-space: nowrap; }
.row-check { color: #3aa757; font-size: 14px; }
.row-del {
  background: none; border: none; color: var(--red); font-size: 14px;
  padding: 2px 4px; border-radius: 3px;
}
.row-del:hover { background: #fbe4e4; }

/* ---------- Viewport ---------- */
.viewport {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg-viewport);
}
.viewport-toolbar {
  display: flex;
  gap: 2px;
  padding: 6px 10px;
  background: #fff;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.viewport-toolbar .icon-btn { color: var(--text); font-size: 15px; }
.viewport-toolbar .icon-btn.active { background: #dfeaf7; color: var(--blue-dark); }

.viewport-scroll {
  flex: 1;
  overflow: auto;
  padding: 24px;
}
.empty-state {
  display: flex; align-items: center; justify-content: center;
  height: 100%; color: var(--text-light); text-align: center;
  font-size: 15px;
}
.empty-state.hidden { display: none; }

.error-content { text-align: left; max-width: 440px; }
.error-title {
  color: var(--red);
  font-weight: 600;
  font-size: 16px;
  text-align: center;
  margin-bottom: 14px;
}
.error-list {
  margin: 0 0 18px;
  padding-left: 20px;
  color: var(--text);
  font-size: 14px;
  text-align: left;
}
.error-list li { margin-bottom: 4px; }
.error-actions { display: flex; gap: 10px; justify-content: center; }

#sheetsContainer { display: flex; flex-direction: column; gap: 40px; align-items: center; }
#sheetsContainer.hidden { display: none; }

.sheet-block { display: flex; flex-direction: column; align-items: center; }
.sheet-label {
  font-size: 13.5px; color: var(--text-light); margin-bottom: 8px; font-weight: 500;
}
.sheet-svg-wrap { position: relative; }

.dim-text { font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; fill: #33474f; }
.dim-text-waste { fill: #8a97a0; }
.dim-outer { stroke: var(--red); fill: var(--red); }
.panel-label { font-size: 12px; fill: #33474f; pointer-events: none; }
.panel-rect { stroke: #55606a; stroke-width: 1.2; cursor: pointer; transition: stroke 0.12s, stroke-width 0.12s; }
.panel-rect:hover { stroke: #ff8f00; stroke-width: 2.5; }
.dim-highlight { stroke: #ff8f00; stroke-width: 3; stroke-linecap: round; pointer-events: none; transition: opacity 0.12s ease; }
.grain-line { stroke: rgba(0,0,0,0.14); stroke-width: 1; }
.edge-band { fill: none; stroke: #d98a2b; stroke-width: 3; }
.waste-rect { fill: var(--waste-fill); }
.waste-rect.hatch { fill: url(#hatchPattern); }

#listView { width: 100%; max-width: 900px; }
#listView.hidden { display: none; }
#listView table { width: 100%; border-collapse: collapse; background: #fff; }
#listView th, #listView td { border: 1px solid var(--border); padding: 6px 10px; font-size: 13.5px; text-align: left; }
#listView th { background: #f7f8f9; }

/* ---------- Stats ---------- */
.stats-grid { display: grid; grid-template-columns: 1fr auto; gap: 6px 10px; font-size: 13.5px; }
.stats-grid .k { color: var(--text-light); }
.stats-grid .v { font-weight: 600; text-align: right; white-space: nowrap; }
.stats-empty { color: var(--text-light); font-size: 13.5px; }

.cuts-table { font-size: 12.5px; }
.cuts-table th, .cuts-table td { padding: 5px 4px; border-bottom: 1px solid var(--border); }
.cuts-table td { color: var(--text); }
.cuts-table td.muted { color: var(--text-light); }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--navy-dark);
  color: #fff;
  padding: 10px 18px;
  border-radius: 6px;
  font-size: 14px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.25);
  z-index: 200;
  transition: opacity 0.2s;
}
.toast.hidden { opacity: 0; pointer-events: none; }

/* ---------- Mobile edit bar (hidden on desktop) ---------- */
.mobile-edit-bar { display: none; }

/* ---------- Mobile layout ---------- */
@media (max-width: 860px) {
  html, body { height: auto; }
  .app-body {
    flex-direction: column;
    height: auto;
    overflow: visible;
  }
  .sidebar-left, .sidebar-right {
    width: 100%;
    max-height: 40vh;
    border-right: none;
    border-left: none;
    border-bottom: 1px solid var(--border);
  }
  .viewport { min-height: 70vh; }

  /* While a result (or error) is shown, hide the side tabs so the
     cutting diagram gets the full screen on small devices. */
  body.has-result .sidebar-left,
  body.has-result .sidebar-right {
    display: none;
  }
  body.has-result .mobile-edit-bar {
    display: flex;
    padding: 8px 10px;
    background: #fff;
    border-bottom: 1px solid var(--border);
  }
  body.has-result.show-inputs .sidebar-left,
  body.has-result.show-inputs .sidebar-right {
    display: block;
  }
  body.has-result.show-inputs .viewport-toolbar,
  body.has-result.show-inputs .viewport-scroll {
    display: none;
  }
  body.has-result.show-inputs .viewport {
    min-height: 0;
  }
}

/* ---------- Print: only the cutting diagram ---------- */
@media print {
  .topbar,
  .sidebar-left,
  .sidebar-right,
  .viewport-toolbar,
  .toast,
  .popover,
  #emptyState,
  #errorState {
    display: none !important;
  }
  html, body, .app-body, .viewport, .viewport-scroll {
    height: auto !important;
    overflow: visible !important;
  }
  .viewport-scroll { padding: 0 !important; }
  .sheet-block { page-break-inside: avoid; break-inside: avoid; }
}
