/* ==========================================================================
   Custom Properties
   ========================================================================== */

:root {
  --color-primary: #5b7a3d;
  --color-primary-dark: #4a6331;
  --color-primary-light: #e8f0de;
  --color-bg: #fafaf7;
  --color-surface: #ffffff;
  --color-text: #2c2c2c;
  --color-text-muted: #6b6b6b;
  --color-border: #d4d4d4;
  --color-error: #c0392b;
  --color-error-bg: #fdecea;
  --color-waste: #e0ddd5;
  --color-kerf: #333333;
  --radius: 8px;
  --shadow: 0 2px 8px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --container-max: 840px;
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 16px;
  --space-lg: 24px;
  --space-xl: 32px;
}

/* ==========================================================================
   Dark Mode
   ========================================================================== */

[data-theme="dark"] {
  --color-primary: #7ba856;
  --color-primary-dark: #a3c97e;
  --color-primary-light: #1e3320;
  --color-bg: #0f172a;
  --color-surface: #1e293b;
  --color-text: #e2e8f0;
  --color-text-muted: #94a3b8;
  --color-border: #334155;
  --color-error: #f87171;
  --color-error-bg: #7f1d1d;
  --color-waste: #334155;
  --color-kerf: #e2e8f0;
  --shadow: 0 2px 8px rgba(0,0,0,0.3), 0 1px 2px rgba(0,0,0,0.2);
  color-scheme: dark;
}

[data-theme="dark"] body {
  background: linear-gradient(180deg, #0f172a 0%, #1e293b 100%);
}

[data-theme="dark"] .header {
  background: linear-gradient(135deg, #4a7d3a 0%, #3d6a32 60%, #4a7d3a 100%);
}

[data-theme="dark"] .header::after {
  background: var(--color-bg);
}

[data-theme="dark"] .setting input,
[data-theme="dark"] .setting select,
[data-theme="dark"] #cut-table input {
  background: #0f172a;
  color: #e2e8f0;
  border-color: #475569;
}

[data-theme="dark"] .setting input::placeholder,
[data-theme="dark"] #cut-table input::placeholder {
  color: #94a3b8;
}

[data-theme="dark"] .setting input:focus,
[data-theme="dark"] .setting select:focus,
[data-theme="dark"] #cut-table input:focus {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(123,168,86,0.2);
  background: #1e293b;
}

[data-theme="dark"] .btn--primary {
  background: var(--color-primary);
  color: #0f172a;
}

[data-theme="dark"] .btn--primary:hover {
  background: #8bbd63;
}

[data-theme="dark"] .btn--secondary {
  background: #334155;
  color: var(--color-text-muted);
}

[data-theme="dark"] .btn--secondary:hover {
  background: #475569;
}

[data-theme="dark"] .btn--outline:hover {
  background: #334155;
}

[data-theme="dark"] .btn-remove {
  color: var(--color-text-muted);
}

[data-theme="dark"] .tip {
  background: #1a2a1e;
  color: #94a3b8;
}

[data-theme="dark"] .stats {
  background: linear-gradient(135deg, #1e3320 0%, #1e293b 100%);
  border-color: rgba(123,168,86,0.2);
}

[data-theme="dark"] .board-bar__piece {
  color: #fff;
}

[data-theme="dark"] .board-bar__waste {
  background: var(--color-waste);
  color: var(--color-text-muted);
}

[data-theme="dark"] .cutlist__chip {
  background: #334155;
}

[data-theme="dark"] .recap tbody tr:hover td,
[data-theme="dark"] .cutlist tbody tr:hover td {
  background: #334155;
}

[data-theme="dark"] .recap tr:not(:last-child) td,
[data-theme="dark"] .cutlist tr:not(:last-child) td {
  border-bottom-color: #334155;
}

[data-theme="dark"] details {
  border-color: var(--color-border);
}

[data-theme="dark"] details[open] summary {
  color: var(--color-primary-dark);
}

[data-theme="dark"] .footer a {
  color: var(--color-text-muted);
}

[data-theme="dark"] .footer a:hover {
  color: var(--color-primary-dark);
}

/* ==========================================================================
   Reset & Base
   ========================================================================== */

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  line-height: 1.5;
  font-size: 16px;
}

.container {
  max-width: var(--container-max);
  margin: 0 auto;
  padding: 0 var(--space-md);
}

h1, h2, h3 { line-height: 1.2; }

/* ==========================================================================
   Utility Classes
   ========================================================================== */

