/* Structural stylesheet — consumes design tokens from theme.css only.
   To change colors, fonts, or sizes, edit theme.css; to change tool
   layout, hotkeys, or presets, edit js/config.js. */

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden; /* the app manages its own scrolling; no stray page bars */
  background: var(--bg);
  color: var(--text);
  font: var(--font);
}

#app { display: flex; flex-direction: column; height: 100vh; }

/* ---------- Top bar & toolbar ---------- */
#topbar {
  position: relative; /* anchors the top-right dropdown menus */
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 6px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

#logo { font-weight: 700; letter-spacing: 0.4px; color: var(--accent); white-space: nowrap; }

#topbar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }

#help-open, .iconbtn {
  width: 30px;
  height: 30px;
  justify-content: center;
  border-radius: 50%;
  padding: 0;
  flex: none;
}
#help-open { font-weight: 700; color: var(--accent); }
.tb-caret { font-size: 9px; opacity: 0.7; margin-left: 2px; }
.menubtn { display: inline-flex; align-items: center; gap: 6px; padding: 5px 12px; border-radius: var(--radius-lg); }
.menubtn svg { display: block; }
.iconbtn svg { display: block; }
#profile-open.authed { border-color: var(--accent); }
#profile-avatar { display: flex; width: 22px; height: 22px; align-items: center; justify-content: center; border-radius: 50%; overflow: hidden; font-size: 14px; }
#profile-avatar svg { width: 100%; height: 100%; }

/* Top-right dropdown menus (☰ project/export, profile/account) */
.top-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 8px;
  z-index: 80;
  width: 270px;
  padding: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.35);
}
.top-menu .menu-title { font-weight: 600; font-size: var(--font-small); opacity: 0.75; margin: 2px 0 6px; }
.top-menu .row { margin: 6px 0; }
.top-menu input[type="email"], .top-menu input[type="text"], .top-menu input[type="password"] { width: 100%; }
.top-menu input[type="text"]#acct-emoji { width: 56px; }
.top-menu hr { border: none; border-top: 1px solid var(--line); margin: 8px 0; }

#help-dialog { max-width: 560px; max-height: 82vh; overflow-y: auto; }
#help-dialog h4 { margin: 14px 0 6px; }
.help-table { border-collapse: collapse; width: 100%; font-size: var(--font-small); }
.help-table td { padding: 4px 8px 4px 0; border-bottom: 1px solid var(--line); vertical-align: top; }
.help-table svg { vertical-align: -3px; }
kbd {
  background: var(--bg);
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 11px;
}

#toolbar { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; margin: 0 auto; }
.tool-group { display: flex; gap: 3px; padding: 2px; border: 1px solid var(--line); border-radius: var(--radius-lg); }

button, .filebtn {
  background: var(--btn-bg);
  color: var(--text);
  border: 1px solid var(--btn-border);
  border-radius: var(--radius);
  padding: 5px 10px;
  cursor: pointer;
  font: inherit;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
button:hover, .filebtn:hover { border-color: var(--accent); }
button.active { background: var(--btn-active-bg); color: var(--btn-active-fg); border-color: var(--btn-active-bg); font-weight: 600; }
button.primary { background: var(--btn-primary-bg); border-color: var(--btn-primary-border); }
button.primary:hover { background: var(--btn-primary-hover); }
button.danger { border-color: var(--danger); color: var(--danger); }

.toolbtn { border-color: transparent; padding: 5px 8px; }
.toolbtn svg { display: block; }
.toolbtn .tb-label { font-size: var(--font-small); }

.chip {
  padding: 2px 8px;
  border-radius: 999px;
  font-size: var(--font-small);
}

.filebtn { position: relative; overflow: hidden; }
.filebtn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

/* ---------- Layout ---------- */
#main { display: flex; flex: 1; min-height: 0; }

#canvas-area {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: grid;
  /* minmax(0, 1fr): a bare 1fr track's MIN size is its content's intrinsic
     size — and the ruler <canvas> intrinsic size is set to clientSize×dpr on
     every repaint, so at devicePixelRatio > 1 the track and the canvas feed
     each other and the layout grows without bound. Track min must be 0. */
  grid-template-columns: var(--ruler-size) minmax(0, 1fr);
  grid-template-rows: var(--ruler-size) minmax(0, 1fr);
}
#ruler-corner { background: var(--ruler-bg); border: 1px solid var(--line); border-width: 0 1px 1px 0; }
#ruler-top { width: 100%; height: var(--ruler-size); min-width: 0; display: block; background: var(--ruler-bg); border-bottom: 1px solid var(--line); }
#ruler-left { width: var(--ruler-size); height: 100%; min-height: 0; display: block; background: var(--ruler-bg); border-right: 1px solid var(--line); }

