:root {
  --bg: #ffffff;
  --page: #f6f6f4;
  --fg: #18181b;
  --muted: #71717a;
  --border: #e4e4e7;
  --subtle: #f4f4f5;
  --accent: #18181b;
  --accent-fg: #ffffff;
  --check-a: #ffffff;
  --check-b: #ececee;
  --gridline: rgba(0, 0, 0, 0.12);
  --guide: rgba(0, 0, 0, 0.05);
  --danger: #dc2626;
  --template: #2563eb;
  --ok: #16a34a;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111113;
    --page: #09090b;
    --fg: #f4f4f5;
    --muted: #a1a1aa;
    --border: #27272a;
    --subtle: #1c1c1f;
    --accent: #f4f4f5;
    --accent-fg: #09090b;
    --check-a: #1a1a1d;
    --check-b: #232327;
    --gridline: rgba(255, 255, 255, 0.1);
    --guide: rgba(255, 255, 255, 0.04);
    --danger: #f87171;
    --template: #60a5fa;
    --ok: #4ade80;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font: 14px/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
}
.mono, .coords { font-family: ui-monospace, "Geist Mono", SFMono-Regular, Menlo, monospace; }
.muted { color: var(--muted); }
.small { font-size: 12px; margin: 0; }
.grow { flex: 1; }

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 24px; border-bottom: 1px solid var(--border);
}
.topbar h1 { margin: 0; font-size: 15px; font-weight: 600; }
.topbar-actions { display: flex; gap: 8px; }

/* Layout */
.layout {
  display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 40px;
  max-width: 1120px; margin: 0 auto; padding: 24px;
}
@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); gap: 24px; padding: 16px; }
}
.sidebar { display: flex; flex-direction: column; gap: 24px; min-width: 0; }
.panel { display: flex; flex-direction: column; gap: 8px; }
.label { font-size: 13px; font-weight: 500; }
.label-row { display: flex; justify-content: space-between; align-items: baseline; }
.row { display: flex; gap: 8px; align-items: flex-start; }

/* Controls */
.btn, .btn-primary, .btn-icon {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--bg); color: var(--fg); font: inherit; font-size: 13px; cursor: pointer;
  white-space: nowrap; transition: background .12s;
}
.btn:hover, .btn-icon:hover { background: var(--subtle); }
.btn-primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.btn-primary:hover { opacity: .9; background: var(--accent); }
.btn-icon { width: 32px; height: 32px; padding: 0; border-color: transparent; }
button:disabled { opacity: .4; cursor: default; }
button:disabled:hover { background: transparent; }
.btn-primary:disabled:hover { background: var(--accent); }
.input {
  height: 34px; padding: 0 10px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--bg); color: var(--fg); font: inherit; width: 100%;
}
.input:focus { outline: 2px solid var(--fg); outline-offset: -1px; }

