/* ============================================================
   GLYPH GRID — The Lab
   Dark arcade / retro-terminal pixel glyph workbench
   ============================================================ */

@font-face {
  font-family: "Press Start 2P";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("/press-start-2p.woff2") format("woff2");
}
@font-face {
  font-family: "Press Start 2P Fallback";
  src: local("Courier New");
  size-adjust: 166.64%;
  ascent-override: 60%;
  descent-override: 0%;
  line-gap-override: 0%;
}

:root {
  color-scheme: dark;

  /* core lab palette */
  --bg: #0a0a12;
  --surface: #12121d;
  --surface-2: #1a1a2a;
  --surface-3: #20203200;
  --line: #28283c;
  --line-strong: #3a3a55;
  --text: #e9e9f2;
  --dim: #9aa0b5;

  --cyan: #45f0ff;
  --magenta: #ff3d8b;
  --gold: #ffc857;
  --green: #41ff8b;
  --red: #ff5560;

  /* semantic */
  --accent: var(--cyan);
  --danger: var(--red);

  --pixel: "Press Start 2P", "Press Start 2P Fallback", "Courier New", monospace;
  --sans: -apple-system, "Segoe UI", system-ui, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;

  --radius: 8px;
  --duration-fast: 150ms;
  --duration-normal: 300ms;
  --ease-out-expo: cubic-bezier(0.16, 1, 0.3, 1);

  --glow-cyan: 0 0 12px rgba(69, 240, 255, 0.45);
  --glow-magenta: 0 0 12px rgba(255, 61, 139, 0.45);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100svh;
  background:
    radial-gradient(ellipse 90% 60% at 50% -10%, rgba(69, 240, 255, 0.07), transparent 60%),
    radial-gradient(ellipse 70% 50% at 90% 105%, rgba(255, 61, 139, 0.06), transparent 60%),
    var(--bg);
  background-attachment: fixed;
  color: var(--text);
  font-family: var(--sans);
  font-size: clamp(1rem, 0.94rem + 0.3vw, 1.0625rem);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--cyan);
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

/* ---------- CRT scanline texture ---------- */
.scanlines {
  position: fixed;
  inset: 0;
  z-index: 9998;
  pointer-events: none;
  background: repeating-linear-gradient(
    0deg,
    rgba(0, 0, 0, 0.16) 0 1px,
    transparent 1px 3px
  );
  mix-blend-mode: multiply;
  opacity: 0.55;
}
.scanlines::after {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(ellipse at center, transparent 62%, rgba(0, 0, 0, 0.38) 100%);
}

/* ---------- skip + lab link ---------- */
.skip-link {
  position: absolute;
  left: 1rem;
  top: -5rem;
  z-index: 200;
  padding: 0.75rem 1rem;
  font-family: var(--pixel);
  font-size: 10px;
  color: var(--bg);
  background: var(--cyan);
  border-radius: 3px;
  text-decoration: none;
  transition: top var(--duration-fast) var(--ease-out-expo);
}
.skip-link:focus-visible {
  top: 1rem;
}

.lab-link {
  position: fixed;
  top: clamp(0.6rem, 2vw, 1rem);
  left: clamp(0.6rem, 2vw, 1rem);
  z-index: 150;
  display: inline-flex;
  align-items: center;
  gap: 0.4em;
  padding: 0.6rem 0.85rem;
  font-family: var(--pixel);
  font-size: 9px;
  letter-spacing: 0.06em;
  color: var(--dim);
  text-decoration: none;
  background: rgba(10, 10, 18, 0.82);
  border: 1px solid var(--line);
  border-radius: 4px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  transition: color var(--duration-fast) ease,
    border-color var(--duration-fast) ease,
    box-shadow var(--duration-fast) ease,
    transform var(--duration-fast) var(--ease-out-expo);
}
.lab-link:hover,
.lab-link:focus-visible {
  color: var(--cyan);
  border-color: var(--cyan);
  box-shadow: var(--glow-cyan);
  transform: translateX(-2px);
}

