@font-face {
  font-family: 'Fraunces';
  src: url('/fonts/fraunces-var.woff2') format('woff2');
  font-weight: 300 900;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: 'Archivo';
  src: url('/fonts/archivo-var.woff2') format('woff2');
  font-weight: 400 700;
  font-style: normal;
  font-display: swap;
}

:root {
  --ground: #16120f;
  --ground-rise: #1c1712;
  --surface: #221c17;
  --surface-raised: #2c241d;
  --ink: #f3ece2;
  --ink-muted: #a99c8c;
  --ink-faint: #776a5c;
  --line: rgba(243, 236, 226, 0.14);
  --line-strong: rgba(243, 236, 226, 0.26);
  --story: #c23a26;
  --story-ink: #ffe4dc;
  --knowledge: #d2a24c;
  --knowledge-ink: #2a1f0e;
  --danger: #d2574a;
  --shadow: rgba(8, 6, 4, 0.5);
  --overlay: rgba(10, 8, 6, 0.72);
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --ground: #f4efe4;
    --ground-rise: #ece4d4;
    --surface: #ffffff;
    --surface-raised: #fbf6ec;
    --ink: #211a13;
    --ink-muted: #6b5d4d;
    --ink-faint: #948572;
    --line: rgba(33, 26, 19, 0.14);
    --line-strong: rgba(33, 26, 19, 0.26);
    --story: #a52e1c;
    --story-ink: #fff6f3;
    --knowledge: #8f6a28;
    --knowledge-ink: #fff9ec;
    --danger: #b23a2c;
    --shadow: rgba(33, 26, 19, 0.18);
    --overlay: rgba(33, 26, 19, 0.45);
  }
}
:root[data-theme="light"] {
  --ground: #f4efe4;
  --ground-rise: #ece4d4;
  --surface: #ffffff;
  --surface-raised: #fbf6ec;
  --ink: #211a13;
  --ink-muted: #6b5d4d;
  --ink-faint: #948572;
  --line: rgba(33, 26, 19, 0.14);
  --line-strong: rgba(33, 26, 19, 0.26);
  --story: #a52e1c;
  --story-ink: #fff6f3;
  --knowledge: #8f6a28;
  --knowledge-ink: #fff9ec;
  --danger: #b23a2c;
  --shadow: rgba(33, 26, 19, 0.18);
  --overlay: rgba(33, 26, 19, 0.45);
}

* { box-sizing: border-box; }
html { color-scheme: light dark; }
body {
  background: var(--ground);
  color: var(--ink);
  font-family: 'Archivo', system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  margin: 0;
}
h1, h2, h3 { font-family: 'Fraunces', Georgia, serif; font-weight: 600; text-wrap: balance; margin: 0; }
p { margin: 0; }
button { font-family: 'Archivo', sans-serif; cursor: pointer; border: none; background: none; color: inherit; }
a { color: inherit; }
input, textarea, select {
  font-family: 'Archivo', sans-serif;
  color: var(--ink);
  background: var(--ground-rise);
  border: 1px solid var(--line-strong);
  border-radius: 0.5rem;
  padding: 0.6rem 0.75rem;
  font-size: 0.95rem;
  width: 100%;
}
input:focus-visible, textarea:focus-visible, select:focus-visible, button:focus-visible, [tabindex]:focus-visible {
  outline: 2px solid var(--story);
  outline-offset: 2px;
}
::selection { background: color-mix(in srgb, var(--story) 40%, transparent); }

.eyebrow { font-size: 0.72rem; font-weight: 700; letter-spacing: 0.12em; text-transform: uppercase; color: var(--ink-faint); }
.eyebrow.story { color: var(--story); }
.eyebrow.knowledge { color: var(--knowledge); }

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

.wrap { max-width: 82rem; margin: 0 auto; padding: 0 clamp(1.1rem, 3vw, 2.25rem); }