.hidden { display: none !important; }
.print-only { display: none !important; }
.screen-only { display: block; }

/* ==========================================================================
   Header
   ========================================================================== */

.header {
  background: linear-gradient(160deg, #6a8c48 0%, var(--color-primary) 40%, #3d5a28 100%);
  color: #fff;
  padding: var(--space-xl) 0 calc(var(--space-xl) + 12px) 0;
  text-align: center;
  position: relative;
}

.header__inner {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

/* Theme toggle */
.theme-toggle {
  position: absolute;
  right: var(--space-md);
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: 2px solid rgba(255,255,255,0.3);
  border-radius: 6px;
  padding: 0.4rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  transition: all 0.2s ease;
  color: rgba(255,255,255,0.7);
}

.theme-toggle:hover {
  border-color: #fff;
  color: #fff;
  background: rgba(255,255,255,0.1);
}

.theme-toggle .sun-icon { display: none; }
.theme-toggle .moon-icon { display: block; }

[data-theme="dark"] .theme-toggle .sun-icon { display: block; }
[data-theme="dark"] .theme-toggle .moon-icon { display: none; }

.header::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 12px;
  background: var(--color-bg);
  border-radius: 12px 12px 0 0;
}

.header h1 {
  font-size: 1.5rem;
  margin-bottom: var(--space-xs);
  letter-spacing: -0.02em;
  text-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.header p {
  font-size: 0.95rem;
  opacity: 0.85;
  font-weight: 300;
}

.header__intro {
  font-size: 0.85rem;
  opacity: 0.7;
  margin-top: var(--space-xs);
  max-width: 520px;
  margin-left: auto;
  margin-right: auto;
}

/* ==========================================================================
   Ad Slots
   ========================================================================== */

.ad-slot {
  min-height: 90px;
  margin: var(--space-md) 0;
}

/* ==========================================================================
   Input Section
   ========================================================================== */

.input-section {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: var(--space-lg);
  margin-bottom: var(--space-lg);
  border: 1px solid rgba(0,0,0,0.04);
}

.input-section h2 {
  font-size: 1.15rem;
  margin-bottom: var(--space-md);
  color: var(--color-primary-dark);
}

.instruction {
  font-size: 0.88rem;
  color: var(--color-text-muted);
  margin-bottom: var(--space-md);
}

.tip {
  display: flex;
  align-items: flex-start;
  gap: var(--space-sm);
  font-size: 0.78rem;
  color: var(--color-text-muted);
  background: #f5f7f2;
  border-left: 3px solid var(--color-primary);
  border-radius: var(--radius);
  padding: var(--space-sm) var(--space-md);
  margin-bottom: var(--space-lg);
  line-height: 1.45;
}

.tip__icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--color-primary);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  font-style: italic;
  flex-shrink: 0;
  margin-top: 1px;
}

/* Settings Row */
.settings-row {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  margin-bottom: var(--space-lg);
}

.setting label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: var(--space-xs);
}

.setting select,
.setting input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 0.95rem;
  background: #f8f9fa;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.setting select:focus,
.setting input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(91,122,61,0.15);
  background: var(--color-surface);
}

/* Cut List Table */
.table-wrap {
  overflow-x: auto;
  margin-bottom: var(--space-md);
}

#cut-table {
  width: 100%;
  border-collapse: collapse;
}

#cut-table th {
  text-align: left;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--color-text-muted);
  padding: var(--space-sm);
  border-bottom: 2px solid var(--color-border);
}

#cut-table td {
  padding: var(--space-xs) var(--space-sm);
}

#cut-table input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 0.95rem;
  background: #f8f9fa;
  transition: border-color 0.15s, box-shadow 0.15s;
}

#cut-table input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(91,122,61,0.15);
  background: var(--color-surface);
}

.input-qty { max-width: 60px; }
.input-length { max-width: 110px; }
.input-label { min-width: 100px; }

.input--error {
  border-color: var(--color-error) !important;
  background: var(--color-error-bg) !important;
}

.btn-remove {
  background: none;
  border: none;
  font-size: 1.3rem;
  color: var(--color-text-muted);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius);
  line-height: 1;
}

.btn-remove:hover { color: var(--color-error); background: var(--color-error-bg); }

/* Error Messages */
#error-messages {
  margin-bottom: var(--space-md);
}

#error-messages:empty {
  display: none;
}

