:root { color-scheme: dark; }

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

body {
  font: 14px/1.5 'Courier Prime', ui-monospace, SFMono-Regular, Menlo, monospace;
  background: #14141b; color: #e8e6f0;
  display: flex; flex-direction: column;
  height: 100vh; overflow: hidden;
}

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

header {
  display: flex; align-items: center; gap: 1rem;
  padding: .6rem 1.25rem;
  border-bottom: 1px solid #33304a;
  background: #1c1b26;
  flex-shrink: 0;
}

header h1 {
  font-family: 'Press Start 2P', monospace;
  font-size: 12px; font-weight: 400; white-space: nowrap;
  letter-spacing: 0.05em;
}
header h1 code { color: #ff6ec7; }

.controls {
  display: flex; align-items: center; gap: .6rem;
  flex-wrap: wrap;
}

/* ── Selects ──────────────────────────────────────────── */

.controls select {
  font: 12px/1 'Courier Prime', monospace;
  color: #e8e6f0;
  background: #282a36;
  border: 1px solid #33304a; border-radius: 6px;
  padding: .35rem 1.8rem .35rem .6rem;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%239d99b5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right .6rem center;
}
.controls select:hover {
  border-color: #7cc7ff;
}
.controls select:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: 0 0 0 1px #3b82f6;
}

/* ── Buttons ──────────────────────────────────────────── */

.controls button {
  font: 12px/1 'Courier Prime', monospace;
  background: #282a36; color: #e8e6f0;
  border: 1px solid #33304a; border-radius: 6px;
  padding: .35rem .6rem; cursor: pointer;
}
.controls button:hover {
  border-color: #7cc7ff;
}
.controls button.primary {
  background: #3b82f6; border-color: #3b82f6; color: #fff;
}
.controls button.primary:hover {
  background: #2563eb;
}

/* ── Nav link ─────────────────────────────────────────── */

.nav-link {
  font: 12px/1 'Courier Prime', monospace;
  color: #9d99b5; text-decoration: none;
  padding: .35rem .6rem;
  border: 1px solid #33304a; border-radius: 6px;
}
.nav-link:hover {
  color: #7cc7ff; border-color: #7cc7ff;
}

/* ── Source toggle ─────────────────────────────────────── */

.source-toggle {
  display: flex; gap: 0;
}

.source-toggle label {
  font: 11px/1 'Courier Prime', monospace;
  padding: .35rem .6rem;
  background: #282a36; color: #9d99b5;
  border: 1px solid #33304a; cursor: pointer;
}
.source-toggle label:first-child { border-radius: 6px 0 0 6px; }
.source-toggle label:last-child  { border-radius: 0 6px 6px 0; }
.source-toggle input { display: none; }
.source-toggle input:checked + span { color: #e8e6f0; }
.source-toggle label:has(input:checked) {
  background: #3b82f6; color: #fff; border-color: #3b82f6;
}

/* ── Tool nav ─────────────────────────────────────────── */

.tool-nav {
  padding: .4rem 1.25rem;
  border-bottom: 1px solid #33304a;
  background: #1c1b26;
  flex-shrink: 0;
}
.tool-nav .back-link {
  font-family: 'Press Start 2P', monospace;
  font-size: 7px;
  color: #9d99b5;
  background: #282a36;
  padding: 6px 10px;
  border: 1px solid #33304a;
  text-decoration: none;
  letter-spacing: 0.1em;
  transition: color 0.15s, border-color 0.15s;
}
.tool-nav .back-link:hover {
  color: #e8e6f0;
  border-color: #7cc7ff;
}

/* ── Main split pane ──────────────────────────────────── */

main {
  display: flex; flex: 1; min-height: 0;
}

#editor-panel {
  flex: 1; min-width: 0; overflow: hidden;
  display: flex; flex-direction: column;
}

#divider {
  width: 8px; cursor: col-resize;
  background: #33304a;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
}
#divider:hover { background: #7cc7ff; }
.divider-grip {
  font-size: 14px; color: #555; line-height: 1;
  pointer-events: none;
}
#divider:hover .divider-grip { color: #14141b; }

#output-panel {
  flex: 1; min-width: 0; overflow: auto;
  display: flex; flex-direction: column;
  background: #14141b;
}

/* ── Editor (CodeMirror) ──────────────────────────────── */

#editor-panel .cm-editor {
  flex: 1; overflow: auto;
}
#editor-panel .cm-editor .cm-scroller {
  overflow: auto;
}
#editor-panel .cm-editor .cm-content {
  font-family: 'Courier Prime', ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 14px;
}

