:root {
  --indigo: #6366f1;
  --indigo-dark: #4f46e5;
  --purple: #9333ea;
  --red: #ef4444;
  --blue: #3b82f6;
  --radius: 0.75rem;
  --radius-lg: 1rem;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  transition: background-color 0.2s ease, color 0.2s ease;
}

body.light {
  background: #f9fafb;
  color: #1f2937;
}

body.dark {
  background: #111827;
  color: #f3f4f6;
}

.app {
  max-width: 1280px;
  margin: 0 auto;
  padding: 1rem;
}

/* HEADER */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.75rem;
  padding-bottom: 1rem;
  margin-bottom: 1.5rem;
  border-bottom: 1px solid #e5e7eb;
}
body.dark .header { border-bottom-color: #1f2937; }

.logo {
  font-size: 1.5rem;
  font-weight: 900;
  letter-spacing: -0.02em;
  color: var(--indigo);
  margin: 0;
}
.logo-suffix { color: #111827; }
body.dark .logo-suffix { color: #fff; }

.tagline {
  font-size: 0.75rem;
  color: #9ca3af;
  margin: 0.15rem 0 0;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.method-toggle {
  display: flex;
  border-radius: 0.5rem;
  padding: 0.25rem;
  background: #e5e7eb;
  font-size: 0.875rem;
}
body.dark .method-toggle { background: #1f2937; }

.method-btn {
  padding: 0.375rem 0.75rem;
  border-radius: 0.375rem;
  font-weight: 500;
  border: none;
  background: transparent;
  color: #9ca3af;
  cursor: pointer;
  transition: all 0.15s ease;
}
.method-btn:hover { color: #4b5563; }
body.dark .method-btn:hover { color: #d1d5db; }
.method-btn.active {
  background: var(--indigo-dark);
  color: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}
.method-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem;
  border-radius: 0.5rem;
  border: 1px solid #d1d5db;
  background: #fff;
  color: var(--indigo);
  cursor: pointer;
}
.icon-btn:hover { background: #f3f4f6; }
body.dark .icon-btn {
  border-color: #374151;
  background: #1f2937;
  color: #facc15;
}
body.dark .icon-btn:hover { background: #374151; }

.icon { display: inline-flex; width: 18px; height: 18px; }
.icon svg { width: 100%; height: 100%; }

/* GRID */
.grid-3col {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1.5rem;
  margin-bottom: 1.5rem;
}
@media (min-width: 1024px) {
  .grid-3col { grid-template-columns: repeat(3, 1fr); }
}

.panel {
  padding: 1rem;
  border-radius: var(--radius-lg);
  border: 1px solid #e5e7eb;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0,0,0,0.04);
  display: flex;
  flex-direction: column;
}
body.dark .panel {
  background: rgba(31,41,55,0.5);
  border-color: #1f2937;
}

.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
  gap: 0.5rem;
}
.panel-head h2 {
  font-size: 1.125rem;
  font-weight: 700;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.panel-actions { display: flex; gap: 0.5rem; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.35rem 0.6rem;
  border-radius: 0.375rem;
  border: none;
  font-size: 0.75rem;
  cursor: pointer;
  font-weight: 500;
}
.chip-blue { background: rgba(59,130,246,0.1); color: var(--blue); }
.chip-blue:hover { background: rgba(59,130,246,0.2); }
.chip-red { background: rgba(239,68,68,0.1); color: var(--red); }
.chip-red:hover { background: rgba(239,68,68,0.2); }

.row-header {
  display: grid;
  grid-template-columns: 1.8fr 1fr 1fr 1fr 1fr 0.9fr 1.4rem;
  gap: 0.4rem;
  padding: 0 0.5rem 0.35rem;
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: #9ca3af;
}
.row-header span { text-align: center; }
.row-header span:first-child { text-align: left; }

.row-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.data-row {
  display: grid;
  grid-template-columns: 1.8fr 1fr 1fr 1fr 1fr 0.9fr 1.4rem;
  gap: 0.4rem;
  align-items: center;
  padding: 0.6rem 0.5rem;
  border-radius: 0.375rem;
  border: 1px solid #f3f4f6;
  background: #f9fafb;
}
body.dark .data-row { border-color: #374151; background: #1f2937; }

.data-row input {
  width: 100%;
  font-size: 0.85rem;
  background: transparent;
  border: 1px solid #d1d5db;
  border-radius: 0.3rem;
  text-align: center;
  padding: 0.4rem 0.15rem;
  color: inherit;
  min-width: 0;
}
.data-row input.name-input {
  text-align: left;
  border: 1px dashed #c7cbd1;
  border-radius: 0.3rem;
  padding: 0.4rem 0.5rem;
}
.data-row input.name-input:hover { border-color: var(--indigo); }
.data-row input.name-input:focus {
  border-style: solid;
  border-color: var(--indigo);
  outline: none;
}
body.dark .data-row input { border-color: #4b5563; }
body.dark .data-row input.name-input { border-color: #565f6f; }

/* Custom number steppers — native browser spin buttons render tiny, cramped,
   and can crowd the digits, and their size/spacing can't be controlled via
   CSS. These replace them with buttons we fully control. Laid out as true
   flex siblings (not overlaid on top of the input) so the input's own text
   area is never covered or squeezed by guesswork padding. */
.num-wrap { display: flex; align-items: stretch; width: 100%; height: 2.375rem; gap: 2px; }
.num-wrap input[type="number"] {
  flex: 1 1 auto;
  min-width: 0;
  height: 100%;
  box-sizing: border-box;
  padding: 0.4rem 0.1rem;
  -moz-appearance: textfield;
}
.num-wrap input[type="number"]::-webkit-outer-spin-button,
.num-wrap input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.num-steppers {
  flex: 0 0 1rem;
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.num-step {
  flex: 1 1 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 0.2rem;
  background: #e5e7eb;
  color: #6b7280;
  cursor: pointer;
  padding: 0;
}
.num-step svg { width: 0.8rem; height: 0.8rem; }
.num-step:hover { background: var(--indigo); color: #fff; }
body.dark .num-step { background: #374151; color: #9ca3af; }
body.dark .num-step:hover { background: var(--indigo); color: #fff; }

.data-row input.qty-input { font-weight: 700; color: #818cf8; }

.row-remove-btn {
  border: none;
  background: transparent;
  color: #f87171;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.row-remove-btn:hover { color: var(--red); }

.add-row-btn {
  width: 100%;
  margin-top: 0.75rem;
  padding: 0.4rem;
  border: 1px dashed #9ca3af;
  border-radius: 0.375rem;
  background: transparent;
  color: #9ca3af;
  font-size: 0.75rem;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.25rem;
  cursor: pointer;
}
.add-row-btn:hover { border-color: var(--indigo); color: var(--indigo); }

/* METRICS */
.metrics-panel { justify-content: space-between; }
.metrics-panel h2 { font-size: 1.125rem; font-weight: 700; margin: 0 0 0.75rem; }
.metrics-body { display: flex; flex-direction: column; gap: 0.5rem; font-size: 0.875rem; }
.metric-row { display: flex; justify-content: space-between; }
.metric-label { color: #9ca3af; }
.metric-value { font-weight: 700; }

.volumetric-box {
  padding: 0.75rem;
  border-radius: 0.5rem;
  border: 1px solid #e5e7eb;
  background: #f9fafb;
  margin: 0.5rem 0;
}
body.dark .volumetric-box { background: #111827; border-color: #374151; }

.volumetric-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
  gap: 0.5rem;
  flex-wrap: wrap;
}
.volumetric-head .metric-label { font-size: 0.75rem; font-weight: 600; }

.divisor-toggle {
  display: flex;
  border: 1px solid #9ca3af;
  border-radius: 0.25rem;
  overflow: hidden;
  font-size: 0.75rem;
}
.divisor-btn {
  padding: 0.25rem 0.5rem;
  border: none;
  background: transparent;
  color: #9ca3af;
  cursor: pointer;
}
.divisor-btn:hover { background: #e5e7eb; }
body.dark .divisor-btn:hover { background: #374151; }
.divisor-btn.active { background: var(--indigo-dark); color: #fff; }

.volumetric-result { display: flex; justify-content: space-between; align-items: baseline; }
.volumetric-value { font-size: 1.125rem; font-weight: 900; color: #818cf8; }

.charge-box {
  padding: 0.75rem;
  border-radius: 0.5rem;
  border: 1px solid rgba(245,158,11,0.35);
  background: rgba(245,158,11,0.08);
  margin: 0.5rem 0;
}
.charge-value { font-size: 1.125rem; font-weight: 900; color: #f59e0b; }
.charge-note { font-size: 0.7rem; color: #9ca3af; margin-top: 0.35rem; }

.metric-status { font-size: 0.75rem; margin-top: 0.25rem; min-height: 1rem; }
.metric-status.ok { color: #22c55e; }
.metric-status.warn { color: #f59e0b; }
.metric-status.err { color: var(--red); }

.calculate-btn {
  width: 100%;
  margin-top: 1rem;
  background: var(--indigo-dark);
  color: #fff;
  font-weight: 700;
  border: none;
  padding: 0.75rem 1rem;
  border-radius: 0.75rem;
  box-shadow: 0 4px 6px rgba(0,0,0,0.1);
  font-size: 0.875rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  cursor: pointer;
}
.calculate-btn:hover:not(:disabled) { background: var(--indigo); }
.calculate-btn:disabled { background: #9ca3af; cursor: not-allowed; }

/* BOOST ROW */
.boost-row { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 0.75rem; margin-bottom: 1rem; }
.boost-btn {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.5rem 1rem;
  background: linear-gradient(to right, var(--purple), var(--indigo-dark));
  color: #fff;
  border: none;
  border-radius: 0.5rem;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
  display: flex;
  align-items: center;
  gap: 0.4rem;
  cursor: pointer;
}
.boost-btn:hover:not(:disabled) { opacity: 0.9; }
.boost-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.bestfit-toggle {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: #0d9488;
  cursor: pointer;
  user-select: none;
}
body.dark .bestfit-toggle { color: #2dd4bf; }
.bestfit-toggle input[type="checkbox"] {
  width: 1.1rem;
  height: 1.1rem;
  accent-color: #0d9488;
  cursor: pointer;
}
.bestfit-toggle:has(input:disabled) { opacity: 0.5; cursor: not-allowed; }

/* VIEWER */
.viewer-frame {
  width: 100%;
  height: 26rem;
  border-radius: 1rem;
  border: 1px solid #d1d5db;
  background: #e5e7eb;
  position: relative;
  overflow: hidden;
}
body.dark .viewer-frame { background: #030712; border-color: #1f2937; }

.viewer-badge {
  position: absolute;
  top: 1rem;
  left: 1rem;
  z-index: 10;
  background: rgba(0,0,0,0.4);
  backdrop-filter: blur(4px);
  font-size: 0.625rem;
  text-transform: uppercase;
  font-family: monospace;
  letter-spacing: 0.1em;
  color: #fff;
  padding: 0.25rem 0.5rem;
  border-radius: 0.25rem;
}

.viewer-canvas-host { position: absolute; inset: 0; }
.viewer-canvas-host canvas { display: block; width: 100%; height: 100%; }

.viewer-3d-label {
  font-size: 0.7rem;
  font-weight: 700;
  color: #fff;
  background: rgba(99, 102, 241, 0.85);
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  white-space: nowrap;
  transform: translateY(-100%);
  pointer-events: none;
}

.viewer-placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 1.5rem;
  pointer-events: none;
}
.viewer-spinner {
  width: 6rem;
  height: 6rem;
  margin: 0 auto 1rem;
  border: 4px dashed rgba(129,140,248,0.5);
  border-radius: 0.75rem;
  animation: spin 12s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.viewer-placeholder-title { font-weight: 600; font-size: 0.875rem; opacity: 0.6; margin: 0; }
.viewer-placeholder-sub { font-size: 0.75rem; opacity: 0.4; max-width: 24rem; margin: 0.25rem auto 0; }

.viewer-legend {
  position: absolute;
  bottom: 0.75rem;
  left: 0.75rem;
  right: 0.75rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  z-index: 10;
}
.legend-chip {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.65rem;
  background: rgba(0,0,0,0.45);
  color: #fff;
  padding: 0.2rem 0.5rem;
  border-radius: 999px;
  backdrop-filter: blur(4px);
}
.legend-swatch { width: 0.6rem; height: 0.6rem; border-radius: 2px; flex: none; }

/* AD SLOT */
.ad-slot {
  margin-top: 1.5rem;
  width: 100%;
  padding: 1rem;
  text-align: center;
  font-size: 0.75rem;
  font-family: monospace;
  letter-spacing: 0.05em;
  border: 2px dashed #9ca3af;
  opacity: 0.4;
  color: #6b7280;
  border-radius: 0.5rem;
}

.footer {
  margin-top: 1.5rem;
  text-align: center;
  font-size: 0.7rem;
  color: #9ca3af;
}

@media (max-width: 640px) {
  .data-row { grid-template-columns: repeat(3, 1fr); }
  .data-row input.name-input { grid-column: 1 / -1; }
}
