:root {
  --bg: #020617;
  --panel: #0f172a;
  --panel-2: #111827;
  --muted: #94a3b8;
  --text: #e5e7eb;
  --heading: #f8fafc;
  --border: rgba(148, 163, 184, 0.22);
  --primary: #38bdf8;
  --primary-dark: #0284c7;
  --danger: #ef4444;
  --success: #22c55e;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
  --radius: 18px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background:
    radial-gradient(circle at top left, rgba(56, 189, 248, 0.13), transparent 34rem),
    radial-gradient(circle at top right, rgba(168, 85, 247, 0.1), transparent 32rem),
    var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
label.file-button {
  font: inherit;
}

button,
label.file-button {
  border: 1px solid var(--border);
  background: #1e293b;
  color: var(--text);
  border-radius: 12px;
  padding: 0.65rem 0.9rem;
  cursor: pointer;
  transition: 0.18s ease;
}

button:hover,
label.file-button:hover {
  background: #334155;
  border-color: rgba(56, 189, 248, 0.5);
}

button.primary {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: #00111c;
  font-weight: 800;
  border-color: transparent;
}

button.primary:hover {
  filter: brightness(1.08);
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

input[type="file"],
input[type="number"],
input[type="text"],
input[type="color"] {
  width: 100%;
  border: 1px solid var(--border);
  background: #020617;
  color: var(--text);
  border-radius: 12px;
  padding: 0.65rem 0.75rem;
  outline: none;
}

input[type="color"] {
  height: 42px;
  padding: 0.2rem;
}

input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.13);
}

.app-header {
  display: flex;
  justify-content: space-between;
  align-items: stretch;
  gap: 1.5rem;
  padding: 2rem;
  border-bottom: 1px solid var(--border);
}

.app-header h1 {
  margin: 0 0 0.6rem;
  color: var(--heading);
  line-height: 1.1;
  font-size: clamp(1.7rem, 3vw, 3rem);
}

.app-header p {
  max-width: 980px;
  color: var(--muted);
  margin: 0;
  line-height: 1.6;
}

.status-card {
  min-width: 280px;
  background: rgba(15, 23, 42, 0.86);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
  display: grid;
  align-content: center;
  gap: 0.6rem;
}

.container {
  width: min(1800px, calc(100% - 2rem));
  margin: 1rem auto 4rem;
}

.panel {
  background: rgba(15, 23, 42, 0.92);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.2rem;
  margin-bottom: 1rem;
}

.panel h2,
.panel h3 {
  color: var(--heading);
  margin-top: 0;
}

.controls-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(180px, 1fr));
  gap: 1rem;
}

.control-block {
  display: grid;
  gap: 0.4rem;
}

.control-block label {
  color: var(--muted);
  font-size: 0.92rem;
}

.class-selector-wrap {
  margin-top: 1.2rem;
}

.class-palette {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.class-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  border-radius: 999px;
  padding: 0.5rem 0.65rem;
}

.class-chip.active {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.16);
}

.class-chip-color {
  width: 15px;
  height: 15px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.55);
}

.class-chip-delete {
  padding: 0 0.1rem;
  border: none;
  background: transparent;
  color: #fca5a5;
  font-weight: 900;
  font-size: 1.05rem;
}

.class-chip-delete:hover {
  background: transparent;
  color: #fecaca;
}

.custom-class-tools {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) 140px auto;
  gap: 1rem;
  align-items: end;
  margin-top: 1rem;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.7rem;
  margin-top: 1.2rem;
}

.persistence-row {
  border-top: 1px solid var(--border);
  padding-top: 1rem;
}

.file-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.note-box {
  margin-top: 1rem;
  padding: 1rem;
  background: rgba(2, 6, 23, 0.72);
  border: 1px solid var(--border);
  border-radius: 14px;
  color: var(--muted);
}

.note-box p {
  margin-top: 0;
}

.note-box ol {
  margin-bottom: 0;
}

.workspace-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(300px, 1fr));
  gap: 1rem;
}

.canvas-panel {
  background: rgba(2, 6, 23, 0.55);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 1rem;
  min-width: 0;
}

.canvas-title-row {
  display: flex;
  justify-content: space-between;
  align-items: start;
  gap: 1rem;
}

.pixel-info {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.4;
  text-align: right;
  min-height: 1.1rem;
}

.pixel-info:empty {
  display: none;
}

.canvas-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.7rem;
  margin-bottom: 0.7rem;
}

.zoom-controls {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-left: auto;
}

.zoom-controls button {
  padding: 0.35rem 0.55rem;
}

