/* ── Reset & base ─────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #0d0000;
  --surface:   #1a0505;
  --surface2:  #250808;
  --border:    #3a1010;
  --red:       #cc2200;
  --red-bright:#ff3311;
  --text:      #e8d8d0;
  --text-dim:  #8a6060;
  --swatch-size: 18px;
  --panel-width: 260px;
}

html, body {
  height: 100%;
  font-family: 'Courier New', Courier, monospace;
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
}

/* ── Header ──────────────────────────────────────────────────────────────── */
header {
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  padding: 12px 20px 8px;
  overflow: hidden;
  position: relative;
}

.github-btn {
  position: absolute;
  top: 14px;
  right: 20px;
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--text);
  text-decoration: none;
  font-size: 11px;
  letter-spacing: 1px;
  background: var(--surface2);
  border: 1px solid #5a2020;
  border-radius: 4px;
  padding: 6px 13px;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.github-btn:hover {
  background: var(--red);
  border-color: var(--red);
  color: #fff;
}

.ascii-logo {
  font-size: 7px;
  line-height: 1.2;
  color: var(--red);
  white-space: pre;
  overflow: hidden;
  letter-spacing: 0;
}

.subtitle {
  color: var(--text-dim);
  font-size: 11px;
  letter-spacing: 4px;
  text-transform: uppercase;
  margin-top: 4px;
}

/* ── Layout ──────────────────────────────────────────────────────────────── */
.layout {
  display: flex;
  height: calc(100vh - 90px);
  overflow: hidden;
}

/* ── Preview area ────────────────────────────────────────────────────────── */
.preview-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 16px;
  gap: 12px;
  overflow-y: auto;
  min-width: 0;
}

/* ── Canvas grid ─────────────────────────────────────────────────────────── */
.canvas-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 10px;
}

.canvas-wrapper {
  position: relative;
  background: #000;
  border: 1px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
  cursor: zoom-in;
}
.canvas-wrapper:hover { border-color: var(--text-dim); }

.canvas-wrapper canvas {
  display: block;
  width: 100%;
  height: auto;
  image-rendering: pixelated;
}

.canvas-remove {
  position: absolute;
  top: 6px; right: 6px;
  width: 20px; height: 20px;
  background: rgba(0,0,0,0.7);
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text-dim);
  font-size: 12px;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.canvas-wrapper:hover .canvas-remove { display: flex; }
.canvas-remove:hover { color: var(--red-bright); border-color: var(--red); }

/* ── Upload zone ─────────────────────────────────────────────────────────── */
.upload-zone {
  border: 1px dashed var(--border);
  border-radius: 6px;
  padding: 20px;
  text-align: center;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  color: var(--text-dim);
  transition: border-color 0.2s, color 0.2s;
}

.upload-zone:hover, .upload-zone.drag-over {
  border-color: var(--red);
  color: var(--text);
}

.upload-zone input { display: none; }

.upload-icon {
  font-size: 24px;
  color: var(--red);
  line-height: 1;
}

.upload-hint { font-size: 11px; color: var(--text-dim); }

.upload-zone.disabled {
  opacity: 0.4;
  cursor: not-allowed;
  pointer-events: none;
}

.upload-zone.disabled:hover {
  border-color: var(--border);
  color: var(--text-dim);
}

/* ── Palette panel ───────────────────────────────────────────────────────── */
.palette-panel {
  width: var(--panel-width);
  min-width: var(--panel-width);
  border-left: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel-search {
  padding: 10px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.panel-search input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  font-family: inherit;
  font-size: 12px;
  padding: 6px 10px;
  outline: none;
}

.panel-search input:focus { border-color: var(--red); }
.panel-search input::placeholder { color: var(--text-dim); }

.palette-section {
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.section-title {
  padding: 8px 12px 4px;
  font-size: 10px;
  letter-spacing: 2px;
  color: var(--text-dim);
  background: var(--surface2);
}

.palette-list {
  overflow-y: auto;
  max-height: 260px;
}

/* Default: solo 1 item, sin scroll */
#section-default .palette-list { max-height: none; }

/* SGB: ocupa todo el espacio restante del panel */
#section-sgb {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}

#section-sgb .palette-list {
  flex: 1;
  max-height: none;
  overflow-y: auto;
  min-height: 0;
}

.palette-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  cursor: pointer;
  border-left: 2px solid transparent;
  transition: background 0.1s;
}

.palette-item:hover { background: var(--surface2); }

.palette-item.active {
  border-left-color: var(--red);
  background: var(--surface2);
}

.palette-item.hidden { display: none; }

.swatches {
  display: flex;
  gap: 2px;
  flex-shrink: 0;
}

