/*
 * Gridsmith — tool chrome (standalone; not Martial Games portal skin)
 * Copyright (c) 2026 Martial Systems LLC. All rights reserved.
 */

:root {
  --bg: #0a0a0b;
  --bg-elevated: #121214;
  --bg-subtle: #1a1a1e;
  --fg: #f4f4f5;
  --fg-muted: #a1a1aa;
  --fg-subtle: #71717a;
  --border: color-mix(in oklab, #f4f4f5 12%, transparent);
  --border-strong: color-mix(in oklab, #f4f4f5 22%, transparent);
  --danger: #f07178;
  --radius-xs: 4px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --motion: 150ms;
}

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

html {
  color-scheme: dark;
  height: 100%;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

body {
  margin: 0;
  height: 100%;
  max-height: 100dvh;
  overflow: hidden;
  font-family: var(--font);
  background: var(--bg);
  color: var(--fg);
}

button:not(:disabled),
[role="button"]:not(:disabled) {
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.4;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
}

/* —— Header (tool chrome, not site nav) —— */
.app-header {
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
  padding: 0.5rem 0.75rem;
}

.app-header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  max-width: 1600px;
  margin: 0 auto;
  width: 100%;
}

.eyebrow {
  margin: 0;
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--fg-subtle);
}

.app-header h1 {
  margin: 0.1rem 0 0;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--fg);
  line-height: 1.25;
}

.header-tag {
  display: none;
  max-width: 16rem;
  margin: 0;
  font-size: 11px;
  line-height: 1.35;
  color: var(--fg-muted);
  text-align: right;
}

@media (min-width: 768px) {
  .header-tag {
    display: block;
  }
  .app-header h1 {
    font-size: 1.125rem;
  }
}

.header-links {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: 0.65rem 0.85rem;
  flex-shrink: 0;
}

.exit-link {
  font-size: 11px;
  color: var(--fg-subtle);
  text-decoration: none;
  white-space: nowrap;
}
.exit-link:hover {
  color: var(--fg);
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* —— Main 3-column layout —— */
.app-main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.5rem;
  max-width: 1600px;
  width: 100%;
  margin: 0 auto;
  overflow: hidden;
}

@media (min-width: 1024px) {
  .app-main {
    flex-direction: row;
    align-items: stretch;
    gap: 0.75rem;
    padding: 0.75rem;
  }
}

/*
 * Side panels: outer shell clips; INNER scrolls.
 * (Never put overflow:hidden and overflow-y:auto on the same node —
 *  later rules win and kill scroll entirely.)
 */
.side {
  flex-shrink: 0;
  min-height: 0;
  max-height: 34vh;
  width: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden; /* shell only */
}

/* The actual scrollport — wheel / trackpad / touch, no bar chrome */
.side-inner {
  flex: 1 1 auto;
  min-height: 0;
  max-height: 100%;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 0 2px 0.5rem 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none; /* Firefox */
  -ms-overflow-style: none; /* legacy Edge */
}
.side-inner::-webkit-scrollbar {
  display: none;
  width: 0;
  height: 0;
  background: transparent;
}

/* Sections inside the scrollport should not shrink away */
.side-inner > section,
.side-inner > .card,
.side-inner > label,
.side-inner > p {
  flex-shrink: 0;
}

@media (min-width: 1024px) {
  .side {
    max-height: none;
    height: 100%;
    align-self: stretch;
  }
  .side-left {
    width: 13rem;
  }
  .side-right {
    width: 15rem;
  }
}

@media (min-width: 1280px) {
  .side-left {
    width: 14rem;
  }
  .side-right {
    width: 16rem;
  }
}

/* Shared “no bar” utility for canvas scroller */
.panel-scroll {
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.panel-scroll::-webkit-scrollbar {
  display: none;
  width: 0;
  height: 0;
  background: transparent;
}

.center {
  order: -1;
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

@media (min-width: 1024px) {
  .center {
    order: 0;
  }
}

.section-title {
  margin: 0 0 0.5rem;
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--fg-subtle);
}

.hint {
  margin: 0;
  font-size: 11px;
  line-height: 1.45;
  color: var(--fg-subtle);
}

.hint-bar {
  flex-shrink: 0;
  margin: 0;
  text-align: center;
  font-size: 10px;
  color: var(--fg-subtle);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

@media (min-width: 640px) {
  .hint-bar {
    font-size: 12px;
  }
}

/* —— Buttons —— */
.btn {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-height: 2.75rem;
  padding: 0 0.75rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--fg-muted);
  font-family: inherit;
  font-size: 0.875rem;
  transition: color var(--motion), border-color var(--motion), background var(--motion);
}

.btn:hover:not(:disabled) {
  border-color: var(--border-strong);
  color: var(--fg);
}

.btn.active {
  border-color: var(--border-strong);
  background: var(--fg);
  color: var(--bg);
}

.btn.primary {
  border-color: var(--border-strong);
  background: var(--fg);
  color: var(--bg);
  font-weight: 500;
}

.btn.primary:hover:not(:disabled) {
  opacity: 0.9;
  color: var(--bg);
}

.btn.ghost {
  background: var(--bg-subtle);
}

.btn.danger:hover:not(:disabled) {
  border-color: color-mix(in oklab, var(--danger) 40%, transparent);
  color: var(--danger);
}

.btn.dashed {
  border-style: dashed;
  border-color: var(--border-strong);
  background: var(--bg-subtle);
  justify-content: center;
}

.btn .kbd {
  margin-left: auto;
  font-family: var(--mono);
  font-size: 10px;
  opacity: 0.6;
}

.tool-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.375rem;
}

@media (min-width: 640px) {
  .tool-grid {
    grid-template-columns: 1fr;
  }
}

.brush-row {
  display: flex;
  gap: 0.375rem;
}

.brush-btn {
  flex: 1;
  min-height: 3rem;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--fg-muted);
  font: inherit;
  padding: 0.35rem 0.25rem;
}

.brush-btn.active {
  border-color: var(--border-strong);
  background: var(--fg);
  color: var(--bg);
}

.brush-preview {
  display: grid;
  place-items: center;
  width: 1.5rem;
  height: 1.5rem;
}

.brush-preview-2 {
  grid-template-columns: 6px 6px;
  gap: 1px;
  place-items: center;
  place-content: center;
}

.brush-preview-3 {
  grid-template-columns: 6px 6px 6px;
  gap: 1px;
  place-items: center;
  place-content: center;
}

.brush-dot {
  width: 6px;
  height: 6px;
  border-radius: 1px;
  background: currentColor;
  opacity: 0.9;
}

.brush-label {
  font-family: var(--mono);
  font-size: 10px;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.canvas-scroll {
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.canvas-scroll::-webkit-scrollbar {
  display: none;
  width: 0;
  height: 0;
}

/* Linked image toggles */
.linked-head {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}
.linked-head img {
  width: 2.5rem;
  height: 2.5rem;
  object-fit: cover;
  border-radius: var(--radius-xs);
  border: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}
.toggle-stack {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}
.toggle-row {
  display: flex;
  width: 100%;
  align-items: flex-start;
  gap: 0.75rem;
  text-align: left;
  padding: 0.5rem 0.65rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: inherit;
  font: inherit;
}
.toggle-row:hover {
  border-color: var(--border-strong);
}
.toggle-row[aria-pressed="true"] {
  border-color: var(--border-strong);
  background: var(--bg-subtle);
}
.toggle-switch {
  position: relative;
  flex-shrink: 0;
  width: 2.25rem;
  height: 1.25rem;
  margin-top: 0.1rem;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--bg);
}
.toggle-switch.on {
  border-color: var(--fg);
  background: var(--fg);
}
.toggle-knob {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 0.875rem;
  height: 0.875rem;
  border-radius: 999px;
  background: var(--fg-muted);
  transition: left 120ms;
}
.toggle-switch.on .toggle-knob {
  left: 1.1rem;
  background: var(--bg);
}
.toggle-label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--fg);
}
.toggle-desc {
  display: block;
  margin-top: 0.15rem;
  font-size: 11px;
  line-height: 1.35;
  color: var(--fg-subtle);
}

.grid-presets {
  display: flex;
  flex-wrap: wrap;
  gap: 0.375rem;
}

