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

:root {
  --font-sans: "DM Sans", system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif;
  --bg-page: #f1f4f9;
  --bg-subtle: #e8edf6;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: rgba(15, 23, 42, 0.09);
  --border-strong: rgba(15, 23, 42, 0.12);
  --text: #0f172a;
  --text-secondary: #334155;
  --muted: #64748b;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-soft: rgba(37, 99, 235, 0.12);
  --accent-focus: rgba(37, 99, 235, 0.22);
  --danger: #c2410c;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-md: 0 4px 16px rgba(15, 35, 90, 0.08), 0 2px 4px rgba(15, 23, 42, 0.04);
  --shadow-lg: 0 12px 40px -12px rgba(15, 35, 90, 0.18);
  --radius-card: 14px;
  --radius-control: 10px;
  --radius-pill: 999px;
  --transition: 0.18s ease;
  --diagram-surface: #f8fafc;
  --header-gradient-1: rgba(255, 255, 255, 0.97);
  --header-gradient-2: rgba(248, 250, 252, 0.95);
  --header-gradient-3: rgba(241, 245, 249, 0.92);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg-page: #0c1017;
    --bg-subtle: #141b26;
    --surface: #151c28;
    --surface-2: #1a2332;
    --border: rgba(148, 163, 184, 0.12);
    --border-strong: rgba(148, 163, 184, 0.18);
    --text: #f1f5f9;
    --text-secondary: #cbd5e1;
    --muted: #94a3b8;
    --accent: #60a5fa;
    --accent-hover: #93c5fd;
    --accent-soft: rgba(96, 165, 250, 0.15);
    --accent-focus: rgba(96, 165, 250, 0.28);
    --danger: #fb923c;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
    --shadow-md: 0 4px 20px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 12px 48px rgba(0, 0, 0, 0.45);
    --diagram-surface: #1e293b;
    --header-gradient-1: rgba(21, 28, 40, 0.96);
    --header-gradient-2: rgba(17, 24, 39, 0.94);
    --header-gradient-3: rgba(15, 23, 42, 0.92);
  }
}

:root[data-theme="dark"] {
  --bg-page: #0c1017;
  --bg-subtle: #141b26;
  --surface: #151c28;
  --surface-2: #1a2332;
  --border: rgba(148, 163, 184, 0.12);
  --border-strong: rgba(148, 163, 184, 0.18);
  --text: #f1f5f9;
  --text-secondary: #cbd5e1;
  --muted: #94a3b8;
  --accent: #60a5fa;
  --accent-hover: #93c5fd;
  --accent-soft: rgba(96, 165, 250, 0.15);
  --accent-focus: rgba(96, 165, 250, 0.28);
  --danger: #fb923c;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
  --shadow-md: 0 4px 20px rgba(0, 0, 0, 0.35);
  --shadow-lg: 0 12px 48px rgba(0, 0, 0, 0.45);
  --diagram-surface: #1e293b;
  --header-gradient-1: rgba(21, 28, 40, 0.96);
  --header-gradient-2: rgba(17, 24, 39, 0.94);
  --header-gradient-3: rgba(15, 23, 42, 0.92);
}

html {
  -webkit-text-size-adjust: 100%;
  color-scheme: light dark;
}

body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: 0.9375rem;
  background: var(--bg-page);
  color: var(--text);
  line-height: 1.55;
  transition: background-color var(--transition), color var(--transition);
}

.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 22px 18px 28px;
}

/* ---- Top header ---- */
.site-chrome {
  background: linear-gradient(
    165deg,
    var(--header-gradient-1) 0%,
    var(--header-gradient-2) 45%,
    var(--header-gradient-3) 100%
  );
  backdrop-filter: blur(14px) saturate(1.35);
  -webkit-backdrop-filter: blur(14px) saturate(1.35);
  border-bottom: 1px solid var(--border);
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.06) inset,
    var(--shadow-lg);
}

.site-header-inner {
  max-width: 1100px;
  margin: 0 auto;
  padding: 14px 20px;
  min-height: 60px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 14px 28px;
}

.site-brand {
  flex: 0 1 auto;
  min-width: 0;
}

.site-nav {
  flex: 0 0 auto;
  margin-left: auto;
}