/* Tools and actions: a column on the left of the grid (a row on phones) */
.canvas-col { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 12px; align-items: start; }
.board-col { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.toolbar {
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 4px;
  border: 1px solid var(--border); border-radius: 10px; position: sticky; top: 12px;
}
.toolbar .divider { width: 20px; height: 1px; margin: 4px 0; }
.segmented.vertical { flex-direction: column; }
.toolbar .upload { cursor: pointer; }
.board-options { display: flex; align-items: center; gap: 4px; max-width: 640px; width: 100%; margin: 0 auto; }
@media (max-width: 640px) {
  .canvas-col { grid-template-columns: minmax(0, 1fr); }
  .toolbar { flex-direction: row; flex-wrap: wrap; justify-content: center; position: static; }
  .toolbar .divider { width: 1px; height: 20px; margin: 0 4px; }
  .segmented.vertical { flex-direction: row; }
}
/* Image import panel */
.import-panel { display: flex; flex-direction: column; gap: 10px; padding: 12px; border: 1px solid var(--fg); border-radius: 10px; max-width: 640px; width: 100%; margin: 0 auto; box-sizing: border-box; }
.import-panel[hidden] { display: none; }
.import-panel p { margin: 0; }
.import-head { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.import-size { display: grid; grid-template-columns: auto minmax(0, 1fr) 56px; align-items: center; gap: 10px; }
.import-size input { width: 100%; }
.import-presets { display: flex; gap: 6px; flex-wrap: wrap; }
.import-presets .btn { height: 28px; font-size: 12px; }
.import-presets .btn[aria-pressed="true"] { border-color: var(--fg); }
.divider { width: 1px; height: 20px; background: var(--border); margin: 0 4px; }
.segmented { display: inline-flex; gap: 2px; padding: 2px; border-radius: 8px; background: var(--subtle); }
.segmented button {
  height: 28px; min-width: 32px; padding: 0 8px; border: 0; border-radius: 6px; background: transparent;
  color: var(--muted); font: inherit; font-size: 13px; cursor: pointer; display: inline-flex; align-items: center; justify-content: center;
}
.segmented button[aria-pressed="true"] { background: var(--bg); color: var(--fg); box-shadow: 0 1px 2px rgba(0,0,0,.12); }
.toggle { display: inline-flex; align-items: center; gap: 5px; font-size: 13px; color: var(--muted); padding: 0 6px; cursor: pointer; user-select: none; }
.coords { margin-left: auto; padding-right: 8px; font-size: 12px; color: var(--muted); }

/* Board */
.board-wrap { width: 100%; max-width: 640px; margin: 0 auto; }
.board.importing { cursor: move; }
.board {
  display: block; width: 100%; aspect-ratio: 1; border: 1px solid var(--border); border-radius: 10px;
  cursor: crosshair; touch-action: none; user-select: none; image-rendering: pixelated;
}
.board[data-tool="picker"] { cursor: copy; }
.board[data-tool="fill"] { cursor: cell; }
.hint { margin: 0; text-align: center; font-size: 12px; color: var(--muted); }

/* Colors */
.current-color { display: flex; gap: 12px; align-items: stretch; padding: 10px; border: 1px solid var(--border); border-radius: 10px; }
.swatch-big { flex: none; width: 52px; height: 52px; border-radius: 8px; border: 1px solid rgba(0,0,0,.12); }
.current-meta { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.current-meta .mono { font-weight: 500; }
.used { display: flex; flex-wrap: wrap; gap: 3px; margin-top: 2px; min-height: 16px; }
.used button { width: 16px; height: 16px; }
/* Palette: one block per colour family, every swatch on the same 16-column grid. */
.palette { display: flex; flex-direction: column; gap: 6px; }
.palette-group { display: grid; grid-template-columns: repeat(16, 1fr); gap: 2px; }
.palette-group button { width: 100%; aspect-ratio: 1; height: auto; border-radius: 3px; }
.used button, .palette-group button { padding: 0; border: 0; box-shadow: inset 0 0 0 1px rgba(0,0,0,.12); cursor: pointer; }
.used button:hover, .palette-group button:hover { transform: scale(1.25); position: relative; z-index: 1; }
.used button[aria-pressed="true"], .palette-group button[aria-pressed="true"] { outline: 2px solid var(--fg); outline-offset: 1px; position: relative; z-index: 2; }

/* Preview */
.preview {
  display: flex; align-items: flex-end; justify-content: center; gap: 20px; padding: 16px;
  border: 1px solid var(--border); border-radius: 10px;
  background: conic-gradient(var(--check-b) 25%, var(--check-a) 0 50%, var(--check-b) 0 75%, var(--check-a) 0) 0 0 / 12px 12px;
}
.preview canvas { image-rendering: pixelated; }

/* Status */
#status.ok { color: var(--ok); }
#status.err { color: var(--danger); }

/* Browser dialog */
.browser {
  width: min(960px, calc(100vw - 32px)); height: min(720px, calc(100vh - 32px));
  padding: 0; border: 1px solid var(--border); border-radius: 14px; background: var(--bg); color: var(--fg);
  flex-direction: column;
}
.browser[open] { display: flex; }
.browser::backdrop { background: rgba(0, 0, 0, .4); }
.browser-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 6px; }
.browser-head h2 { margin: 0; font-size: 15px; font-weight: 600; }
.browser-bar { display: flex; flex-wrap: wrap; gap: 8px; padding: 8px 16px 12px; border-bottom: 1px solid var(--border); align-items: center; }
.browser-bar .input { flex: 1; min-width: 160px; width: auto; }
.items {
  flex: 1; overflow-y: auto; padding: 16px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 8px; align-content: start;
}
.item {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 10px 6px 8px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--bg); color: var(--fg); cursor: pointer; font: inherit;
}
.item:hover { background: var(--subtle); }
.item img {
  width: 64px; height: 64px; image-rendering: pixelated; border-radius: 4px;
  background: conic-gradient(var(--check-b) 25%, var(--check-a) 0 50%, var(--check-b) 0 75%, var(--check-a) 0) 0 0 / 8px 8px;
}
.item span { font-size: 11px; color: var(--muted); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .del {
  position: absolute; top: 4px; right: 4px; width: 22px; height: 22px; border-radius: 6px; border: 0;
  background: transparent; color: var(--muted); display: none; align-items: center; justify-content: center; cursor: pointer;
}
.item:hover .del { display: inline-flex; }
.item .del:hover { color: var(--danger); background: var(--subtle); }
.empty { grid-column: 1 / -1; text-align: center; color: var(--muted); padding: 40px 0; }
.browser-foot { padding: 8px 16px 12px; border-top: 1px solid var(--border); }

/* Categories & layers */
.segmented.full { display: flex; }
.segmented.full button { flex: 1; }
.layers { display: flex; flex-direction: column; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.layer { display: flex; align-items: center; gap: 10px; padding: 6px 6px 6px 8px; border-top: 1px solid var(--border); min-height: 48px; }
.layer:first-child { border-top: 0; }
.layer.editing { background: var(--subtle); }
.layer img {
  flex: none; width: 36px; height: 36px; image-rendering: pixelated; border-radius: 4px; border: 1px solid var(--border);
  background: conic-gradient(var(--check-b) 25%, var(--check-a) 0 50%, var(--check-b) 0 75%, var(--check-a) 0) 0 0 / 6px 6px;
}
.layer-text { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.25; }
.layer-text b { font-size: 12px; font-weight: 500; }
.layer-text span { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layer .btn { height: 28px; padding: 0 8px; font-size: 12px; }
.layer .btn-icon { width: 28px; height: 28px; }
#preview-composed { margin-left: 4px; padding-left: 20px; border-left: 1px solid var(--border); box-sizing: content-box; }
.section-title { grid-column: 1 / -1; margin: 8px 0 0; font-size: 12px; font-weight: 500; color: var(--muted); }
.section-title:first-child { margin-top: 0; }
.item.new { border-style: dashed; justify-content: center; color: var(--muted); }
.item.new .plus { width: 64px; height: 64px; display: flex; align-items: center; justify-content: center; font-size: 28px; font-weight: 300; }

/* Layer rows with a load dropdown */
.layer { cursor: pointer; }
.layer:hover:not(.editing) { background: var(--subtle); }
.layer.editing { box-shadow: inset 3px 0 0 var(--fg); }
.layer .select {
  flex: none; width: 140px; height: 30px; padding: 0 6px; border-radius: 7px; border: 1px solid var(--border);
  background: var(--bg); color: var(--fg); font: inherit; font-size: 12px; cursor: pointer;
}
.layer-text .tag { color: var(--fg); font-weight: 500; }

/* Layer rows: title line with opacity + dice, item dropdown below */
.layer { align-items: flex-start; padding: 8px; }
.layer img { margin-top: 2px; }
.layer-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.layer-head { display: flex; align-items: center; gap: 6px; min-height: 28px; }
.layer-head b { font-size: 12px; font-weight: 500; white-space: nowrap; }
.layer-sub { flex: 1; min-width: 0; font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layer .select { width: 100%; }
.layer .eye { color: var(--muted); }
.layer .eye:hover { color: var(--fg); }
.layer.masked .eye { color: var(--fg); background: var(--subtle); }
.layer.masked .layer-head b, .layer.masked .layer-sub { opacity: .5; }
.btn-small { height: 28px; padding: 0 10px; font-size: 12px; }

/* Edit panels only show while an item is being created/edited */
body:not(.editing) .edit-only { display: none; }
body.editing .view-only { display: none; }
#edit-hint { padding: 12px; border: 1px dashed var(--border); border-radius: 10px; }
#edit-hint p { margin: 0; }

/* Dropdown + Éditer / Effacer */
.layer-select-row { display: flex; gap: 4px; }
/* Fond: Nouns background behind the layers (board only) */
.layer-select-row .select { flex: 1; min-width: 0; width: auto; }
.layer .btn-icon.framed { width: 30px; height: 30px; border-color: var(--border); }
.layer .btn-icon.danger:not(:disabled):hover { color: var(--danger); }

/* Searchable item picker (replaces the native dropdowns) */
.combo-btn {
  flex: 1; min-width: 0; height: 30px; display: flex; align-items: center; gap: 4px; padding: 0 6px 0 8px;
  border: 1px solid var(--border); border-radius: 7px; background: var(--bg); color: var(--fg);
  font: inherit; font-size: 12px; cursor: pointer; text-align: left;
}
.combo-btn:hover { background: var(--subtle); }
.combo-btn span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.combo-btn .chevron { flex: none; font-size: 12px; color: var(--muted); }
.picker {
  position: fixed; z-index: 50; display: flex; flex-direction: column; overflow: hidden;
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 12px 32px rgba(0,0,0,.18), 0 2px 6px rgba(0,0,0,.08);
}
.picker[hidden] { display: none; }
.picker-search {
  flex: none; height: 38px; margin: 0; padding: 0 12px; border: 0; border-bottom: 1px solid var(--border);
  background: var(--bg); color: var(--fg); font: inherit; font-size: 13px; outline: none;
}
.picker-list { flex: 1; overflow-y: auto; padding: 4px; }
.picker-group {
  position: sticky; top: -4px; z-index: 1; margin: 4px -4px 2px; padding: 6px 12px 4px;
  background: var(--bg); font-size: 11px; font-weight: 500; color: var(--muted);
}
.picker-item { display: flex; align-items: center; gap: 8px; padding: 3px 8px; border-radius: 6px; cursor: pointer; font-size: 12px; }
.picker-item.active { background: var(--subtle); }
.picker-item img, .picker-icon {
  flex: none; width: 28px; height: 28px; border-radius: 4px; image-rendering: pixelated;
  background: conic-gradient(var(--check-b) 25%, var(--check-a) 0 50%, var(--check-b) 0 75%, var(--check-a) 0) 0 0 / 7px 7px;
}
.picker-icon { display: flex; align-items: center; justify-content: center; background: var(--subtle); color: var(--muted); font-size: 15px; }
.picker-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.picker-check { color: var(--fg); font-size: 12px; }
.picker-empty { padding: 16px; text-align: center; color: var(--muted); font-size: 12px; }

/* Status & rarity in the edit panel */
.topbar { gap: 16px; }
.topbar .topnav { margin-right: auto; }
.meta-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.meta-field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.meta-field .sel { width: 100%; }
.meta-field .sel select { width: 100%; }
.meta-row.disabled, .meta-title.disabled { opacity: .55; }
.meta-title { margin-bottom: 8px; }
.meta-title input { height: 34px; padding: 0 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); color: var(--fg); font: inherit; font-size: 14px; }
.picker-label small { display: block; font-family: ui-monospace, Menlo, monospace; font-size: 10.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; }

/* Name field: the category prefix is part of the filename, shown but not editable */
.name-input { display: flex; align-items: center; height: 34px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); overflow: hidden; }
.name-input:focus-within { outline: 2px solid var(--fg); outline-offset: -1px; }
.name-input #name-prefix { padding: 0 0 0 10px; color: var(--muted); font-size: 14px; white-space: nowrap; user-select: none; }
.name-input input { flex: 1; min-width: 0; height: 100%; border: 0; padding: 0 10px 0 0; background: transparent; color: var(--fg); font: inherit; font-size: 14px; outline: none; }

/* Read-only (deployed site): editing happens locally */
.ro-badge { display: inline-block; margin-left: 8px; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 500; vertical-align: 2px; color: var(--muted); border: 1px solid var(--border); }
body.readonly #btn-save,
body.readonly .layer .btn-icon.framed,
body.readonly .request,
body.readonly .menu-item.danger,
body.readonly #file-input,
body.readonly .item .del { display: none !important; }
body.readonly .sel select, body.readonly #meta-status, body.readonly #meta-rarity { pointer-events: none; opacity: .7; }

/* Icons: Pixicons kit (pixiconsio), 1em squares in the current text colour */
.px { flex: none; }
.btn-icon .px, .segmented button .px, .btn .px { font-size: 16px; }
.layer .btn-icon .px { font-size: 14px; }
.item .del .px { font-size: 13px; }
.picker-icon .px { font-size: 14px; }
.item.new .plus .px { font-size: 26px; }
.px.mirror { transform: scaleX(-1); }
