/*
 * Baldrick Cloud design system. Rules and rationale: CLAUDE.md and docs/design-remit.md.
 * Palette and fonts from the Baldrick Store theme.
 */

:root {
  --violet: #6d28d9;
  --violet-hover: #5b21b6;
  --violet-ink: #4c1d95;
  --violet-wash: #f2ecfd;

  --ink: #17151f;
  --ink-2: #4a4656;
  --paper: #ffffff;
  --panel: #f7f5fb;
  --line: #e7e3f0;
  --line-2: #d9d4e6;

  --green: #046c4e;
  --green-wash: #e6f6ef;
  --red: #b42318;
  --red-wash: #fdeceb;

  --font-display: "Space Grotesk", system-ui, sans-serif;
  --font-body: "Hanken Grotesk", system-ui, sans-serif;
  --font-mono: "Space Mono", ui-monospace, SFMono-Regular, Menlo, monospace;

  --text-sm: 0.875rem;   /* 14 */
  --text-md: 1rem;       /* 16 */
  --text-lg: 1.125rem;   /* 18 */
  --text-xl: 1.375rem;   /* 22 */
  --text-2xl: 1.75rem;   /* 28 */

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-6: 24px;
  --space-8: 32px;
  --space-12: 48px;

  --radius-s: 4px;
  --radius-m: 8px;

  --focus: 0 0 0 3px rgba(109, 40, 217, 0.35);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: var(--font-body);
  font-size: var(--text-md);
  line-height: 1.5;
  background: var(--paper);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 {
  font-family: var(--font-display);
  font-weight: 600;
  line-height: 1.25;
  margin: 0 0 var(--space-3);
}

h1 { font-size: var(--text-2xl); }
h2 { font-size: var(--text-xl); }
h3 { font-size: var(--text-lg); }

p { margin: 0 0 var(--space-4); max-width: 65ch; }

a { color: var(--violet-ink); text-underline-offset: 2px; }
a:hover { color: var(--violet); }

:focus-visible { outline: none; box-shadow: var(--focus); border-radius: var(--radius-s); }

code, .mono { font-family: var(--font-mono); font-size: 0.9375em; }

.secondary { color: var(--ink-2); }

/* ---------- Brand ---------- */

.brand {
  display: inline-flex;
  align-items: center;
  gap: var(--space-3);
  font-family: var(--font-display);
  font-weight: 700;
  font-size: var(--text-lg);
  color: var(--ink);
  text-decoration: none;
}

.brand:hover { color: var(--ink); }

.brand img { width: 48px; height: auto; display: block; }

/* ---------- App layout ---------- */

.layout {
  display: grid;
  grid-template-columns: 232px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: var(--space-6) var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-8);
}

.sidebar nav { display: grid; gap: var(--space-1); }

.sidebar nav a {
  display: block;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-s);
  color: var(--ink-2);
  font-weight: 600;
  text-decoration: none;
}

.sidebar nav a:hover { background: var(--line); color: var(--ink); }

.sidebar nav a[aria-current="page"] { background: var(--violet-wash); color: var(--violet-ink); }

.sidebar-account {
  margin-top: auto;
  padding-top: var(--space-4);
  border-top: 1px solid var(--line);
  font-size: var(--text-sm);
}

.sidebar-account strong { display: block; font-size: var(--text-md); }

.sidebar-account .secondary { overflow-wrap: anywhere; margin-bottom: var(--space-3); }

.main {
  padding: var(--space-8) var(--space-12);
  min-width: 0;
  max-width: 1080px;
}

.page-header {
  margin-bottom: var(--space-8);
}

.page-header h1 { margin-bottom: var(--space-1); }
.page-header p { margin: 0; color: var(--ink-2); }

.section { margin-bottom: var(--space-12); }

.section-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-4);
  padding-bottom: var(--space-3);
  margin-bottom: var(--space-4);
  border-bottom: 1px solid var(--line);
}

.section-header h2 { margin: 0; }

.empty-state {
  padding: var(--space-2) 0;
  color: var(--ink-2);
}

.empty-state p:last-child { margin-bottom: 0; }

.details {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: var(--space-2) var(--space-8);
  margin: 0;
}

.details dt { color: var(--ink-2); }
.details dd { margin: 0; overflow-wrap: anywhere; }

/* ---------- Buttons ---------- */

