/* Oddsprig Weapons Studio - layout and themes.
   Light: cream with light blue. Dark: charcoal with a restrained warm metal accent. */

:root,
[data-oddsprig-theme="light"] {
  color-scheme: light;
  --bg: #f4efe2;
  --bg-2: #ece5d3;
  --panel: #fbf7ec;
  --panel-2: #f3eddc;
  --line: #ddd3bc;
  --line-strong: #c7bb9f;
  --text: #2a2722;
  --muted: #6c655a;
  --accent: #3d7aa3;
  --accent-soft: #d6e7f2;
  --accent-text: #ffffff;
  --danger: #b04a36;
  --warn: #9a6a16;
  --ok: #3f7a4c;
  --focus: #3d7aa3;
  --view-bg: #e3ecf1;
  --view-bg-2: #f2efe4;
  --view-grid: #a9b8c2;
  --view-grid-2: #cfd8dd;
  --outline-fallback: #5b6670;
  --shadow: 0 1px 2px rgba(60, 50, 30, .08), 0 4px 14px rgba(60, 50, 30, .06);
}

[data-oddsprig-theme="dark"] {
  color-scheme: dark;
  --bg: #1b1c1f;
  --bg-2: #222428;
  --panel: #25272b;
  --panel-2: #2c2f34;
  --line: #3a3d43;
  --line-strong: #4b4f56;
  --text: #e9e5dc;
  --muted: #a39d92;
  --accent: #c99a5b;
  --accent-soft: #3d342a;
  --accent-text: #1b1c1f;
  --danger: #e07a62;
  --warn: #d9a84a;
  --ok: #7fb58a;
  --focus: #d8ab6d;
  --view-bg: #17181b;
  --view-bg-2: #24262a;
  --view-grid: #4a4d53;
  --view-grid-2: #303237;
  --outline-fallback: #b8b2a6;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 14px rgba(0, 0, 0, .25);
}

*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

a { color: var(--accent); }
a:hover { text-decoration-thickness: 2px; }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

[hidden] { display: none !important; }

/* ---------- shell ---------- */

.ws {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
}

/* Explicit rows: the notice keeps row 2 even while hidden, so the workspace always fills row 3. */
.topbar { grid-row: 1; }
.boot-error { grid-row: 2; }
.workspace { grid-row: 3; }

.topbar {
  display: flex;
  align-items: center;
  gap: 10px 14px;
  flex-wrap: wrap;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: relative;
  z-index: 20;
}

.brand {
  display: flex; align-items: center; gap: 8px;
  color: var(--text); text-decoration: none; min-width: 0;
}
.oddsprig-logo { width: 28px; height: 28px; display: block; flex: none; }
.brand-text { display: flex; flex-direction: column; line-height: 1.1; }
.brand-site { font-size: 11px; color: var(--muted); letter-spacing: .02em; }
.brand-tool { font-weight: 650; font-size: 15px; }

.sitenav { display: flex; gap: 2px; }
.sitenav a {
  color: var(--muted); text-decoration: none; padding: 6px 8px; border-radius: 6px; font-size: 13px;
}
.sitenav a:hover { color: var(--text); background: var(--panel-2); }

.toolbar { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; flex: 1 1 auto; justify-content: center; }
.toolbar .sep { width: 1px; height: 22px; background: var(--line); margin: 0 2px; }
.save-state { font-size: 12px; color: var(--muted); min-width: 7em; }
.save-state[data-state="dirty"] { color: var(--warn); }
.save-state[data-state="saved"] { color: var(--ok); }

.topright { display: flex; align-items: center; gap: 6px; margin-left: auto; }
.howto { font-size: 13px; padding: 6px 8px; border-radius: 6px; font-weight: 600; }

/* ---------- buttons and fields ---------- */

.btn {
  appearance: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 34px; padding: 0 12px;
  border: 1px solid var(--line-strong); border-radius: 7px;
  background: var(--panel); color: var(--text);
  font: inherit; font-size: 13px; font-weight: 550;
  cursor: pointer; user-select: none; white-space: nowrap;
  list-style: none;
}
.btn::-webkit-details-marker { display: none; }
.btn:hover:not(:disabled) { background: var(--panel-2); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.06); background: var(--accent); }
.btn.small { min-height: 28px; padding: 0 9px; font-size: 12px; }
.icon-btn { padding: 0 8px; }
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }

.file-btn { position: relative; overflow: hidden; }
.file-btn input[type="file"] { position: absolute; inset: 0; opacity: 0; cursor: pointer; font-size: 0; }
.file-btn:focus-within { outline: 2px solid var(--focus); outline-offset: 2px; }

input[type="text"], input[type="number"], select {
  width: 100%;
  min-height: 32px; padding: 4px 8px;
  border: 1px solid var(--line-strong); border-radius: 6px;
  background: var(--bg); color: var(--text);
  font: inherit; font-size: 13px;
}
input[type="number"] { font-variant-numeric: tabular-nums; }
input[type="range"] { width: 100%; accent-color: var(--accent); min-height: 28px; touch-action: pan-y; }
input[type="checkbox"] { accent-color: var(--accent); width: 16px; height: 16px; }

.field { display: flex; flex-direction: column; gap: 4px; margin: 0 0 10px; min-width: 0; }
.field.compact { margin: 0; }
.field-label { font-size: 12px; color: var(--muted); font-weight: 550; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.inline { display: flex; gap: 6px; }
.inline input { flex: 1 1 auto; min-width: 0; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; cursor: pointer; min-height: 28px; }
.hint { margin: 4px 0 8px; font-size: 12px; color: var(--muted); }

/* ---------- menus ---------- */

.menu { position: relative; }
.menu > summary { list-style: none; }
.menu-pop {
  position: absolute; top: calc(100% + 4px); left: 0; z-index: 30;
  display: flex; flex-direction: column; min-width: 200px; padding: 6px;
  background: var(--panel); border: 1px solid var(--line-strong); border-radius: 8px; box-shadow: var(--shadow);
}
.menu-pop.menu-right { left: auto; right: 0; }
.menu-pop a, .menu-pop button {
  display: block; text-align: left; padding: 8px 10px; border-radius: 6px;
  background: none; border: 0; color: var(--text); font: inherit; font-size: 13px; text-decoration: none; cursor: pointer;
}
.menu-pop a:hover, .menu-pop button:hover { background: var(--panel-2); }
.menu-check { display: flex; align-items: center; gap: 6px; padding: 8px 10px; font-size: 13px; border-top: 1px solid var(--line); margin-top: 4px; }

/* ---------- workspace ---------- */

.boot-error {
  margin: 8px 12px 0; padding: 10px 12px; border-radius: 8px;
  background: var(--panel); border: 1px solid var(--danger); color: var(--danger);
}

.workspace {
  display: grid;
  grid-template-columns: 232px minmax(0, 1fr) 312px;
  grid-template-areas: "library stage inspector";
  gap: 10px;
  padding: 10px;
  min-height: 0;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  overflow: auto;
  min-height: 0;
  overscroll-behavior: contain;
}
.library { grid-area: library; }
.inspector { grid-area: inspector; padding: 4px 10px; }
.stage { grid-area: stage; display: flex; flex-direction: column; gap: 8px; min-height: 0; min-width: 0; }

.panel-title { margin: 2px 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.subpanel { padding: 10px 0; border-bottom: 1px solid var(--line); }
.subpanel:last-child { border-bottom: 0; }
.subpanel h3 { margin: 0 0 8px; font-size: 13px; font-weight: 650; }
.subpanel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.subpanel-head h3 { margin: 0; }
.library .subpanel { border-top: 1px solid var(--line); border-bottom: 0; margin-top: 4px; }

/* ---------- preset library ---------- */

.preset-list { list-style: none; margin: 0 0 10px; padding: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.preset-btn {
  width: 100%; display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 8px 4px 6px; min-height: 72px;
  border: 1px solid var(--line); border-radius: 8px; background: var(--bg);
  color: var(--text); font: inherit; font-size: 12px; font-weight: 550; cursor: pointer; text-align: center;
}
.preset-btn:hover { border-color: var(--line-strong); background: var(--panel-2); }
.preset-btn[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent); }
.preset-btn svg { width: 34px; height: 34px; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
.preset-btn[aria-pressed="true"] svg { color: var(--accent); }

.reference .ref-preview { display: flex; align-items: center; gap: 8px; margin: 8px 0; min-height: 40px; }
.reference img { width: 64px; height: 48px; object-fit: contain; background: var(--bg-2); border-radius: 6px; border: 1px solid var(--line); }
.ref-name { font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }

/* ---------- viewport ---------- */

.viewport {
  position: relative;
  flex: 1 1 auto;
  min-height: 280px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: radial-gradient(ellipse at 50% 35%, var(--view-bg-2), var(--view-bg) 70%);
  overflow: hidden;
}
.outline-canvas, .view-canvas-host, .view-canvas-host canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.view-canvas-host canvas { display: block; touch-action: none; outline: none; }
.view-canvas-host canvas:focus-visible { outline: 2px solid var(--focus); outline-offset: -2px; }
.viewport[data-preview="on"] .outline-canvas { display: none; }

.ref-overlay {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain;
  pointer-events: none; mix-blend-mode: normal;
}

.preview-gate {
  position: absolute; left: 50%; bottom: 18px; transform: translateX(-50%);
  width: min(440px, calc(100% - 24px));
  padding: 12px 14px; border-radius: 10px;
  background: var(--panel);
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  border: 1px solid var(--line); box-shadow: var(--shadow);
  text-align: center;
}
.gate-title { margin: 0 0 2px; font-weight: 650; }
.gate-text { margin: 0 0 10px; font-size: 12px; color: var(--muted); }
.gate-error { margin: 8px 0 0; font-size: 12px; color: var(--danger); }

.view-tools { position: absolute; top: 8px; right: 8px; display: flex; gap: 6px; }
.view-tools .btn { background: var(--panel); background: color-mix(in srgb, var(--panel) 90%, transparent); }

.view-stats {
  position: absolute; left: 10px; top: 8px;
  font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums;
  pointer-events: none;
}

/* ---------- timeline ---------- */

.timeline {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 8px 10px; border-radius: 10px;
  background: var(--panel); border: 1px solid var(--line);
}
.timeline select { width: auto; min-width: 140px; }
.timeline input[type="range"] { flex: 1 1 160px; width: auto; }
.time-label { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; min-width: 8.5em; }
.timeline-hint { flex-basis: 100%; margin: 0; }
.timeline-hint:empty { display: none; }

.status-line { margin: 0; min-height: 1.4em; font-size: 12px; color: var(--muted); padding: 0 4px; }
.status-line[data-kind="error"] { color: var(--danger); }
.status-line[data-kind="ok"] { color: var(--ok); }

/* ---------- inspector ---------- */

.param-row { margin-bottom: 10px; }
.param-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.param-head label { font-size: 12px; font-weight: 550; }
.param-unit { font-size: 11px; color: var(--muted); }
.param-controls { display: grid; grid-template-columns: minmax(0, 1fr) 82px; gap: 8px; align-items: center; }
.param-controls input[type="number"] { min-height: 28px; padding: 2px 6px; }

.color-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.color-field { display: flex; flex-direction: column; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); cursor: pointer; }
.color-field input[type="color"] {
  width: 100%; height: 34px; padding: 2px; border: 1px solid var(--line-strong); border-radius: 6px; background: var(--bg); cursor: pointer;
}

.part-list { list-style: none; margin: 0 0 10px; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.part-item { display: flex; align-items: center; gap: 4px; }
.part-pick {
  flex: 1 1 auto; text-align: left; min-height: 30px; padding: 4px 8px;
  border: 1px solid transparent; border-radius: 6px; background: none; color: var(--text); font: inherit; font-size: 13px; cursor: pointer;
}
.part-pick:hover { background: var(--panel-2); }
.part-pick[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); }
.part-pick .part-id { color: var(--muted); font-size: 11px; margin-left: 6px; }
.part-item[data-hidden="true"] .part-pick { color: var(--muted); text-decoration: line-through; }
.part-eye {
  width: 32px; min-height: 30px; border: 1px solid var(--line); border-radius: 6px;
  background: var(--bg); color: var(--muted); cursor: pointer; display: inline-flex; align-items: center; justify-content: center;
}
.part-eye svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.part-eye[aria-pressed="false"] { color: var(--text); }

.part-inspector { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px 10px; margin: 0; }
.part-inspector legend { padding: 0 4px; font-size: 13px; font-weight: 650; }
.part-inspector:disabled { opacity: .6; }
.vec-field { display: grid; grid-template-columns: 66px repeat(3, minmax(0, 1fr)); gap: 4px; align-items: center; margin: 6px 0; }
.vec-field input { min-height: 28px; padding: 2px 4px; text-align: right; }

.stats { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; margin: 0 0 8px; font-size: 12px; }
.stats dt { color: var(--muted); }
.stats dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.validation { list-style: none; margin: 0; padding: 0; font-size: 12px; }
.validation li { padding: 3px 0 3px 18px; position: relative; }
.validation li::before { content: ""; position: absolute; left: 2px; top: 8px; width: 8px; height: 8px; border-radius: 50%; }
.validation li[data-kind="ok"]::before { background: var(--ok); }
.validation li[data-kind="warning"]::before { background: var(--warn); }
.validation li[data-kind="error"] { color: var(--danger); }
.validation li[data-kind="error"]::before { background: var(--danger); }

.steps { margin: 0 0 6px; padding-left: 18px; font-size: 12px; }
.steps li { margin-bottom: 4px; }

/* ---------- dialog ---------- */

.dialog {
  width: min(480px, calc(100vw - 24px));
  max-height: calc(100dvh - 40px);
  border: 1px solid var(--line-strong); border-radius: 12px;
  background: var(--panel); color: var(--text); padding: 14px 16px; box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgba(20, 18, 14, .45); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; }
.dialog h2 { margin: 0; font-size: 16px; }
.dialog h3 { margin: 14px 0 6px; font-size: 13px; color: var(--muted); }
.slot-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 4px; max-height: 40vh; overflow: auto; }
.slot { display: flex; align-items: center; gap: 6px; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; }
.slot-info { flex: 1 1 auto; min-width: 0; }
.slot-name { font-weight: 600; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.slot-meta { font-size: 11px; color: var(--muted); }

/* ---------- responsive ---------- */

@media (max-width: 1180px) {
  .workspace { grid-template-columns: 200px minmax(0, 1fr) 280px; }
  .sitenav a { padding: 6px 6px; }
}

@media (max-width: 980px) {
  .sitenav { order: 3; }
  .toolbar { order: 4; flex-basis: 100%; justify-content: flex-start; }
  .workspace {
    grid-template-columns: minmax(0, 1fr) 280px;
    grid-template-areas: "stage inspector" "library inspector";
    grid-template-rows: minmax(360px, 1fr) auto;
    overflow: auto;
  }
  .library { overflow: visible; }
  .preset-list { grid-template-columns: repeat(4, 1fr); }
}

@media (max-width: 720px) {
  .ws { height: auto; min-height: 100dvh; }
  body { font-size: 15px; }
  .brand-site { display: none; }
  .sitenav { flex-basis: 100%; order: 3; overflow-x: auto; }
  .topright { margin-left: auto; }
  .save-state { min-width: 0; flex-basis: 100%; }
  .workspace {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: "stage" "library" "inspector";
    grid-template-rows: auto;
    padding: 8px; gap: 8px;
  }
  .panel { overflow: visible; }
  .viewport { flex: none; height: 58vh; height: 58svh; min-height: 260px; }
  .preset-list { display: flex; overflow-x: auto; gap: 6px; padding-bottom: 4px; scroll-snap-type: x proximity; }
  .preset-list li { flex: 0 0 84px; scroll-snap-align: start; }
  .btn { min-height: 40px; }
  .btn.small { min-height: 36px; }
  input[type="text"], input[type="number"], select { min-height: 40px; font-size: 16px; }
  .param-controls { grid-template-columns: minmax(0, 1fr) 92px; }
  .param-controls input[type="number"] { min-height: 36px; }
  .vec-field input { min-height: 36px; }
  .part-pick, .part-eye { min-height: 38px; }
  .part-eye { width: 42px; }
  .timeline input[type="range"] { flex-basis: 100%; order: 5; }
  .preview-gate { bottom: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  .btn:active:not(:disabled) { transform: none; }
}