/* ---------- focus ---------- */
:focus-visible {
  outline: 3px solid var(--cyan);
  outline-offset: 3px;
  box-shadow: 0 0 0 6px rgba(69, 240, 255, 0.22);
  border-radius: 2px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- shell ---------- */
.app-shell {
  width: 100%;
  max-width: 88rem;
  margin: 0 auto;
  padding: clamp(3.75rem, 5vw, 4.5rem) clamp(0.85rem, 3vw, 2rem) 2rem;
}

/* ---------- header / wordmark ---------- */
.app-header {
  display: grid;
  gap: 1.1rem;
  align-items: end;
  margin: clamp(1rem, 3vw, 2rem) 0 1.75rem;
}

.level {
  margin-bottom: 0.7rem;
  font-family: var(--pixel);
  font-size: 9px;
  letter-spacing: 0.18em;
  color: var(--magenta);
  text-shadow: var(--glow-magenta);
}

.wordmark {
  position: relative;
  margin: 0;
  font-family: var(--pixel);
  font-weight: 400;
  font-size: clamp(1.45rem, 0.6rem + 5vw, 3.25rem);
  line-height: 1.12;
  letter-spacing: 0.02em;
  color: var(--text);
  text-shadow: 0 0 18px rgba(69, 240, 255, 0.4), 0 0 52px rgba(69, 240, 255, 0.16);
}
.wordmark::before,
.wordmark::after {
  content: attr(data-text);
  position: absolute;
  inset: 0;
  opacity: 0;
  pointer-events: none;
  text-shadow: none;
}
.wordmark::before { color: var(--cyan); }
.wordmark::after { color: var(--magenta); }
.wordmark:hover::before { animation: glitch-a 0.42s steps(3, jump-none) 1; }
.wordmark:hover::after { animation: glitch-b 0.42s steps(3, jump-none) 1; }

@keyframes glitch-a {
  0%   { opacity: 0.8; clip-path: inset(8% 0 72% 0);  transform: translate3d(-4px, -2px, 0); }
  33%  { opacity: 0.8; clip-path: inset(54% 0 22% 0); transform: translate3d(3px, 1px, 0); }
  66%  { opacity: 0.8; clip-path: inset(28% 0 50% 0); transform: translate3d(-2px, 2px, 0); }
  100% { opacity: 0;   transform: none; }
}
@keyframes glitch-b {
  0%   { opacity: 0.8; clip-path: inset(64% 0 16% 0); transform: translate3d(4px, 2px, 0); }
  33%  { opacity: 0.8; clip-path: inset(12% 0 70% 0); transform: translate3d(-3px, -1px, 0); }
  66%  { opacity: 0.8; clip-path: inset(42% 0 36% 0); transform: translate3d(2px, -2px, 0); }
  100% { opacity: 0;   transform: none; }
}

.tagline {
  margin: 0.7rem 0 0;
  font-family: var(--mono);
  font-size: 0.85rem;
  letter-spacing: 0.04em;
  color: var(--dim);
}

.cursor {
  display: inline-block;
  margin-left: 0.15em;
  color: var(--cyan);
  animation: blink 1.1s steps(2, jump-none) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.privacy-note {
  margin-bottom: 0;
  max-width: 46ch;
  font-size: 0.9rem;
  color: var(--dim);
}
.muted,
.status-text {
  color: var(--dim);
}
.status-text {
  font-family: var(--mono);
  font-size: 0.82rem;
}

/* ---------- panels ---------- */
.panel {
  position: relative;
  padding: clamp(1rem, 2.5vw, 1.4rem);
  background: linear-gradient(180deg, var(--surface-2), var(--surface) 70%);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 14px 34px rgba(0, 0, 0, 0.4);
}

/* subtle top-edge connector tab — arcade cabinet feel */
.panel::before {
  content: "";
  position: absolute;
  top: -1px;
  left: 1.4rem;
  width: 2.4rem;
  height: 3px;
  background: linear-gradient(90deg, var(--cyan), var(--magenta));
  border-radius: 0 0 3px 3px;
  opacity: 0.7;
}

.section-heading {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  justify-content: space-between;
  margin-bottom: 1.1rem;
}
.section-heading > div { min-width: 0; }
.section-heading p { margin-bottom: 0; }

h2 {
  margin: 0 0 0.3rem;
  font-family: var(--pixel);
  font-weight: 400;
  font-size: clamp(11px, 2.4vw, 13px);
  letter-spacing: 0.04em;
  color: var(--text);
  text-shadow: 0 0 14px rgba(69, 240, 255, 0.22);
}
.h-deco {
  color: var(--magenta);
  text-shadow: var(--glow-magenta);
}

h3 {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  margin: 0;
  font-family: var(--mono);
  font-size: 0.92rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--text);
}

/* ---------- inputs ---------- */
textarea,
input[type="text"],
input[type="number"] {
  width: 100%;
  min-height: 2.75rem;
  padding: 0.7rem 0.8rem;
  color: var(--text);
  background: rgba(8, 8, 14, 0.6);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  transition: border-color var(--duration-fast) ease, box-shadow var(--duration-fast) ease;
}
textarea {
  resize: vertical;
  font-family: var(--mono);
  font-size: 0.82rem;
  line-height: 1.5;
  color: var(--green);
}
input[type="number"] {
  max-width: 7rem;
}
textarea:hover,
input[type="text"]:hover,
input[type="number"]:hover {
  border-color: var(--line-strong);
}
textarea:focus,
input[type="text"]:focus,
input[type="number"]:focus {
  border-color: var(--cyan);
  box-shadow: 0 0 0 2px rgba(69, 240, 255, 0.18);
  outline: none;
}
input::placeholder { color: var(--dim); opacity: 0.7; }

.field-row {
  display: grid;
  gap: 0.4rem;
  margin-bottom: 1rem;
}
.field-row label,
.sprite-controls label {
  font-family: var(--mono);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--dim);
}

