/* ------------------------------------------------------------------ */
/* Tokens                                                              */
/* ------------------------------------------------------------------ */
:root {
  color-scheme: light;
  --surface-0: #f4f4f2;
  --surface-1: #fcfcfb;
  --surface-2: #ffffff;
  --border: #e3e2dd;
  --border-strong: #c9c8c2;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #7a7974;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --accent-soft: #e3eefb;
  --danger: #d03b3b;
  --danger-soft: #fbe6e6;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 8px 24px rgba(0, 0, 0, .08);
  --radius: 10px;

  /* Status hues: categorical slots in fixed order (never cycled). */
  --st-progress: #eda100;     /* slot 4 yellow  */
  --st-review: #2a78d6;       /* slot 1 blue    */
  --st-awaiting: #eb6834;     /* slot 2 orange  */
  --st-received: #1baf7a;     /* slot 3 aqua    */
  --st-hold: #7a7974;         /* neutral        */
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface-0: #121211;
    --surface-1: #1a1a19;
    --surface-2: #232322;
    --border: #2f2f2d;
    --border-strong: #454541;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #8f8e86;
    --accent: #3987e5;
    --accent-soft: #1c2f47;
    --danger: #e66767;
    --danger-soft: #3a1f1f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .5);
    --st-progress: #c98500;
    --st-review: #3987e5;
    --st-awaiting: #d95926;
    --st-received: #199e70;
    --st-hold: #8f8e86;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-0: #121211;
  --surface-1: #1a1a19;
  --surface-2: #232322;
  --border: #2f2f2d;
  --border-strong: #454541;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #8f8e86;
  --accent: #3987e5;
  --accent-soft: #1c2f47;
  --danger: #e66767;
  --danger-soft: #3a1f1f;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .5);
  --st-progress: #c98500;
  --st-review: #3987e5;
  --st-awaiting: #d95926;
  --st-received: #199e70;
  --st-hold: #8f8e86;
}

/* ------------------------------------------------------------------ */
/* Base                                                                */
/* ------------------------------------------------------------------ */
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  color: var(--text-primary);
  background: var(--surface-0);
  min-height: 100vh;
}
[hidden] { display: none !important; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: 18px; }
h2 { font-size: 16px; }
p { margin: 0 0 8px; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; border: 1px solid var(--border); }
.muted { color: var(--text-secondary); }
.small { font-size: 12px; }
.center { text-align: center; padding: 32px 16px; }
.error { color: var(--danger); font-size: 13px; }
.spacer { flex: 1; }