.site-nav-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: 6px 4px;
}

.site-nav-list a {
  display: inline-block;
  padding: 9px 16px;
  font-size: 0.875rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  color: var(--text-secondary);
  text-decoration: none;
  border-radius: var(--radius-pill);
  transition: background var(--transition), color var(--transition), box-shadow var(--transition);
}

.site-nav-list a:hover {
  background: var(--accent-soft);
  color: var(--text);
}

.site-nav-list a:focus-visible {
  outline: none;
  background: var(--accent-focus);
  color: var(--text);
  box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--accent);
}

.site-nav__actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.theme-toggle,
.theme-system {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: auto;
  min-height: 38px;
  margin-top: 0;
  padding: 0 14px;
  font-family: inherit;
  font-size: 0.8125rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  border-radius: var(--radius-pill);
  cursor: pointer;
  transition: background-color var(--transition), color var(--transition), border-color var(--transition), box-shadow var(--transition);
}

.theme-toggle {
  background: var(--accent-soft);
  color: var(--text-secondary);
  border: 1px solid var(--border);
}

.theme-toggle:hover {
  background: var(--accent-focus);
  color: var(--text);
}

.theme-toggle:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--accent);
}

.theme-system {
  background: transparent;
  color: var(--muted);
  border: 1px solid transparent;
}

.theme-system:hover {
  color: var(--text-secondary);
  border-color: var(--border);
}

.theme-system:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--accent);
}

.theme-system.theme-system--active {
  color: var(--accent);
  border-color: var(--border-strong);
}

h1 {
  margin: 0 0 0.35em;
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  color: var(--text);
  line-height: 1.2;
}

h1.brand-heading {
  margin: 0;
  font-size: 0;
  line-height: 0;
}

.brand-link {
  display: inline-block;
  line-height: 0;
  text-decoration: none;
  border-radius: 12px;
  transition: opacity var(--transition), transform var(--transition);
}

.brand-link:hover {
  opacity: 0.9;
  transform: translateY(-1px);
}

.brand-link:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--accent);
}

.brand-logo {
  display: block;
  height: clamp(42px, 7.5vw, 54px);
  width: auto;
  max-width: min(100vw - 140px, 280px);
  object-fit: contain;
  object-position: left center;
  filter: drop-shadow(0 2px 8px rgba(15, 35, 90, 0.1));
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .brand-logo {
    filter: drop-shadow(0 2px 10px rgba(0, 0, 0, 0.4));
  }
}

:root[data-theme="dark"] .brand-logo {
  filter: drop-shadow(0 2px 10px rgba(0, 0, 0, 0.4));
}

@media (prefers-reduced-motion: reduce) {
  body,
  .brand-link,
  .site-nav-list a,
  .theme-toggle,
  .theme-system,
  .card,
  .primary,
  .custom-header button,
  input,
  select,
  .results-table tbody tr,
  .companion-table tbody tr {
    transition: none !important;
  }

  .brand-link:hover {
    transform: none;
  }
}

h2 {
  margin: 0 0 14px;
  font-size: 1.125rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
  line-height: 1.3;
}

h3 {
  margin: 0 0 8px;
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text-secondary);
  line-height: 1.35;
}

.muted {
  color: var(--muted);
  margin-top: 0;
}

.container > .tagline {
  margin-bottom: 18px;
  line-height: 1.5;
  font-size: 0.9375rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: 20px 22px 22px;
  margin-bottom: 16px;
  box-shadow: var(--shadow-sm);
  transition: box-shadow var(--transition), border-color var(--transition);
}

@media (hover: hover) {
  .card:hover {
    box-shadow: var(--shadow-md);
  }
}

.card > h2 + .cut-order-help,
.card > h2 + .grid {
  margin-top: 0;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px 16px;
}

label {
  display: block;
  font-size: 0.875rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text-secondary);
}

input,
select,
button {
  width: 100%;
  margin-top: 8px;
  min-height: 42px;
  padding: 0 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-control);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9375rem;
  transition: border-color var(--transition), box-shadow var(--transition), background-color var(--transition);
}

select {
  padding-right: 36px;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  }
}

:root[data-theme="dark"] select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
}

