/* Everything sits on an 8px UI grid. Tiles are separated by a 2px gap.
   Type is Monaspace Krypton, medium weight; labels are lowercase.
   Three states, one meaning each:
     ink (black)   = this panel is open      (text, controls, info only)
     darker grey   = the current value       (selected options, slider fills)
     grey text     = not available           (disabled; text that didn't fit)
   The theme flips with the canvas background. */
:root {
  --u: 8px;
  --gap: 2px;
  /* one unit, x = the row height; widths are multiples of it:
     panel 14x, text/controls 7x each, info 3x */
  --row: 20px;
  --panel: calc(var(--row) * 14);
  --bg: #ffffff;
  --tile: #e6e6e6;
  --tile-hover: #dadada;
  --fill: #c8c8c8;
  --on: #111111;
  --on-text: #ffffff;
  --text: #111111;
  --muted: #8f8f8f;
  --ui: "Monaspace Krypton", ui-monospace, Menlo, monospace;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #111111;
  --tile: #262626;
  --tile-hover: #303030;
  --fill: #474747;
  --on: #ffffff;
  --on-text: #111111;
  --text: #ededed;
  --muted: #7a7a7a;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 500 11px/1.5 var(--ui);
  letter-spacing: 0.04em;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; letter-spacing: inherit; color: inherit; background: none; border: 0; padding: 0; margin: 0; cursor: pointer; border-radius: 0; }
:focus-visible { outline: 2px solid var(--on); outline-offset: -2px; }

#stage { position: fixed; inset: 0; overflow: hidden; }
#canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; cursor: crosshair; }

/* ------------------------------------------------------------ tiles */

.row { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); gap: var(--gap); }
.tile {
  position: relative;
  min-height: var(--row);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--u);
  padding: 0 var(--u);
  background: var(--tile);
  color: var(--text);
  white-space: nowrap;
  user-select: none;
  -webkit-user-select: none;
}
/* hover never overrides a state, so a click shows straight away */
button.tile:hover:not(:disabled):not([aria-pressed="true"]):not(.on), .slider:hover { background: var(--tile-hover); }
/* open panel */
.tile[aria-pressed="true"], .tile.on { background: var(--on); color: var(--on-text); }
/* current value inside a panel */
.panel .tile[aria-pressed="true"] { background: var(--fill); color: var(--text); }
.tile:disabled { color: var(--muted); cursor: default; }

