html,
body {
  height: 100%;
  overflow: hidden;
}

:root {
  --color-bg: #ffffff;
  --color-surface: #ffffff;
  --color-header-bg: #24292f;
  --color-header-text: #ffffff;
  --color-header-text-muted: #a0a7b0;
  --color-muted: #f6f8fa;
  --color-border: #d0d7de;
  --color-text: #24292f;
  --color-primary: #0969da;
  --color-accent: #fd8c73;
  --color-warning: #fff8c5;
  --color-danger: #cf222e;
  --color-success: #1a7f37;
  --color-text-light: #ffffff;
  --color-text-dark: #24292f;
  --color-axis-x: #cf222e;
  --color-axis-y: #1a7f37;
  --color-axis-z: #0969da;
  --color-text-muted: #57606a;
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif;
  --radius: 6px;
  --shadow: 0 1px 0 rgba(27, 31, 36, 0.04);
  --space-xs: 0.25rem;
  --space-s: 0.5rem;
  --space-m: 1rem;
  --space-l: 1.5rem;
  --space-xl: 2rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #0d1117;
    --color-surface: #161b22;
    --color-header-bg: #161b22;
    --color-header-text: #e6edf3;
    --color-header-text-muted: #9ea8b3;
    --color-muted: #21262d;
    --color-border: #30363d;
    --color-text: #e6edf3;
    --color-primary: #58a6ff;
    --color-accent: #f78166;
    --color-warning: #3d2b00;
    --color-danger: #f85149;
    --color-success: #3fb950;
    --color-text-light: #e6edf3;
    --color-text-dark: #0d1117;
    --color-text-muted: #8b949e;
  }
}

body {
  margin: 0;
  font-family: var(--font-family);
  display: flex;
  flex-direction: column;
  background: var(--color-bg);
  color: var(--color-text);
  font-size: 1rem;
  padding-right: 320px; /* space for fixed chat area */
}

/* ─── Header ─────────────────────────────────────────────── */

header {
  background: var(--color-header-bg);
  position: sticky;
  top: 0;
  z-index: 200;
  flex: 0 0 auto;
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.625rem 1rem;
}

.header-logo {
  font-size: 1rem;
  font-weight: 600;
  color: var(--color-header-text);
  letter-spacing: -0.01em;
  margin: 0;
}

/* ─── Top Tab Nav ─────────────────────────────────────────── */

.tabs {
  display: flex;
  padding: 0 0.5rem;
  margin: 0;
  gap: 0;
}

.tab-button {
  padding: 0.5rem 1rem;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--color-header-text-muted);
  cursor: pointer;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 500;
  white-space: nowrap;
  transition: color 0.15s ease, border-color 0.15s ease;
  position: relative;
  outline: none;
}

.tab-button:hover {
  color: var(--color-header-text);
  background: rgba(255, 255, 255, 0.08);
  border-radius: var(--radius) var(--radius) 0 0;
}

.tab-button.active {
  color: var(--color-header-text);
  border-bottom-color: var(--color-accent);
  font-weight: 600;
}

.tab-button:focus-visible {
  box-shadow: 0 0 0 2px var(--color-primary);
  border-radius: var(--radius) var(--radius) 0 0;
}

.tab-button.notice {
  position: relative;
}

.tab-button.notice::after {
  content: '';
  position: absolute;
  top: 6px;
  right: 6px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--color-accent);
}

.tab-button.is-locked {
  color: var(--color-header-text-muted);
  opacity: 0.55;
  cursor: not-allowed;
}

.tab-button.is-locked:hover {
  color: var(--color-header-text-muted);
  background: transparent;
  border-radius: 0;
}

/* ─── Main content ────────────────────────────────────────── */

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow-y: auto;
}

.tab-content {
  display: none;
  flex: 1;
  min-height: 0;
}

.tab-content.active {
  display: flex;
}

#controls.tab-content.active {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
}