.error-msg {
  color: var(--color-error);
  font-size: 0.88rem;
  padding: var(--space-sm) var(--space-md);
  background: var(--color-error-bg);
  border-radius: var(--radius);
  margin-bottom: var(--space-xs);
}

/* Buttons */
.button-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-sm);
}

.button-row__right {
  display: flex;
  gap: var(--space-sm);
  margin-left: auto;
}

.btn {
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, box-shadow 0.15s, transform 0.1s;
}

.btn:active { transform: translateY(1px); }

.btn--primary {
  background: linear-gradient(180deg, var(--color-primary) 0%, var(--color-primary-dark) 100%);
  color: #fff;
  box-shadow: 0 2px 4px rgba(91,122,61,0.3);
}

.btn--primary:hover {
  background: linear-gradient(180deg, #6a8c48 0%, var(--color-primary) 100%);
  box-shadow: 0 3px 8px rgba(91,122,61,0.35);
}

.btn--secondary {
  background: var(--color-primary-light);
  color: var(--color-primary-dark);
}

.btn--secondary:hover { background: #d9e8c8; }

.btn--outline {
  background: transparent;
  border: 1px solid var(--color-border);
  color: var(--color-text-muted);
}

.btn--outline:hover { background: #f0f0f0; }

.btn--print {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  font-size: 0.88rem;
}

.btn--print svg { flex-shrink: 0; }

/* ==========================================================================
   Results Section
   ========================================================================== */

.results-section {
  margin-bottom: var(--space-lg);
}

/* Results Toolbar */
.results-toolbar {
  display: flex;
  justify-content: flex-end;
  margin-bottom: var(--space-md);
}

/* Recap Table */
.recap {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: var(--space-lg);
  margin-bottom: var(--space-md);
  border: 1px solid rgba(0,0,0,0.04);
}

.recap h3 {
  font-size: 1.05rem;
  margin-bottom: var(--space-md);
  color: var(--color-primary-dark);
}

.recap table {
  width: 100%;
  border-collapse: collapse;
}

.recap th, .recap td {
  text-align: left;
  padding: var(--space-xs) var(--space-sm);
  font-size: 0.9rem;
}

.recap th {
  border-bottom: 2px solid var(--color-border);
  color: var(--color-text-muted);
  font-size: 0.82rem;
}

.recap tr:not(:last-child) td {
  border-bottom: 1px solid #eee;
}

.recap tbody tr:hover td {
  background: #f8faf5;
}

.recap__color-swatch {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 2px;
  margin-right: 6px;
  vertical-align: middle;
}

/* Stats Summary */
.stats {
  background: linear-gradient(135deg, var(--color-primary-light) 0%, #f0f5e8 100%);
  border-radius: var(--radius);
  padding: var(--space-sm) var(--space-md);
  margin-bottom: var(--space-md);
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-sm);
  border: 1px solid rgba(91,122,61,0.12);
}

.stat {
  text-align: center;
  flex: 1 1 auto;
  min-width: 0;
}

.stat__value {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--color-primary-dark);
}

.stat__label {
  font-size: 0.75rem;
  color: var(--color-text-muted);
}

/* Cut Diagrams */
.diagrams {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: var(--space-md) var(--space-lg);
  margin-bottom: var(--space-md);
  border: 1px solid rgba(0,0,0,0.04);
}

.diagrams h3 {
  font-size: 1.05rem;
  margin-bottom: var(--space-md);
  color: var(--color-primary-dark);
}

.board-diagram {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: var(--space-md);
  margin-bottom: var(--space-md);
  break-inside: avoid;
  border: 1px solid rgba(0,0,0,0.04);
}

.board-diagram__label {
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: var(--space-sm);
  color: var(--color-text);
}

.board-diagram__waste {
  font-weight: 400;
  color: var(--color-text-muted);
  font-size: 0.8rem;
}

.board-bar {
  display: flex;
  height: 42px;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--color-border);
  box-shadow: inset 0 1px 2px rgba(0,0,0,0.06);
}

.board-bar__piece {
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 0.72rem;
  font-weight: 600;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  padding: 0 4px;
  min-width: 0;
}

.board-bar__kerf {
  background: var(--color-kerf);
  flex-shrink: 0;
  min-width: 2px;
}

.board-bar__waste {
  background: var(--color-waste);
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--color-text-muted);
  font-size: 0.72rem;
}

/* Cut Order List */
.cutlist {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: var(--space-lg);
  margin-bottom: var(--space-md);
  border: 1px solid rgba(0,0,0,0.04);
}