.toolbar {
  position: sticky; top: 0; z-index: 30;
  background: color-mix(in srgb, var(--ground) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.toolbar .wrap { display: flex; align-items: center; justify-content: space-between; height: 3.9rem; gap: 1rem; }
.brand { display: flex; align-items: baseline; gap: 0.45rem; font-weight: 700; font-size: 0.92rem; letter-spacing: 0.04em; }
.brand .dim { color: var(--ink-faint); font-weight: 500; }
.toolbar-hint { color: var(--ink-faint); font-size: 0.82rem; display: none; }
@media (min-width: 760px) { .toolbar-hint { display: block; } }
.btn-reset {
  display: inline-flex; align-items: center; gap: 0.4rem;
  font-size: 0.85rem; font-weight: 600; color: var(--ink-muted);
  border: 1px solid var(--line-strong); border-radius: 999px;
  padding: 0.45rem 0.9rem;
  transition: color .15s ease, border-color .15s ease;
}
.btn-reset:hover { color: var(--ink); border-color: var(--ink-muted); }
.btn-reset svg { width: 0.95rem; height: 0.95rem; }
.save-indicator { font-size: 0.78rem; color: var(--ink-faint); min-width: 5.5rem; text-align: right; }

main { padding: clamp(2rem, 4vw, 3rem) 0 5rem; }

/* ---------- rail ---------- */

.rail { margin-bottom: clamp(2.25rem, 4vw, 3rem); }
.rail.drag-over-rail { outline: 2px dashed var(--line-strong); outline-offset: 6px; border-radius: 1rem; }

.rail-head { display: flex; align-items: center; gap: 0.65rem; margin-bottom: 1rem; padding-bottom: 0.1rem; }
.rail-handle {
  cursor: grab; color: var(--ink-faint); width: 1.4rem; height: 1.4rem;
  display: flex; align-items: center; justify-content: center; border-radius: 0.4rem; flex-shrink: 0;
}
.rail-handle:hover { color: var(--ink-muted); background: var(--surface); }
.rail-handle:active { cursor: grabbing; }
.rail-accent-dot { width: 0.55rem; height: 0.55rem; border-radius: 999px; flex-shrink: 0; }
.rail-accent-dot.story { background: var(--story); }
.rail-accent-dot.knowledge { background: var(--knowledge); }
.rail-title {
  font-family: 'Fraunces', serif; font-size: 1.35rem; font-weight: 600;
  border-radius: 0.35rem; padding: 0.1rem 0.35rem; margin: -0.1rem -0.35rem; outline: none;
}
.rail-title:hover { background: var(--surface); }
.rail-title:focus { background: var(--surface); box-shadow: 0 0 0 2px var(--line-strong); }
.rail-count { color: var(--ink-faint); font-size: 0.85rem; margin-left: 0.1rem; }
.rail-del {
  margin-left: auto; color: var(--ink-faint); width: 1.7rem; height: 1.7rem;
  display: flex; align-items: center; justify-content: center; border-radius: 999px; flex-shrink: 0;
}
.rail-del:hover { color: var(--danger); background: var(--surface); }
.rail-del svg { width: 0.9rem; height: 0.9rem; }

.rail-track { display: flex; gap: 1rem; overflow-x: auto; padding: 0.3rem 0.3rem 1rem; scroll-snap-type: x proximity; }
.rail-track::-webkit-scrollbar { height: 8px; }
.rail-track::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 999px; }

/* ---------- card ---------- */

.card {
  position: relative; flex: 0 0 15.5rem; scroll-snap-align: start;
  background: var(--surface); border: 1px solid var(--line); border-radius: 0.85rem; overflow: hidden;
  display: flex; flex-direction: column;
  transition: transform .15s ease, border-color .15s ease, box-shadow .15s ease, opacity .15s ease;
}
.card:hover { border-color: var(--line-strong); box-shadow: 0 16px 30px -18px var(--shadow); }
.card.is-dragging { opacity: 0.35; }
.card.drop-left { box-shadow: inset 3px 0 0 0 var(--story); }
.card.drop-right { box-shadow: inset -3px 0 0 0 var(--story); }

.card-art {
  aspect-ratio: 4 / 3; background-size: cover; background-position: center; position: relative;
  display: flex; align-items: center; justify-content: center;
}
.card-art.knowledge-accent { border-bottom: 3px solid var(--knowledge); }
.card-art.placeholder { background: linear-gradient(155deg, var(--ground-rise), var(--surface-raised)); }
.card-art.placeholder svg { width: 46%; opacity: 0.35; }

.card-toolbar {
  position: absolute; top: 0.5rem; right: 0.5rem; z-index: 2; display: flex; gap: 0.3rem;
  opacity: 0; transform: translateY(-3px); transition: opacity .15s ease, transform .15s ease;
}
.card:hover .card-toolbar, .card:focus-within .card-toolbar { opacity: 1; transform: translateY(0); }
.icon-btn {
  width: 1.9rem; height: 1.9rem; border-radius: 999px; display: flex; align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--ground) 78%, transparent); color: var(--ink); backdrop-filter: blur(4px);
}
.icon-btn:hover { background: var(--ground); }
.icon-btn svg { width: 0.95rem; height: 0.95rem; }

