:root {
  --bg: #ffffff;
  --card: #ffffff;
  --text: #1f2024;
  --muted: #6b6e76;
  --stroke: #d8d4c8;
  --accent: #ff7043;
  --accent-dark: #e35a30;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.06);
  --radius: 12px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }

body {
  font-family: "Inter", "Avenir Next", "Segoe UI", system-ui, sans-serif;
  color: var(--text);
  background: #ffffff;
  min-height: 100vh;
}

h1, h2, h3 { margin: 0; }
h1 { font-size: 26px; }
h2 { font-size: 16px; margin-bottom: 12px; }
h3 { font-size: 14px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }

.topbar {
  padding: 18px 24px;
  background: var(--card);
  border-bottom: 1px solid var(--stroke);
  display: flex;
  align-items: baseline;
  gap: 16px;
  flex-wrap: wrap;
}
.status {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

.layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 18px;
  padding: 18px;
  max-width: 1600px;
  margin: 0 auto;
}

@media (max-width: 1000px) {
  .layout { grid-template-columns: 1fr; }
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.card {
  background: var(--card);
  border: 1px solid var(--stroke);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}

details.card > summary {
  list-style: none;
  cursor: pointer;
}
details.card > summary::-webkit-details-marker { display: none; }
details.card > summary h2 { display: inline-block; }

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 10px;
  font-size: 13px;
  color: var(--muted);
}
.field > span { font-weight: 500; }
.field input[type="text"],
.field input[type="number"],
.field textarea,
.field select {
  font: inherit;
  color: var(--text);
  padding: 6px 8px;
  border: 1px solid var(--stroke);
  border-radius: 8px;
  background: #fff;
  width: 100%;
}
.field textarea { font-family: ui-monospace, "SF Mono", monospace; font-size: 12px; resize: vertical; }

.check {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  font-size: 13px;
}

.segmented {
  display: flex;
  background: #f0eee6;
  border-radius: 8px;
  padding: 3px;
  margin-bottom: 10px;
}
.seg-btn {
  flex: 1;
  background: transparent;
  border: none;
  padding: 6px 8px;
  font: inherit;
  font-size: 13px;
  color: var(--muted);
  border-radius: 6px;
  cursor: pointer;
}
.seg-btn.active {
  background: #fff;
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.panel { margin-bottom: 6px; }
.panel:last-child { margin-bottom: 0; }
.hint { color: var(--muted); font-size: 12px; margin-top: 4px; }

.preview {
  margin-top: 10px;
  border: 1px solid var(--stroke);
  border-radius: 8px;
  overflow: hidden;
  max-height: 160px;
  display: flex;
  justify-content: center;
  background: #ffffff;
}
.preview[hidden] { display: none; }

.palette-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
  padding: 8px;
  border: 1px solid var(--stroke);
  border-radius: 8px;
  margin-top: 6px;
  max-height: 150px;
  overflow-y: auto;
  background: #ffffff;
}
.palette-preview-error {
  color: #b03a18;
  font-size: 12px;
}
.palette-square {
  width: 22px;
  height: 22px;
  border-radius: 4px;
  position: relative;
  border: 1px solid rgba(0, 0, 0, 0.12);
  flex: 0 0 auto;
}
.palette-square-num {
  position: absolute;
  bottom: 0;
  right: 2px;
  font-size: 9px;
  line-height: 1;
  color: #ffffff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
  font-weight: 700;
}

.mono {
  font-family: ui-monospace, "SF Mono", monospace;
  background: #faf9f3;
  padding: 1px 6px;
  border-radius: 6px;
  font-size: 12px;
  color: var(--text);
  margin-left: 4px;
}
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  background: var(--stroke);
  border-radius: 4px;
  outline: none;
  margin: 6px 0 0;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 2px solid #ffffff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
}
input[type="range"]::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 2px solid #ffffff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
}
.preview img { max-width: 100%; max-height: 160px; display: block; }

