/* ============================================================
   PXL·CRT — sprite workshop
   CRT-console crafting: chunky pixel grid, phosphor accents,
   sprite-sheet workshop, indie-dev cosy.
   ============================================================ */

:root {
  --bg0: #0b0e12;          /* deepest console */
  --bg1: #10141a;          /* panel base */
  --bg2: #161c24;          /* raised panel */
  --bg3: #1e2630;          /* hover */
  --line: #2a3442;         /* chunky border */
  --line-hi: #3d4c60;
  --ink: #c8d3e0;          /* primary text */
  --ink-dim: #7d8ba0;      /* secondary text */
  --ink-faint: #4d5a6d;
  --phos: #7cff6b;         /* phosphor green accent */
  --phos-dim: #3fae4e;
  --amber: #ffa300;
  --cyan: #29adff;
  --red: #ff5370;
  --magenta: #ff004d;
  --hard: #05070a;         /* hard shadow */
  --mono: "IBM Plex Mono", "SF Mono", ui-monospace, Menlo, Consolas, "Courier New", monospace;
  --tap: 42px;
}

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

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: 0.04em;
  color: var(--ink);
  background:
    radial-gradient(120% 90% at 50% 0%, #131a22 0%, var(--bg0) 60%, #07090d 100%);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-font-smoothing: none;
}

::selection { background: var(--phos); color: #08130a; }

/* ---------------- scrollbars ---------------- */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: var(--bg1); }
::-webkit-scrollbar-thumb { background: var(--line); border: 2px solid var(--bg1); }
::-webkit-scrollbar-thumb:hover { background: var(--line-hi); }

/* ---------------- generic controls ---------------- */