.slider { cursor: ew-resize; touch-action: pan-y; overflow: hidden; }
.slider .fill { position: absolute; inset: 0 auto 0 0; background: var(--fill); }
.slider .label { position: relative; }
/* highlight colours: four swatches side by side, each opens the colour picker */
.swatches { position: relative; display: grid; grid-template-columns: repeat(4, 1fr); min-height: var(--row); background: var(--tile); }
.swatch { cursor: pointer; background: var(--c); }
.swatch:hover:not(:disabled) { box-shadow: inset 0 0 0 2px var(--on); }
/* off: solid, faded into the tile so it reads as unavailable but still hints at the colours */
.swatch:disabled { background: color-mix(in srgb, var(--c) 18%, var(--tile)); cursor: default; }
.swatch[aria-pressed="true"] { box-shadow: inset 0 0 0 2px var(--on); }
/* the picker: saturation/brightness square, hue strip, hex field */
.picker { flex-direction: column; align-items: stretch; gap: var(--gap); padding: var(--u); }
.picker[hidden] { display: none; }
.picker .sv { position: relative; height: calc(var(--row) * 6); cursor: crosshair; touch-action: none; }
.picker .hue { position: relative; height: calc(var(--row) * 0.6); cursor: ew-resize; touch-action: none;
  background: linear-gradient(to right, #f00, #ff0, #0f0, #0ff, #00f, #f0f, #f00); }
.picker .dot { position: absolute; width: 8px; height: 8px; margin: -4px 0 0 -4px; box-shadow: 0 0 0 1px #111, inset 0 0 0 1px #fff; pointer-events: none; }
.picker .hue .dot { top: -2px; height: calc(100% + 4px); margin-top: 0; }
.picker .hex { font: inherit; letter-spacing: inherit; color: var(--text); background: var(--bg); border: 0; padding: 0 var(--u); height: var(--row); outline: none; }

/* ------------------------------------------------------------ floating controls */

#ui {
  position: absolute;
  left: calc(var(--u) * 2);
  top: calc(var(--u) * 2);
  width: var(--panel);
  max-height: calc(100% - var(--u) * 4);
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  z-index: 3;
}
#bar { width: 100%; }
.panel { display: flex; flex-direction: column; gap: var(--gap); min-height: 0; }
.panel[hidden] { display: none; }

/* text box: the text scrolls inside the tile; the count is pinned to the bottom.
   A mirror behind the textarea greys out what didn't fit. */
.textbox { display: flex; flex-direction: column; align-items: stretch; padding: 0; white-space: normal; }
.textbox .scroll {
  max-height: min(45vh, 336px);
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: calc(var(--u) * 2);
  scrollbar-width: none;
}
.textbox .scroll::-webkit-scrollbar { display: none; }
.textbox { position: relative; }
.textbox .track { position: absolute; right: 2px; width: 4px; }
.textbox .track[hidden] { display: none; }
.textbox .thumb { width: 100%; background: var(--fill); cursor: ns-resize; touch-action: none; }
.textbox .thumb:hover, .textbox .thumb.drag { background: var(--muted); }
.textwrap { position: relative; }
.textwrap .text, .textwrap .mirror {
  font: 500 12px/1.6 var(--ui);
  letter-spacing: 0.02em;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: normal;
  margin: 0;
  padding: 0;
  border: 0;
}
.textwrap .mirror { position: absolute; inset: 0; pointer-events: none; color: var(--text); }
.textwrap .mirror .off { color: var(--muted); }
.textwrap .text {
  position: relative;
  display: block;
  width: 100%;
  min-height: calc(var(--u) * 12);
  resize: none;
  overflow: hidden;
  background: transparent;
  color: transparent;
  caret-color: var(--text);
  outline: none;
}
.textwrap .text::selection { background: var(--fill); color: transparent; }
.textbox .status {
  padding: var(--u) calc(var(--u) * 2);
  border-top: var(--gap) solid var(--bg);
  white-space: nowrap;
}

/* ------------------------------------------------------------ info */

#info-btn {
  position: absolute;
  right: calc(var(--u) * 2);
  bottom: calc(var(--u) * 2);
  width: calc(var(--row) * 3);
  z-index: 3;
}
#info {
  position: absolute;
  right: calc(var(--u) * 2);
  bottom: calc(var(--u) * 2 + var(--row) + var(--gap));
  width: var(--panel);
  z-index: 3;
}
#info .about { display: block; padding: calc(var(--u) * 2); font-size: 12px; line-height: 1.6; letter-spacing: 0.02em; white-space: normal; }
#info .about p { margin: 0; }
#info .about p + p { margin-top: calc(var(--u) * 2); }
#info a { color: var(--text); text-decoration: underline; text-underline-offset: 2px; }
#info a:hover { background: var(--on); color: var(--on-text); text-decoration: none; }

/* ------------------------------------------------------------ hint + toast */

#hint {
  position: absolute;
  left: 50%;
  bottom: calc(var(--u) * 3);
  transform: translate(-50%, 6px);
  color: var(--text);
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.5s, transform 0.5s;
}
#hint.show { opacity: 1; transform: translate(-50%, 0); }

#toast {
  position: absolute;
  left: 50%;
  top: calc(var(--u) * 2);
  transform: translate(-50%, -8px);
  max-width: calc(100% - var(--u) * 4);
  padding: calc(var(--u) * 2);
  background: var(--on);
  color: var(--on-text);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s, transform 0.18s;
  z-index: 6;
  text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ------------------------------------------------------------ phone: panels go full width */

@media (max-width: 560px) {
  :root { --row: 32px; }
  #ui {
    left: var(--u);
    right: var(--u);
    top: calc(env(safe-area-inset-top, 0px) + var(--u));
    width: auto;
    max-height: calc(100% - var(--u) * 2 - var(--row) - env(safe-area-inset-top, 0px));
  }
  #p-ctrl { overflow-y: auto; }
  #info { left: var(--u); right: var(--u); width: auto; bottom: calc(var(--u) + var(--row) + var(--gap) + env(safe-area-inset-bottom, 0px)); }
  #info-btn { right: var(--u); bottom: calc(var(--u) + env(safe-area-inset-bottom, 0px)); }
  #hint { bottom: calc(var(--u) * 2 + var(--row)); white-space: normal; text-align: center; width: 70%; }
}

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