#viewer-wrapper {
  position: relative;
}

#viewer-wrapper.tab-content.active {
  display: flex;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
}

#controls {
  width: 100%;
  padding: var(--space-m);
  box-sizing: border-box;
  background: var(--color-surface);
}

#report {
  width: 100%;
  padding: var(--space-m);
  box-sizing: border-box;
  background: var(--color-surface);
}

#graph-report.tab-content.active {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  gap: var(--space-m);
  overflow-y: auto;
  overscroll-behavior: contain;
}

.dashboard-grid {
  display: grid;
  gap: var(--space-m);
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
}

.dashboard-card {
  background: var(--color-surface);
  box-shadow: var(--shadow);
  padding: var(--space-m);
  border-radius: var(--radius);
  border: 1px solid var(--color-border);
}

.graph-report-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-m);
  flex-wrap: wrap;
}

.graph-report-header h2 {
  margin: 0;
}

.graph-report-header p {
  margin: var(--space-xs) 0 0;
  color: var(--color-text-muted);
  max-width: 64ch;
}

#graph-report button {
  align-self: flex-start;
  flex: 0 0 auto;
}

#graph-report-status {
  padding: var(--space-m);
  background: var(--color-muted);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  color: var(--color-text);
}

.graph-report-grid {
  display: grid;
  gap: var(--space-m);
  grid-template-columns: repeat(12, minmax(0, 1fr));
  align-items: stretch;
  grid-auto-flow: dense;
}

.graph-report-card {
  grid-column: span 6;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: var(--space-s);
  min-height: 360px;
  height: 100%;
  overflow: hidden;
  contain: layout paint;
}

.graph-report-card--wide {
  grid-column: span 8;
}

.graph-report-card--compact {
  grid-column: span 4;
}

.graph-report-card--square .graph-chart-frame {
  aspect-ratio: 1 / 1;
}

.graph-card-header {
  display: grid;
  gap: 4px;
}

.graph-report-card h3 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.graph-card-header p {
  margin: 0;
  color: var(--color-text-muted);
  font-size: 0.82rem;
  line-height: 1.45;
}

.graph-chart-frame {
  position: relative;
  min-height: 0;
  height: 100%;
  aspect-ratio: 16 / 10;
  border-radius: calc(var(--radius) - 4px);
  background: linear-gradient(180deg, rgba(15, 23, 42, 0.02), rgba(15, 23, 42, 0.05));
  padding: var(--space-s);
  box-sizing: border-box;
}

.graph-chart-frame canvas {
  width: 100% !important;
  height: 100% !important;
  display: block;
}

.graph-card-note {
  margin: 0;
  color: var(--color-text-muted);
  font-size: 0.8rem;
  line-height: 1.5;
}

.graph-report-card.is-empty .graph-chart-frame {
  display: none;
}

.graph-report-card.is-empty {
  min-height: 220px;
}

@media (max-width: 980px) {
  .graph-report-card,
  .graph-report-card--wide,
  .graph-report-card--compact {
    grid-column: span 12;
  }

  .graph-report-card {
    min-height: 320px;
  }
}

@media (max-width: 640px) {
  .graph-chart-frame,
  .graph-report-card--square .graph-chart-frame {
    aspect-ratio: 4 / 3;
  }
}

/* ─── Controls panel ──────────────────────────────────────── */

.controls-chat-wrapper {
  display: flex;
  gap: var(--space-m);
  align-items: stretch;
  flex-wrap: wrap;
  flex: 1 1 auto;
  min-height: 0;
}

.controls-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: var(--space-m);
  min-height: 0;
  background: var(--color-muted);
  padding: var(--space-m);
}

#controls .categories {
  display: flex;
  gap: var(--space-m);
  align-items: flex-start;
}

#controls .settings-container {
  display: flex;
  flex-direction: column;
  gap: var(--space-m);
  flex: 1;
}

#controls .fillers-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: var(--space-m);
}

