/* Component styles. Values come from tokens.css (colour, type, radius, motion) and spacing.css (space, sizes). */
html, body { background: var(--canvas); color: var(--ink); font-family: var(--font-ui); font-size: var(--fs-md); line-height: var(--lh); }
body { -webkit-text-size-adjust: 100%; }
#ui, #toasts, #statusbar, #tooltip, #empty { font-family: var(--font-ui); }

button { font: inherit; color: inherit; background: none; border: 0; margin: 0; padding: 0; cursor: pointer;
  -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
button:disabled { cursor: default; }
:focus { outline: none; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
kbd { font-family: var(--font-mono); font-size: var(--fs-sm); background: var(--paper-3); border: 1px solid var(--line);
  border-bottom-width: 2px; border-radius: var(--s-1); padding: 1px var(--s-2); color: var(--ink); white-space: nowrap; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
[hidden] { display: none !important; }
svg.ic { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ---- layers ---- */
#ui { position: fixed; inset: 0; pointer-events: none; z-index: 10; }
#ui.ui-hidden > * { display: none; }
.panel { pointer-events: auto; background: var(--panel); border: 1px solid var(--line-soft); border-radius: var(--radius-panel);
  box-shadow: var(--shadow-panel); }

/* ---- generic buttons ---- */
.btn { min-width: var(--btn); min-height: var(--btn); display: inline-flex; align-items: center; justify-content: center; gap: var(--s-2);
  border-radius: var(--radius); padding: 0 var(--s-3); font-size: var(--fs-sm); font-weight: var(--fw-medium); background: var(--paper-3); }
.btn:hover:not(:disabled) { background: var(--line-soft); }
.btn:active:not(:disabled) { transform: scale(0.96); }
.btn:disabled { opacity: 0.4; }
.btn[aria-pressed="true"], .btn.on { background: var(--accent); color: var(--on-accent); }
.btn.ghost { background: transparent; }
.btn.ghost:hover:not(:disabled) { background: var(--paper-3); }
.btn.ghost[aria-pressed="true"] { background: var(--accent); }
.btn.primary { background: var(--accent); color: var(--on-accent); }
.btn.danger { background: var(--danger-bg); color: var(--danger); }
.btn.round { border-radius: var(--radius-round); }

/* ---- top bar ---- */
#topbar { position: absolute; left: var(--edge-l); top: var(--edge-t); display: flex; gap: var(--s-1); padding: var(--s-2); }

/* ---- left rail ---- */
#rail { position: absolute; left: var(--edge-l); top: var(--below-top); width: var(--rail-w); box-sizing: border-box;
  display: flex; flex-direction: column; align-items: center; gap: var(--s-1); padding: var(--s-2) 0;
  max-height: calc(100% - var(--below-top) - var(--above-status)); overflow-y: auto; overscroll-behavior: contain; scrollbar-width: none; }
#rail::-webkit-scrollbar { display: none; }
.rail-btn { width: var(--rail-btn-w); height: var(--rail-btn-h); flex: none; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 1px; border-radius: var(--radius); font-size: var(--fs-xs); line-height: 1.1; font-weight: var(--fw-medium); white-space: nowrap; }
.rail-btn svg.ic { width: 20px; height: 20px; }
.rail-btn:hover:not(:disabled) { background: var(--paper-3); }
.rail-btn:active:not(:disabled) { transform: scale(0.95); }
.rail-btn:disabled { opacity: 0.4; }
.rail-btn[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }
.rail-sep { width: 36px; height: 1px; background: var(--line); margin: var(--s-1) 0; flex: none; }

/* ---- context strip ---- */
#ctx { position: absolute; left: calc(var(--edge-l) + var(--rail-w) + var(--s-2)); top: var(--below-top); display: flex; align-items: center;
  gap: var(--s-1); padding: var(--s-2); min-height: calc(var(--btn) + var(--s-2) * 2); box-sizing: border-box; max-width: calc(100vw - var(--edge-l) - var(--rail-w) - var(--colors-w) - var(--s-8) * 2); }
#ctx .lbl { font-size: var(--fs-sm); color: var(--muted); padding: 0 var(--s-2); white-space: nowrap; }
#ctx .hint { font-size: var(--fs-sm); color: var(--ink); padding: 0 var(--s-2); }
#ctx .btn { white-space: nowrap; }
#ctx .btn.num { min-width: var(--btn); padding: 0; font-family: var(--font-mono); font-size: var(--fs-md); }

/* ---- right colour rail ---- */
#colors { position: absolute; right: var(--edge-r); top: var(--edge-t); width: var(--colors-w); box-sizing: border-box; padding: var(--s-2);
  display: grid; grid-template-columns: repeat(2, var(--swatch)); gap: var(--s-1); justify-content: center; }
#colors .cur-wrap { grid-column: 1 / -1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding-bottom: var(--s-1);
  border-bottom: 1px solid var(--line); margin-bottom: var(--s-1); }
