/* Tokens: see DESIGN.md for the reasoning behind each choice. */
:root {
  --paper: #f2efe8;
  --surface: #fbfaf7;
  --sunken: #e9e5dc;
  --ink: #1c1b19;
  --ink-2: #57534b;
  --line: #d6d0c4;
  --accent: #b8400f;
  --accent-ink: #ffffff;
  --accent-soft: #f6e3d9;
  --error: #a3261b;
  --checker: #e3dfd6;
  --focus: #b8400f;
  --radius: 6px;
  --font-ui: 'Instrument Sans', ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, 'SF Mono', Menlo, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --paper: #141311;
    --surface: #1d1c19;
    --sunken: #26241f;
    --ink: #eeeae2;
    --ink-2: #aaa498;
    --line: #3a3732;
    --accent: #f0804f;
    --accent-ink: #1c1b19;
    --accent-soft: #3a2519;
    --error: #ff8a7a;
    --checker: #2b2925;
    --focus: #f0804f;
    color-scheme: dark;
  }
}

:root[data-theme='dark'] {
  --paper: #141311;
  --surface: #1d1c19;
  --sunken: #26241f;
  --ink: #eeeae2;
  --ink-2: #aaa498;
  --line: #3a3732;
  --accent: #f0804f;
  --accent-ink: #1c1b19;
  --accent-soft: #3a2519;
  --error: #ff8a7a;
  --checker: #2b2925;
  --focus: #f0804f;
  color-scheme: dark;
}

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

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 400 15px/1.5 var(--font-ui);
  min-height: 100vh;
}

img {
  max-width: 100%;
  display: block;
}

h1,
h2,
h3 {
  margin: 0;
  line-height: 1.2;
  letter-spacing: -0.01em;
}

h1 {
  font-size: clamp(24px, 3vw, 32px);
  font-weight: 650;
}

h2 {
  font-size: 18px;
  font-weight: 600;
}

h3 {
  font-size: 15px;
  font-weight: 600;
}

p {
  margin: 0;
}

.mono {
  font-family: var(--font-mono);
  font-size: 12.5px;
  font-variant-numeric: tabular-nums;
}

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

.hint {
  color: var(--ink-2);
  font-size: 13.5px;
}

.error-text {
  color: var(--error);
  font-size: 13.5px;
}

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

.skip {
  position: absolute;
  left: 16px;
  top: -60px;
  background: var(--ink);
  color: var(--paper);
  padding: 10px 14px;
  border-radius: var(--radius);
  z-index: 10;
}

.skip:focus {
  top: 12px;
}

/* ---- Top bar ---------------------------------------------------------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px clamp(16px, 3vw, 32px);
  border-bottom: 1px solid var(--line);
  background: var(--paper);
  position: sticky;
  top: 0;
  z-index: 5;
}

.wordmark {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 18px;
  color: var(--ink);
  text-decoration: none;
  min-height: 44px;
  letter-spacing: -0.02em;
}

.wordmark svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: var(--accent);
  stroke-width: 3;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.library-status {
  color: var(--ink-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---- Buttons & fields ----------------------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 16px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font: 500 14.5px/1 var(--font-ui);
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  transition: background-color 120ms ease, border-color 120ms ease;
}

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

.btn[disabled],
.btn[aria-disabled='true'] {
  opacity: 0.55;
  cursor: not-allowed;
  pointer-events: none;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

.btn-primary:hover {
  border-color: var(--ink);
}

.btn-quiet {
  background: transparent;
  border-color: transparent;
}

.btn-quiet:hover {
  border-color: var(--line);
}

.btn-small {
  min-height: 36px;
  padding: 0 12px;
  font-size: 13.5px;
}

.btn-danger {
  color: var(--error);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.grow {
  flex: 1;
}

.label {
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
}

input[type='text'],
input[type='search'],
input[type='number'],
select {
  min-height: 44px;
  width: 100%;
  padding: 0 12px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font: 400 15px var(--font-ui);
}

input::placeholder {
  color: var(--ink-2);
  opacity: 0.85;
}

input[type='color'] {
  width: 44px;
  height: 44px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
  flex: none;
}

input[type='range'] {
  width: 100%;
  accent-color: var(--accent);
  min-height: 44px;
}

.check {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  cursor: pointer;
  font-size: 14.5px;
}

.check input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
  margin: 0;
}

/* Segmented control built from real radio inputs, so arrow keys work natively. */
.segmented {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--sunken);
  padding: 3px;
  gap: 3px;
  max-width: 100%;
}

.segmented label {
  position: relative;
  flex: 1;
}