.btn {
  display: inline-block;
  padding: var(--space-2) var(--space-4);
  border: 1px solid var(--violet);
  border-radius: var(--radius-s);
  background: var(--violet);
  color: #fff;
  font: inherit;
  font-weight: 600;
  line-height: 1.5;
  text-decoration: none;
  cursor: pointer;
  transition: background-color 0.12s, border-color 0.12s, color 0.12s;
}

.btn:hover { background: var(--violet-hover); border-color: var(--violet-hover); color: #fff; }

.btn-secondary { background: var(--paper); border-color: var(--line-2); color: var(--ink); }

.btn-secondary:hover { background: var(--line); border-color: var(--line-2); color: var(--ink); }

.btn-small { padding: var(--space-1) var(--space-3); font-size: var(--text-sm); }

/* ---------- Flash ---------- */

.flash {
  padding: var(--space-3) var(--space-4);
  border: 1px solid #bfe7d6;
  border-radius: var(--radius-s);
  margin-bottom: var(--space-6);
  background: var(--green-wash);
  color: var(--green);
}

.flash-alert { background: var(--red-wash); border-color: #f6cfcb; color: var(--red); }

/* ---------- Forms ---------- */

.form { display: grid; gap: var(--space-4); }

.field { display: grid; gap: var(--space-1); }

.field label { font-weight: 600; }

.field input[type="text"],
.field input[type="email"],
.field input[type="password"],
.field input[type="number"],
.field input[type="search"],
.field select,
.field textarea,
.cfx-layer select,
.cfx-layer input[type="number"] {
  width: 100%;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-s);
  background: var(--paper);
  color: var(--ink);
  font: inherit;
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--violet);
  box-shadow: var(--focus);
}

.field input[type="color"],
.cfx-layer input[type="color"] {
  width: 64px;
  height: 42px;
  padding: var(--space-1);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-s);
  background: var(--paper);
}

.field-hint { font-size: var(--text-sm); color: var(--ink-2); }

.field-check {
  display: flex;
  align-items: flex-start;
  gap: var(--space-2);
}

.field-check input {
  margin: 0.3em 0 0;
  width: 1rem;
  height: 1rem;
  accent-color: var(--violet);
}

.field_with_errors { display: contents; }

.field_with_errors input { border-color: var(--red) !important; }

.form-errors {
  padding: var(--space-3) var(--space-4);
  border: 1px solid #f6cfcb;
  border-radius: var(--radius-s);
  background: var(--red-wash);
  color: var(--red);
}

.form-errors ul { margin: var(--space-1) 0 0; padding-left: var(--space-6); }

/* ---------- Auth pages: a single form column ---------- */

.auth {
  max-width: 400px;
  margin: 0 auto;
  padding: var(--space-12) var(--space-4);
}

.auth .brand { margin-bottom: var(--space-12); }

.auth h1 { margin-bottom: var(--space-6); }

.auth .form .btn { width: 100%; margin-top: var(--space-2); }

.auth-switch {
  margin-top: var(--space-6);
  padding-top: var(--space-4);
  border-top: 1px solid var(--line);
  color: var(--ink-2);
}

.auth--wide { max-width: 680px; }

/* ---------- Tables ---------- */

.table-wrap { overflow-x: auto; }

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-md);
}

.table th,
.table td {
  text-align: left;
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}

.table th:first-child, .table td:first-child { padding-left: 0; }
.table th:last-child, .table td:last-child { padding-right: 0; }

.table thead th {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--ink-2);
  border-bottom-color: var(--line-2);
  white-space: nowrap;
}

.table .nowrap { white-space: nowrap; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table .actions { text-align: right; white-space: nowrap; }
.table .actions > * { display: inline-block; margin-left: var(--space-2); }
.table a.row-link { font-weight: 600; color: var(--ink); text-decoration: none; }
.table a.row-link:hover { color: var(--violet-ink); text-decoration: underline; }

/* ---------- Status ---------- */

.status { font-weight: 600; white-space: nowrap; }
.status-online { color: var(--green); }
.status-offline { color: var(--red); }
.status-unknown { color: var(--ink-2); }

.badge {
  display: inline-block;
  padding: 0 var(--space-2);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-s);
  font-size: var(--text-sm);
  color: var(--ink-2);
  white-space: nowrap;
}