#colors .cur-idx { font-family: var(--font-mono); font-size: var(--fs-xs); color: var(--muted); }
.swatch { width: var(--swatch); height: var(--swatch); border-radius: var(--radius); box-sizing: border-box; border: 1px solid var(--line); position: relative; }
.swatch.round { border-radius: var(--radius-round); }
.swatch:active { transform: scale(0.94); }
.swatch[aria-pressed="true"] { box-shadow: inset 0 0 0 2px var(--ink), inset 0 0 0 4px var(--paper); }
.swatch.cur { width: var(--btn); height: var(--btn); border-width: 2px; }
.swatch.cur[aria-expanded="true"] { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---- popovers ---- */
.popover { position: absolute; z-index: 3; pointer-events: auto; background: var(--panel-solid); border: 1px solid var(--line); border-radius: var(--radius-panel);
  box-shadow: var(--shadow-pop); padding: var(--s-2); box-sizing: border-box; min-width: 248px; max-width: calc(100vw - var(--s-4) * 2);
  max-height: calc(100% - var(--below-top) - var(--above-status)); overflow-y: auto; overscroll-behavior: contain; }
.popover[hidden] { display: none; }
.pop-h { font-size: var(--fs-xs); color: var(--muted); text-transform: none; letter-spacing: 0.04em; padding: var(--s-2) var(--s-2) var(--s-1); font-weight: var(--fw-medium); }
.pop-sep { height: 1px; background: var(--line); margin: var(--s-2) 0; }
.menu-item { width: 100%; justify-content: flex-start; background: transparent; font-size: var(--fs-md); font-weight: var(--fw-regular); }
.menu-item:hover:not(:disabled) { background: var(--paper-3); }
.row-btn { width: 100%; min-height: var(--btn); display: flex; align-items: center; justify-content: space-between; gap: var(--s-4);
  border-radius: var(--radius); padding: 0 var(--s-3); text-align: left; font-size: var(--fs-sm); }
.row-btn:hover { background: var(--paper-3); }
.sw { width: 40px; height: 22px; border-radius: var(--radius-round); background: var(--line); position: relative; flex: none; }
.sw::after { content: ""; position: absolute; left: 3px; top: 3px; width: 16px; height: 16px; border-radius: var(--radius-round); background: var(--paper); transition: transform var(--dur-1) var(--ease); }
.row-btn[aria-pressed="true"] .sw { background: var(--accent); }
.row-btn[aria-pressed="true"] .sw::after { transform: translateX(18px); background: var(--on-accent); }
.seg { display: flex; gap: var(--s-1); padding: 0 var(--s-1); }
.seg .btn { flex: 1; }
.field { display: flex; align-items: center; gap: var(--s-2); padding: 0 var(--s-2); min-height: var(--btn); font-size: var(--fs-sm); }
.field > span { color: var(--muted); width: 1.4em; }
input[type="number"], input[type="text"].hex { font: inherit; font-family: var(--font-mono); font-size: var(--fs-md); color: var(--ink); background: var(--paper); border: 1px solid var(--line);
  border-radius: var(--radius); min-height: var(--btn); box-sizing: border-box; padding: 0 var(--s-2); width: 72px; -webkit-user-select: text; user-select: text; }
input[type="text"].hex { width: 108px; text-transform: uppercase; }
input[type="range"] { flex: 1; min-height: var(--btn); accent-color: var(--accent); margin: 0; }
input[type="color"] { width: var(--btn); height: var(--btn); border: 1px solid var(--line); border-radius: var(--radius); background: var(--paper); padding: 2px; }

/* ---- palette popover (right of the colour rail) ---- */
#palette { right: calc(var(--edge-r) + var(--colors-w) + var(--s-2)); top: var(--edge-t); left: auto;
  width: min(752px, calc(100vw - var(--edge-l) - var(--topbar-w) - var(--s-2) - var(--colors-w) - var(--s-2) - var(--edge-r)));
  max-height: calc(100vh - var(--edge-t) - var(--gesture-zone) - var(--safe-b) - var(--gizmo-h) - var(--s-4)); }
#palette .tabs { display: flex; gap: var(--s-1); align-items: center; }
#palette .tabs .btn { flex: 0 0 auto; min-width: 96px; }
#palette .tabs .grow { flex: 1; }
#palette .recent { display: flex; flex-wrap: wrap; gap: var(--s-1); padding: var(--s-2) 0; border-bottom: 1px solid var(--line); margin-bottom: var(--s-2); min-height: var(--swatch); }
#palette .grid { display: grid; grid-template-columns: repeat(auto-fill, var(--swatch)); gap: var(--s-1); max-height: calc(var(--swatch) * 4 + var(--s-1) * 3 + 4px); overflow-y: auto; overscroll-behavior: contain; justify-content: center; padding: 2px; }
#palette .picker { display: flex; flex-direction: column; gap: 0; padding: var(--s-1); }
#palette .picker .hint { font-size: var(--fs-sm); color: var(--muted); }
#palette .picker .top { display: flex; align-items: center; gap: var(--s-3); flex-wrap: wrap; }
.chan { display: flex; align-items: center; gap: var(--s-2); font-size: var(--fs-sm); }
.chan > span { width: 1.2em; color: var(--muted); font-family: var(--font-mono); }
.chan output { width: 3ch; text-align: right; font-family: var(--font-mono); color: var(--muted); }

/* ---- stats ---- */
#stats { left: calc(var(--edge-l) + var(--rail-w) + var(--s-2)); bottom: var(--above-status); top: auto; min-width: 0; padding: var(--s-2) var(--s-3);
  font-family: var(--font-mono); font-size: var(--fs-sm); color: var(--muted); line-height: 1.5; pointer-events: none; }

/* ---- gizmo ---- */
#gizmo { position: absolute; right: var(--edge-r); bottom: calc(var(--gesture-zone) + var(--safe-b)); padding: var(--s-2); box-sizing: border-box; width: calc(var(--gizmo-size) + var(--s-2) * 2 + 2px); }
#gizmo .stage { position: relative; width: var(--gizmo-size); height: var(--gizmo-size); }
#gizmo .stage > svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
#gizmo .gz-axis { position: absolute; left: 0; top: 0; width: var(--btn); height: var(--btn); border-radius: var(--radius-round); background: transparent; }
#gizmo .gz-axis:hover { background: var(--paper-3); opacity: 0.6; }
#gizmo .acts { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s-1); margin-top: var(--s-1); }
#gizmo .acts .btn { padding: 0 var(--s-1); min-width: 0; font-size: var(--fs-xs); flex-direction: column; gap: 1px; }
#gizmo .acts svg.ic { width: 18px; height: 18px; }

/* ---- selection bar ---- */
#selbar { position: absolute; z-index: 2; left: 0; top: 0; display: flex; align-items: center; gap: var(--s-1); padding: var(--s-1); will-change: transform; }
#selbar .sel-size { font-family: var(--font-mono); font-size: var(--fs-sm); color: var(--muted); padding: 0 var(--s-2); white-space: nowrap; }
#selbar .btn { padding: 0 var(--s-2); }

/* ---- selection handles (SVG layer, handles.js) ---- */
#handles { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; touch-action: none; }
#handles [data-h] { pointer-events: auto; cursor: grab; touch-action: none; }
#handles [data-h].drag { cursor: grabbing; }
#handles .hit { fill: transparent; }
#handles .arrow-line { stroke: currentColor; stroke-width: 4; stroke-linecap: round; }
#handles .head { fill: currentColor; stroke: var(--paper); stroke-width: 1; stroke-linejoin: round; }
#handles [data-h="move-x"] { color: var(--axis-x); }
#handles [data-h="move-y"] { color: var(--axis-y); }
#handles [data-h="move-z"] { color: var(--axis-z); }
#handles .sq { fill: var(--paper); stroke: var(--accent); stroke-width: 2; }
#handles [data-h]:hover .sq, #handles [data-h].drag .sq { fill: var(--accent); }
#handles [data-h].back { opacity: 0.35; }
#dimlabel { position: absolute; left: 0; top: 0; z-index: 1; pointer-events: none; will-change: transform; white-space: nowrap;
  font-family: var(--font-mono); font-size: var(--fs-sm); color: var(--ink); background: var(--panel); border: 1px solid var(--line-soft);
  border-radius: var(--radius); padding: 2px var(--s-2); }

/* ---- status bar (stays when the UI is hidden) ---- */
#statusbar { position: fixed; left: var(--edge-l); bottom: var(--safe-b); height: var(--status-h); z-index: 11; display: flex; align-items: center; gap: var(--s-3);
  font-size: var(--fs-xs); color: var(--muted); padding: 0 var(--s-3); box-sizing: border-box; border-radius: var(--radius); background: var(--panel); pointer-events: none;
  max-width: calc(100vw - var(--edge-l) - var(--edge-r)); white-space: nowrap; overflow: hidden; }
#statusbar .num { font-family: var(--font-mono); color: var(--ink); }
#statusbar .state { color: var(--ink); display: inline-flex; align-items: center; gap: var(--s-1); }
#statusbar .state::before { content: ""; width: 8px; height: 8px; border-radius: var(--radius-round); background: var(--ok); }
#statusbar .state[data-s="dirty"]::before, #statusbar .state[data-s="saving"]::before { background: var(--warn); }
#statusbar .chip { border-radius: var(--radius-round); padding: 1px var(--s-2); font-weight: var(--fw-medium); }
#statusbar .chip.warn { background: var(--warn-bg); color: var(--warn); }
#statusbar .chip.err { background: var(--danger-bg); color: var(--danger); }

/* ---- toasts ---- */
#toasts { position: fixed; left: 50%; top: var(--edge-t); transform: translateX(-50%); z-index: 30; display: flex; flex-direction: column; align-items: center;
  gap: var(--s-2); pointer-events: none; width: max-content; max-width: min(440px, calc(100vw - var(--s-4) * 2)); }
.toast { pointer-events: auto; background: var(--panel-solid); border: 1px solid var(--line); border-radius: var(--radius-panel); box-shadow: var(--shadow-pop);
  padding: var(--s-2) var(--s-4); font-size: var(--fs-sm); line-height: var(--lh); animation: toast-in var(--dur-2) var(--ease); display: flex; align-items: center; gap: var(--s-3); }
.toast.out { opacity: 0; transform: translateY(-6px); transition: opacity var(--dur-2) var(--ease), transform var(--dur-2) var(--ease); }
.toast.fail { background: var(--danger-bg); border-color: var(--danger); }
.toast.fail .btn { flex: none; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: none; } }
@media (max-width: 900px) { #toasts { top: calc(var(--edge-t) + var(--topbar-h) + var(--s-2)); } }

/* ---- empty state ---- */
#empty { position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%); z-index: 9; pointer-events: none; width: min(360px, calc(100vw - 2 * (var(--rail-w) + var(--s-8)))); box-sizing: border-box;
  background: var(--panel); border: 1px solid var(--line-soft); border-radius: var(--radius-panel); box-shadow: var(--shadow-panel); padding: var(--s-4); text-align: center; }
#empty h2 { margin: 0 0 var(--s-3); font-size: var(--fs-lg); font-weight: var(--fw-bold); font-style: normal; }
#empty .examples { display: flex; gap: var(--s-2); justify-content: center; margin-bottom: var(--s-3); }
#empty .examples .btn { pointer-events: auto; flex: 1; font-size: var(--fs-md); }
#empty p { margin: 0; font-size: var(--fs-sm); color: var(--muted); }
#empty p + p { margin-top: var(--s-1); }

/* ---- tooltip ---- */
#tooltip { position: fixed; z-index: 40; pointer-events: none; background: var(--paper-3); color: var(--ink); border: 1px solid var(--line); border-radius: var(--radius);
  padding: var(--s-1) var(--s-2); font-size: var(--fs-sm); max-width: 280px; box-shadow: var(--shadow-panel); opacity: 0; transform: translateY(2px); transition: opacity var(--dur-1) var(--ease), transform var(--dur-1) var(--ease); }
#tooltip.show { opacity: 1; transform: none; }

/* ---- dialogs ---- */
dialog { color: var(--ink); background: var(--panel-solid); border: 1px solid var(--line); border-radius: var(--radius-panel); box-shadow: var(--shadow-pop);
  padding: 0; width: min(880px, calc(100vw - var(--s-4) * 2)); max-height: calc(100vh - var(--s-8)); overflow: hidden; font-family: var(--font-ui); }
dialog[open] { display: flex; flex-direction: column; }
dialog::backdrop { background: var(--scrim); }
.dlg-h { display: flex; align-items: center; justify-content: space-between; gap: var(--s-3); padding: var(--s-3) var(--s-4); border-bottom: 1px solid var(--line); }
.dlg-h h2 { margin: 0; font-size: var(--fs-lg); font-weight: var(--fw-bold); font-style: normal; }
.dlg-b { padding: var(--s-3) var(--s-4) var(--s-4); overflow-y: auto; overscroll-behavior: contain; }
.dlg-b p { margin: 0 0 var(--s-3); font-size: var(--fs-sm); color: var(--muted); }
table.keys { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); }
table.keys th { text-align: left; font-size: var(--fs-xs); color: var(--muted); font-weight: var(--fw-medium); padding: var(--s-2) var(--s-3); border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel-solid); }
table.keys td { padding: var(--s-2) var(--s-3); border-bottom: 1px solid var(--line-soft); vertical-align: top; }
table.keys td .d { color: var(--muted); margin-left: var(--s-2); }
table.keys td.none { color: var(--muted); }
.backups { list-style: none; margin: 0 0 var(--s-3); padding: 0; display: flex; flex-direction: column; gap: var(--s-1); }
.backups li { display: flex; align-items: center; justify-content: space-between; gap: var(--s-3); padding: var(--s-1) var(--s-1) var(--s-1) var(--s-3); background: var(--paper-2); border-radius: var(--radius); font-size: var(--fs-sm); }
.backups .meta { font-family: var(--font-mono); }
.dlg-f { display: flex; flex-wrap: wrap; gap: var(--s-2); }

/* ---- error recovery (full screen) ---- */
#recover { position: fixed; inset: 0; z-index: 100; background: var(--paper); color: var(--ink); display: flex; align-items: center; justify-content: center;
  padding: calc(var(--safe-t) + var(--s-4)) var(--s-4) calc(var(--safe-b) + var(--s-4)); box-sizing: border-box; font-family: var(--font-ui); }
#recover .box { max-width: 520px; width: 100%; }
#recover h1 { font-size: var(--fs-lg); margin: 0 0 var(--s-3); font-style: normal; font-weight: var(--fw-bold); }
#recover p { color: var(--muted); font-size: var(--fs-md); margin: 0 0 var(--s-4); }
#recover pre { font-family: var(--font-mono); font-size: var(--fs-xs); color: var(--muted); background: var(--paper-2); border-radius: var(--radius); padding: var(--s-3); max-height: 120px; overflow: auto; white-space: pre-wrap; margin: 0 0 var(--s-4); -webkit-user-select: text; user-select: text; }
#recover .acts { display: flex; flex-wrap: wrap; gap: var(--s-2); }

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