.segmented input {
  position: absolute;
  opacity: 0;
  inset: 0;
  margin: 0;
  cursor: pointer;
}

.segmented span {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 38px;
  padding: 0 12px;
  border-radius: 4px;
  font-size: 14px;
  font-weight: 500;
  color: var(--ink-2);
  white-space: nowrap;
}

.segmented input:checked + span {
  background: var(--surface);
  color: var(--ink);
  box-shadow: inset 0 0 0 1px var(--line);
}

.segmented input:focus-visible + span {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
}

/* ---- Layout ------------------------------------------------------------- */

main {
  padding: clamp(20px, 3vw, 36px) clamp(16px, 3vw, 32px) 80px;
  max-width: 1480px;
  margin: 0 auto;
}

/* main takes programmatic focus on navigation for screen readers; no ring needed there. */
main:focus,
main:focus-visible {
  outline: none;
}

.page-head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 28px;
}

.page-head .sub {
  margin-top: 6px;
  color: var(--ink-2);
  max-width: 60ch;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

/* ---- Home --------------------------------------------------------------- */

.home {
  display: grid;
  grid-template-columns: minmax(280px, 380px) 1fr;
  gap: clamp(24px, 4vw, 56px);
  align-items: start;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.panel-section {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel-section + .panel-section {
  border-top: 1px solid var(--line);
  padding-top: 18px;
}

.panel-title {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-2);
}

.library-card {
  border: 1px dashed var(--accent);
  background: var(--accent-soft);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 28px;
}

.progress {
  height: 6px;
  background: var(--sunken);
  border-radius: 3px;
  overflow: hidden;
}

.progress > div {
  height: 100%;
  background: var(--accent);
  transition: width 200ms linear;
}

.set-list {
  list-style: none;
  margin: 0;
  padding: 0;
  border-top: 1px solid var(--line);
}

.set-row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 12px;
  padding: 14px 0;
  border-bottom: 1px solid var(--line);
}

.set-row a {
  color: var(--ink);
  text-decoration: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 44px;
  justify-content: center;
  min-width: 0;
}

.set-row a:hover .set-name {
  text-decoration: underline;
  text-decoration-color: var(--accent);
  text-underline-offset: 3px;
}

.set-name {
  font-weight: 600;
  font-size: 16px;
  overflow-wrap: anywhere;
}

.empty {
  padding: 32px 0;
  color: var(--ink-2);
  max-width: 48ch;
}

/* ---- Set editor ---------------------------------------------------------- */

.editor {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: clamp(20px, 3vw, 40px);
  align-items: start;
}

.editor .panel {
  position: sticky;
  top: 84px;
  max-height: calc(100vh - 100px);
  overflow-y: auto;
}

input[type='text'].name-input {
  font: 650 clamp(22px, 3vw, 30px) / 1.2 var(--font-ui);
  letter-spacing: -0.01em;
  border: 1px solid transparent;
  background: transparent;
  color: var(--ink);
  padding: 4px 8px;
  margin-left: -9px;
  border-radius: var(--radius);
  width: min(100%, 560px);
  min-height: 44px;
}

input[type='text'].name-input:hover,
input[type='text'].name-input:focus {
  border-color: var(--line);
  background: var(--surface);
}

.crumb {
  font-size: 13.5px;
  color: var(--ink-2);
  text-decoration: none;
  display: flex;
  width: max-content;
  min-height: 44px;
  align-items: center;
}

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

.row {
  display: flex;
  gap: 10px;
  align-items: flex-end;
}

.chosen-frame {
  display: grid;
  grid-template-columns: 48px 1fr;
  gap: 12px;
  align-items: center;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
}

.chosen-frame img {
  height: 72px;
  width: 48px;
  object-fit: contain;
}

.save-state {
  font-size: 12.5px;
  color: var(--ink-2);
  min-height: 18px;
}

.dropzone {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  text-align: center;
  padding: 28px 16px;
  border: 1.5px dashed var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
  margin-bottom: 24px;
  transition: border-color 120ms ease, background-color 120ms ease;
}

.dropzone:hover,
.dropzone.is-over {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.dropzone:focus-within {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.dropzone input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.dropzone strong {
  font-size: 16px;
}

.sheet-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.sheet {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 28px 20px;
}

.shot {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
}

/* The identity motif: proof-sheet crop marks around every preview. */
.proof {
  --mark: var(--ink-2);
  --len: 14px;
  position: relative;
  padding: 14px;
  background:
    linear-gradient(var(--mark), var(--mark)) left top / var(--len) 1px no-repeat,
    linear-gradient(var(--mark), var(--mark)) left top / 1px var(--len) no-repeat,
    linear-gradient(var(--mark), var(--mark)) right top / var(--len) 1px no-repeat,
    linear-gradient(var(--mark), var(--mark)) right top / 1px var(--len) no-repeat,
    linear-gradient(var(--mark), var(--mark)) left bottom / var(--len) 1px no-repeat,
    linear-gradient(var(--mark), var(--mark)) left bottom / 1px var(--len) no-repeat,
    linear-gradient(var(--mark), var(--mark)) right bottom / var(--len) 1px no-repeat,
    linear-gradient(var(--mark), var(--mark)) right bottom / 1px var(--len) no-repeat;
}

.shot.is-fresh .proof {
  --mark: var(--accent);
}

.proof-area {
  aspect-ratio: 9 / 16;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--sunken);
  overflow: hidden;
  position: relative;
}

/* Only behind transparent device-only output: shows where the PNG is see-through. */
.proof-area.checker {
  background-color: var(--surface);
  background-image:
    linear-gradient(45deg, var(--checker) 25%, transparent 25%),
    linear-gradient(-45deg, var(--checker) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--checker) 75%),
    linear-gradient(-45deg, transparent 75%, var(--checker) 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0;
}

.proof-area img {
  max-height: 100%;
  max-width: 100%;
  object-fit: contain;
}

.proof-area .source {
  max-height: 86%;
  max-width: 86%;
  opacity: 0.9;
}

.proof-tag {
  position: absolute;
  left: 8px;
  top: 8px;
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 2px 7px;
  font-size: 12px;
  font-weight: 500;
}

.proof-tag.stale {
  border-color: var(--accent);
  color: var(--ink);
}

.proof-tag.busy {
  background: var(--ink);
  color: var(--paper);
  border-color: var(--ink);
}

.shot-meta {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.shot-meta .name {
  font-weight: 600;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.shot-meta .mono {
  color: var(--ink-2);
  overflow-wrap: anywhere;
}

.shot-meta .warn {
  font-size: 12.5px;
  color: var(--ink-2);
}

.shot-fields {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.shot-fields input {
  min-height: 40px;
  font-size: 14px;
}

.shot-actions {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.shot-actions > div {
  display: flex;
  gap: 6px;
}

.shot-actions .output-actions .btn {
  flex: 1;
}

.shot-actions .order-actions .btn-danger {
  margin-left: auto;
}

.shot-actions .btn {
  min-height: 44px;
  padding: 0 10px;
  font-size: 13.5px;
}

.icon-btn {
  width: 44px;
  padding: 0 !important;
}

.icon-btn svg {
  width: 18px;
  height: 18px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.loading {
  color: var(--ink-2);
  padding: 40px 0;
}

/* ---- Frame picker -------------------------------------------------------- */

.picker {
  width: min(1100px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 24px 60px rgba(20, 18, 15, 0.28);
  overflow: hidden;
}

.picker[open] {
  display: flex;
  flex-direction: column;
}

.picker::backdrop {
  background: rgba(20, 18, 15, 0.5);
}

.picker-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  margin: 0;
}

.picker-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
  padding: 16px 20px 6px;
}

.picker > .hint {
  padding: 0 20px 10px;
}

.picker-list {
  overflow-y: auto;
  padding: 6px 20px 24px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
}

.model {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
}

.model.fits {
  border-color: var(--accent);
}

.model-thumb {
  height: 160px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.model-thumb img {
  max-height: 160px;
  object-fit: contain;
}

.fit-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
}

:root[data-theme='dark'] .fit-label {
  color: var(--accent);
}

.colours {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.colours .btn {
  min-height: 36px;
  font-size: 12.5px;
  padding: 0 10px;
}

.colours .btn[aria-pressed='true'] {
  border-color: var(--accent);
  background: var(--accent-soft);
}

/* ---- Toasts --------------------------------------------------------------- */

.toasts {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 20;
  width: min(480px, calc(100vw - 32px));
  pointer-events: none;
}

.toast {
  background: var(--ink);
  color: var(--paper);
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 14px;
  pointer-events: auto;
}

.toast.error {
  border-left: 4px solid var(--error);
}

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

@media (max-width: 900px) {
  .home,
  .editor {
    grid-template-columns: 1fr;
  }

  .editor .panel {
    position: static;
    max-height: none;
    order: 2;
  }
}

@media (max-width: 560px) {
  .library-status {
    display: none;
  }

  .sheet {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 24px 12px;
  }

  .page-head .actions,
  .page-head .actions .btn {
    width: 100%;
  }

  .segmented {
    display: flex;
    width: 100%;
  }

  .picker-list {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  }

  .model-thumb,
  .model-thumb img {
    height: 120px;
  }
}

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