.cutlist h3 {
  font-size: 1.05rem;
  margin-bottom: var(--space-md);
  color: var(--color-primary-dark);
}

.cutlist table {
  width: 100%;
  border-collapse: collapse;
}

.cutlist th, .cutlist td {
  text-align: left;
  padding: var(--space-xs) var(--space-sm);
  font-size: 0.88rem;
  vertical-align: top;
}

.cutlist td:first-child {
  white-space: nowrap;
  font-weight: 600;
}

.cutlist th {
  border-bottom: 2px solid var(--color-border);
  color: var(--color-text-muted);
  font-size: 0.82rem;
}

.cutlist tr:not(:last-child) td {
  border-bottom: 1px solid #eee;
}

.cutlist tbody tr:hover td {
  background: #f8faf5;
}

.cutlist__chip {
  display: inline-block;
  background: #f5f5f5;
  border-radius: 4px;
  padding: 2px 8px;
  margin: 2px 4px 2px 0;
  font-size: 0.84rem;
  white-space: nowrap;
}

.cutlist__color-swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  margin-right: 4px;
  vertical-align: middle;
}

/* ==========================================================================
   FAQ Section
   ========================================================================== */

.faq-section {
  margin-bottom: var(--space-xl);
}

.faq-section h2 {
  font-size: 1.15rem;
  margin-bottom: var(--space-md);
  color: var(--color-primary-dark);
}

details {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: var(--space-sm);
  padding: var(--space-md);
  border: 1px solid rgba(0,0,0,0.04);
  transition: box-shadow 0.15s;
}

details:hover {
  box-shadow: 0 3px 12px rgba(0,0,0,0.08);
}

summary {
  font-weight: 600;
  font-size: 0.95rem;
  cursor: pointer;
  list-style: none;
}

summary::-webkit-details-marker { display: none; }
summary::before {
  content: "▸ ";
  color: var(--color-primary);
}

details[open] > summary::before { content: "▾ "; }

details p, details ol {
  margin-top: var(--space-sm);
  font-size: 0.9rem;
  color: var(--color-text-muted);
}

details ol {
  padding-left: var(--space-lg);
}

details li {
  margin-bottom: var(--space-xs);
}

/* ==========================================================================
   Footer
   ========================================================================== */

.footer {
  text-align: center;
  padding: var(--space-lg) 0;
  font-size: 0.82rem;
  color: var(--color-text-muted);
  border-top: 1px solid var(--color-border);
}

.footer p {
  margin-bottom: var(--space-xs);
}

.footer p:last-child {
  margin-bottom: 0;
}

.footer a {
  color: var(--color-text-muted);
  text-decoration: none;
  transition: color 0.2s ease;
}

.footer a:hover {
  color: var(--color-primary);
  text-decoration: underline;
}

/* ==========================================================================
   Desktop (600px+)
   ========================================================================== */

@media (min-width: 600px) {
  .header h1 { font-size: 1.8rem; }

  .settings-row {
    flex-direction: row;
  }

  .setting {
    flex: 1;
  }
}

/* ==========================================================================
   Print Styles
   ========================================================================== */

@media print {
  body { background: #fff; font-size: 11pt; }

  .header, .ad-slot, .input-section, .faq-section, .footer,
  .button-row, .screen-only, .results-toolbar {
    display: none !important;
  }

  .print-only {
    display: block !important;
  }

  .hidden {
    /* results-section gets .hidden removed by JS, but ensure print works */
  }

  #results-section {
    display: block !important;
  }

  .results-section {
    margin: 0;
  }

  .container {
    max-width: 100%;
    padding: 0;
  }

  .board-diagram {
    box-shadow: none;
    border: 1px solid #ccc;
    break-inside: avoid;
  }

  .recap, .cutlist, .stats, .diagrams {
    box-shadow: none;
    border: 1px solid #ccc;
  }

  .stats {
    background: #f5f5f5;
  }

  #print-header {
    text-align: center;
    margin-bottom: 16px;
  }

  #print-header h2 {
    font-size: 1.3rem;
  }

  #print-shopping-list {
    border: 1px solid #ccc;
    padding: 12px;
    margin-bottom: 16px;
    break-inside: avoid;
  }

  #print-shopping-list h3 {
    font-size: 1rem;
    margin-bottom: 8px;
  }

  #print-shopping-list ul {
    padding-left: 20px;
    font-size: 0.9rem;
  }

  #print-shopping-list li {
    margin-bottom: 2px;
  }
}