/* ------------------------------------------------------------------ */
/* Buttons & inputs                                                    */
/* ------------------------------------------------------------------ */
.btn {
  display: inline-flex; align-items: center; gap: 8px;
  font: inherit; font-weight: 500; font-size: 13px;
  padding: 8px 14px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--border-strong); background: var(--surface-2); color: var(--text-primary);
  white-space: nowrap; transition: background .12s, border-color .12s, opacity .12s;
}
.btn:hover { border-color: var(--text-muted); text-decoration: none; }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { filter: brightness(1.05); border-color: var(--accent); }
.btn-ghost { background: transparent; border-color: var(--border); }
.btn-danger { color: var(--danger); border-color: var(--danger); background: var(--surface-2); }
.btn-danger:not(.btn-ghost) { background: var(--danger); color: #fff; }
.btn-sm { padding: 5px 10px; font-size: 12px; border-radius: 6px; }
.btn-lg { padding: 12px 20px; font-size: 15px; border-radius: 10px; }
.btn-x { background: none; border: 0; font-size: 22px; line-height: 1; color: var(--text-muted); cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.btn-x:hover { background: var(--surface-0); color: var(--text-primary); }

.input, .field input, .field select, .field textarea {
  font: inherit; font-size: 13px; color: var(--text-primary);
  background: var(--surface-2); border: 1px solid var(--border-strong); border-radius: 8px;
  padding: 8px 10px; min-width: 0; width: 100%;
}
.input:focus, .field input:focus, .field select:focus, .field textarea:focus,
.cell-editor:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.field > span { font-size: 12px; font-weight: 500; color: var(--text-secondary); }
.field textarea { resize: vertical; line-height: 1.5; }
.inline { display: flex; gap: 8px; align-items: center; }
.inline input { flex: 1; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.two-col .span-2 { grid-column: 1 / -1; }
.toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-secondary); cursor: pointer; white-space: nowrap; }

/* ------------------------------------------------------------------ */
/* Gate                                                                */
/* ------------------------------------------------------------------ */
.gate { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.gate-card {
  width: min(420px, 100%); background: var(--surface-1); border: 1px solid var(--border);
  border-radius: 16px; padding: 32px 28px; text-align: center; box-shadow: var(--shadow);
}
.gate-card h1 { font-size: 22px; margin-bottom: 8px; }
.gate-card .btn { margin: 16px auto 8px; }
.gate-logo, .brand-mark {
  width: 40px; height: 40px; border-radius: 10px; background: var(--accent);
  display: inline-block; position: relative; margin: 0 auto 12px;
}
.gate-logo::after, .brand-mark::after {
  content: ""; position: absolute; inset: 11px 10px; border-top: 3px solid #fff; border-bottom: 3px solid #fff;
}

/* ------------------------------------------------------------------ */
/* Layout                                                              */
/* ------------------------------------------------------------------ */
.app { display: flex; flex-direction: column; min-height: 100vh; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px 24px; background: var(--surface-1); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { width: 32px; height: 32px; margin: 0; border-radius: 8px; }
.brand-mark::after { inset: 9px 8px; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.user-chip { display: flex; align-items: center; gap: 10px; padding-left: 12px; border-left: 1px solid var(--border); }
.user-chip img { border-radius: 50%; background: var(--surface-0); }
.avatar-initials {
  width: 28px; height: 28px; border-radius: 50%; background: var(--accent); color: var(--accent-ink);
  font-size: 11px; font-weight: 600; letter-spacing: .02em; display: inline-flex; align-items: center; justify-content: center; flex: none;
}
.user-meta { display: flex; flex-direction: column; line-height: 1.2; }
.user-name { font-weight: 500; font-size: 13px; }
.role-badge { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .04em; }
main { padding: 20px 24px; display: flex; flex-direction: column; gap: 16px; flex: 1; }
.foot { display: flex; gap: 16px; flex-wrap: wrap; padding: 12px 24px; border-top: 1px solid var(--border); }

/* ------------------------------------------------------------------ */
/* Summary strip                                                       */
/* ------------------------------------------------------------------ */
.summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.tile {
  background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; text-align: left; cursor: pointer; font: inherit; color: inherit;
  display: flex; flex-direction: column; gap: 4px; transition: border-color .12s, box-shadow .12s;
}
.tile:hover { border-color: var(--border-strong); }
.tile.active { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.tile-label { font-size: 12px; color: var(--text-secondary); display: flex; align-items: center; gap: 6px; }
.tile-value { font-size: 26px; font-weight: 600; letter-spacing: -0.02em; line-height: 1.1; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; background: var(--dot, var(--st-hold)); flex: none; }

/* ------------------------------------------------------------------ */
/* Toolbar                                                             */
/* ------------------------------------------------------------------ */
.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.toolbar .input { width: auto; }
#filter-search { min-width: 240px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  font: inherit; font-size: 12px; padding: 5px 10px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--border-strong); background: var(--surface-2); color: var(--text-secondary);
  display: inline-flex; align-items: center; gap: 6px;
}
.chip.active { border-color: var(--accent); color: var(--text-primary); background: var(--accent-soft); }

/* ------------------------------------------------------------------ */
/* Table                                                               */
/* ------------------------------------------------------------------ */
.table-wrap { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); overflow: auto; }
.grid { width: 100%; border-collapse: separate; border-spacing: 0; min-width: 1180px; table-layout: auto; }
.grid th {
  position: sticky; top: 0; z-index: 2; background: var(--surface-1);
  text-align: left; font-size: 12px; font-weight: 600; color: var(--text-secondary);
  padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap;
}
.grid td { padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
.grid tbody tr:last-child td { border-bottom: 0; }
.grid tbody tr:hover td { background: color-mix(in oklab, var(--surface-1), var(--text-primary) 3%); }
.grid tr.group-row td {
  background: var(--surface-0); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--text-secondary); padding: 6px 12px;
}
.col-idx { width: 36px; color: var(--text-muted); font-size: 12px; }
.col-creator { width: 130px; }
.col-num { width: 80px; }
.col-date { width: 110px; white-space: nowrap; }
.col-link { width: 19%; }
.col-status { width: 170px; }
.col-notes { width: 19%; }
.col-footage { width: 14%; }
.col-actions { width: 200px; min-width: 200px; }
td.col-idx { color: var(--text-muted); }

.cell { min-height: 20px; border-radius: 6px; padding: 2px 4px; margin: -2px -4px; }
.cell.editable { cursor: text; }
.cell.editable:hover { background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--border-strong); }
.cell.empty { color: var(--text-muted); font-style: italic; }
.cell-editor {
  font: inherit; font-size: 13px; color: var(--text-primary); background: var(--surface-2);
  border: 1px solid var(--accent); border-radius: 6px; padding: 4px 6px; width: 100%;
}
textarea.cell-editor { min-height: 60px; resize: vertical; }
.link-cell { display: flex; align-items: flex-start; gap: 6px; }
.link-cell .grow { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.icon-btn {
  background: none; border: 1px solid transparent; border-radius: 6px; cursor: pointer; padding: 2px 6px;
  color: var(--text-muted); font: inherit; font-size: 12px; line-height: 1.4; flex: none;
}
.icon-btn:hover { color: var(--text-primary); border-color: var(--border); background: var(--surface-2); }
.notes-text { white-space: pre-wrap; overflow-wrap: anywhere; }
.footage a { overflow-wrap: anywhere; }

/* Status pill: color + label, never color alone. */
.pill {
  display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 500;
  padding: 3px 9px 3px 7px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text-primary); cursor: pointer; font-family: inherit; white-space: nowrap;
}
.pill:hover { border-color: var(--border-strong); }
.pill[disabled] { cursor: default; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.actions .btn { padding: 4px 8px; }
.sent-meta { display: block; font-size: 11px; color: var(--text-muted); margin-top: 3px; }

/* ------------------------------------------------------------------ */
/* Modals                                                              */
/* ------------------------------------------------------------------ */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .45); display: grid; place-items: center;
  padding: 16px; z-index: 50; animation: fade .12s ease-out;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.modal-form {
  width: min(680px, 100%); max-height: calc(100vh - 32px); overflow: auto;
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; margin: 0;
}
.modal-sm { width: min(440px, 100%); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px 8px; }
.modal-body { padding: 8px 20px; }
.modal-foot { display: flex; align-items: center; gap: 8px; justify-content: flex-end; padding: 12px 20px 16px; border-top: 1px solid var(--border); }
.attachment {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px; border: 1px dashed var(--border-strong);
  border-radius: 8px; background: var(--surface-2); margin-bottom: 10px;
}
.attachment > div { flex: 1; min-width: 0; }
.attachment-icon { font-size: 18px; }
.attachment-name { font-weight: 500; overflow-wrap: anywhere; }
.activity { list-style: none; margin: 0; padding: 0; }
.activity li { padding: 8px 0; border-bottom: 1px solid var(--border); display: grid; grid-template-columns: 150px 1fr; gap: 12px; font-size: 13px; }
.activity li:last-child { border-bottom: 0; }
.activity time { color: var(--text-muted); font-size: 12px; white-space: nowrap; }
.steps { padding-left: 20px; }
.steps li { margin-bottom: 8px; }

.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%);
  background: var(--text-primary); color: var(--surface-1); padding: 10px 16px; border-radius: 10px;
  box-shadow: var(--shadow); z-index: 60; font-size: 13px; max-width: min(520px, calc(100vw - 32px));
}
.toast.error { background: var(--danger); color: #fff; }

/* ------------------------------------------------------------------ */
/* Roles & responsive                                                  */
/* ------------------------------------------------------------------ */
body[data-role="chapter"] .ts-only { display: none !important; }

@media (max-width: 720px) {
  .topbar { padding: 10px 16px; flex-wrap: wrap; }
  main { padding: 16px; }
  .foot { padding: 12px 16px; }
  .user-meta { display: none; }
  .two-col { grid-template-columns: 1fr; }
  #filter-search { min-width: 0; flex: 1; }
  .activity li { grid-template-columns: 1fr; gap: 2px; }
}

/* Status select inside the pill (native select, unstyled) */
.status-select {
  font: inherit; font-size: 12px; font-weight: 500; color: var(--text-primary);
  background: transparent; border: 0; padding: 0 14px 0 0; cursor: pointer; max-width: 150px; text-overflow: ellipsis;
  appearance: none; -webkit-appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--text-muted) 50%), linear-gradient(135deg, var(--text-muted) 50%, transparent 50%);
  background-position: calc(100% - 7px) 55%, calc(100% - 3px) 55%;
  background-size: 4px 4px, 4px 4px; background-repeat: no-repeat;
}
.status-select:focus { outline: none; }
.pill:has(.status-select:focus) { outline: 2px solid var(--accent); outline-offset: 1px; }