.controls-panel .fields {
  display: flex;
  flex-direction: column;
  gap: var(--space-s);
}

.controls-panel .card {
  padding: var(--space-m);
  border-radius: var(--radius);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
}

#controls .card {
  flex: 1;
}

.controls-panel .card h2 {
  margin-top: 0;
  margin-bottom: var(--space-s);
  font-size: 1rem;
  font-weight: 600;
  color: var(--color-text);
}

#controls .buttons {
  margin-bottom: var(--space-m);
}

#filler-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-m);
}

@media (max-width: 768px) {
  #filler-list {
    grid-template-columns: 1fr;
  }
}

#filler-ratio-summary {
  display: flex;
  height: 1em;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--color-muted);
}

.ratio-bar {
  flex: 1;
  height: 100%;
}

.filler-entry {
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  padding: var(--space-s);
  border-radius: var(--radius);
  margin-bottom: 0;
}

.filler-entry .filler-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
}

.filler-entry .filler-header .toggle-icon {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1em;
  padding: 0;
}

.filler-entry .fields {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-s);
  align-items: flex-end;
  margin-top: var(--space-s);
}

.filler-entry .fields.collapsed {
  display: none;
}

.filler-entry .field {
  display: flex;
  gap: var(--space-xs);
  align-items: center;
}

/* ─── Viewer ──────────────────────────────────────────────── */

#viewer {
  flex: 1;
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

#voxel-info {
  flex: 0 0 160px;
  box-sizing: border-box;
  padding: 1em;
  background: var(--color-muted);
  border-right: 1px solid var(--color-border);
}

#material-stats {
  margin-top: 1em;
  font-size: 0.8em;
}

#material-stats canvas {
  display: block;
  width: 100%;
  height: auto;
  margin-top: 0.5em;
  background: var(--color-muted);
}

#material-legend {
  list-style: none;
  margin: 0.5em 0 0;
  padding: 0;
}

#material-legend li {
  display: flex;
  align-items: center;
  margin-bottom: 0.25em;
}

/* ─── View-settings modal ─────────────────────────────────── */

#view-settings {
  position: fixed;
  inset: 0;
  z-index: 100;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.18s ease;
}

#view-settings.is-open {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.view-settings-panel {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  width: min(420px, calc(100vw - (2 * var(--space-l))));
  max-height: calc(100vh - (2 * var(--space-l)));
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(140, 149, 159, 0.2);
  overflow: hidden;
}

.view-settings-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.22);
}

.view-settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-s);
  padding: var(--space-m);
  border-bottom: 1px solid var(--color-border);
  background: var(--color-muted);
}

.view-settings-header h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

#view-settings .content {
  box-sizing: border-box;
  padding: var(--space-m);
  overflow-y: auto;
  background: var(--color-surface);
  display: flex;
  flex-direction: column;
  gap: var(--space-s);
}

#closeViewSettings {
  flex: 0 0 auto;
}

#viewer canvas {
  width: 100%;
  height: 100%;
  display: block;
}

/* ─── Form fields ─────────────────────────────────────────── */

.field {
  display: grid;
  grid-template-columns: 8em 1fr;
  gap: var(--space-s);
  align-items: center;
}

.field label {
  margin: 0;
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  font-weight: 600;
  font-size: 0.8125rem;
}

.field label .unit {
  font-weight: normal;
  font-size: 0.8em;
  color: var(--color-text-muted);
}

.field label .icon {
  margin-right: var(--space-xs);
}

.field.full-row {
  grid-template-columns: 1fr;
}

.field.action-field {
  grid-template-columns: 8em 1fr auto;
}

.field input,
.field select {
  width: 100%;
}

.field .warning {
  grid-column: 2;
}

#view-settings .content .field {
  flex: 0 0 auto;
}

#view-settings .content #applyViewSettings {
  align-self: flex-start;
}

.buttons button {
  margin-right: var(--space-s);
}