.swatch {
  width: var(--swatch-size);
  height: var(--swatch-size);
  border-radius: 2px;
  border: 1px solid rgba(255,255,255,0.08);
}

.palette-name {
  font-size: 12px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Builder ─────────────────────────────────────────────────────────────── */
.builder {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 12px 14px;
  flex-shrink: 0;
}

.builder-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.builder-title-group {
  display: flex;
  align-items: baseline;
  gap: 10px;
}

.builder-title {
  font-size: 10px;
  letter-spacing: 2px;
  color: var(--text-dim);
}

.builder-preset-name {
  font-size: 13px;
  color: var(--red-bright);
  letter-spacing: 1px;
}

/* ── Code bar ────────────────────────────────────────────────────────────── */
.builder-code-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.code-label {
  font-size: 9px;
  letter-spacing: 2px;
  color: var(--text-dim);
  flex-shrink: 0;
}

.code-input {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text);
  font-family: inherit;
  font-size: 12px;
  padding: 4px 8px;
  outline: none;
  letter-spacing: 1px;
  text-transform: uppercase;
}
.code-input:focus { border-color: var(--red); }
.code-input.copied { border-color: #2a9d2a; color: #4caf50; }

.code-btn {
  flex-shrink: 0;
  background: none;
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text-dim);
  font-family: inherit;
  font-size: 11px;
  padding: 4px 10px;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}
.code-btn:hover { border-color: var(--text); color: var(--text); }

.invert-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-family: inherit;
  font-size: 11px;
  letter-spacing: 1px;
  padding: 5px 11px;
  border-radius: 3px;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}
.invert-btn:hover { border-color: var(--text); color: var(--text); }

.export-btn {
  background: var(--red);
  border: none;
  color: #fff;
  font-family: inherit;
  font-size: 11px;
  letter-spacing: 1px;
  padding: 5px 13px;
  border-radius: 3px;
  cursor: pointer;
  transition: background 0.15s;
}
.export-btn:hover { background: var(--red-bright); }

.builder-slots {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.builder-slot {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.slot-label {
  font-size: 9px;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--text-dim);
  text-align: center;
}

.slot-swatch-wrap {
  position: relative;
  border: 1px solid var(--border);
  border-radius: 3px;
  overflow: hidden;
  cursor: pointer;
  transition: border-color 0.15s;
}
.slot-swatch-wrap:hover { border-color: var(--text-dim); }
.builder-slot.locked .slot-swatch-wrap { border-color: var(--red); box-shadow: 0 0 0 1px var(--red); }

.slot-swatch { height: 46px; width: 100%; display: block; }

.color-input {
  position: absolute;
  top: 0; left: 0;
  width: 1px; height: 1px;
  opacity: 0;
  pointer-events: none;
}

.slot-footer {
  display: flex;
  gap: 4px;
  align-items: center;
}

.hex-input {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text);
  font-family: inherit;
  font-size: 11px;
  padding: 3px 6px;
  outline: none;
  text-transform: uppercase;
}
.hex-input:focus { border-color: var(--red); }

.lock-btn {
  flex-shrink: 0;
  background: none;
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text-dim);
  font-size: 13px;
  line-height: 1;
  padding: 3px 5px;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}
.lock-btn:hover { border-color: var(--text-dim); }
.lock-btn.locked { border-color: var(--red); color: var(--red); }

/* ── Zoom modal ──────────────────────────────────────────────────────────── */
.zoom-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.92);
  z-index: 200;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
}
.zoom-overlay.visible { display: flex; }

.zoom-img {
  image-rendering: pixelated;
  border: 1px solid var(--border);
  cursor: default;
  display: block;
}

.zoom-hint {
  color: rgba(255,255,255,0.35);
  font-size: 11px;
  letter-spacing: 1px;
  margin-top: 10px;
  text-align: center;
  pointer-events: none;
  user-select: none;
}

/* fill-height mode: image taller than viewport — allow horizontal scroll */
.zoom-overlay.fill-height {
  align-items: flex-start;
  justify-content: flex-start;
  overflow: auto;
  padding: 8px;
  cursor: default;
}

.zoom-close {
  position: fixed;
  top: 14px;
  right: 18px;
  background: none;
  border: 1px solid rgba(255,255,255,0.2);
  color: rgba(255,255,255,0.5);
  font-size: 18px;
  width: 30px;
  height: 30px;
  border-radius: 3px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  transition: color 0.15s, border-color 0.15s;
}
.zoom-close:hover { color: #fff; border-color: #fff; }

/* ── Export modal ────────────────────────────────────────────────────────── */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  z-index: 100;
  align-items: center;
  justify-content: center;
}
.modal-overlay.visible { display: flex; }

