*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #1a1a2e;
  --bg-panel: #222240;
  --bg-input: #2a2a4a;
  --text: #e0e0e0;
  --text-dim: #999;
  --accent: #4ecdc4;
  --accent-hover: #3db8b0;
  --danger: #e05555;
  --danger-hover: #c44444;
  --border: #3a3a5c;
  --radius: 6px;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

.layout {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

.header {
  text-align: center;
  padding: 1rem 1rem 0.5rem;
  flex-shrink: 0;
}
.header h1 {
  font-size: 1.4rem;
  color: var(--accent);
  font-weight: 600;
}
.subtitle {
  color: var(--text-dim);
  font-size: 0.85rem;
}

.main {
  display: flex;
  flex: 1;
  gap: 1rem;
  padding: 0.75rem 1rem 1rem;
  min-height: 0;
  overflow: hidden;
}

.panel {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  overflow-y: auto;
}

.panel-inputs {
  width: 32%;
  min-width: 260px;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.panel-results {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.input-section h2, .results-section h2 {
  font-size: 1rem;
  color: var(--accent);
  margin-bottom: 0.5rem;
  font-weight: 500;
}

.field-row {
  display: flex;
  gap: 0.75rem;
}
.field-row label {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
.field-row label span {
  font-size: 0.8rem;
  color: var(--text-dim);
}

.input-wrap {
  display: flex;
  gap: 4px;
}

input[type="number"] {
  width: 100%;
  padding: 0.4rem 0.5rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.9rem;
  outline: none;
}
input[type="number"]:focus {
  border-color: var(--accent);
}

.unit-select {
  padding: 0.4rem 0.3rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.8rem;
  cursor: pointer;
}

.unit-label {
  display: flex;
  align-items: center;
  padding: 0 0.3rem;
  color: var(--text-dim);
  font-size: 0.8rem;
  white-space: nowrap;
}

.dim-mode-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
}
.dim-mode-label {
  font-size: 0.8rem;
  color: var(--text-dim);
}
.dim-mode-toggle {
  display: flex;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.mode-btn {
  padding: 0.3rem 0.7rem;
  background: var(--bg-input);
  border: none;
  color: var(--text-dim);
  font-size: 0.8rem;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.mode-btn + .mode-btn {
  border-left: 1px solid var(--border);
}
.mode-btn.active {
  background: var(--accent);
  color: #111;
  font-weight: 600;
}

.thickness-fields {
  margin-bottom: 0.4rem;
  padding: 0.5rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.thickness-fields .field-row {
  margin-bottom: 0.25rem;
}
.thickness-fields.hidden {
  display: none;
}
.thickness-note {
  font-size: 0.75rem;
  color: var(--text-dim);
  margin-bottom: 0.4rem;
}
.thickness-hint {
  font-size: 0.7rem;
  color: var(--text-dim);
  opacity: 0.7;
  margin-top: 0.2rem;
}

.shelf-list {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  max-height: 40vh;
  overflow-y: auto;
  padding-right: 4px;
}

.shelf-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.4rem 0.5rem;
}
.shelf-row .shelf-label {
  font-size: 0.75rem;
  color: var(--accent);
  min-width: 1.5rem;
  font-weight: 600;
}
.shelf-row input {
  width: 60px;
  padding: 0.3rem 0.4rem;
  font-size: 0.85rem;
}
.shelf-row .dim-x {
  color: var(--text-dim);
  font-size: 0.8rem;
}
.shelf-row .shelf-unit {
  color: var(--text-dim);
  font-size: 0.75rem;
  min-width: 1.5rem;
}
.shelf-row .qty-label {
  color: var(--text-dim);
  font-size: 0.75rem;
  margin-left: 0.3rem;
}
.shelf-row .qty-input {
  width: 40px;
}
.shelf-remove {
  background: none;
  border: none;
  color: var(--danger);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 0 0.2rem;
  line-height: 1;
}
.shelf-remove:hover {
  color: var(--danger-hover);
}

.btn {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 500;
  transition: background 0.15s;
}
.btn-add {
  background: var(--bg-input);
  color: var(--accent);
  border: 1px dashed var(--border);
  width: 100%;
}
.btn-add:hover { border-color: var(--accent); }

.btn-calculate {
  background: var(--accent);
  color: #111;
  flex: 1;
}
.btn-calculate:hover { background: var(--accent-hover); }

.btn-clear {
  background: var(--bg-input);
  color: var(--danger);
  border: 1px solid var(--border);
}
.btn-clear:hover { border-color: var(--danger); }

.actions {
  display: flex;
  gap: 0.5rem;
  margin-top: auto;
  padding-top: 0.5rem;
}

.results-section {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

.stats {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 0.75rem;
}
.stat-card {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.4rem 0.75rem;
  flex: 1;
  min-width: 100px;
}
.stat-card .stat-value {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--accent);
}
.stat-card .stat-label {
  font-size: 0.7rem;
  color: var(--text-dim);
}

.canvas-container {
  flex: 1;
  min-height: 0;
  position: relative;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--bg);
}
#plan-canvas {
  width: 100%;
  height: 100%;
  display: block;
}

.cut-list {
  max-height: 20%;
  overflow-y: auto;
  margin-top: 0.5rem;
  font-size: 0.8rem;
}
.cut-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.25rem 0.4rem;
  border-bottom: 1px solid var(--border);
}
.cut-swatch {
  width: 12px;
  height: 12px;
  border-radius: 2px;
  flex-shrink: 0;
}
.cut-text {
  color: var(--text-dim);
}
.cut-text strong {
  color: var(--text);
  font-weight: 500;
}

/* Comparison table */
.comparison-table {
  margin-bottom: 0.5rem;
  flex-shrink: 0;
}
.comparison-table table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
}
.comparison-table thead th {
  text-align: left;
  padding: 0.35rem 0.5rem;
  color: var(--text-dim);
  font-weight: 500;
  border-bottom: 1px solid var(--border);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.comparison-table .comp-row {
  cursor: pointer;
  transition: background 0.12s;
}
.comparison-table .comp-row:hover {
  background: rgba(78, 205, 196, 0.08);
}
.comparison-table .comp-row td {
  padding: 0.35rem 0.5rem;
  border-bottom: 1px solid var(--border);
  color: var(--text-dim);
}
.comparison-table .comp-row td strong {
  color: var(--text);
  font-weight: 500;
}
.comparison-table .method-desc {
  display: block;
  font-size: 0.7rem;
  color: var(--text-dim);
  opacity: 0.7;
}
.comparison-table .comp-row.active-row {
  background: rgba(78, 205, 196, 0.12);
}
.comparison-table .comp-row.active-row td {
  color: var(--text);
}
.comparison-table .comp-row.best-row td:first-child strong::after {
  content: ' \2605';
  color: var(--accent);
  font-size: 0.75rem;
}
.comparison-table .comp-fail {
  color: var(--danger);
  font-weight: 600;
}

/* Side-by-side comparison */
.compare-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.5rem;
  flex-shrink: 0;
}
.compare-header h2 {
  font-size: 1rem;
  color: var(--accent);
  font-weight: 500;
  white-space: nowrap;
}
.compare-selectors {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.method-select {
  padding: 0.3rem 0.5rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.8rem;
  cursor: pointer;
  max-width: 180px;
}
.compare-vs {
  color: var(--text-dim);
  font-size: 0.8rem;
  font-style: italic;
}

.compare-panels {
  display: flex;
  gap: 0.75rem;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}
.compare-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  overflow: hidden;
}
.compare-panel-header {
  padding: 0.4rem 0.6rem;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.compare-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--accent);
}
.compare-stats {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  padding: 0.4rem 0.5rem;
  flex-shrink: 0;
}
.compare-stats .stat-card {
  padding: 0.25rem 0.5rem;
  min-width: 70px;
}
.compare-stats .stat-card .stat-value {
  font-size: 0.9rem;
}
.compare-stats .stat-card .stat-label {
  font-size: 0.65rem;
}
.compare-canvas-container {
  flex: 1;
  min-height: 0;
  position: relative;
  overflow: hidden;
}
.compare-canvas-container canvas {
  width: 100%;
  height: 100%;
  display: block;
}
.compare-cut-list {
  max-height: 25%;
  overflow-y: auto;
  border-top: 1px solid var(--border);
  font-size: 0.75rem;
}
.compare-cut-list .cut-item {
  padding: 0.2rem 0.4rem;
}