.tbtn {
  font: inherit;
  letter-spacing: 0.06em;
  color: var(--ink);
  background: var(--bg2);
  border: 2px solid var(--line);
  box-shadow: 0 3px 0 var(--hard);
  padding: 0 10px;
  height: 32px;
  min-width: 32px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  user-select: none;
  white-space: nowrap;
}
.tbtn:hover { background: var(--bg3); border-color: var(--line-hi); color: #fff; }
.tbtn:active, .tbtn.pressed { transform: translateY(2px); box-shadow: 0 1px 0 var(--hard); }
.tbtn.on {
  color: #0a140b;
  background: var(--phos);
  border-color: var(--phos);
  text-shadow: none;
}
.tbtn.on:hover { background: #9dff90; }
.tbtn.accent {
  color: #0a140b;
  background: var(--phos);
  border-color: var(--phos);
  font-weight: 700;
}
.tbtn.accent:hover { background: #9dff90; border-color: #9dff90; }
.tbtn.tiny { height: 24px; min-width: 24px; padding: 0 6px; font-size: 11px; }
.tbtn:disabled {
  opacity: 0.38;
  cursor: not-allowed;
  transform: none;
  box-shadow: 0 3px 0 var(--hard);
}
.tbtn:focus-visible, .seg:focus-visible, select:focus-visible, input:focus-visible {
  outline: 2px solid var(--cyan);
  outline-offset: 1px;
}

.seg.on { color: #0a140b; background: var(--phos); border-color: var(--phos); }

select, input[type="text"], input[type="number"] {
  font: inherit;
  color: var(--ink);
  background: var(--bg0);
  border: 2px solid var(--line);
  height: 32px;
  padding: 0 8px;
}
select:hover, input:hover { border-color: var(--line-hi); }
input[type="color"] {
  border: 2px solid var(--line);
  background: var(--bg0);
  width: 40px; height: 32px; padding: 2px;
  cursor: pointer;
}

.group-label {
  color: var(--ink-faint);
  font-size: 10px;
  letter-spacing: 0.18em;
  padding: 0 4px;
  align-self: center;
}

/* tooltips */
[data-tip] { position: relative; }
[data-tip]:hover::after {
  content: attr(data-tip);
  position: absolute;
  bottom: calc(100% + 7px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg0);
  border: 2px solid var(--line-hi);
  color: var(--ink);
  padding: 4px 8px;
  font-size: 10px;
  letter-spacing: 0.05em;
  white-space: nowrap;
  z-index: 60;
  pointer-events: none;
  box-shadow: 0 3px 0 var(--hard);
}
.rail [data-tip]:hover::after, .frame-actions [data-tip]:hover::after {
  left: 0; transform: none;
}

/* ---------------- topbar ---------------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 12px;
  background: linear-gradient(180deg, #171e27, var(--bg1));
  border-bottom: 2px solid var(--line);
  box-shadow: 0 3px 0 var(--hard);
  z-index: 20;
  flex-wrap: wrap;
}

.brand { display: flex; align-items: baseline; gap: 8px; }
.brand-mark {
  width: 14px; height: 14px; align-self: center;
  background:
    conic-gradient(from 0deg, var(--phos) 0 25%, var(--cyan) 0 50%, var(--amber) 0 75%, var(--magenta) 0);
  box-shadow: 0 0 8px rgba(124,255,107,.5), 2px 2px 0 var(--hard);
  image-rendering: pixelated;
}
.brand-name {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 0.14em;
  color: #eef5ff;
  text-shadow: 0 0 12px rgba(124,255,107,.35), 2px 2px 0 var(--hard);
}
.brand-name em { color: var(--phos); font-style: normal; }
.brand-sub { color: var(--ink-faint); font-size: 10px; letter-spacing: 0.22em; text-transform: uppercase; }

.topbar-group { position: relative; display: flex; gap: 6px; align-items: center; }
.topbar-spacer { flex: 1; }

.menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  min-width: 190px;
  background: var(--bg1);
  border: 2px solid var(--line-hi);
  box-shadow: 0 4px 0 var(--hard), 0 0 24px rgba(0,0,0,.6);
  z-index: 50;
  display: flex;
  flex-direction: column;
}
.menu button {
  font: inherit;
  text-align: left;
  color: var(--ink);
  background: none;
  border: none;
  border-bottom: 1px solid var(--line);
  padding: 10px 14px;
  cursor: pointer;
}
.menu button:last-child { border-bottom: none; }
.menu button:hover { background: var(--bg3); color: var(--phos); }

/* ---------------- workbench layout ---------------- */

.workbench {
  flex: 1;
  display: grid;
  grid-template-columns: 64px 1fr 268px;
  min-height: 0;
}

.rail {
  background: var(--bg1);
  border-right: 2px solid var(--line);
  padding: 8px 6px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow-y: auto;
  align-items: stretch;
}
.rail-sep { height: 2px; background: var(--line); margin: 4px 2px; }

.toolbox { display: flex; flex-direction: column; gap: 6px; align-items: center; }

.tool {
  width: 44px; height: 44px;
  padding: 0;
}
.tool svg { display: block; shape-rendering: crispEdges; }
.tool.on { box-shadow: 0 1px 0 var(--hard), 0 0 12px rgba(124,255,107,.45); }

.tool-wide { width: 100%; font-size: 10px; padding: 0 4px; }

.hist-btns { display: flex; gap: 6px; }
.hist-btns .tbtn { flex: 1; padding: 0; font-size: 15px; }

.tool-opts { display: flex; flex-direction: column; gap: 6px; align-items: stretch; }
.tool-opts .opt-label { color: var(--ink-faint); font-size: 9px; letter-spacing: .15em; text-align: center; }
.opt-seg { display: flex; gap: 3px; justify-content: center; }
.opt-seg .tbtn { flex: 1; padding: 0; min-width: 0; height: 26px; font-size: 10px; }
.opt-check {
  display: flex; align-items: center; gap: 6px; justify-content: center;
  color: var(--ink-dim); font-size: 10px; cursor: pointer; user-select: none;
  padding: 4px 0;
}
.opt-check input { accent-color: var(--phos); width: 14px; height: 14px; }

/* ---------------- stage / viewport ---------------- */

.stage { display: flex; flex-direction: column; min-width: 0; min-height: 0; }

.viewport {
  flex: 1;
  position: relative;
  min-height: 0;
  overflow: hidden;
  background:
    repeating-conic-gradient(#12161c 0% 25%, #0d1116 0% 50%) 0 0 / 24px 24px;
  cursor: crosshair;
}
#editor {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none;
}
.scanlines {
  position: absolute; inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(0deg, rgba(0,0,0,0.11) 0 1px, transparent 1px 3px);
  mix-blend-mode: multiply;
  opacity: .55;
}
.viewport::after {           /* soft CRT vignette */
  content: "";
  position: absolute; inset: 0;
  pointer-events: none;
  box-shadow: inset 0 0 120px rgba(0,0,0,.55);
}
.viewport.phosphor-flash #editor { animation: phos-flash .28s steps(4) 1; }
@keyframes phos-flash {
  0%   { filter: brightness(2.1) saturate(.2); }
  55%  { filter: brightness(1.35) saturate(1.6) hue-rotate(18deg); }
  100% { filter: none; }
}
@media (prefers-reduced-motion: reduce) {
  .viewport.phosphor-flash #editor { animation: none; }
}

.viewport-empty {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  color: var(--ink-faint); text-align: center; line-height: 2;
  pointer-events: none;
}

/* ---------------- frame strip ---------------- */

.framebar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  background: var(--bg1);
  border-top: 2px solid var(--line);
  min-height: 74px;
}
.frame-strip {
  flex: 1;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 4px 2px;
  min-width: 0;
}
.frame-thumb {
  position: relative;
  flex: 0 0 auto;
  width: 52px; height: 52px;
  border: 2px solid var(--line);
  background: repeating-conic-gradient(#1a212b 0% 25%, #141920 0% 50%) 0 0 / 12px 12px;
  cursor: pointer;
  box-shadow: 0 3px 0 var(--hard);
}
.frame-thumb canvas {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  image-rendering: pixelated;
}
.frame-thumb:hover { border-color: var(--line-hi); }
.frame-thumb.active {
  border-color: var(--phos);
  box-shadow: 0 3px 0 var(--hard), 0 0 10px rgba(124,255,107,.45);
}
.frame-thumb.dragging { opacity: .35; }
.frame-thumb .fnum {
  position: absolute; right: -2px; bottom: -2px;
  background: var(--bg0); color: var(--ink-dim);
  border: 1px solid var(--line);
  font-size: 9px; padding: 0 3px; line-height: 1.4;
}
.frame-thumb.active .fnum { color: var(--phos); border-color: var(--phos-dim); }
.frame-actions { display: flex; gap: 6px; }

/* ---------------- side panel ---------------- */

.side {
  background: var(--bg1);
  border-left: 2px solid var(--line);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
.panel { border-bottom: 2px solid var(--line); padding: 10px; }
.panel-head {
  color: var(--ink-faint);
  font-size: 10px; letter-spacing: .2em;
  display: flex; justify-content: space-between;
  margin-bottom: 8px;
}
.panel-note { color: var(--ink-faint); font-size: 9px; letter-spacing: .1em; }

.preview-row { display: flex; gap: 10px; align-items: flex-start; }
#pv-big {
  width: 96px; height: 96px;
  border: 2px solid var(--line);
  background: repeating-conic-gradient(#1a212b 0% 25%, #141920 0% 50%) 0 0 / 10px 10px;
  image-rendering: pixelated;
}
.preview-col { display: flex; flex-direction: column; gap: 4px; align-items: center; }
#pv-1x {
  border: 2px solid var(--line);
  background: repeating-conic-gradient(#1a212b 0% 25%, #141920 0% 50%) 0 0 / 6px 6px;
  image-rendering: pixelated;
}
.preview-ctl { display: flex; gap: 8px; align-items: center; margin-top: 10px; }
.mini-label { color: var(--ink-faint); font-size: 10px; display: flex; gap: 5px; align-items: center; }
.minisel { height: 26px; font-size: 11px; }

/* ---------------- palette ---------------- */

.pal-select-row { display: flex; gap: 6px; margin-bottom: 10px; }
.pal-select { flex: 1; min-width: 0; }

.swatches {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 4px;
  margin-bottom: 8px;
}
.swatch {
  aspect-ratio: 1;
  border: 2px solid var(--line);
  cursor: pointer;
  position: relative;
  box-shadow: inset -2px -2px 0 rgba(0,0,0,.28);
  min-height: 0;
}
.swatch:hover { border-color: #fff; z-index: 2; }
.swatch.fg { border-color: var(--phos); box-shadow: 0 0 0 1px var(--phos), 0 0 8px rgba(124,255,107,.6); z-index: 1; }
.swatch.bg::after {
  content: "";
  position: absolute; right: 0; bottom: 0;
  border-style: solid; border-width: 0 0 8px 8px;
  border-color: transparent transparent var(--cyan) transparent;
}
.swatch.transparent {
  background: repeating-conic-gradient(#3a4453 0% 25%, #232b36 0% 50%) 0 0 / 8px 8px !important;
}
.swatch.dragging { opacity: .3; }
.swatch.drop-target { outline: 2px dashed var(--amber); outline-offset: 1px; }

.pal-warning {
  color: var(--amber);
  font-size: 10px;
  line-height: 1.5;
  border: 1px dashed var(--amber);
  padding: 5px 7px;
  margin-bottom: 8px;
}

.pal-actions { display: flex; flex-wrap: wrap; gap: 5px; }
.pal-actions .tbtn { flex: 1 1 30%; font-size: 10px; padding: 0 4px; }

.pal-fgbg { display: flex; align-items: center; gap: 8px; margin-top: 10px; }
.fgbg-chip {
  flex: 1;
  display: flex; align-items: center; gap: 8px;
  border: 2px solid var(--line);
  background: var(--bg0);
  padding: 4px 8px;
  font-size: 10px; color: var(--ink-dim);
}
.fgbg-chip i {
  width: 20px; height: 20px;
  border: 2px solid var(--line-hi);
  display: inline-block;
  box-shadow: inset -2px -2px 0 rgba(0,0,0,.3);
}
.fgbg-chip i.transparent { background: repeating-conic-gradient(#3a4453 0% 25%, #232b36 0% 50%) 0 0 / 8px 8px !important; }

/* ---------------- status bar ---------------- */

.statusbar {
  display: flex;
  align-items: center;
  gap: 0;
  background: var(--bg1);
  border-top: 2px solid var(--line);
  box-shadow: 0 -3px 0 var(--hard);
  min-height: 30px;
  padding: 0 6px;
  overflow-x: auto;
}
.st-cell {
  color: var(--ink-dim);
  font-size: 10px;
  letter-spacing: .08em;
  padding: 0 10px;
  border-right: 1px solid var(--line);
  white-space: nowrap;
  display: flex; align-items: center; gap: 5px;
}
.st-cell:last-child { border-right: none; }
.st-cell strong { color: var(--phos); font-weight: 700; }
.st-flex { flex: 1; }
.st-hint { color: var(--ink-faint); }
#st-save.saved { color: var(--phos-dim); }
#st-save.dirty { color: var(--amber); }
#st-save.dirty::before {
  content: ""; width: 7px; height: 7px; background: var(--amber);
  display: inline-block;
  animation: blink 1.1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: .25; } }

/* ---------------- modals ---------------- */

.modal-veil {
  position: fixed; inset: 0;
  background: rgba(4,6,9,.72);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 18px;
}
.modal {
  background: var(--bg1);
  border: 2px solid var(--line-hi);
  box-shadow: 0 6px 0 var(--hard), 0 0 60px rgba(0,0,0,.7), 0 0 24px rgba(124,255,107,.07);
  min-width: 320px;
  max-width: min(560px, 96vw);
  max-height: 92vh;
  overflow-y: auto;
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 2px solid var(--line);
  color: #eef5ff;
  letter-spacing: .18em; font-size: 11px;
  background: var(--bg2);
}
.modal-head button { font: inherit; background: none; border: none; color: var(--ink-dim); cursor: pointer; font-size: 14px; padding: 0 4px; }
.modal-head button:hover { color: var(--red); }
.modal-body { padding: 14px; display: flex; flex-direction: column; gap: 12px; }
.modal-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 0 14px 14px; }

.field { display: flex; flex-direction: column; gap: 5px; }
.field > span { color: var(--ink-faint); font-size: 10px; letter-spacing: .14em; }
.field-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.field-row .tbtn.seg { flex: 0 0 auto; }

.radio-row { display: flex; gap: 6px; flex-wrap: wrap; }
.radio-row .tbtn { flex: 1 1 auto; }

textarea.pal-io {
  font: inherit; font-size: 10px;
  width: 100%; min-height: 130px;
  background: var(--bg0); color: var(--ink);
  border: 2px solid var(--line);
  padding: 8px; resize: vertical;
  white-space: pre;
}

.exp-preview {
  align-self: center;
  max-width: 100%;
  border: 2px solid var(--line);
  background: repeating-conic-gradient(#1a212b 0% 25%, #141920 0% 50%) 0 0 / 12px 12px;
  image-rendering: pixelated;
}
.exp-dims { color: var(--ink-faint); font-size: 10px; text-align: center; }

.help-table { width: 100%; border-collapse: collapse; font-size: 10px; }
.help-table td { border-bottom: 1px solid var(--line); padding: 5px 6px; }
.help-table td:first-child { color: var(--phos); white-space: nowrap; width: 40%; }
.help-note { color: var(--ink-dim); font-size: 10px; line-height: 1.7; }

/* color picker */
.picker-grid { display: flex; gap: 12px; }
#pk-sv { width: 172px; height: 172px; border: 2px solid var(--line); cursor: crosshair; touch-action: none; }
.pk-col { display: flex; flex-direction: column; gap: 8px; flex: 1; min-width: 0; }
#pk-hue { width: 100%; height: 18px; border: 2px solid var(--line); cursor: crosshair; touch-action: none; }
.pk-preview { display: flex; height: 30px; border: 2px solid var(--line); }
.pk-preview b { flex: 1; display: block; }
.pk-hex { display: flex; gap: 6px; align-items: center; }
.pk-hex input { flex: 1; min-width: 0; font-size: 12px; }
.pk-rgb { display: flex; gap: 6px; }
.pk-rgb label { flex: 1; display: flex; flex-direction: column; gap: 3px; color: var(--ink-faint); font-size: 9px; text-align: center; }
.pk-rgb input { width: 100%; padding: 0 2px; text-align: center; font-size: 11px; }

/* ---------------- toasts ---------------- */

.toasts {
  position: fixed; left: 14px; bottom: 42px;
  display: flex; flex-direction: column; gap: 6px;
  z-index: 200; pointer-events: none;
}
.toast {
  background: var(--bg0);
  border: 2px solid var(--line-hi);
  border-left: 4px solid var(--phos);
  color: var(--ink);
  padding: 8px 14px;
  font-size: 11px;
  box-shadow: 0 4px 0 var(--hard);
  animation: toast-in .18s steps(3) both;
  max-width: 70vw;
}
.toast.warn { border-left-color: var(--amber); }
.toast.err { border-left-color: var(--red); }
.toast.out { animation: toast-out .2s steps(3) both; }
@keyframes toast-in { from { transform: translateX(-12px); opacity: 0; } }
@keyframes toast-out { to { transform: translateX(-12px); opacity: 0; } }

/* ---------------- responsive ---------------- */

@media (max-width: 980px) {
  .workbench { grid-template-columns: 56px 1fr 228px; }
  .pal-actions .tbtn { font-size: 9px; }
}

@media (max-width: 780px) {
  body { overflow: auto; }
  .workbench {
    display: flex; flex-direction: column;
    overflow: visible;
  }
  .workbench > .rail, .workbench > .stage, .workbench > .side { flex-shrink: 0; }
  .rail {
    flex-direction: row;
    flex-wrap: wrap;
    border-right: none;
    border-bottom: 2px solid var(--line);
    align-items: center;
    padding: 6px;
    order: 1;
  }
  .toolbox { flex-direction: row; flex-wrap: wrap; }
  .tool { width: var(--tap); height: var(--tap); }
  .tool-wide { width: auto; flex: 0 0 auto; height: var(--tap); }
  .rail-sep { display: none; }
  .tool-opts { flex-direction: row; align-items: center; }
  .opt-seg .tbtn { min-width: 30px; height: 30px; }
  .opt-check { padding: 0 4px; }

  .stage { order: 2; }
  .viewport { min-height: min(78vw, 430px); flex: none; }
  .side { order: 3; border-left: none; border-top: 2px solid var(--line); }
  .framebar { flex-wrap: wrap; }
  .statusbar { position: sticky; bottom: 0; z-index: 30; }
  .brand-sub, .st-hint { display: none; }
}