input::placeholder {
  color: var(--muted);
  opacity: 0.85;
}

input:hover,
select:hover {
  border-color: var(--muted);
}

input:focus-visible,
select:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.checks {
  margin-top: 14px;
  display: flex;
  gap: 12px 20px;
  flex-wrap: wrap;
}

.checks label {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  margin-top: 0;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-control);
  font-size: 0.9375rem;
  font-weight: 500;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text);
  cursor: pointer;
  transition: border-color var(--transition), background-color var(--transition);
}

.checks label:hover {
  border-color: var(--muted);
}

.checks input[type="checkbox"] {
  width: 1.1rem;
  height: 1.1rem;
  min-height: unset;
  margin: 0;
  padding: 0;
  border-radius: 6px;
  cursor: pointer;
  accent-color: var(--accent);
}

.mode-field {
  margin-bottom: 14px;
}

.mode-field label {
  max-width: 100%;
  text-transform: none;
  font-size: 0.9375rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text-secondary);
}

@media (min-width: 520px) {
  .mode-field label {
    max-width: min(100%, 320px);
  }
}

details.mode-descriptions {
  margin-top: 10px;
  max-width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-control);
  font-size: 0.875rem;
  line-height: 1.5;
  color: var(--text-secondary);
}

@media (min-width: 520px) {
  details.mode-descriptions {
    max-width: min(100%, 520px);
  }
}

details.mode-descriptions summary {
  padding: 12px 16px;
  cursor: pointer;
  list-style: none;
  font-weight: 600;
  color: var(--accent);
  user-select: none;
  transition: background-color var(--transition);
}

details.mode-descriptions summary:hover {
  background: var(--accent-soft);
}

details.mode-descriptions summary::-webkit-details-marker {
  display: none;
}

details.mode-descriptions summary::after {
  content: "";
  display: inline-block;
  width: 0.45em;
  height: 0.45em;
  margin-left: 0.4em;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(45deg) translateY(-2px);
  vertical-align: middle;
}

details.mode-descriptions[open] summary::after {
  transform: rotate(-135deg) translateY(1px);
}

details.mode-descriptions[open] summary {
  border-bottom: 1px solid var(--border);
}

.mode-descriptions-body {
  padding: 14px 16px 16px;
}

.mode-descriptions-body p {
  margin: 0 0 10px;
}

.mode-descriptions-body p:last-child {
  margin-bottom: 0;
}

.mode-descriptions-body strong {
  color: var(--text);
}

.cut-order-help {
  margin: 10px 0 0;
  font-size: 0.875rem;
  line-height: 1.5;
}

.cut-order-help:last-child {
  margin-bottom: 0;
}

#results {
  scroll-margin-top: 20px;
}

#planner-form {
  margin-bottom: 28px;
}

#planner-form > .primary {
  margin-top: 20px;
}

.primary {
  width: auto;
  min-height: 46px;
  padding: 0 22px;
  font-family: inherit;
  font-size: 0.9375rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  background: linear-gradient(180deg, var(--accent) 0%, var(--accent-hover) 100%);
  color: #fff;
  border: 1px solid var(--accent-hover);
  border-radius: var(--radius-control);
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(37, 99, 235, 0.35);
  transition: transform var(--transition), box-shadow var(--transition), filter var(--transition);
}

.primary:hover {
  filter: brightness(1.05);
  box-shadow: 0 4px 14px rgba(37, 99, 235, 0.4);
}

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

.primary:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--accent), 0 4px 14px rgba(37, 99, 235, 0.4);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .primary {
    border-color: rgba(30, 64, 175, 0.6);
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.35);
  }
}

:root[data-theme="dark"] .primary {
  border-color: rgba(30, 64, 175, 0.6);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.35);
}

.custom-header {
  margin-top: 20px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.custom-header .subhead {
  margin: 0;
  flex: 1;
  min-width: 0;
}

.custom-header button {
  width: auto;
  min-height: 40px;
  padding: 0 16px;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-secondary);
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-control);
  cursor: pointer;
  transition: background-color var(--transition), border-color var(--transition), color var(--transition);
}

.custom-header button:hover {
  background: var(--bg-subtle);
  border-color: var(--muted);
  color: var(--text);
}