/* ---------- controls layout ---------- */
.control-grid,
.glyph-actions,
.editor-actions,
.button-row,
.sprite-controls,
.import-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  align-items: center;
}
.control-grid { align-items: end; gap: 0.85rem; }

/* ---------- segmented control ---------- */
.segmented-control {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  min-width: min(100%, 17rem);
  margin: 0;
  padding: 0.45rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(8, 8, 14, 0.5);
}
.segmented-control legend {
  padding: 0 0.35rem;
  font-family: var(--mono);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--dim);
}
.segmented-control label {
  flex: 1 1 4.5rem;
  margin: 0;
}
.segmented-control input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.segmented-control span,
.button,
.file-picker {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 2.75rem;
  width: 100%;
  padding: 0.6rem 0.85rem;
  font-family: var(--mono);
  font-weight: 700;
  font-size: 0.85rem;
  letter-spacing: 0.02em;
  text-align: center;
  text-decoration: none;
  color: var(--dim);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 5px;
  transition: background-color var(--duration-fast) ease,
    border-color var(--duration-fast) ease,
    color var(--duration-fast) ease,
    box-shadow var(--duration-fast) ease,
    transform var(--duration-fast) var(--ease-out-expo);
}
.segmented-control label:hover span {
  color: var(--text);
  border-color: var(--line-strong);
}
.segmented-control input:checked + span {
  color: var(--bg);
  background: var(--cyan);
  border-color: var(--cyan);
  box-shadow: var(--glow-cyan);
}
.segmented-control input:focus-visible + span {
  outline: 3px solid var(--gold);
  outline-offset: 2px;
}

/* ---------- buttons ---------- */
.button,
.file-picker {
  width: auto;
  color: var(--bg);
  background: var(--cyan);
  border-color: var(--cyan);
  box-shadow: 0 0 0 1px rgba(69, 240, 255, 0.2), 0 6px 18px rgba(69, 240, 255, 0.12);
}
.button:hover,
.file-picker:hover {
  transform: translateY(-2px);
  box-shadow: 0 0 0 1px var(--cyan), var(--glow-cyan), 0 8px 22px rgba(69, 240, 255, 0.2);
}
.button:active,
.file-picker:active { transform: translateY(0); }

.button-secondary {
  color: var(--text);
  background: var(--surface-2);
  border-color: var(--line);
  box-shadow: none;
}
.button-secondary:hover {
  color: var(--cyan);
  border-color: var(--cyan);
  box-shadow: 0 0 12px rgba(69, 240, 255, 0.18);
}

