:root {
  --bg: #F3F2EF;
  --panel: #FBFAF8;
  --ink: #1D1C1A;
  --muted: #6C6860;
  --faint: #A29D93;
  --line: rgba(29, 28, 26, 0.09);
  --line-strong: rgba(29, 28, 26, 0.16);
  --field: #FFFFFF;
  --accent: #1F5C78;
  --accent-ink: #FFFFFF;
  --ok: #2E6B45;
  --warn: #8A5A12;
  --err: #9E3239;
  --hl: #FFF4D6;
  --r: 10px;
  --r-sm: 7px;
  --shadow: 0 0 0 1px var(--line), 0 1px 2px rgba(20, 18, 14, 0.05);
  --shadow-lift: 0 0 0 1px var(--line), 0 12px 32px -12px rgba(20, 18, 14, 0.25);
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 Inter, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
h2 { margin: 0; font-size: 14px; font-weight: 600; }

/* ---------- top bar ---------- */
.bar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px;
  height: 58px; padding: 0 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 9px; font-weight: 600; letter-spacing: -0.01em; white-space: nowrap; }
.mark {
  width: 22px; height: 22px; border-radius: 6px;
  background: conic-gradient(from 200deg, #2F8193, #58A7AB, #F2AE60, #CC5535, #2F8193);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.formats { display: flex; gap: 4px; margin: 0 auto; padding: 3px; background: rgba(29, 28, 26, 0.05); border-radius: 10px; }
.formats button {
  display: flex; align-items: center; gap: 7px;
  height: 32px; padding: 0 12px; border: 0; border-radius: 8px; white-space: nowrap; flex: none;
  background: transparent; color: var(--muted); font-weight: 500; cursor: pointer;
  transition: background-color 0.15s var(--ease), color 0.15s var(--ease), transform 0.12s var(--ease);
}
.formats button[aria-pressed="true"] { background: var(--field); color: var(--ink); box-shadow: var(--shadow); }
.ratio { display: inline-block; border: 1.5px solid currentColor; border-radius: 2px; }
.r4x5 { width: 10px; height: 12.5px; } .r1x1 { width: 11px; height: 11px; }
.r9x16 { width: 8px; height: 14px; } .r16x9 { width: 15px; height: 8.5px; }
.actions { display: flex; align-items: center; gap: 6px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 13px; border: 0; border-radius: 8px;
  background: var(--field); box-shadow: var(--shadow); font-weight: 500; cursor: pointer; white-space: nowrap;
  transition: background-color 0.15s var(--ease), box-shadow 0.15s var(--ease), transform 0.12s var(--ease);
}
.btn:active, .formats button:active, .swatch:active { transform: scale(0.98); }
.btn.ghost { background: transparent; box-shadow: none; color: var(--ink); }
.btn.primary { background: var(--accent); color: var(--accent-ink); box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.12); }
.btn.small { height: 30px; padding: 0 11px; font-size: 13px; }
.btn.icon { width: 34px; padding: 0; font-size: 16px; }
.btn.wide { width: 100%; }
.btn.scale { padding-right: 8px; appearance: auto; }
@media (hover: hover) and (pointer: fine) {
  .btn.ghost:hover, .formats button:hover:not([aria-pressed="true"]) { background: rgba(29, 28, 26, 0.06); }
  .btn.primary:hover { background: color-mix(in srgb, var(--accent) 88%, #000); }
  .btn:not(.ghost):not(.primary):hover { box-shadow: 0 0 0 1px var(--line-strong), 0 1px 2px rgba(20, 18, 14, 0.06); }
}

.menu { position: relative; }
.menu summary { list-style: none; }
.menu summary::-webkit-details-marker { display: none; }
.menu-pop {
  position: absolute; right: 0; top: calc(100% + 8px); width: 300px; max-height: 60vh; overflow: auto;
  padding: 10px; background: var(--field); border-radius: var(--r); box-shadow: var(--shadow-lift);
}
.saved { list-style: none; margin: 8px 0 0; padding: 0; }
.saved li { display: flex; align-items: center; gap: 4px; }
.saved li.none { padding: 10px 6px; color: var(--muted); }
.saved button { border: 0; background: none; cursor: pointer; }
.saved [data-open] { flex: 1; display: flex; justify-content: space-between; gap: 10px; padding: 8px 6px; border-radius: 6px; text-align: left; }
.saved [data-open] strong { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.saved [data-open] span { color: var(--faint); font-size: 12px; flex: none; }
.saved .del { width: 28px; height: 28px; border-radius: 6px; color: var(--faint); }
@media (hover: hover) and (pointer: fine) { .saved [data-open]:hover, .saved .del:hover { background: rgba(29, 28, 26, 0.05); } }

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr) 270px;
  height: calc(100vh - 58px);
}
.panel { overflow: auto; background: var(--panel); }
.controls { border-right: 1px solid var(--line); padding: 4px 0 40px; }
.values { border-left: 1px solid var(--line); padding: 14px 12px 40px; }

.controls details { border-bottom: 1px solid var(--line); padding: 4px 16px 14px; }
.controls details > summary {
  list-style: none; cursor: pointer; padding: 12px 0 8px;
  font-weight: 600; font-size: 13px; letter-spacing: 0.01em; display: flex; justify-content: space-between;
}
.controls details > summary::-webkit-details-marker { display: none; }
.controls details > summary::after { content: "–"; color: var(--faint); font-weight: 400; }
.controls details:not([open]) > summary::after { content: "+"; }
.controls details:not([open]) { padding-bottom: 4px; }

.field { display: flex; flex-direction: column; gap: 5px; margin: 10px 0 0; }
.field > span { color: var(--muted); font-size: 12px; font-weight: 500; display: flex; justify-content: space-between; }
.field input:not([type="range"]):not([type="checkbox"]), .field select, .field textarea {
  width: 100%; min-height: 34px; padding: 7px 10px; border: 0; border-radius: var(--r-sm);
  background: var(--field); box-shadow: 0 0 0 1px var(--line-strong) inset; outline: none;
  transition: box-shadow 0.15s var(--ease);
}
.field textarea { resize: vertical; line-height: 1.4; }
.field input:focus, .field select:focus, .field textarea:focus { box-shadow: 0 0 0 1.5px var(--accent) inset, 0 0 0 4px rgba(31, 92, 120, 0.12); }
.field input[type="range"] { width: 100%; accent-color: var(--accent); }
.row { display: flex; align-items: flex-end; gap: 8px; }
.row .field { margin-top: 10px; }
.row.three > .field { flex: 1; min-width: 0; }
.grow { flex: 1; min-width: 0; }
.row > .btn { margin-bottom: 0; }
.field .row { margin: 0; }
.field .row input { flex: 1; }
.check { display: flex; align-items: center; gap: 8px; margin: 12px 0 0; font-size: 13px; color: var(--ink); cursor: pointer; white-space: nowrap; }
.check input { accent-color: var(--accent); width: 15px; height: 15px; }
.row .check { margin-bottom: 8px; }
.btn.wide { margin-top: 12px; }
.hint { margin: 10px 0 0; font-size: 12px; color: var(--muted); text-wrap: pretty; }
.status { margin: 10px 0 0; min-height: 18px; font-size: 12px; color: var(--muted); text-wrap: pretty; }
.status[data-kind="ok"] { color: var(--ok); }
.status[data-kind="warn"] { color: var(--warn); }
.status[data-kind="err"] { color: var(--err); }
.status[data-kind="busy"]::before {
  content: ""; display: inline-block; width: 10px; height: 10px; margin-right: 7px; vertical-align: -1px;
  border-radius: 50%; border: 1.5px solid var(--faint); border-top-color: transparent; animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.tabs { display: flex; gap: 2px; padding: 3px; margin-top: 2px; border-radius: 9px; background: rgba(29, 28, 26, 0.05); }
.tabs button { flex: 1; height: 28px; border: 0; border-radius: 7px; background: none; color: var(--muted); font-size: 12.5px; font-weight: 500; cursor: pointer; }
.tabs button[aria-selected="true"] { background: var(--field); color: var(--ink); box-shadow: var(--shadow); }

.palettes { display: grid; grid-template-columns: repeat(4, 1fr); gap: 7px; margin-top: 4px; }
.swatch {
  height: 26px; border: 0; border-radius: 6px; cursor: pointer; background: var(--g);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
  transition: box-shadow 0.15s var(--ease), transform 0.12s var(--ease);
}
.swatch[aria-pressed="true"] { box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.1), 0 0 0 2px var(--panel), 0 0 0 3.5px var(--ink); }

/* ---------- dropdowns drawn in the page (see js/dropdown.js) ---------- */
.dd-native { position: absolute !important; width: 1px !important; height: 1px !important; opacity: 0; pointer-events: none; }
.dd {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  width: 100%; min-height: 34px; padding: 6px 9px 6px 10px; border: 0; border-radius: var(--r-sm);
  background: var(--field); box-shadow: 0 0 0 1px var(--line-strong) inset; color: var(--ink);
  text-align: left; cursor: pointer; transition: box-shadow 0.15s var(--ease), transform 0.12s var(--ease);
}
.dd-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dd-chev { width: 12px; height: 12px; flex: none; color: var(--muted); transition: transform 0.15s var(--ease); }
.dd[aria-expanded="true"] { box-shadow: 0 0 0 1.5px var(--accent) inset, 0 0 0 4px rgba(31, 92, 120, 0.12); }
.dd[aria-expanded="true"] .dd-chev { transform: rotate(180deg); }
.dd:focus-visible { outline: none; box-shadow: 0 0 0 1.5px var(--accent) inset, 0 0 0 4px rgba(31, 92, 120, 0.12); }
.dd:disabled { opacity: 0.55; cursor: default; }
.dd:active:not(:disabled) { transform: scale(0.98); }
.dd.btn { width: auto; min-height: 34px; background: transparent; box-shadow: none; padding: 0 10px 0 13px; font-weight: 500; }
@media (hover: hover) and (pointer: fine) {
  .dd:not(.btn):not(:disabled):hover { box-shadow: 0 0 0 1px rgba(29, 28, 26, 0.3) inset; }
  .dd.btn:hover { background: rgba(29, 28, 26, 0.06); }
}
.dd-pop {
  position: fixed; z-index: 50; overflow: auto; padding: 4px; outline: none;
  background: var(--field); border-radius: var(--r); box-shadow: var(--shadow-lift);
  transform-origin: top center; animation: dd-in 0.14s var(--ease);
}
.dd-pop[data-side="top"] { transform-origin: bottom center; }
/* scale only, no fade: background tabs can stall animations, and a stalled fade would leave the list see-through */
@keyframes dd-in { from { transform: scale(0.97); } }
.dd-group { padding: 9px 9px 4px; font-size: 11.5px; font-weight: 600; color: var(--faint); }
.dd-opt {
  position: relative; padding: 7px 30px 7px 9px; border-radius: 6px; cursor: pointer;
  font-size: 13.5px; line-height: 1.35; text-wrap: pretty;
}
.dd-opt.active { background: rgba(29, 28, 26, 0.06); }
.dd-opt[aria-selected="true"] { font-weight: 600; color: var(--accent); }
.dd-opt[aria-selected="true"]::after {
  content: ""; position: absolute; right: 10px; top: 50%; width: 9px; height: 5px; margin-top: -4px;
  border: solid currentColor; border-width: 0 0 1.75px 1.75px; transform: rotate(-45deg);
}

/* ---------- stage ---------- */
.stage { display: flex; flex-direction: column; min-width: 0; min-height: 0; padding: 24px 24px 12px; }
.canvas-wrap { position: relative; flex: 1; min-height: 0; }
/* absolutely placed so max-width/max-height shrink it to fit while keeping its shape */
#map {
  position: absolute; inset: 0; margin: auto;
  max-width: 100%; max-height: 100%; width: auto; height: auto;
  border-radius: 4px; cursor: grab; touch-action: none;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.06), 0 1px 2px rgba(0, 0, 0, 0.05), 0 18px 40px -18px rgba(30, 25, 15, 0.35);
}
#map.dragging { cursor: grabbing; }
.tip {
  position: absolute; left: 0; top: 0; pointer-events: none; padding: 6px 9px; border-radius: 7px;
  background: rgba(24, 23, 21, 0.92); color: #fff; font-size: 12px; font-weight: 500; white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.stage-hint { margin: 12px 0 0; text-align: center; font-size: 12px; color: var(--faint); text-wrap: balance; }

/* ---------- values ---------- */
.values-head { display: flex; flex-direction: column; gap: 2px; padding: 0 4px 10px; }
.values-head p { margin: 0; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.values table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.values td { padding: 1px 0; vertical-align: middle; }
.values tr.empty .name { color: var(--faint); }
.values tr.hl td { background: var(--hl); }
.values tr.hl td:first-child { border-radius: 6px 0 0 6px; } .values tr.hl td:last-child { border-radius: 0 6px 6px 0; }
.values .rank { width: 26px; color: var(--faint); font-size: 11.5px; text-align: right; padding-right: 8px; }
.values .name { max-width: 110px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.values .val { width: 92px; }
.values .val input {
  width: 100%; height: 27px; padding: 0 7px; border: 0; border-radius: 6px; background: transparent; text-align: right;
  font-variant-numeric: tabular-nums; outline: none; transition: background-color 0.15s var(--ease), box-shadow 0.15s var(--ease);
}
.values .val input:focus { background: var(--field); box-shadow: 0 0 0 1.5px var(--accent) inset; }
.values tr.edited .val input { color: var(--accent); font-weight: 600; }
.values .reset { width: 24px; }
.values .reset button { width: 22px; height: 22px; border: 0; border-radius: 5px; background: none; color: var(--accent); cursor: pointer; }
@media (hover: hover) and (pointer: fine) {
  .values .val input:hover:not(:focus) { background: rgba(29, 28, 26, 0.045); }
  .values .reset button:hover { background: rgba(31, 92, 120, 0.1); }
}

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  padding: 10px 16px; border-radius: 10px; background: #1D1C1A; color: #fff; font-weight: 500; font-size: 13px;
  box-shadow: 0 10px 30px -10px rgba(0, 0, 0, 0.4); animation: rise 0.22s var(--ease);
}
@keyframes rise { from { opacity: 0; transform: translate(-50%, 6px); } }
dialog { border: 0; border-radius: 12px; padding: 18px; width: min(520px, 92vw); box-shadow: var(--shadow-lift); }
dialog h2 { margin: 0 0 10px; }
dialog .dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
dialog input[type="text"] {
  width: 100%; height: 36px; padding: 0 10px; border: 0; border-radius: 8px; outline: none;
  box-shadow: 0 0 0 1px var(--line-strong) inset; font: inherit;
}
dialog input[type="text"]:focus { box-shadow: 0 0 0 1.5px var(--accent) inset, 0 0 0 4px rgba(31, 92, 120, 0.12); }
dialog::backdrop { background: rgba(20, 18, 14, 0.28); }
dialog textarea { width: 100%; font: 13px/1.45 Inter, sans-serif; padding: 10px; border-radius: 8px; border: 1px solid var(--line-strong); }

/* ---------- smaller screens ---------- */
@media (max-width: 1180px) {
  .layout { grid-template-columns: 300px minmax(0, 1fr); }
  .values { display: none; }
}
@media (max-width: 820px) {
  .bar { height: auto; flex-wrap: wrap; padding: 10px 16px; gap: 10px; }
  .formats { order: 3; margin: 0; width: 100%; overflow-x: auto; }
  .actions { margin-left: auto; }
  .actions #caption, .actions #save { display: none; }
  .layout { grid-template-columns: 1fr; height: auto; }
  .stage { order: -1; height: 75vh; padding: 16px; }
  .controls { border-right: 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; }
}
@media (prefers-reduced-transparency: reduce) {
  .bar { background: var(--bg); backdrop-filter: none; }
}
@media (prefers-contrast: more) {
  :root { --line: rgba(0, 0, 0, 0.3); --line-strong: rgba(0, 0, 0, 0.5); --muted: #45423D; }
}

/* public copy */
.by { margin-left: 2px; font-size: 12px; font-weight: 500; letter-spacing: 0; color: var(--muted); text-decoration: underline; text-decoration-color: var(--line-strong); text-underline-offset: 3px; }
@media (hover: hover) and (pointer: fine) { .by:hover { color: var(--ink); } }
