:root {
  --bg: #f4f5f7;
  --card: #fff;
  --border: #e5e7eb;
  --text: #1f2937;
  --text-secondary: #6b7280;
  --text-tertiary: #9ca3af;
  --accent: #10b981;
  --accent-hover: #059669;
  --danger: #ef4444;
  --warning: #f59e0b;
  --info: #3b82f6;
  --radius: 0.5rem;
  --radius-lg: 0.75rem;
  --shadow: 0 1px 3px rgba(0,0,0,0.1);
}
[data-theme="dark"] {
  --bg: #0f172a;
  --card: #1e293b;
  --border: #334155;
  --text: #f8fafc;
  --text-secondary: #94a3b8;
  --text-tertiary: #64748b;
  --accent: #34d399;
  --accent-hover: #10b981;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.site-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
.site-title { font-size: 1.25rem; font-weight: 700; flex: 1; }
.menu-toggle {
  display: none;
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  padding: 0.25rem;
}
.theme-toggle {
  background: none;
  border: none;
  font-size: 1.25rem;
  cursor: pointer;
  padding: 0.25rem;
}

.container { padding: 1rem; max-width: 960px; margin: 0 auto; }

.tabs-scroll {
  display: flex;
  gap: 0.5rem;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  padding-bottom: 0.5rem;
  margin-bottom: 1rem;
}
.tabs-scroll::-webkit-scrollbar { display: none; }
.tab-btn {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 1rem;
  cursor: pointer;
  white-space: nowrap;
  color: var(--text);
}
.tab-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.panel { display: none; }
.panel.active { display: block; }

.controls-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
  background: var(--card);
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}
.controls-row label {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.9rem;
  color: var(--text-secondary);
}
.controls-row input[type="number"] {
  width: 5ch;
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
}
.controls-row select {
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
}
.btn-accent {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 0.45rem 1rem;
  cursor: pointer;
  font-weight: 600;
}
.btn-accent:hover { background: var(--accent-hover); }

.hint {
  color: var(--text-tertiary);
  font-size: 0.85rem;
  margin: -0.5rem 0 1rem;
}

.layer-controls {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
  background: var(--card);
  padding: 0.5rem 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

.guide-wrap {
  display: inline-flex;
  flex-direction: column;
  gap: 1px;
  background: var(--border);
  border: 1px solid var(--border);
  padding: 4px;
  border-radius: var(--radius);
  overflow: auto;
  max-width: 100%;
}
.row {
  display: flex;
  gap: 1px;
}
.cell {
  width: 16px;
  height: 16px;
  background: var(--card);
}
.cell.fill { background: var(--text); }
.cell.empty { background: var(--bg); }

/* Sphere layer mode: use accent for filled */
.cell.layer-fill { background: var(--accent); }

.stats {
  margin-top: 0.75rem;
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.image-preview-row {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}
.preview-box {
  flex: 1 1 280px;
  min-width: 0;
}
.preview-label {
  font-size: 0.8rem;
  color: var(--text-tertiary);
  margin-bottom: 0.35rem;
}
#imgPreview {
  display: block;
  max-width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card);
}

.thresh-val {
  font-variant-numeric: tabular-nums;
  min-width: 2.5ch;
  display: inline-block;
  text-align: right;
  font-size: 0.9rem;
  color: var(--text-secondary);
}

.palette-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.75rem;
}
.palette-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.25rem 0.5rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.8rem;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}
.palette-swatch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 3px;
  border: 1px solid var(--border);
}

/* 3D preview viewport */
.viewport-3d {
  width: 100%;
  height: 380px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-top: 1rem;
  position: relative;
  overflow: hidden;
}
.viewport-3d canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
  border-radius: var(--radius);
  touch-action: none;
}
.viewport-3d-label {
  position: absolute;
  top: 0.5rem;
  left: 0.75rem;
  font-size: 0.75rem;
  color: var(--text-tertiary);
  pointer-events: none;
  background: var(--card);
  padding: 0.15rem 0.5rem;
  border-radius: var(--radius);
  opacity: 0.85;
  z-index: 1;
}
.viewport-3d-empty {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-tertiary);
  font-size: 0.9rem;
  text-align: center;
  padding: 1rem;
}
.viewport-3d.has-content .viewport-3d-empty { display: none; }

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  backdrop-filter: blur(2px);
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity .15s ease;
}
.modal-overlay.active { opacity: 1; pointer-events: auto; }
.modal {
  background: var(--card);
  border-radius: var(--radius-lg);
  box-shadow: 0 25px 50px -12px rgba(0,0,0,0.25);
  max-width: 600px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  animation: modalIn .2s ease;
}
.modal-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1.25rem;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 { margin: 0; flex: 1; }
.modal-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: var(--text-secondary);
}
.modal-body { padding: 1.25rem; }

@keyframes modalIn { from { transform: translateY(10px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

@media (max-width: 768px) {
  .menu-toggle { display: block; position: fixed; z-index: 1001; left: 0.75rem; top: 0.75rem; }
  .tabs-scroll {
    position: fixed;
    top: 0; left: 0;
    width: 260px; height: 100vh;
    background: var(--card);
    box-shadow: 4px 0 20px rgba(0,0,0,0.25);
    border-right: 1px solid var(--border);
    z-index: 1000;
    padding: 4rem 1rem 1rem;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    flex-direction: column;
  }
  .tabs-scroll.open { transform: translateX(0); }
}

@media (max-width: 640px) {
  .site-title { margin: 0 auto; text-align: center; }
  .modal-overlay { padding: 0; align-items: flex-end; }
  .modal { max-width: 100%; width: 100%; max-height: 92vh; border-radius: var(--radius-lg) var(--radius-lg) 0 0; }
  .modal-header { padding: 1rem; }
}