.badge-warn { border-color: #f3d3a6; background: #fdf2e3; color: #92400e; }

.swatch {
  display: inline-block;
  width: 0.9em;
  height: 0.9em;
  border: 1px solid rgba(0, 0, 0, 0.2);
  border-radius: 2px;
  vertical-align: -0.1em;
  margin-right: var(--space-2);
}

/* ---------- Board page ---------- */

.board-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-6);
}

.board-header h1 { margin-bottom: var(--space-1); }
.board-header p { margin: 0; color: var(--ink-2); }

.page-nav {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1) var(--space-4);
  padding: var(--space-3) 0;
  margin-bottom: var(--space-8);
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  font-size: var(--text-sm);
  font-weight: 600;
}

.page-nav a { color: var(--ink-2); text-decoration: none; }
.page-nav a:hover { color: var(--violet-ink); }

.facts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: var(--space-4) var(--space-8);
  margin: 0;
}

.facts dt { font-size: var(--text-sm); color: var(--ink-2); }
.facts dd { margin: 0; font-weight: 600; overflow-wrap: anywhere; }

.port + .port { margin-top: var(--space-8); }

.empty-ports a { display: inline-block; min-width: 2em; padding: var(--space-1) var(--space-2); text-align: center; }

.port-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-2);
}

.port-header h3 { margin: 0; }

.notice {
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-s);
  background: var(--panel);
  margin-bottom: var(--space-4);
}

progress {
  display: block;
  width: 100%;
  height: 8px;
  margin-top: var(--space-2);
  accent-color: var(--violet);
}

.form-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: var(--space-4);
}

.form-narrow { max-width: 560px; }

.form-actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-3); }

.inline-form { display: inline; }

.radio-group { display: flex; flex-wrap: wrap; gap: var(--space-4); }
.radio-group label { display: flex; align-items: center; gap: var(--space-2); font-weight: 400; }
.radio-group input { accent-color: var(--violet); }

fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }
fieldset[disabled] { opacity: 0.6; }
legend { font-weight: 600; padding: 0; margin-bottom: var(--space-1); }

.columns {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: var(--space-8);
}

.columns h3 { font-size: var(--text-md); }

details > summary { cursor: pointer; font-weight: 600; }
details[open] > summary { margin-bottom: var(--space-3); }

pre.raw {
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-s);
  padding: var(--space-3) var(--space-4);
  overflow: auto;
  max-height: 480px;
}

.btn-danger { background: var(--paper); border-color: #f6cfcb; color: var(--red); }
.btn-danger:hover { background: var(--red-wash); border-color: var(--red); color: var(--red); }

.link-button {
  background: none;
  border: 0;
  padding: 0;
  font: inherit;
  color: var(--violet-ink);
  text-decoration: underline;
  text-underline-offset: 2px;
  cursor: pointer;
}

.link-button:hover { color: var(--violet); }
.link-button.danger { color: var(--red); }

.code-input {
  font-family: var(--font-mono);
  font-size: var(--text-xl) !important;
  letter-spacing: 0.02em;
}

/* ---------- Responsive ---------- */

@media (max-width: 1200px) {
  .table .hide-md { display: none; }
}


@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }

  .sidebar {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .sidebar .brand { margin-right: auto; }

  .sidebar nav { order: 2; width: 100%; display: flex; gap: var(--space-1); }

  .sidebar-account { margin: 0; padding: 0; border: 0; }

  .sidebar-account strong,
  .sidebar-account .secondary { display: none; }

  .main { padding: var(--space-6) var(--space-4); }

  .details { grid-template-columns: 1fr; gap: 0; }

  .table .hide-sm,
  .table .hide-md { display: none; }
  .details dd { margin-bottom: var(--space-3); }
}

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

.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.columns h3:not(:first-child) { margin-top: var(--space-8); }

/* ---------- Busk board ---------- */

.busk-status {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: var(--space-2) var(--space-4);
  padding: var(--space-3) var(--space-4);
  margin-bottom: var(--space-6);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-s);
  background: var(--panel);
}

.busk-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: var(--space-3);
}

.busk-grid form { display: contents; }

.busk-button {
  min-height: 96px;
  padding: var(--space-3);
  border: 1px solid rgba(0, 0, 0, 0.15);
  border-radius: var(--radius-m);
  font: inherit;
  font-family: var(--font-display);
  font-size: var(--text-lg);
  font-weight: 600;
  cursor: pointer;
  overflow-wrap: anywhere;
  transition: filter 0.12s;
}