/* ─── Inputs and buttons ──────────────────────────────────── */

button,
input[type="number"],
input[type="checkbox"],
select {
  padding: 0.3125em 0.625em;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.8125rem;
  background: var(--color-surface);
  color: var(--color-text);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  outline: none;
}

button {
  cursor: pointer;
  font-weight: 500;
}

button:hover,
input[type="number"]:hover,
select:hover {
  border-color: var(--color-primary);
}

button:hover {
  background: var(--color-muted);
}

button:focus-visible,
input[type="number"]:focus,
select:focus {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(9, 105, 218, 0.3);
}

#addFiller,
#applyViewSettings,
#download {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: #ffffff;
}

#addFiller:hover,
#applyViewSettings:hover,
#download:hover {
  background: #0860ca;
  border-color: #0860ca;
  color: #ffffff;
}

#generate {
  background: #2da44e;
  border: 1px solid rgba(31, 35, 40, 0.15);
  color: #ffffff;
  border-radius: var(--radius);
  padding: 0.3125em 0.875em;
  font-weight: 600;
  font-size: 0.8125rem;
}

#generate:hover {
  background: #2c974b;
}

.action-field .remove-btn {
  background: var(--color-muted);
  border: 1px solid var(--color-border);
  color: var(--color-text);
}

.action-field .remove-btn:hover {
  background: var(--color-border);
}

select.filler-material option {
  color: var(--color-text-dark);
}

button:disabled {
  opacity: 0.5;
  cursor: default;
}

/* ─── Material property entries ───────────────────────────── */

.property-entry {
  display: flex;
  gap: var(--space-s);
  align-items: center;
  margin-bottom: var(--space-s);
}

.property-entry input {
  width: 5em;
}

.property-entry select {
  width: 5em;
}

.property-entry .prop-id {
  width: 4em;
}

.hidden {
  display: none;
}

/* ─── Progress & status ───────────────────────────────────── */

#progressBar {
  width: 100%;
  height: 0.5rem;
  margin-top: var(--space-s);
  border-radius: var(--radius);
  accent-color: var(--color-primary);
}

#progress {
  margin-top: var(--space-xs);
  font-size: 0.75rem;
  color: var(--color-text-muted);
}

#summary {
  margin-top: var(--space-s);
  font-size: 0.75rem;
}

.backend-status {
  margin-top: var(--space-s);
  padding: var(--space-xs) var(--space-s);
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--color-text);
  background: var(--color-muted);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
}

.warning {
  color: var(--color-danger);
  font-size: 0.75rem;
}

/* ─── Console log ─────────────────────────────────────────── */

#console {
  background: var(--color-muted);
  border-top: 1px solid var(--color-border);
  padding: var(--space-s);
  height: 150px;
  overflow-y: auto;
}

#console.hidden {
  display: none;
}

#console pre {
  margin: 0;
  font-size: 0.75rem;
  height: 100%;
}

/* ─── Viewer buttons ──────────────────────────────────────── */

#viewer-buttons {
  position: absolute;
  bottom: var(--space-m);
  left: var(--space-m);
  z-index: 10;
  display: flex;
  gap: var(--space-s);
}

/* ─── Chat container ──────────────────────────────────────── */

#chat-container {
  display: flex;
  position: fixed;
  top: 0;
  right: 0;
  gap: var(--space-m);
  width: 320px;
  max-width: 320px;
  flex-direction: column;
  min-height: 0;
  height: 100vh;
  z-index: 1000;
  background: var(--color-surface);
  border-left: 1px solid var(--color-border);
  padding: var(--space-s);
  box-sizing: border-box;
}

#viewLog {
  align-self: flex-end;
  font-size: 0.75rem;
}

#chat-log h2 {
  margin: 0 0 var(--space-s);
  font-size: 1rem;
  font-weight: 600;
}