/* ── Output panel ─────────────────────────────────────── */

#output-content {
  flex: 1; padding: 1rem; overflow: auto;
}

#output-content canvas {
  display: block; max-width: 100%;
  border: 1px solid #33304a;
}

#console-log {
  border-top: 1px solid #33304a;
  padding: .5rem 1rem;
  max-height: 30vh; overflow: auto;
  font-size: 12px;
  background: #1c1b26;
}

.console-line {
  padding: .125rem 0;
  border-bottom: 1px solid #1e1d28;
  white-space: pre-wrap; word-break: break-all;
}
.console-line.log { color: #e8e6f0; }
.console-line.warn { color: #e5c07b; }
.console-line.error { color: #ff6b6b; }

/* ── Empty state ──────────────────────────────────────── */

.empty-state {
  display: flex; align-items: center; justify-content: center;
  flex: 1; color: #9d99b5; font-size: 12px;
}

/* ── Status bar ───────────────────────────────────────── */

footer {
  display: flex; align-items: center; gap: 1rem;
  padding: .25rem 1rem;
  border-top: 1px solid #33304a;
  background: #1c1b26;
  font-size: 11px; color: #9d99b5;
  flex-shrink: 0;
}

footer .dot {
  display: inline-block; width: 8px; height: 8px;
  border-radius: 50%; margin-right: .25rem;
}
footer .dot.ok      { background: #6ee7a8; }
footer .dot.err     { background: #ff6b6b; }
footer .dot.loading { background: #9d99b5; }

/* ── Mobile ───────────────────────────────────────────── */

@media (max-width: 768px) {
  main { flex-direction: column; }
  #divider {
    width: auto; height: 8px; cursor: row-resize;
  }
  header { flex-wrap: wrap; }
}

/* ── Tiles: Canvas ────────────────────────────────────── */

#canvas-panel {
  flex: 1; min-width: 0;
  display: flex; align-items: flex-start; justify-content: center;
  padding: 1.5rem;
  overflow: auto;
}

#grid-canvas {
  border: 1px solid #33304a;
  cursor: crosshair;
  image-rendering: pixelated;
}

/* ── Tiles: Sidebar ───────────────────────────────────── */

#sidebar {
  width: 240px; flex-shrink: 0;
  padding: .75rem;
  border-left: 1px solid #33304a;
  background: #1c1b26;
  overflow-y: auto;
}

#sidebar section {
  margin-bottom: 1rem;
}

#sidebar h2 {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px; font-weight: 400;
  color: #ff6ec7; margin-bottom: .5rem;
  letter-spacing: 0.05em;
}

/* ── Tiles: Palette ───────────────────────────────────── */

#palette {
  display: flex; flex-wrap: wrap; gap: 6px;
}