.button-danger {
  color: var(--red);
  background: rgba(255, 85, 96, 0.08);
  border-color: rgba(255, 85, 96, 0.4);
  box-shadow: none;
}
.button-danger:hover {
  color: var(--bg);
  background: var(--red);
  border-color: var(--red);
  box-shadow: 0 0 14px rgba(255, 85, 96, 0.4);
}

.button:disabled {
  cursor: not-allowed;
  opacity: 0.4;
  transform: none;
  box-shadow: none;
}
.button:disabled:hover {
  transform: none;
  box-shadow: none;
  color: var(--text);
  border-color: var(--line);
}

/* ---------- switch ---------- */
.switch-control {
  display: inline-flex;
  align-items: center;
  min-height: 2.75rem;
  gap: 0.5rem;
  font-family: var(--mono);
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--text);
}
.switch-control input {
  width: 1.15rem;
  height: 1.15rem;
  accent-color: var(--cyan);
}

/* ---------- workspace ---------- */
.workspace {
  display: grid;
  gap: 1rem;
  margin-top: 1rem;
}
.glyph-actions,
.editor-actions { margin-bottom: 1rem; }

/* ---------- glyph list ---------- */
.glyph-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(7.5rem, 1fr));
  gap: 0.65rem;
}

.glyph-item {
  display: grid;
  gap: 0.5rem;
  justify-items: start;
  min-height: 6.5rem;
  padding: 0.7rem;
  color: var(--text);
  background: rgba(8, 8, 14, 0.55);
  border: 1px solid var(--line);
  border-radius: 6px;
  text-align: left;
  transition: border-color var(--duration-fast) ease,
    box-shadow var(--duration-fast) ease,
    transform var(--duration-fast) var(--ease-out-expo),
    background-color var(--duration-fast) ease;
}
.glyph-item:hover {
  transform: translateY(-2px);
  border-color: var(--line-strong);
  background: var(--surface-2);
}
.glyph-item[aria-current="true"] {
  border-color: var(--cyan);
  background: var(--surface-2);
  box-shadow: 0 0 0 1px var(--cyan), 0 0 18px rgba(69, 240, 255, 0.25);
}
.glyph-item[aria-current="true"] .glyph-item__name { color: var(--cyan); }

.glyph-item__name {
  width: 100%;
  overflow: hidden;
  font-family: var(--mono);
  font-weight: 800;
  font-size: 0.9rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.glyph-item__meta {
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.03em;
  color: var(--dim);
}

/* ---------- mini preview ---------- */
.mini-preview {
  --thumb-cell: 0.42rem;
  display: grid;
  gap: 1px;
  padding: 0.25rem;
  background: #07070d;
  border: 1px solid var(--line);
  border-radius: 3px;
}
.mini-preview__cell {
  width: var(--thumb-cell);
  aspect-ratio: 1;
  background: transparent;
}
.mini-preview__cell.is-on {
  background: var(--cyan);
  box-shadow: 0 0 3px rgba(69, 240, 255, 0.6);
}

/* ---------- pixel grid editor ---------- */
.pixel-grid-wrap {
  display: grid;
  justify-content: center;
  overflow-x: auto;
  padding: clamp(0.6rem, 2vw, 1rem);
  background:
    linear-gradient(45deg, rgba(255, 255, 255, 0.025) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(255, 255, 255, 0.025) 25%, transparent 25%),
    #07070d;
  background-size: 1rem 1rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: inset 0 0 40px rgba(0, 0, 0, 0.6);
}

.pixel-grid {
  --cols: 8;
  --cell-size: min(10vw, 2.6rem);
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell-size));
  gap: 2px;
  padding: 2px;
  background: var(--line);
  border-radius: 4px;
  touch-action: none;
  user-select: none;
}

.pixel-cell {
  width: var(--cell-size);
  aspect-ratio: 1;
  min-width: 1.6rem;
  min-height: 1.6rem;
  padding: 0;
  background: #0d0d16;
  border: 0;
  border-radius: 2px;
  transition: background-color 80ms ease, box-shadow 80ms ease;
}
.pixel-grid.show-grid .pixel-cell {
  box-shadow: inset 0 0 0 1px rgba(58, 58, 85, 0.6);
}
.pixel-cell:hover {
  background: #181826;
}
.pixel-cell.is-on {
  background: var(--cyan);
  box-shadow: 0 0 6px rgba(69, 240, 255, 0.5), inset 0 0 0 1px rgba(255, 255, 255, 0.25);
}
.pixel-cell.is-on:hover {
  background: #6cf4ff;
}
.pixel-cell:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 1px;
  z-index: 1;
}

