:root {
  --bg: #f4f1ea;
  --surface: #ffffff;
  --surface-2: #ebe6db;
  --ink: #1d1b26;
  --ink-soft: #5e5a6b;
  --line: #d6cfc0;
  --accent: #ff004d;
  --accent-ink: #ffffff;
  --focus: #29adff;
  --checker-a: #e9e5dc;
  --checker-b: #f8f6f1;
  --shadow: 0 2px 0 rgba(29, 27, 38, .12);
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15131c;
    --surface: #1f1c29;
    --surface-2: #2a2637;
    --ink: #f1eee8;
    --ink-soft: #a9a3b8;
    --line: #3a3549;
    --checker-a: #2b2838;
    --checker-b: #34303f;
    --shadow: 0 2px 0 rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #15131c;
  --surface: #1f1c29;
  --surface-2: #2a2637;
  --ink: #f1eee8;
  --ink-soft: #a9a3b8;
  --line: #3a3549;
  --checker-a: #2b2838;
  --checker-b: #34303f;
  --shadow: 0 2px 0 rgba(0, 0, 0, .35);
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}

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

button {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 11px;
  cursor: pointer;
  box-shadow: var(--shadow);
}
button:hover { border-color: var(--ink-soft); }
button:active { transform: translateY(1px); box-shadow: none; }
button:disabled { opacity: .45; cursor: default; transform: none; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

kbd {
  font: 11px ui-monospace, monospace;
  color: var(--ink-soft);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 4px;
}

h2 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--ink-soft);
  margin: 0 0 10px;
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  max-width: 1180px;
  margin: 0 auto;
  padding: 18px 16px 8px;
}
.logo { display: flex; align-items: center; gap: 10px; margin: 0; font-size: 26px; letter-spacing: -.02em; }
.logo-mark {
  width: 26px; height: 26px;
  background:
    linear-gradient(#ff004d, #ff004d) 0 0 / 50% 50% no-repeat,
    linear-gradient(#ffec27, #ffec27) 100% 0 / 50% 50% no-repeat,
    linear-gradient(#29adff, #29adff) 0 100% / 50% 50% no-repeat,
    linear-gradient(#00e436, #00e436) 100% 100% / 50% 50% no-repeat;
  border-radius: 4px;
}
.new-buttons { display: flex; gap: 8px; }

.layout {
  display: grid;
  grid-template-columns: 190px minmax(0, 1fr) 260px;
  gap: 18px;
  max-width: 1180px;
  margin: 0 auto;
  padding: 8px 16px;
  align-items: start;
}

.tools, .panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
}
.tools { display: flex; flex-direction: column; gap: 14px; }
.tool-group { display: flex; flex-direction: column; gap: 6px; }
.tool {
  display: flex; align-items: center; gap: 8px;
  text-align: left; box-shadow: none;
}
.tool span { flex: 1; }
.tool svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; stroke-linecap: round; }
.tool[aria-pressed="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.tool[aria-pressed="true"] kbd { color: var(--bg); border-color: var(--bg); opacity: .7; }

.selection-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.selection-actions button, .history button { padding: 6px 8px; font-size: 13px; }
.history { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.toggle { display: flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; }

.stage { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.canvas-wrap {
  width: 100%;
  max-width: 640px;
  aspect-ratio: 1;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  overflow: hidden;
  box-shadow: 0 6px 0 rgba(29, 27, 38, .10);
}
#canvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  touch-action: none;
  cursor: crosshair;
}
.status { margin: 0; min-height: 1.4em; font-size: 13px; color: var(--ink-soft); font-variant-numeric: tabular-nums; }

.side { display: flex; flex-direction: column; gap: 18px; }
.current { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.swatch-big { width: 44px; height: 44px; border-radius: 8px; border: 2px solid var(--ink); flex: none; }
.custom { display: flex; flex-direction: column; font-size: 13px; color: var(--ink-soft); gap: 4px; }
.custom input { width: 64px; height: 30px; padding: 0; border: 1px solid var(--line); border-radius: 6px; background: none; cursor: pointer; }
.palette { display: grid; grid-template-columns: repeat(8, 1fr); gap: 5px; }
.swatch {
  aspect-ratio: 1;
  padding: 0;
  border-radius: 6px;
  border: 2px solid transparent;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .15);
}
.swatch[aria-checked="true"] { border-color: var(--ink); transform: scale(1.08); }

.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--ink-soft); margin-bottom: 10px; }
.field input {
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--ink);
}
.save-row { display: flex; gap: 8px; flex-wrap: wrap; }
.hint { font-size: 13px; color: var(--ink-soft); margin: 8px 0 0; }

.gallery-section { max-width: 1180px; margin: 18px auto 40px; padding: 0 16px; }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 14px; }
.card {
  display: flex; flex-direction: column; gap: 6px;
  padding: 8px; text-align: left;
  background: var(--surface);
}
.card img {
  width: 100%; aspect-ratio: 1;
  image-rendering: pixelated;
  border-radius: 6px;
  background: repeating-conic-gradient(var(--checker-a) 0 25%, var(--checker-b) 0 50%) 0 0 / 16px 16px;
}
.card .t { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .a { font-size: 12px; color: var(--ink-soft); }
.card.mine .a::after { content: " · yours"; color: var(--accent); }

@media (max-width: 980px) {
  .layout { grid-template-columns: 1fr; }
  .stage { order: -1; }
  .tools { flex-direction: row; flex-wrap: wrap; }
  .tool-group { flex-direction: row; flex-wrap: wrap; }
  .tool kbd, .tool span { display: none; }
  .tool { padding: 9px; }
  .side { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
}