.busk-button:hover { filter: brightness(1.08); }
.busk-button:active { filter: brightness(0.92); }
.busk-button:disabled { opacity: 0.5; cursor: not-allowed; }
.busk-blackout { background: var(--ink); color: #fff; }

.busk-edit {
  padding-bottom: var(--space-6);
  border-bottom: 1px solid var(--line);
}

.busk-steps select { width: 100%; padding: var(--space-1) var(--space-2); font: inherit; border: 1px solid var(--line-2); border-radius: var(--radius-s); }
.busk-seconds { width: 6em; padding: var(--space-1) var(--space-2); font: inherit; border: 1px solid var(--line-2); border-radius: var(--radius-s); }

@media (max-width: 600px) {
  .busk-grid { grid-template-columns: repeat(2, 1fr); }
}

/* ---------- CunningFX editor ---------- */

.cfx-layers {
  list-style: none;
  counter-reset: layer;
  padding: 0;
  margin: 0 0 var(--space-4);
}

.cfx-layer {
  counter-increment: layer;
  padding: var(--space-4) 0;
  border-top: 1px solid var(--line);
}

.cfx-layer:last-child { border-bottom: 1px solid var(--line); }

.cfx-layer-head {
  display: grid;
  grid-template-columns: 2em 1fr auto;
  gap: var(--space-3);
  align-items: end;
}

.cfx-layer-number::before {
  content: counter(layer);
  font-family: var(--font-display);
  font-weight: 600;
  font-size: var(--text-lg);
  color: var(--ink-2);
}

.cfx-layer-tools { display: flex; gap: var(--space-3); padding-bottom: var(--space-2); }

.cfx-layer label { display: grid; gap: var(--space-1); font-weight: 600; }
.cfx-layer label.field-check { display: flex; font-weight: 400; align-self: end; }

.cfx-args { margin: var(--space-3) 0 0 calc(2em + var(--space-3)); }

.field-label { font-weight: 600; }

.colour-list { display: flex; flex-wrap: wrap; gap: var(--space-2); }
.colour-item { display: inline-flex; align-items: center; gap: var(--space-1); }

@media (max-width: 600px) {
  .cfx-layer-head { grid-template-columns: 1fr; }
  .cfx-layer-number { display: none; }
  .cfx-args { margin-left: 0; }
}

.power-on { margin-top: var(--space-8); }

/* ---------- Props and catalogue ---------- */

.prop-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 260px;
  gap: var(--space-8);
  margin-bottom: var(--space-12);
}

.prop-canvas-wrap { min-width: 0; }
.prop-canvas-wrap canvas { display: block; border-radius: var(--radius-m); }

.submodel-list { list-style: none; padding: 0; margin: 0; }
.submodel-list li { padding: var(--space-1) 0; border-bottom: 1px solid var(--line); }
.submodel-list button { text-decoration: none; color: var(--ink); width: 100%; text-align: left; padding: var(--space-1) var(--space-2); border-radius: var(--radius-s); }
.submodel-list button[aria-pressed="true"] { background: var(--violet-wash); color: var(--violet-ink); font-weight: 600; }

.product-grid {
  list-style: none;
  padding: 0;
  margin: 0 0 var(--space-6);
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: var(--space-4);
}

.product-link { display: grid; gap: var(--space-1); color: var(--ink); text-decoration: none; }
.product-link:hover .product-name { text-decoration: underline; color: var(--violet-ink); }
.product-link img, .product-noimage {
  width: 100%;
  aspect-ratio: 1;
  object-fit: contain;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-m);
}
.product-name { font-weight: 600; }
.product-link .secondary { font-size: var(--text-sm); }

.product-photo { width: 100%; max-width: 420px; border: 1px solid var(--line); border-radius: var(--radius-m); background: var(--panel); }

@media (max-width: 900px) {
  .prop-layout { grid-template-columns: 1fr; }
}

.cfx-preview { margin-bottom: var(--space-8); }
.cfx-preview-canvas { min-width: 0; }
.cfx-preview-canvas canvas { display: block; border-radius: var(--radius-m); }

.design-layout {
  display: grid;
  grid-template-columns: minmax(220px, 340px) minmax(0, 1fr);
  gap: var(--space-8);
  align-items: start;
}

.design-layout .cfx-preview { position: sticky; top: var(--space-4); }

@media (max-width: 900px) {
  .design-layout { grid-template-columns: 1fr; }
  .design-layout .cfx-preview { position: static; }
}

.library-canvas canvas { margin-top: var(--space-2); }