.drag-handle {
  position: absolute; top: 0.5rem; left: 0.5rem; z-index: 2; width: 1.9rem; height: 1.9rem; border-radius: 999px;
  display: flex; align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--ground) 78%, transparent); color: var(--ink-muted); cursor: grab;
  opacity: 0; transition: opacity .15s ease; backdrop-filter: blur(4px);
}
.card:hover .drag-handle, .card:focus-within .drag-handle { opacity: 1; }
.drag-handle:active { cursor: grabbing; }
.drag-handle svg { width: 1rem; height: 1rem; }

.card-body { padding: 0.9rem 0.95rem 1.05rem; display: flex; flex-direction: column; gap: 0.4rem; flex: 1; }
.card-label { font-size: 0.68rem; font-weight: 700; letter-spacing: 0.09em; text-transform: uppercase; }
.card-label.story { color: var(--story); }
.card-label.knowledge { color: var(--knowledge); }
.card-title { font-family: 'Fraunces', serif; font-size: 1.05rem; line-height: 1.2; font-weight: 600; }
.card-desc { color: var(--ink-muted); font-size: 0.85rem; line-height: 1.4; }
.card-link {
  margin-top: auto; padding-top: 0.5rem; font-size: 0.78rem; font-weight: 600; color: var(--ink-faint);
  text-decoration: none; display: inline-flex; align-items: center; gap: 0.3rem; width: fit-content;
}
.card-link:hover { color: var(--ink); }

.add-tile {
  flex: 0 0 15.5rem; aspect-ratio: 4 / 3; align-self: flex-start;
  border: 1.5px dashed var(--line-strong); border-radius: 0.85rem;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0.5rem;
  color: var(--ink-faint); transition: border-color .15s ease, color .15s ease, background .15s ease;
}
.add-tile:hover { border-color: var(--story); color: var(--ink); background: var(--surface); }
.add-tile svg { width: 1.5rem; height: 1.5rem; }
.add-tile span { font-size: 0.85rem; font-weight: 600; }

.add-rail-btn {
  display: flex; align-items: center; justify-content: center; gap: 0.5rem; width: 100%; padding: 1.1rem;
  border: 1.5px dashed var(--line-strong); border-radius: 0.85rem; color: var(--ink-faint);
  font-weight: 600; font-size: 0.92rem; transition: border-color .15s ease, color .15s ease;
}
.add-rail-btn:hover { border-color: var(--story); color: var(--ink); }
.add-rail-btn svg { width: 1.05rem; height: 1.05rem; }

/* ---------- modal ---------- */