/* Saved calculations */
.saved-list {
  max-height: 20vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}
.empty-saved {
  color: var(--text-dim);
  font-size: 0.8rem;
  font-style: italic;
  padding: 0.3rem 0;
}
.saved-item {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.35rem 0.5rem;
  cursor: pointer;
  transition: border-color 0.15s;
}
.saved-item:hover {
  border-color: var(--accent);
}
.saved-item-info {
  flex: 1;
  min-width: 0;
}
.saved-item-name {
  font-size: 0.8rem;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.saved-item-detail {
  font-size: 0.7rem;
  color: var(--text-dim);
}
.saved-item-delete {
  background: none;
  border: none;
  color: var(--danger);
  font-size: 0.9rem;
  cursor: pointer;
  padding: 0 0.2rem;
  line-height: 1;
  flex-shrink: 0;
}
.saved-item-delete:hover {
  color: var(--danger-hover);
}

.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-dim);
  font-size: 0.9rem;
  text-align: center;
  padding: 2rem;
}

@media (max-width: 700px) {
  .main {
    flex-direction: column;
  }
  .panel-inputs {
    width: 100%;
    min-width: auto;
  }
  .shelf-list {
    max-height: 25vh;
  }
  .compare-panels {
    flex-direction: column;
  }
  .compare-header {
    flex-direction: column;
    align-items: flex-start;
  }
}
