:root {
  --bg: #f4f1ec;
  --surface: #fffdfa;
  --ink: #23201c;
  --muted: #6a645b;
  --line: #e0d9cd;
  --accent: #37564a;
  --accent-dark: #2a4338;
  --accent-soft: #e3ebe5;
  --danger: #9b3d2f;
  --stage: #171614;
  --radius: 10px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 var(--sans);
}

h1, h2 { font-family: var(--serif); font-weight: 600; margin: 0; letter-spacing: -0.01em; }
h1 { font-size: clamp(2rem, 5vw, 2.75rem); line-height: 1.1; }
h2 { font-size: 1.2rem; }
p { margin: 0; }

.hint { color: var(--muted); font-size: 0.875rem; }

/* Buttons */
.btn {
  appearance: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 0.4em;
  min-height: 44px; padding: 0 1.1em;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface); color: var(--ink);
  font: inherit; font-weight: 550; text-decoration: none; white-space: nowrap;
  cursor: pointer;
  transition: background-color 0.15s, border-color 0.15s, transform 0.1s;
}
.btn:hover { border-color: #c9c0b1; background: #fff; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-dark); border-color: var(--accent-dark); }
.btn.small { min-height: 38px; padding: 0 0.85em; font-size: 0.9rem; }
.btn.icon { width: 44px; padding: 0; font-size: 1.5rem; line-height: 1; }
.btn.danger { color: var(--danger); }
.btn.danger.armed { background: var(--danger); border-color: var(--danger); color: #fff; }
.link {
  appearance: none; border: 0; background: none; padding: 0; margin-top: 0.4rem;
  font: inherit; font-size: 0.875rem; color: var(--accent); text-decoration: underline; cursor: pointer;
}
:focus-visible { outline: 3px solid #7fa391; outline-offset: 2px; }

input[type="text"], input[type="number"] {
  min-height: 44px; padding: 0 0.75em;
  border: 1px solid var(--line); border-radius: 8px;
  background: #fff; color: var(--ink); font: inherit;
}
input.num { width: 4em; min-height: 38px; padding: 0 0.5em; text-align: center; }

/* Home */
.home { min-height: 100vh; display: grid; place-items: center; padding: 2rem 1.25rem; }
.home-card { width: 100%; max-width: 34rem; display: grid; gap: 1.5rem; }
.lede { font-size: 1.125rem; color: #45403a; }
.new-form { display: grid; gap: 0.5rem; }
.new-form label { font-weight: 600; }
.new-row { display: flex; gap: 0.6rem; }
.new-row input { flex: 1; min-width: 0; }
.steps { margin: 0; padding-left: 1.3rem; color: #45403a; display: grid; gap: 0.35rem; }
.recent h2 { font-size: 1.05rem; margin-bottom: 0.5rem; }
.recent ul { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--line); }
.recent li { border-bottom: 1px solid var(--line); }
.recent a { display: flex; justify-content: space-between; gap: 1rem; padding: 0.7rem 0; color: var(--ink); text-decoration: none; }
.recent a:hover { color: var(--accent); }
.recent a span:last-child { color: var(--muted); font-size: 0.875rem; white-space: nowrap; }

/* App bar */
.bar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 1rem;
  padding: 0.6rem 1.25rem;
  background: rgba(244, 241, 236, 0.94); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { font-family: var(--serif); font-weight: 600; font-size: 1.1rem; color: var(--accent); text-decoration: none; white-space: nowrap; }
.title {
  flex: 1; min-width: 0;
  border-color: transparent !important; background: transparent !important;
  font-family: var(--serif) !important; font-size: 1.15rem !important; font-weight: 600;
  text-overflow: ellipsis;
}
.title:hover { border-color: var(--line) !important; }
.title:focus { background: #fff !important; border-color: var(--line) !important; }
.bar-actions { display: flex; gap: 0.5rem; }

/* Work area */
.work {
  display: grid; grid-template-columns: minmax(0, 1fr) 21.5rem; gap: 1.25rem;
  padding: 1.25rem; max-width: 1500px; margin: 0 auto; align-items: start;
}
.stage-col { display: grid; gap: 0.75rem; min-width: 0; }
.stage-wrap {
  scroll-margin-top: 5rem;
  position: relative; display: grid; place-items: center;
  background: var(--stage); border-radius: var(--radius); overflow: hidden;
  min-height: 16rem; padding: 0.75rem;
}
#stage {
  display: block; max-width: 100%; max-height: 62vh; width: auto; height: auto;
  background: #000; touch-action: none; cursor: grab;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.08);
}
#stage.dragging { cursor: grabbing; }
#stage.playing { cursor: default; }
.stage-empty {
  position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 0.75rem;
  padding: 1.5rem; text-align: center; color: #d9d3c8; background: var(--stage);
}
.stage-empty-title { font-family: var(--serif); font-size: 1.5rem; color: #fff; }
.stage-empty .hint { color: #a39c90; max-width: 26rem; }

.transport { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.pos { color: var(--muted); font-size: 0.9rem; margin-left: 0.4rem; font-variant-numeric: tabular-nums; }
.hint-drag { margin-left: auto; }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.1rem 1.2rem; display: grid; gap: 1.1rem; }
.field { display: grid; gap: 0.45rem; }
.label { font-weight: 600; font-size: 0.925rem; }
.label output { font-weight: 400; color: var(--muted); margin-left: 0.35em; font-variant-numeric: tabular-nums; }
.row { display: flex; align-items: center; gap: 0.6rem; }
.row.wrap { flex-wrap: wrap; }
.row input[type="range"] { flex: 1; min-width: 0; accent-color: var(--accent); height: 38px; }

.seg { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.seg button {
  appearance: none; min-height: 38px; padding: 0 0.8em;
  border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--ink);
  font: inherit; font-size: 0.9rem; cursor: pointer;
  transition: background-color 0.15s, border-color 0.15s;
}
.seg button:hover { border-color: #c9c0b1; }
.seg button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }

.radio { display: flex; align-items: center; flex-wrap: wrap; gap: 0.45rem; font-size: 0.95rem; }
.radio input[type="radio"] { width: 1.15rem; height: 1.15rem; margin: 0; accent-color: var(--accent); }

.summary { padding-top: 0.9rem; border-top: 1px solid var(--line); font-weight: 600; }

/* Photos */
.photos { padding: 0 1.25rem 4rem; max-width: 1500px; margin: 0 auto; display: grid; gap: 0.75rem; }
.photos-head { display: flex; align-items: center; gap: 0.5rem 1rem; flex-wrap: wrap; }
.photos-head .hint { flex: 1; min-width: 14rem; }
.photos-actions { display: flex; gap: 0.5rem; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(112px, 1fr)); gap: 8px; }
.tile {
  position: relative; appearance: none; padding: 0; border: 0; border-radius: 6px; overflow: visible;
  aspect-ratio: 1; background: #ddd6ca; cursor: pointer;
}
.tile img { display: block; width: 100%; height: 100%; object-fit: cover; border-radius: 6px; pointer-events: none; }
.tile .num {
  position: absolute; left: 5px; top: 5px; min-width: 1.6em; padding: 0 0.4em;
  border-radius: 999px; background: rgba(20, 19, 17, 0.72); color: #fff;
  font-size: 0.75rem; line-height: 1.6; text-align: center; font-variant-numeric: tabular-nums;
}
.tile .own {
  position: absolute; right: 5px; top: 5px; padding: 0 0.5em;
  border-radius: 999px; background: rgba(255, 253, 250, 0.9); color: var(--ink);
  font-size: 0.7rem; line-height: 1.7;
}
.tile.sel { outline: 3px solid var(--accent); outline-offset: 2px; }
.tile.now { outline: 3px solid #c9a24a; outline-offset: 2px; }
.tile.dragging { opacity: 0.35; }
.tile.drop-before::before, .tile.drop-after::after {
  content: ""; position: absolute; top: 0; bottom: 0; width: 4px; border-radius: 2px; background: var(--accent);
}
.tile.drop-before::before { left: -6px; }
.tile.drop-after::after { right: -6px; }

.upload-bar { display: flex; align-items: center; gap: 0.75rem; font-size: 0.9rem; color: var(--muted); }
.progress { flex: 1; height: 8px; border-radius: 999px; background: var(--line); overflow: hidden; }
.progress > div { height: 100%; width: 0; background: var(--accent); border-radius: 999px; transition: width 0.2s; }
.progress.big { height: 12px; }
.upload-errors { margin: 0; padding: 0.7rem 1rem 0.7rem 2rem; border-radius: 8px; background: #f6e7e2; color: #6d2a20; font-size: 0.9rem; }

.drop-veil {
  position: fixed; inset: 0; z-index: 50; display: grid; place-items: center;
  background: rgba(42, 67, 56, 0.88); color: #fff; font-family: var(--serif); font-size: 1.75rem; pointer-events: none;
}

/* Dialog */
.dlg { width: min(34rem, calc(100vw - 2rem)); border: 0; border-radius: 14px; padding: 1.5rem; background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3); }
.dlg::backdrop { background: rgba(23, 22, 20, 0.6); }
.dlg > div { display: grid; gap: 0.9rem; }
.dlg video { width: 100%; max-height: 50vh; background: #000; border-radius: 8px; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 0.4rem; }

.toast {
  position: fixed; left: 50%; bottom: 1.5rem; transform: translateX(-50%); z-index: 60;
  padding: 0.7rem 1.1rem; border-radius: 999px; background: var(--ink); color: #fff; font-size: 0.95rem;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25); max-width: calc(100vw - 2rem); text-align: center;
}

@media (max-width: 1000px) {
  .work { grid-template-columns: minmax(0, 1fr); }
  #stage { max-height: 50vh; }
}
@media (max-width: 640px) {
  .bar { flex-wrap: wrap; gap: 0.4rem 0.75rem; padding: 0.5rem 0.9rem; }
  .title { order: 3; flex-basis: 100%; }
  .bar-actions { margin-left: auto; }
  .work { padding: 0.9rem; }
  .photos { padding: 0 0.9rem 3rem; }
  .hint-drag { display: none; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(88px, 1fr)); gap: 6px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