.zoom-label {
  min-width: 56px;
  text-align: center;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.canvas-viewport {
  width: 100%;
  max-height: 620px;
  overflow: auto;
  border-radius: 14px;
  border: 1px solid var(--border);
  background:
    linear-gradient(45deg, rgba(148, 163, 184, 0.12) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(148, 163, 184, 0.12) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, rgba(148, 163, 184, 0.12) 75%),
    linear-gradient(-45deg, transparent 75%, rgba(148, 163, 184, 0.12) 75%);
  background-size: 18px 18px;
  background-position: 0 0, 0 9px, 9px -9px, -9px 0;
  overscroll-behavior: contain;
  touch-action: none;
}

.canvas-viewport:focus-within {
  outline: 2px solid rgba(56, 189, 248, 0.35);
  outline-offset: 2px;
}

canvas {
  max-width: 100%;
  background: #020617;
  border-radius: 12px;
}

#trainCanvas,
#testCanvas {
  display: block;
  max-width: none;
  cursor: crosshair;
  image-rendering: auto;
  transform-origin: top left;
}

#segmentationCanvas {
  width: 100%;
  border: 1px solid var(--border);
}

#undoTrainLabelsBtn,
#undoValidationLabelsBtn {
  border-color: rgba(56, 189, 248, 0.38);
}

#undoTrainLabelsBtn:not(:disabled),
#undoValidationLabelsBtn:not(:disabled) {
  color: #bae6fd;
}

#undoTrainLabelsBtn:disabled,
#undoValidationLabelsBtn:disabled {
  opacity: 0.42;
}

.summary-grid,
.chart-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(280px, 1fr));
  gap: 1rem;
}

.summary-card,
.chart-card,
.table-card {
  background: rgba(2, 6, 23, 0.55);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 1rem;
  min-width: 0;
}

.chart-card {
  min-height: 330px;
}

.chart-card canvas {
  width: 100% !important;
}

.large-chart {
  grid-column: span 2;
  overflow: hidden;
}

.tree-chart-card {
  min-height: 560px;
}

.tree-title-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  margin-bottom: 0.7rem;
}

.tree-title-row h3 {
  margin: 0;
}

.tree-zoom-controls {
  flex-shrink: 0;
}

.tree-viewport {
  width: 100%;
  height: 620px;
  overflow: auto;
  border-radius: 14px;
  border: 1px solid var(--border);
  background:
    linear-gradient(45deg, rgba(148, 163, 184, 0.08) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(148, 163, 184, 0.08) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, rgba(148, 163, 184, 0.08) 75%),
    linear-gradient(-45deg, transparent 75%, rgba(148, 163, 184, 0.08) 75%);
  background-size: 18px 18px;
  background-position: 0 0, 0 9px, 9px -9px, -9px 0;
  overscroll-behavior: contain;
  touch-action: none;
}

.tree-viewport #treeCanvas {
  display: block;
  max-width: none !important;
  min-width: 0;
  transform-origin: top left;
}

#treeCanvas {
  display: block;
  max-width: none;
  min-width: 0;
}

.table-card {
  margin-top: 1rem;
  overflow-x: auto;
}

.stat-table,
.matrix-table,
.log-table {
  width: 100%;
  border-collapse: collapse;
  color: var(--text);
  font-size: 0.9rem;
}

.stat-table th,
.stat-table td,
.matrix-table th,
.matrix-table td,
.log-table th,
.log-table td {
  border-bottom: 1px solid var(--border);
  padding: 0.6rem;
  text-align: left;
  white-space: nowrap;
}

.stat-table th,
.matrix-table th,
.log-table th {
  color: var(--heading);
  background: rgba(15, 23, 42, 0.95);
}

.kv {
  display: grid;
  gap: 0.45rem;
  color: var(--muted);
}

.kv strong {
  color: var(--heading);
}

@media (max-width: 1200px) {
  .controls-grid,
  .workspace-grid,
  .summary-grid,
  .chart-grid {
    grid-template-columns: repeat(2, minmax(260px, 1fr));
  }

  .large-chart {
    grid-column: span 1;
  }

  .app-header {
    flex-direction: column;
  }

  .status-card {
    min-width: 0;
  }
}

@media (max-width: 760px) {
  .container {
    width: min(100% - 1rem, 1800px);
  }

  .app-header {
    padding: 1rem;
  }

  .controls-grid,
  .workspace-grid,
  .summary-grid,
  .chart-grid,
  .custom-class-tools {
    grid-template-columns: 1fr;
  }

  .canvas-title-row {
    flex-direction: column;
  }

  .pixel-info {
    text-align: left;
  }

  .button-row {
    display: grid;
    grid-template-columns: 1fr;
  }

  button,
  label.file-button {
    width: 100%;
  }

  .tree-title-row {
    flex-direction: column;
    align-items: flex-start;
  }

  .tree-zoom-controls {
    width: 100%;
  }

  .tree-viewport {
    height: 480px;
  }
}