#canvas-wrap { position: relative; min-width: 0; min-height: 0; background: var(--canvas-bg); overflow: hidden; }
#canvas { width: 100%; height: 100%; display: block; touch-action: none; }

#panel {
  width: var(--panel-width);
  overflow-y: auto;
  background: var(--panel);
  border-left: 1px solid var(--line);
  padding: var(--gap);
}

/* ---------- Action bar (primary CTAs, always visible) ---------- */
#action-bar {
  position: sticky;
  top: calc(-1 * var(--gap));
  z-index: 10;
  display: flex;
  gap: 6px;
  padding: var(--gap);
  margin: calc(-1 * var(--gap)) calc(-1 * var(--gap)) var(--gap);
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.cta {
  flex: 1;
  justify-content: center;
  padding: 10px 12px;
  font-weight: 700;
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.cta:hover { filter: brightness(1.12); border-color: var(--accent); }
.cta-success {
  justify-content: center;
  padding: 10px 12px;
  font-weight: 700;
  background: var(--success);
  border-color: var(--success);
  color: #fff;
}
.cta-success:hover { background: var(--success-hover); border-color: var(--success-hover); }
.cta-ghost { padding: 10px; color: var(--muted); }

.row.dimmed { opacity: 0.45; pointer-events: none; }

/* ---------- Welcome / empty state ---------- */
#empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 24px;
  pointer-events: none;
}
.es-card {
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  width: 190px;
  padding: 28px 18px;
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.15s;
}
.es-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.es-icon { font-size: 34px; }
.es-sub { color: var(--muted); font-size: var(--font-small); text-align: center; }

/* ---------- Floating tool panels (GIMP-style, contextual) ---------- */
.float-panel {
  position: absolute;
  width: 252px;
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  z-index: 8;
}
#float-color { right: 14px; top: 64px; }
#float-brush { right: 14px; bottom: 16px; }
#float-dims  { left: 70px; top: 64px; }
.float-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 8px 6px 12px;
  font-weight: 600;
  border-bottom: 1px solid var(--line);
  cursor: grab;
  user-select: none;
}
.float-head:active { cursor: grabbing; }
.float-panel.docked { border-color: color-mix(in srgb, var(--accent) 40%, var(--line)); }
.float-close { border: none; background: none; color: var(--muted); padding: 2px 6px; }
.float-close:hover { color: var(--danger); border: none; }
.float-body { padding: 4px 12px 10px; }
.float-body .row { margin: 7px 0; }

/* ---------- Density ramp strips ---------- */
.density-strip {
  position: absolute;
  background: var(--density-bg);
  z-index: 5;
}
#density-x { top: 0; left: 0; right: 0; height: 54px; border-bottom: 1px solid var(--line); }
#density-y { top: 0; left: 0; bottom: 0; width: 54px; border-right: 1px solid var(--line); }
.density-strip svg { width: 100%; height: 100%; display: block; }
.density-curve { fill: none; stroke: var(--density-curve); stroke-width: 2px; }
.density-handle { fill: var(--density-handle); stroke: var(--density-curve); stroke-width: 2px; cursor: grab; }
.density-strip button {
  position: absolute;
  right: 2px;
  bottom: 2px;
  padding: 1px 6px;
  font-size: 12px;
}
#density-y button { right: 2px; bottom: 2px; }