#chat-area {
  flex: 1;
  min-width: 0;
  min-height: 0;
  width: 100%;
  max-width: none;
  display: flex;
  flex-direction: column;
  gap: var(--space-s);
}

#chat-log {
  display: none;
  flex: 0 0 200px;
  flex-direction: column;
  min-width: 0;
  gap: var(--space-s);
}

#chat-container.show-log #chat-log {
  display: flex;
}

#chat-log-output {
  flex: 1;
  height: 100%;
  overflow-y: auto;
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  padding: var(--space-s);
  margin: 0;
  font-family: monospace;
  white-space: pre-wrap;
  min-height: 0;
  font-size: 0.75rem;
}

#chat-messages {
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  padding: var(--space-s);
  margin-bottom: var(--space-s);
  display: flex;
  flex-direction: column;
  gap: var(--space-s);
  flex: 1 1 auto;
  min-height: 0;
  height: 100%;
  overflow-y: auto;
  border-radius: var(--radius);
}

.chat-message {
  position: relative;
  padding: var(--space-s) var(--space-m);
  border-radius: var(--radius);
  max-width: 85%;
  white-space: pre-wrap;
  font-size: 0.8125rem;
}

.chat-message.user {
  align-self: flex-end;
  background: var(--color-primary);
  color: #ffffff;
}

.chat-message.user::after {
  content: '';
  position: absolute;
  right: -6px;
  top: 10px;
  border-width: 6px 0 6px 6px;
  border-style: solid;
  border-color: transparent transparent transparent var(--color-primary);
}

.chat-message.ai {
  align-self: flex-start;
  background: none;
  color: var(--color-text);
  padding: 0;
  border-radius: 0;
  position: static;
}

.chat-message.ai::after {
  content: none;
}

.chat-input-area {
  display: flex;
  flex: 0 0 auto;
  gap: var(--space-s);
  position: sticky;
  bottom: 0;
}

#chat-input {
  flex: 1;
  min-height: calc(var(--space-m) * 2.5);
  resize: vertical;
  padding: var(--space-xs) var(--space-s);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.8125rem;
}

/* ─── Responsive ──────────────────────────────────────────── */

@media (max-width: 768px) {
  body {
    font-size: 0.875rem;
    padding-right: 0;
  }

  #controls {
    padding: var(--space-s);
  }

  #controls .categories {
    flex-direction: column;
  }

  .controls-chat-wrapper {
    flex-direction: column;
  }

  #chat-container {
    position: static;
    flex: 1 1 auto;
    width: 100%;
    max-width: none;
    flex-direction: column;
    height: 100vh;
    border-left: none;
    border-top: 1px solid var(--color-border);
  }

  #chat-log {
    flex: 1 1 auto;
  }

  #viewer-wrapper {
    flex-direction: column;
  }

  #voxel-info,
  #console {
    display: none;
  }

  #openViewSettings {
    display: none;
  }

  #viewer-buttons {
    bottom: var(--space-s);
    left: var(--space-s);
  }
}

@media (min-width: 1200px) {
  body {
    font-size: 0.875rem;
  }

  #controls {
    padding: var(--space-xl);
  }
}

/* ─── AI rainbow animation ────────────────────────────────── */

@keyframes ai-rainbow-flow {
  0% {
    background-position: 0% 50%;
  }
  100% {
    background-position: 100% 50%;
  }
}

.ai-updated {
  position: relative;
  box-sizing: border-box;
  border: 4px solid transparent;
  border-radius: var(--radius);
  background-clip: padding-box;
}

.ai-updated::before {
  content: "";
  position: absolute;
  inset: 0;
  padding: 4px;
  border-radius: inherit;
  background: linear-gradient(
    90deg,
    red,
    orange,
    yellow,
    green,
    blue,
    indigo,
    violet,
    red
  );
  background-size: 400% 100%;
  -webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
  -webkit-mask-composite: destination-out;
          mask-composite: exclude;
  animation: ai-rainbow-flow 5s linear infinite;
}