.drop-zone {
  position: relative;
  border: 2px dashed var(--stroke);
  border-radius: 10px;
  padding: 18px;
  text-align: center;
  cursor: pointer;
  background: #ffffff;
  transition: border-color 120ms, background 120ms, transform 120ms;
}
.drop-zone:hover { border-color: var(--accent); background: #ffffff; }
.drop-zone.dragging {
  border-color: var(--accent);
  background: #ffffff;
  transform: scale(1.01);
}
.drop-zone-inner { display: flex; flex-direction: column; gap: 4px; color: var(--muted); }
.drop-zone-inner strong { color: var(--text); }

.primary {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 12px;
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  box-shadow: 0 4px 10px rgba(255, 112, 67, 0.3);
}
.primary:hover { background: var(--accent-dark); }
.primary:disabled { opacity: 0.6; cursor: progress; }

.error {
  background: #fff0eb;
  color: #b03a18;
  border: 1px solid #f5c0a8;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  margin: 8px 0 0;
}

.content {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
}

.image-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 14px;
}
.image-grid .panel {
  background: var(--card);
  border: 1px solid var(--stroke);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: var(--shadow);
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.image-grid .panel.empty {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 180px;
}
.image-grid .panel .ph {
  color: var(--muted);
  font-size: 13px;
  border: 2px dashed var(--stroke);
  border-radius: 8px;
  padding: 24px;
  width: 100%;
  text-align: center;
}
.image-grid img {
  width: 100%;
  height: auto;
  display: block;
  border-radius: 8px;
  border: 1px solid var(--stroke);
  background: #fff;
}

.painter-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 8px;
}
.painter-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.painter-actions button {
  background: #fff;
  border: 1px solid var(--stroke);
  border-radius: 8px;
  padding: 6px 10px;
  font: inherit;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
}
.painter-actions button:hover:not(:disabled) { background: #f5f4ee; }
.painter-actions button:disabled { opacity: 0.45; cursor: not-allowed; }
.painter-actions button#downloadNumberedBtn { background: #fff5ec; border-color: #f3c6a9; }
.painter-actions button#downloadNumberedBtn:hover { background: #ffe9d9; }

.palette-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 6px;
  background: #ffffff;
  border: 1px solid var(--stroke);
  border-radius: 8px;
  margin: 8px 0 12px;
}
.swatch {
  position: relative;
  width: 36px;
  height: 36px;
  border-radius: 6px;
  border: 2px solid transparent;
  cursor: pointer;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
}
.swatch.selected { border-color: var(--accent); transform: scale(1.05); }
.swatch.empty-swatch {
  background: repeating-conic-gradient(#fff 0% 25%, #e8e6dd 25% 50%) 50% / 12px 12px;
  color: var(--muted);
  text-shadow: none;
}

.paint-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
  margin: 10px 0 8px;
}
.zoom-controls, .paint-tools {
  display: flex;
  align-items: center;
  gap: 6px;
}
.zoom-controls button,
.paint-tools button {
  background: #fff;
  border: 1px solid var(--stroke);
  border-radius: 8px;
  padding: 5px 10px;
  font: inherit;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  min-width: 32px;
}
.zoom-controls button:hover,
.paint-tools button:hover { background: #f5f4ee; }
.zoom-controls button:disabled,
.paint-tools button:disabled { opacity: 0.45; cursor: not-allowed; }
.zoom-label {
  font-size: 12px;
  color: var(--muted);
  min-width: 50px;
  text-align: center;
}
.counter {
  font-size: 12px;
  color: var(--muted);
  padding: 4px 8px;
  background: #ffffff;
  border-radius: 8px;
}

.canvas-host {
  position: relative;
  width: 100%;
  border-radius: 8px;
  overflow: auto;
  border: 1px solid var(--stroke);
  background: #ffffff;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  /* Clear visual separation from the image grid above. */
  margin-top: 4px;
  max-height: 80vh;
}
.canvas-frame {
  position: relative;
  display: block;
  background: #ffffff;
  /* Width and height are set by JS based on zoom. */
}
.canvas-frame canvas {
  display: block;
}
#paintOverlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
#paintCanvas { touch-action: none; cursor: crosshair; }

.paint-status {
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
  font-size: 13px;
  color: var(--muted);
}

.thinking {
  position: fixed;
  inset: 0;
  background: rgba(20, 22, 26, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
.thinking .panel {
  background: var(--card);
  padding: 18px 24px;
  border-radius: var(--radius);
  font-size: 14px;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: 12px;
}
.thinking .spinner {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 3px solid var(--stroke);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }