/* Pixel Art Editor: dark theme, mobile first (one column), editor grid from 900px. */

:root {
  color-scheme: dark;
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #27364d;
  --surface-3: #33445f;
  --stage: #0b1221;
  --border: rgba(148, 163, 184, 0.16);
  --border-strong: rgba(148, 163, 184, 0.32);
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #fb923c;
  --accent-hover: #fdba74;
  --accent-soft: rgba(251, 146, 60, 0.16);
  --accent-ink: #1c0f04;
  --ok: #4ade80;
  --warn: #fbbf24;
  --danger: #f87171;
  --radius: 12px;
  --radius-small: 8px;
  --shadow: 0 12px 32px rgba(2, 6, 23, 0.45);
  --shadow-small: 0 2px 10px rgba(2, 6, 23, 0.35);
  --speed: 180ms;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

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

[hidden] {
  display: none !important;
}

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

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(900px 480px at 80% -10%, rgba(251, 146, 60, 0.08), transparent 70%),
    var(--bg);
  color: var(--text);
  font: 15px/1.45 var(--font);
}

button,
select,
input {
  font: inherit;
  color: inherit;
}

h1,
h2,
p,
figure {
  margin: 0;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.sprite {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
}

.icon {
  width: 20px;
  height: 20px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-small {
  width: 16px;
  height: 16px;
}

/* Header */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  background: rgba(15, 23, 42, 0.72);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  width: 28px;
  height: 28px;
  flex: none;
  filter: drop-shadow(0 2px 8px rgba(251, 146, 60, 0.35));
}

.brand h1 {
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: none;
}

/* Buttons */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: 40px;
  min-height: 40px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  line-height: 1.2;
  cursor: pointer;
  transition: background-color var(--speed), border-color var(--speed), color var(--speed), transform var(--speed), box-shadow var(--speed);
}

.btn:hover {
  background: var(--surface-2);
  border-color: var(--border-strong);
}

.btn:active {
  transform: translateY(1px);
}

.btn-lang {
  min-width: 48px;
  padding: 0 10px;
  font-size: 0.85rem;
  letter-spacing: 0.06em;
}

.btn-small {
  min-height: 40px;
  padding: 0 12px;
  font-size: 0.85rem;
}

.btn-primary {
  border-color: transparent;
  background: var(--accent);
  color: var(--accent-ink);
  box-shadow: 0 6px 18px rgba(251, 146, 60, 0.25);
}

.btn-primary:hover {
  border-color: transparent;
  background: var(--accent-hover);
}

.btn-block {
  width: 100%;
  min-height: 44px;
}

.btn-meta {
  margin-left: auto;
  font-size: 0.8rem;
  font-weight: 600;
  opacity: 0.72;
  font-variant-numeric: tabular-nums;
}

/* Editor layout: one column on phones */

.editor {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  padding: 12px 16px 8px;
}

.toolbar,
.stage-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 3px;
}

.tool-group {
  display: flex;
  gap: 3px;
}

.tool {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 40px;
  height: 44px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
  transition: background-color var(--speed), border-color var(--speed), color var(--speed), box-shadow var(--speed), transform var(--speed);
}

.tool:hover:not(:disabled) {
  background: var(--surface-2);
  color: var(--text);
}

.tool:active:not(:disabled) {
  transform: translateY(1px);
}

.tool[data-tool][aria-pressed="true"] {
  border-color: transparent;
  background: var(--accent);
  color: var(--accent-ink);
  box-shadow: 0 4px 14px rgba(251, 146, 60, 0.35);
}

.toggle[aria-pressed="true"] {
  border-color: rgba(251, 146, 60, 0.55);
  background: var(--accent-soft);
  color: var(--accent);
}

.tool:disabled {
  opacity: 0.38;
  cursor: default;
}

.tool-wide {
  width: 72px;
  font-size: 0.8rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.zoom-label {
  min-width: 2.5em;
  text-align: left;
}

/* Stage (the play area) */

.workspace {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}

.stage {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  max-height: 72vh;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background-color: var(--stage);
  background-image: radial-gradient(circle at 1px 1px, rgba(148, 163, 184, 0.13) 1px, transparent 0);
  background-size: 18px 18px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.03), var(--shadow);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.stage canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
}

.stage canvas.is-pan-ready {
  cursor: grab;
}

.stage canvas.is-panning {
  cursor: grabbing;
}

.toast {
  position: absolute;
  left: 50%;
  bottom: 12px;
  max-width: calc(100% - 24px);
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.94);
  box-shadow: var(--shadow-small);
  color: var(--text);
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
  opacity: 0;
  transform: translate(-50%, 8px);
  transition: opacity 200ms ease, transform 200ms ease;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.status-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 20px;
  padding: 0 2px;
  color: var(--muted);
  font-size: 0.78rem;
}

.coords {
  flex: none;
  font-variant-numeric: tabular-nums;
}

.hint {
  flex: 1;
  min-width: 0;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.save-status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex: none;
}

.save-status::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--ok);
  box-shadow: 0 0 0 3px rgba(74, 222, 128, 0.15);
  transition: background-color var(--speed), box-shadow var(--speed);
}

.save-status[data-state="pending"]::before {
  background: var(--warn);
  box-shadow: 0 0 0 3px rgba(251, 191, 36, 0.15);
}

.save-status[data-state="failed"]::before {
  background: var(--danger);
  box-shadow: 0 0 0 3px rgba(248, 113, 113, 0.15);
}

.save-status[data-state="demo"]::before {
  background: var(--muted);
  box-shadow: 0 0 0 3px rgba(148, 163, 184, 0.15);
}

/* Side panels */

.sidebar {
  display: grid;
  gap: 12px;
  align-content: start;
  min-width: 0;
}

.panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-small);
}

.panel-title {
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-width: 0;
}

.panel-head-sub {
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.current-color {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.current-swatch {
  width: 40px;
  height: 40px;
  flex: none;
  border-radius: 10px;
  background: var(--swatch, transparent);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.16), 0 4px 12px rgba(2, 6, 23, 0.4);
  transition: background-color var(--speed);
}

.current-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.current-hex {
  font-family: var(--mono);
  font-size: 0.9rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

/* Eight columns while every swatch stays at least 40px wide, fewer on very narrow screens.
   The clear ring around each chip may reach into the panel padding. */
.swatches {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(max(40px, 12.4%), 1fr));
  margin-inline: -4px;
}

.swatch {
  position: relative;
  aspect-ratio: 1 / 1;
  min-width: 0;
  padding: 0;
  border: 0;
  border-radius: 10px;
  background: transparent;
  cursor: pointer;
}

.swatch::before {
  content: "";
  position: absolute;
  inset: 3px;
  border-radius: 8px;
  background: var(--swatch);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.14), 0 1px 3px rgba(2, 6, 23, 0.5);
  transition: transform var(--speed), box-shadow var(--speed);
}

.swatch:hover::before {
  transform: scale(1.07);
}

.swatch[aria-pressed="true"]::before {
  box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px #f8fafc;
}

.add-color {
  position: relative;
  overflow: hidden;
}

.add-color:focus-within {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.add-color input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  padding: 0;
  border: 0;
  opacity: 0;
  cursor: pointer;
}

.empty-note {
  color: var(--muted);
  font-size: 0.82rem;
}

.previews {
  display: flex;
  align-items: flex-end;
  gap: 20px;
  min-height: 64px;
}

.preview-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 700;
}

.preview {
  display: block;
  border-radius: 3px;
  background: repeating-conic-gradient(#dfe5ee 0% 25%, #f1f5f9 0% 50%) 0 0 / 8px 8px;
  box-shadow: 0 0 0 1px var(--border-strong), 0 6px 16px rgba(2, 6, 23, 0.45);
  image-rendering: crisp-edges;
  image-rendering: pixelated;
}

.fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 600;
}

.select {
  width: 100%;
  min-width: 0;
  min-height: 40px;
  padding: 0 32px 0 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5l5 5 5-5' fill='none' stroke='%2394a3b8' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") no-repeat right 12px center / 11px 7px;
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  transition: border-color var(--speed), background-color var(--speed);
}

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

.select option {
  background: var(--surface);
  color: var(--text);
}

.color-panel .select {
  width: auto;
  max-width: 50%;
}

/* Footer */

.footer {
  display: flex;
  justify-content: center;
  padding: 2px 16px 8px;
}

.footer a {
  display: inline-block;
  padding: 10px 12px;
  line-height: 20px;
  border-radius: var(--radius-small);
  color: var(--muted);
  font-size: 0.85rem;
  text-decoration: none;
  transition: color var(--speed);
}

.footer a:hover {
  color: var(--accent);
}

/* Phones: the hint gets its own line below the size and the save status. */
@media (max-width: 599px) {
  .status-bar {
    flex-wrap: wrap;
    row-gap: 2px;
  }

  .hint {
    order: 1;
    flex-basis: 100%;
    text-align: left;
  }

  .save-status {
    margin-left: auto;
  }
}

/* Small phones: the clear button shows only its icon, and the color panel
   makes room for eight 40px swatches down to 360px wide screens. */
@media (max-width: 419px) {
  .btn-text {
    display: none;
  }

  #clear-btn {
    padding: 0;
  }

  .color-panel {
    padding-inline: 6px;
  }
}

/* Below 380px the tool rows shrink their buttons to fit the screen; a row that
   still does not fit (a window with a classic scrollbar) wraps instead. */
@media (max-width: 419px) {
  .editor {
    --tool: min(40px, calc((100vw - 54px) / 8));
  }

  .toolbar,
  .stage-bar {
    flex-wrap: wrap;
    row-gap: 6px;
  }

  .tool {
    width: var(--tool);
  }

  .tool-wide {
    width: calc(var(--tool) * 1.8);
  }
}

@media (max-width: 359px) {
  .brand h1 {
    font-size: 0.9rem;
  }
}

@media (min-width: 420px) {
  .tool {
    width: 44px;
  }

  .tool-group {
    gap: 4px;
  }

  .tool-wide {
    width: 76px;
  }
}

@media (min-width: 600px) and (max-width: 899px) {
  .sidebar {
    grid-template-columns: 1fr 1fr;
  }

  .color-panel {
    grid-column: 1 / -1;
  }
}

/* Desktop: toolbar | stage | side panels, filling the window. */
@media (min-width: 900px) {
  body {
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  .editor {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) 344px;
    gap: 16px;
    max-width: none;
    min-height: 0;
    padding: 16px 16px 4px;
  }

  .toolbar {
    flex-direction: column;
    justify-content: flex-start;
    align-self: start;
    gap: 6px;
    padding: 6px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow-small);
  }

  .toolbar .tool-group {
    flex-direction: column;
  }

  .toolbar .tool-group + .tool-group {
    padding-top: 6px;
    border-top: 1px solid var(--border);
  }

  .toolbar .tool {
    border-color: transparent;
    background: transparent;
  }

  .toolbar .tool:hover:not(:disabled) {
    background: var(--surface-2);
  }

  .toolbar .tool[data-tool][aria-pressed="true"] {
    background: var(--accent);
  }

  .workspace {
    min-height: 0;
  }

  .stage {
    flex: 1 1 auto;
    min-height: 0;
    max-height: none;
    aspect-ratio: auto;
  }

  .sidebar {
    min-height: 0;
    overflow-y: auto;
    padding-bottom: 4px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}