/* ---------- import / export ---------- */
.import-panel,
.export-panel {
  margin-top: 1rem;
}
.import-layout,
.export-grid {
  display: grid;
  gap: 1rem;
}
.import-controls { align-content: start; }

.export-card {
  display: grid;
  gap: 0.75rem;
  min-width: 0;
  padding: 0.9rem;
  background: rgba(8, 8, 14, 0.5);
  border: 1px solid var(--line);
  border-radius: 6px;
}
.export-card__header {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  align-items: center;
  justify-content: space-between;
}
.export-card__tag {
  display: inline-grid;
  place-items: center;
  min-width: 2.2rem;
  padding: 0.2rem 0.4rem;
  font-family: var(--pixel);
  font-size: 8px;
  border: 1px solid currentColor;
  border-radius: 3px;
}
.tag-cyan { color: var(--cyan); text-shadow: var(--glow-cyan); }
.tag-magenta { color: var(--magenta); text-shadow: var(--glow-magenta); }
.tag-green { color: var(--green); text-shadow: 0 0 10px rgba(65, 255, 139, 0.4); }
.tag-gold { color: var(--gold); text-shadow: 0 0 10px rgba(255, 200, 87, 0.4); }

.sprite-preview-wrap {
  max-width: 100%;
  overflow: auto;
  padding: 0.75rem;
  background: #07070d;
  border: 1px solid var(--line);
  border-radius: 6px;
}

#spriteCanvas {
  display: block;
  max-width: 100%;
  height: auto;
  image-rendering: pixelated;
  background:
    linear-gradient(45deg, #15151f 25%, transparent 25%),
    linear-gradient(-45deg, #15151f 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #15151f 75%),
    linear-gradient(-45deg, transparent 75%, #15151f 75%);
  background-color: #0a0a12;
  background-position: 0 0, 0 0.5rem, 0.5rem -0.5rem, -0.5rem 0;
  background-size: 1rem 1rem;
}

/* ---------- footer ---------- */
.app-footer {
  margin-top: 2.5rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--line);
  text-align: center;
  display: grid;
  gap: 0.8rem;
  color: var(--dim);
  font-size: 0.85rem;
}
.app-footer a { color: var(--cyan); }
.app-footer a:hover { text-shadow: var(--glow-cyan); }
.app-footer__flavor {
  margin: 0;
  font-family: var(--pixel);
  font-size: 9px;
  letter-spacing: 0.18em;
  color: var(--gold);
  text-shadow: 0 0 12px rgba(255, 200, 87, 0.3);
}

/* ============================================================
   RESPONSIVE
   ============================================================ */
@media (min-width: 600px) {
  .app-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
  }
  .section-heading {
    flex-direction: row;
    align-items: flex-start;
  }
  .pixel-grid { --cell-size: 2.4rem; }
}

@media (min-width: 768px) {
  .import-layout {
    grid-template-columns: minmax(0, 1fr) 18rem;
  }
  .export-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .pixel-grid { --cell-size: 2.6rem; }
}

@media (min-width: 1024px) {
  .workspace {
    grid-template-columns: minmax(17rem, 0.8fr) minmax(0, 1.4fr);
    align-items: start;
  }
  .pixel-grid { --cell-size: 2.5rem; }
}

/* keep big 16x16 grid usable on small screens without overflow */
@media (max-width: 420px) {
  .pixel-grid { --cell-size: min(9vw, 2.2rem); }
  .pixel-cell { min-width: 1.2rem; min-height: 1.2rem; }
}

/* ============================================================
   REDUCED MOTION
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  .cursor { animation: none; }
  .wordmark:hover::before,
  .wordmark:hover::after { animation: none !important; opacity: 0 !important; }
  .button,
  .file-picker,
  .glyph-item,
  .lab-link,
  .segmented-control span,
  .pixel-cell {
    transition: none;
  }
  .button:hover,
  .file-picker:hover,
  .glyph-item:hover,
  .lab-link:hover { transform: none; }
}