#panel details { border: 1px solid var(--line); border-radius: var(--radius-lg); margin-bottom: var(--gap); background: var(--panel2); }
#panel summary { padding: 7px 10px; font-weight: 600; cursor: pointer; user-select: none; }
#panel details > *:not(summary) { margin: 6px 10px; }
#panel hr { border: none; border-top: 1px solid var(--line); }

.row { display: flex; align-items: center; gap: var(--gap); flex-wrap: wrap; }
.row label { display: flex; align-items: center; gap: 6px; }
.row.rgb input { width: 52px; }

input[type="text"], input[type="email"], input[type="password"], input[type="number"], select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 4px 6px;
  font: inherit;
  width: 72px;
}
input[type="text"] { width: 84px; }
select { width: auto; }
input[type="range"] { accent-color: var(--accent); }
input[type="color"] { width: 34px; height: 26px; border: 1px solid var(--line); border-radius: var(--radius); background: none; padding: 1px; }
input[type="file"] { max-width: 230px; color: var(--muted); }
input::placeholder { color: var(--muted); }
input:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.hint { color: var(--muted); font-size: var(--font-small); }

.form-error {
  background: color-mix(in srgb, var(--danger) 14%, transparent);
  border: 1px solid var(--danger);
  border-radius: var(--radius);
  color: var(--danger);
  font-size: var(--font-small);
  padding: 6px 8px;
  margin-bottom: 8px;
}

#acct-avatar { display: inline-flex; }

#emoji-grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px; margin: 6px 0; }
.emoji-choice {
  border-color: transparent;
  padding: 2px 0;
  font-size: 15px;
  justify-content: center;
  border-radius: var(--radius);
}
.emoji-choice.active { border-color: var(--accent); background: var(--btn-bg); }

/* Crop mode overlay */
.crop-shade { fill: rgba(0, 0, 0, 0.45); pointer-events: none; }
.crop-rect {
  fill: transparent;
  stroke: var(--accent);
  stroke-width: 1.5px;
  vector-effect: non-scaling-stroke;
  stroke-dasharray: 6 4;
  cursor: move;
  pointer-events: all;
}
.crop-handle { fill: var(--accent); stroke: #fff; stroke-width: 1.5px; vector-effect: non-scaling-stroke; }

/* Full-screen gate shown when the server runs with REQUIRE_AUTH=1. The
   topbar (and its account menu) stays above and usable. */
#login-wall {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: flex;
  align-items: center;
  justify-content: center;
  background: color-mix(in srgb, var(--bg) 72%, transparent);
  backdrop-filter: blur(7px);
}
#topbar { z-index: 90; }
.wall-card {
  width: min(400px, 86vw);
  padding: 26px 30px;
  text-align: center;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.45);
}
.wall-card .wall-glyph { font-size: 34px; color: var(--accent); }
.wall-card h3 { margin: 10px 0 6px; }
.wall-card button { margin-top: 12px; }
#acct-avatar svg { width: 36px; height: 36px; border-radius: 8px; border: 1px solid var(--line); }

#statusbar {
  display: flex;
  gap: 18px;
  padding: 4px 12px;
  background: var(--panel);
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
}
#status-pos { min-width: 90px; font-variant-numeric: tabular-nums; }
#status-hint { flex: 1; text-align: center; }

/* ---------- Palette builder dialog ---------- */
dialog {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 16px;
  max-width: 380px;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.55); }
dialog h3 { margin: 0 0 6px; }
dialog .row { margin: 8px 0; }

.pb-base { display: inline-flex; align-items: center; gap: 2px; }
.pb-base button { padding: 2px 7px; }