.modal-overlay {
  position: fixed; inset: 0; background: var(--overlay); display: flex; align-items: center; justify-content: center;
  padding: 1.5rem; z-index: 100; animation: fadeIn .15s ease both;
}
.modal-overlay[hidden] { display: none; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.modal {
  background: var(--surface); border: 1px solid var(--line-strong); border-radius: 1rem;
  width: 100%; max-width: 26rem; max-height: 90vh; overflow-y: auto; padding: 1.5rem;
  animation: riseIn .18s cubic-bezier(.22,.61,.36,1) both;
}
@keyframes riseIn { from { opacity: 0; transform: translateY(10px) scale(.98); } to { opacity: 1; transform: translateY(0) scale(1); } }

.modal h2 { font-size: 1.2rem; margin-bottom: 1.1rem; }
.field { margin-bottom: 1rem; }
.field label { display: block; font-size: 0.78rem; font-weight: 700; letter-spacing: 0.04em; color: var(--ink-muted); margin-bottom: 0.4rem; text-transform: uppercase; }
.field textarea { min-height: 4.5rem; resize: vertical; }

.image-drop {
  position: relative; aspect-ratio: 16 / 10; border-radius: 0.65rem; border: 1.5px dashed var(--line-strong);
  background: var(--ground-rise); display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 0.4rem; color: var(--ink-faint); overflow: hidden; cursor: pointer; background-size: cover; background-position: center;
}
.image-drop:hover { border-color: var(--story); }
.image-drop svg { width: 1.5rem; height: 1.5rem; }
.image-drop span { font-size: 0.82rem; font-weight: 600; }
.image-drop.has-image span, .image-drop.has-image svg.upload-icon { display: none; }
.image-drop.is-uploading span { display: block; }
.image-remove {
  position: absolute; top: 0.5rem; right: 0.5rem; background: color-mix(in srgb, var(--ground) 80%, transparent);
  color: var(--ink); width: 1.8rem; height: 1.8rem; border-radius: 999px; display: none; align-items: center; justify-content: center;
}
.image-drop.has-image .image-remove { display: flex; }
.image-remove svg { width: 0.85rem; height: 0.85rem; }

.url-row { display: flex; gap: 0.5rem; margin-top: 0.5rem; }
.url-row input { flex: 1; font-size: 0.85rem; padding: 0.5rem 0.65rem; }
.url-row .btn { padding: 0.5rem 0.9rem; font-size: 0.85rem; flex-shrink: 0; }

.modal-actions { display: flex; align-items: center; gap: 0.75rem; margin-top: 1.3rem; }
.btn { font-weight: 600; font-size: 0.92rem; padding: 0.7rem 1.1rem; border-radius: 0.55rem; transition: opacity .15s ease, transform .15s ease; }
.btn:active { transform: scale(.98); }
.btn-primary { background: var(--story); color: var(--story-ink); margin-left: auto; }
.btn-primary:hover { opacity: 0.92; }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-secondary { color: var(--ink-muted); }
.btn-secondary:hover { color: var(--ink); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { opacity: 0.8; }

.accent-picker { display: flex; gap: 0.6rem; }
.accent-opt {
  flex: 1; display: flex; align-items: center; justify-content: center; gap: 0.5rem;
  border: 1.5px solid var(--line-strong); border-radius: 0.55rem; padding: 0.6rem; font-size: 0.85rem; font-weight: 600; color: var(--ink-muted);
  position: relative;
}
.accent-opt input { position: absolute; opacity: 0; width: 0; height: 0; }
.accent-opt .swatch { width: 0.7rem; height: 0.7rem; border-radius: 999px; }
.accent-opt .swatch.story { background: var(--story); }
.accent-opt .swatch.knowledge { background: var(--knowledge); }
.accent-opt:has(input:checked) { border-color: var(--ink-muted); color: var(--ink); background: var(--ground-rise); }

@media (prefers-reduced-motion: reduce) {
  .modal-overlay, .modal, .card, .card-toolbar, .drag-handle { animation: none; transition: none; }
}
@media (max-width: 560px) {
  .card, .add-tile { flex-basis: 12.5rem; }
}

/* ---------- story (article) page ---------- */

.story-body { background: var(--ground); }
.story-nav { padding: 1.5rem clamp(1.1rem, 3vw, 2.25rem) 0; }
.back-link { font-size: 0.88rem; font-weight: 600; color: var(--ink-muted); text-decoration: none; }
.back-link:hover { color: var(--ink); }
.story-wrap { max-width: 42rem; margin: 0 auto; padding: 2rem clamp(1.1rem, 3vw, 2.25rem) 5rem; display: flex; flex-direction: column; gap: 1rem; }
.story-hero {
  aspect-ratio: 16 / 9; border-radius: 1rem; background-size: cover; background-position: center;
  margin-bottom: 0.5rem; border: 1px solid var(--line);
}
.story-hero--placeholder { background: linear-gradient(155deg, var(--ground-rise), var(--surface-raised)); }
.story-wrap h1 { font-size: clamp(1.9rem, 4vw, 2.6rem); }
.story-desc { font-size: 1.05rem; color: var(--ink-muted); line-height: 1.6; }
.story-note { font-size: 0.82rem; color: var(--ink-faint); border-top: 1px solid var(--line); padding-top: 1rem; margin-top: 0.5rem; }