.custom-header button:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.custom-row {
  margin-top: 12px;
}

/* ---- Tables ---- */
.results-table,
.companion-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.875rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-control);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

.results-table th,
.results-table td,
.companion-table th,
.companion-table td {
  padding: 12px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
}

.results-table th:last-child,
.results-table td:last-child,
.companion-table th:last-child,
.companion-table td:last-child {
  border-right: none;
}

.results-table tr:last-child td,
.companion-table tr:last-child td {
  border-bottom: none;
}

.results-table thead th,
.companion-table thead th {
  background: var(--surface-2);
  font-weight: 600;
  font-size: 0.75rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.results-table tbody tr,
.companion-table tbody tr {
  background: var(--surface);
  transition: background-color var(--transition);
}

.results-table tbody tr:hover,
.companion-table tbody tr:hover {
  background: var(--surface-2);
}

.results-table td:nth-child(3),
.results-table td:nth-child(5),
.results-table td:nth-child(6),
.results-table th:nth-child(3),
.results-table th:nth-child(5),
.results-table th:nth-child(6) {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* ---- Diagram ---- */
.diagram-frame {
  margin-top: 12px;
  max-width: 760px;
  border-radius: var(--radius-card);
  overflow: hidden;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-md);
  background: var(--diagram-surface);
}

.diagram {
  display: block;
  width: 100%;
  max-width: 760px;
  background: var(--diagram-surface);
}

.diagram-caption {
  margin-top: 12px;
}

.sheet-rect {
  fill: #ffffff;
  stroke: #1f2e4a;
  stroke-width: 2px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .sheet-rect {
    fill: #f8fafc;
    stroke: #cbd5e1;
  }
}

:root[data-theme="dark"] .sheet-rect {
  fill: #f8fafc;
  stroke: #cbd5e1;
}

.piece-rect {
  fill: #bfdbfe;
  stroke: #1d4ed8;
  stroke-width: 1px;
}

.piece-rect.rotated {
  fill: #93c5fd;
  stroke: #1e40af;
  stroke-width: 1px;
}

.waste-rect {
  fill: #e2e8f0;
  stroke: #64748b;
  stroke-width: 1px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .waste-rect {
    fill: #334155;
    stroke: #94a3b8;
  }

  :root:not([data-theme="light"]) .piece-rect {
    fill: #1e3a5f;
    stroke: #60a5fa;
  }

  :root:not([data-theme="light"]) .piece-rect.rotated {
    fill: #2563eb;
    stroke: #93c5fd;
  }
}

:root[data-theme="dark"] .waste-rect {
  fill: #334155;
  stroke: #94a3b8;
}

:root[data-theme="dark"] .piece-rect {
  fill: #1e3a5f;
  stroke: #60a5fa;
}

:root[data-theme="dark"] .piece-rect.rotated {
  fill: #2563eb;
  stroke: #93c5fd;
}

.cut-line {
  stroke: #dc2626;
  stroke-width: 2px;
  stroke-dasharray: 8 5;
}

.diagram-label {
  fill: #0f172a;
  font-family: var(--font-sans);
  font-weight: 600;
  paint-order: stroke fill;
  stroke: #ffffff;
  stroke-width: 2.5px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .diagram-label {
    fill: #f1f5f9;
    stroke: #0f172a;
  }
}

:root[data-theme="dark"] .diagram-label {
  fill: #f1f5f9;
  stroke: #0f172a;
}

.diagram-label--compact {
  font-weight: 500;
  stroke-width: 1.2px;
}

.diagram-label--piece {
  font-weight: 500;
  stroke-width: 1.35px;
}

.subhead {
  margin: 16px 0 8px;
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--text-secondary);
  line-height: 1.35;
}

.card > .subhead:first-child {
  margin-top: 0;
}

.companion-table {
  margin-bottom: 10px;
}

.small-sum {
  font-size: 0.875rem;
  margin-bottom: 0;
}

.machine-warnings {
  margin: 10px 0 0;
  padding-left: 1.25rem;
  color: var(--danger);
  line-height: 1.5;
}

.machine-warnings li {
  margin-bottom: 8px;
}

select[multiple] {
  min-height: 180px;
  padding: 10px;
  background-image: none;
}