#pb-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
  margin: 10px 0;
  min-height: 24px;
}
.pb-swatch {
  width: 26px;
  height: 26px;
  border-radius: 5px;
  border: 1px solid var(--line);
  display: inline-block;
}

/* ---------- Region brush ---------- */
#brush-cursor {
  fill: color-mix(in srgb, var(--accent) 14%, transparent);
  stroke: var(--accent);
  stroke-width: 1.5px;
  stroke-dasharray: 6 5;
  vector-effect: non-scaling-stroke;
  pointer-events: none;
}

/* ---------- Toast ---------- */
#toast {
  position: fixed;
  left: 50%;
  bottom: 42px;
  transform: translateX(-50%);
  max-width: 520px;
  background: var(--warning-bg);
  border: 1px solid var(--warning-border);
  color: var(--warning-text);
  border-radius: var(--radius-lg);
  padding: 10px 16px;
  z-index: 50;
  box-shadow: 0 4px 18px rgba(0,0,0,0.4);
}

/* ---------- SVG geometry ---------- */
.artboard { fill: var(--artboard-bg); stroke: var(--artboard-line); stroke-width: 1px; vector-effect: non-scaling-stroke; }

#grid-rect { fill: url(#dotgrid); pointer-events: none; }
#dotgrid circle { fill: var(--grid-dot); fill-opacity: var(--grid-dot-opacity); }

.node {
  fill: var(--node-fill);
  stroke: var(--node-stroke);
  stroke-width: 1.5px;
  vector-effect: non-scaling-stroke;
  cursor: grab;
}
.node:hover { fill: var(--node-hover); }
.node.selected { fill: var(--node-selected); stroke: var(--node-selected); }
#layer-nodes.nodes-hidden .node { display: none; }
#layer-nodes.nodes-hidden .node.peek,
#layer-nodes.nodes-hidden .node.selected { display: inline; }

.edge-vis { fill: none; stroke-linecap: round; }
.edge-vis.selected { stroke: var(--node-selected) !important; }
.edge-hit { fill: none; stroke: transparent; stroke-width: 10px; vector-effect: non-scaling-stroke; cursor: pointer; }

#layer-faces path { cursor: pointer; }
#layer-faces path.selected { stroke: var(--node-selected); stroke-width: 2px; vector-effect: non-scaling-stroke; }

.preview-face { opacity: 0.92; }
.preview-edge { stroke-width: 1.2px; vector-effect: non-scaling-stroke; fill: none; }
#layer-preview path { pointer-events: none; }

.select-region {
  fill: color-mix(in srgb, var(--accent) 10%, transparent);
  stroke: var(--accent);
  stroke-width: 1px;
  stroke-dasharray: 5 4;
  vector-effect: non-scaling-stroke;
}

.rubber {
  stroke: var(--rubber-color);
  stroke-width: 1.5px;
  stroke-dasharray: 5 4;
  vector-effect: non-scaling-stroke;
}

.sweep-line {
  stroke: var(--sweep-color);
  stroke-width: 2px;
  vector-effect: non-scaling-stroke;
  stroke-dasharray: 8 5;
}

/* ---------- Dimensions ---------- */
.dim-arc { fill: none; stroke: var(--dim-color); stroke-width: 1.5px; vector-effect: non-scaling-stroke; pointer-events: none; }
/* font-size is set per-element by the app (scaled to stay constant on screen);
   only family/weight come from the theme */
.dim-label, .dim-glyph {
  fill: var(--dim-color);
  font-family: var(--dim-font-family);
  font-weight: var(--dim-font-weight);
  cursor: pointer;
  user-select: none;
}
.dim-conflict .dim-arc, .dim-conflict.dim-arc { stroke: var(--dim-conflict); }
.dim-conflict .dim-label, .dim-conflict.dim-label, .dim-conflict.dim-glyph { fill: var(--dim-conflict); }
.dim-selected.dim-label, .dim-selected.dim-glyph { fill: var(--dim-selected); }