.swatch {
  width: 28px; height: 28px;
  border-radius: 6px;
  border: 2px solid #33304a;
  cursor: pointer;
  position: relative;
  display: flex; align-items: center; justify-content: center;
}
.swatch:hover { border-color: #7cc7ff; }
.swatch.active {
  border-color: #3b82f6;
  box-shadow: 0 0 0 1px #3b82f6;
}

.swatch-label {
  font: bold 9px/1 'Courier Prime', monospace;
  color: rgba(0,0,0,0.7);
  pointer-events: none;
  text-shadow: 0 0 2px rgba(255,255,255,0.3);
}
.swatch-light .swatch-label { color: rgba(255,255,255,0.8); text-shadow: none; }

.swatch input[type="color"] {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  opacity: 0; cursor: pointer;
}

/* ── Tiles: Grid size inputs ──────────────────────────── */

.row {
  display: flex; align-items: center; gap: .4rem;
}

.label-text {
  font-size: 11px; color: #9d99b5;
  display: block; margin-bottom: .15rem;
}

.separator { color: #9d99b5; }

input[type="number"] {
  font: 12px/1 'Courier Prime', monospace;
  color: #e8e6f0;
  background: #282a36;
  border: 1px solid #33304a; border-radius: 4px;
  padding: .3rem .4rem;
  width: 50px;
  text-align: center;
}
input[type="number"]:focus {
  outline: none;
  border-color: #3b82f6;
}

/* ── Tiles: Standalone buttons ────────────────────────── */

button {
  font: 12px/1 'Courier Prime', monospace;
  background: #282a36; color: #e8e6f0;
  border: 1px solid #33304a; border-radius: 6px;
  padding: .35rem .6rem; cursor: pointer;
}
button:hover { border-color: #7cc7ff; }
button.primary {
  background: #3b82f6; border-color: #3b82f6; color: #fff;
}
button.primary:hover { background: #2563eb; }

/* ── Tiles: Solid tiles checkboxes ────────────────────── */

#solid-tiles {
  display: flex; flex-wrap: wrap; gap: 4px;
}

.solid-check {
  display: flex; align-items: center; gap: .25rem;
  font-size: 11px; color: #9d99b5;
  padding: .2rem .4rem;
  border: 1px solid #33304a; border-radius: 4px;
  cursor: pointer;
}
.solid-check:has(input:checked) {
  border-color: #ff6ec7;
  color: #e8e6f0;
}
.solid-check input {
  width: 12px; height: 12px;
  accent-color: #ff6ec7;
  cursor: pointer;
}

/* ── Tiles: Tool buttons ──────────────────────────────── */

.tool-row, .action-row {
  display: flex; flex-wrap: wrap; gap: 4px;
}

.tool-btn {
  font-size: 11px;
}
.tool-btn.active {
  background: #3b82f6; border-color: #3b82f6; color: #fff;
}

/* ── Tiles: Export panel ──────────────────────────────── */

#export-panel {
  border-top: 1px solid #33304a;
  background: #1c1b26;
  flex-shrink: 0;
}

.export-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: .4rem 1rem;
  border-bottom: 1px solid #1e1d28;
  font-size: 11px; color: #9d99b5;
}

#export-output {
  width: 100%;
  font: 12px/1.4 'Courier Prime', monospace;
  color: #e8e6f0;
  background: #14141b;
  border: none;
  padding: .5rem 1rem;
  resize: vertical;
  min-height: 80px;
}
#export-output:focus { outline: none; }

/* ── Tiles: Preview panel ─────────────────────────────── */

#preview-panel {
  border-top: 1px solid #33304a;
  background: #1c1b26;
  flex-shrink: 0;
}
#preview-panel.hidden { display: none; }

.preview-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: .4rem 1rem;
  border-bottom: 1px solid #1e1d28;
  font-size: 11px; color: #9d99b5;
}

#preview-content {
  padding: .75rem 1rem;
  display: flex; gap: 1rem; align-items: flex-start;
}

#preview-content canvas {
  border: 1px solid #33304a;
}

.preview-hint {
  font-size: 11px; color: #9d99b5;
}

/* ── Tiles: Import modal ──────────────────────────────── */

dialog {
  font: 13px/1.5 'Courier Prime', monospace;
  color: #e8e6f0;
  background: #1c1b26;
  border: 1px solid #33304a;
  border-radius: 12px;
  padding: 0;
  max-width: 500px;
  width: calc(100vw - 2rem);
  max-height: 80vh;
  overflow: auto;
  margin: auto;
}
dialog::backdrop {
  background: rgba(0, 0, 0, 0.6);
}

.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: .75rem 1rem;
  border-bottom: 1px solid #33304a;
}
.modal-header h2 {
  font-family: 'Press Start 2P', monospace;
  font-size: 9px; font-weight: 400;
}
.modal-header h2 code { color: #ff6ec7; }

.modal-close {
  font-size: 20px; line-height: 1;
  background: none; border: none; color: #9d99b5;
  cursor: pointer; padding: 0 .25rem;
}
.modal-close:hover { color: #7cc7ff; }

.modal-body { padding: 1rem; }
.modal-body p { margin: 0 0 .75rem; }
.modal-body code {
  background: #14141b;
  border: 1px solid #33304a; border-radius: 4px;
  padding: .1rem .35rem;
  font-size: 12px; color: #e5c07b;
}

.modal-body textarea {
  width: 100%;
  font: 12px/1.4 'Courier Prime', monospace;
  color: #e8e6f0;
  background: #14141b;
  border: 1px solid #33304a; border-radius: 6px;
  padding: .5rem;
  resize: vertical;
  margin-bottom: .75rem;
}
.modal-body textarea:focus { outline: none; border-color: #3b82f6; }

.modal-actions {
  display: flex; gap: .5rem; justify-content: flex-end;
}

/* ── Tiles: Mobile ────────────────────────────────────── */

@media (max-width: 768px) {
  #sidebar { width: 100%; border-left: none; border-top: 1px solid #33304a; }
  #canvas-panel { padding: .75rem; }
}