.modal {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 24px;
  width: 360px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-title {
  font-size: 11px;
  letter-spacing: 3px;
  color: var(--red-bright);
  text-transform: uppercase;
}

.modal input[type="text"] {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  padding: 8px 12px;
  outline: none;
}
.modal input[type="text"]:focus { border-color: var(--red); }

.modal-hint {
  font-size: 11px;
  color: var(--text-dim);
  line-height: 1.7;
}
.modal-hint code {
  color: var(--text);
  background: var(--bg);
  padding: 1px 5px;
  border-radius: 2px;
  font-family: inherit;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}
.modal-actions button {
  background: none;
  border: 1px solid var(--border);
  color: var(--text);
  font-family: inherit;
  font-size: 12px;
  padding: 6px 16px;
  border-radius: 3px;
  cursor: pointer;
}
.modal-actions button:hover { border-color: var(--text-dim); }
.btn-primary { background: var(--red) !important; border-color: var(--red) !important; color: #fff !important; }
.btn-primary:hover { background: var(--red-bright) !important; border-color: var(--red-bright) !important; }

/* ── Scrollbar ───────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 4px; height: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
::-webkit-scrollbar-thumb:hover { background: var(--red); }

/* ── Palette drawer (mobile only) ───────────────────────────────────────── */
.palette-trigger { display: none; }

.palette-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 45;
}
.palette-overlay.visible { display: block; }

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

/* ≤ 900px — header: stack logo → subtitle → github, scale ASCII to fit */
@media (max-width: 900px) {
  header {
    padding: 10px 14px;
    overflow: visible;
  }

  .header-inner {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
  }

  /* ASCII at 7px is ~798px wide — scale it to always fit with padding */
  .ascii-logo {
    font-size: min(7px, 0.82vw);
    overflow: visible;
    order: 1;
  }

  .subtitle { order: 2; }

  .github-btn {
    position: static;
    order: 3;
    align-self: flex-start;
  }
}

/* ≤ 640px — phone: 2×2 builder, vertical layout */
@media (max-width: 640px) {
  /* Builder header: title full-width first line, buttons share second line */
  .builder-header {
    flex-wrap: wrap;
    row-gap: 8px;
  }

  .builder-title-group {
    flex-basis: 100%;
  }

  .invert-btn,
  .export-btn {
    flex: 1;
    justify-content: center;
    font-size: 10px;
    padding: 5px 6px;
    letter-spacing: 0.5px;
  }

  /* Code bar */
  .builder-code-bar { flex-wrap: wrap; row-gap: 6px; }
  .code-input       { flex: 1 1 auto; min-width: 0; }
  .code-btn         { flex: 1; }

  /* Swatches 2x2 — minmax(0,1fr) overrides the implicit min-content floor of 1fr */
  .builder-slots { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .builder-slot  { min-width: 0; overflow: hidden; }
  .hex-input     { min-width: 0; width: 100%; }

  /* Layout: vertical, pad bottom for the trigger bar */
  .layout { flex-direction: column; height: auto; overflow: visible; padding-bottom: 50px; }

  /* Palette panel → fixed bottom sheet */
  .palette-panel {
    position: fixed;
    bottom: 50px;
    left: 0; right: 0;
    width: 100% !important;
    min-width: 0;
    max-height: 72vh;
    border-radius: 14px 14px 0 0;
    border-left: none;
    border-top: 1px solid var(--border);
    transform: translateY(100%);
    transition: transform 0.32s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 46;
    overflow-y: auto;
    overflow-x: hidden;
  }

  /* drag handle */
  .palette-panel::before {
    content: '';
    display: block;
    width: 36px; height: 4px;
    background: var(--border);
    border-radius: 2px;
    margin: 10px auto 2px;
  }

  .palette-panel.open { transform: translateY(0); }

  #section-sgb               { flex: none; }
  #section-sgb .palette-list { max-height: none; }

  /* Trigger bar */
  .palette-trigger {
    display: flex;
    position: fixed;
    bottom: 0; left: 0; right: 0;
    align-items: center;
    justify-content: space-between;
    background: var(--surface2);
    border: none;
    border-top: 1px solid var(--border);
    color: var(--text);
    font-family: inherit;
    font-size: 11px;
    letter-spacing: 2px;
    text-transform: uppercase;
    padding: 0 20px;
    height: 50px;
    cursor: pointer;
    z-index: 50;
    transition: background 0.15s;
  }
  .palette-trigger:hover { background: var(--surface); }

  .trigger-left { display: flex; align-items: center; gap: 8px; }

  .trigger-arrow {
    font-size: 13px;
    transition: transform 0.32s cubic-bezier(0.4, 0, 0.2, 1);
  }
  .palette-trigger.open .trigger-arrow { transform: rotate(180deg); }
}
