/* =========================================================
   Functions Plot page. Load after styles.css and tool.css.
   ========================================================= */

.plot-switch { margin-bottom: 16px; }

.plot-layout {
  display: grid;
  grid-template-columns: minmax(0, 340px) minmax(0, 1fr);
  grid-template-areas: "controls stage";
  gap: 20px;
  align-items: start;
}
.plot-layout > * { min-width: 0; }
.plot-controls { grid-area: controls; }
@media (max-width: 860px) {
  .plot-layout { grid-template-columns: minmax(0, 1fr); grid-template-areas: "stage" "controls"; }
}

/* ---------- Canvas stage ---------- */
.plot-stage {
  grid-area: stage;
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  overflow: hidden;
  aspect-ratio: 4 / 3;
  min-height: 300px;
}
@media (min-width: 861px) { .plot-stage { position: sticky; top: 16px; } }
@media (max-width: 600px) { .plot-stage { aspect-ratio: 1 / 1; } }
.plot-stage canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
  outline: none;
}
.plot-stage[data-plot-mode="3d"] canvas { cursor: grab; }
.plot-stage canvas.grabbing { cursor: grabbing; }
.plot-stage canvas:focus-visible { box-shadow: inset 0 0 0 2px var(--accent); }

.plot-stage.expanded {
  position: fixed;
  inset: 0;
  z-index: 200;
  border-radius: 0;
  border: 0;
  aspect-ratio: auto;
  min-height: 0;
}
html.plot-expanded { overflow: hidden; }

.plot-tools {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.plot-tools button {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  color: var(--text);
  cursor: pointer;
  box-shadow: var(--shadow);
  padding: 0;
}
.plot-tools button:hover { background: var(--surface-2); }
.plot-tools button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.plot-hint {
  position: absolute;
  left: 10px;
  bottom: 10px;
  font-size: 0.75rem;
  color: var(--muted);
  background: color-mix(in srgb, var(--surface) 85%, transparent);
  padding: 3px 8px;
  border-radius: 6px;
  pointer-events: none;
}

/* ---------- Function rows ---------- */
.fn-list { display: grid; gap: 10px; }
.fn-row {
  display: grid;
  grid-template-columns: auto auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  padding: 4px 6px 4px 8px;
}
.fn-row:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-ring); }
.fn-row.has-error { border-color: #dc2626; }
.fn-swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--c);
  background: var(--c);
  cursor: pointer;
  padding: 0;
}
.fn-swatch[aria-pressed="false"] { background: transparent; }
.fn-row.is-hidden .fn-input { color: var(--muted); }
.fn-label {
  font-family: var(--font-mono, ui-monospace, monospace);
  font-size: 0.95rem;
  color: var(--muted);
  white-space: nowrap;
}
.fn-input, .z-input {
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-family: var(--font-mono, ui-monospace, monospace);
  font-size: 1rem;
  height: 38px;
  min-width: 0;
  width: 100%;
  padding: 0 2px;
}
.fn-input:focus, .z-input:focus { outline: none; }
.fn-remove {
  width: 30px;
  height: 30px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
}
.fn-remove:hover { background: var(--surface-2); color: var(--text); }
.fn-error {
  grid-column: 1 / -1;
  font-size: 0.82rem;
  color: #dc2626;
  padding: 0 4px 4px;
}
.fn-error:empty { display: none; }
html[data-mode="dark"] .fn-error { color: #f87171; }
@media (prefers-color-scheme: dark) { html[data-mode="auto"] .fn-error { color: #f87171; } }

.z-row { grid-template-columns: auto minmax(0, 1fr); }

.add-fn { margin-top: 10px; width: 100%; justify-content: center; }

/* ---------- Examples ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 999px;
  padding: 5px 11px;
  font-family: var(--font-mono, ui-monospace, monospace);
  font-size: 0.85rem;
  cursor: pointer;
}
.chip:hover { border-color: var(--accent); }
.panel h3.mini { font-size: 0.85rem; color: var(--muted); font-weight: 600; margin: 18px 0 8px; }

/* ---------- Ranges & options ---------- */
.range-grid {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto minmax(0, 1fr);
  gap: 8px;
  align-items: center;
  font-size: 0.88rem;
}
.range-grid label { color: var(--muted); font-weight: 600; }
.range-grid input {
  width: 100%;
  min-width: 0;
  height: 36px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-variant-numeric: tabular-nums;
  padding: 0 8px;
}
.range-grid input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-ring); }
.opt-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; }
.opt-grid select {
  width: 100%;
  height: 38px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  padding: 0 8px;
}
.opt-grid .field { margin-bottom: 0; }
.plot-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.plot-actions .btn { flex: 1 1 auto; justify-content: center; }
.plot-hint { transition: opacity 0.6s; }
.plot-hint.faded { opacity: 0; }

/* Phones: 16px fields so iPhones don't zoom in when one is tapped */
@media (max-width: 720px) { .range-grid input { font-size: 16px; } }