.chip {
  min-height: 2.25rem;
  min-width: 2.75rem;
  padding: 0 0.5rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--fg-muted);
  font-family: var(--mono);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.chip.active {
  border-color: var(--border-strong);
  background: var(--fg);
  color: var(--bg);
}

.edit-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.375rem;
}

.color-chip {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.5rem;
  padding: 0.375rem 0.5rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
}

.color-chip .sw {
  width: 1.25rem;
  height: 1.25rem;
  border-radius: var(--radius-xs);
  border: 1px solid var(--border-strong);
  flex-shrink: 0;
}

/* Palette */
.swatches {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.375rem;
}

.swatch {
  aspect-ratio: 1;
  min-height: 2.25rem;
  border-radius: var(--radius-xs);
  border: 2px solid transparent;
  padding: 0;
  background: transparent;
  transition: transform 80ms, border-color 80ms;
}

.swatch:hover {
  border-color: var(--border-strong);
}

.swatch.active {
  border-color: var(--fg);
  transform: scale(1.05);
  box-shadow: 0 2px 8px rgb(0 0 0 / 0.35);
}

/* Canvas */
.canvas-wrap {
  flex: 1;
  min-height: 0;
  position: relative;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  overflow: hidden;
  touch-action: none;
}

.canvas-scroll {
  width: 100%;
  height: 100%;
  overflow: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    linear-gradient(45deg, #1a1a1e 25%, transparent 25%),
    linear-gradient(-45deg, #1a1a1e 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #1a1a1e 75%),
    linear-gradient(-45deg, transparent 75%, #1a1a1e 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0;
  background-color: #121214;
}

#pixel-canvas {
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  flex-shrink: 0;
  box-shadow: 0 0 0 1px var(--border);
}

.zoom-bar {
  position: absolute;
  right: 0.5rem;
  bottom: 0.5rem;
  display: flex;
  gap: 0.25rem;
  background: color-mix(in oklab, var(--bg) 88%, transparent);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.2rem;
  backdrop-filter: blur(6px);
}

.zoom-bar button {
  min-width: 2rem;
  min-height: 2rem;
  border: none;
  border-radius: var(--radius-xs);
  background: transparent;
  color: var(--fg-muted);
  font-size: 1rem;
  line-height: 1;
}

.zoom-bar button:hover {
  color: var(--fg);
  background: var(--bg-subtle);
}

.zoom-bar span {
  min-width: 2.75rem;
  text-align: center;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--fg-subtle);
  line-height: 2rem;
}

/* Export column */
.stack {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
}

.card {
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  padding: 0.75rem;
}

.field label {
  display: block;
  margin-bottom: 0.25rem;
  font-size: 11px;
  color: var(--fg-subtle);
}

.field input,
.field select {
  width: 100%;
  min-height: 2.5rem;
  padding: 0 0.625rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  font-family: inherit;
  font-size: 0.875rem;
  outline: none;
}

.field input:focus,
.field select:focus {
  border-color: var(--border-strong);
}

.field + .field {
  margin-top: 0.5rem;
}

.range-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 0.35rem;
}

.range-row .val {
  font-family: var(--mono);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: var(--fg);
}

input[type="range"] {
  width: 100%;
  height: 0.5rem;
  appearance: none;
  border-radius: 999px;
  background: var(--bg);
  accent-color: var(--fg);
}

.range-ticks {
  display: flex;
  justify-content: space-between;
  margin-top: 0.35rem;
  font-family: var(--mono);
  font-size: 10px;
  color: var(--fg-subtle);
}

.range-ticks button {
  border: none;
  background: none;
  color: inherit;
  font: inherit;
  padding: 0;
}

.range-ticks button:hover {
  color: var(--fg);
}

.status {
  min-height: 1.1rem;
  margin: 0.25rem 0 0;
  font-size: 11px;
  color: var(--fg-subtle);
}
.status.ok {
  color: #7dcea0;
}
.status.err {
  color: var(--danger);
}

/* Footer */
.app-footer {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  padding: 0.35rem 0.75rem;
}

.app-footer-inner {
  max-width: 1600px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  font-size: 10px;
  color: var(--fg-subtle);
  line-height: 1.2;
}

.app-footer a {
  color: var(--fg-muted);
  text-decoration: none;
  font-family: var(--mono);
}
.app-footer a:hover {
  color: var(--fg);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}
