:root {
  color-scheme: light;
  --bg: #f3efe8;
  --panel: #fffaf2;
  --panel-strong: #ffffff;
  --ink: #1f2328;
  --muted: #6f6a63;
  --line: #d9d0c3;
  --line-soft: #e7dfd3;
  --green: #2d7a46;
  --green-soft: #dff2e7;
  --blue: #2b5f9e;
  --blue-soft: #dfeaf8;
  --amber: #a16412;
  --amber-soft: #f8e8c9;
  --red: #ac3b31;
  --red-soft: #f2dbd8;
  --shadow: 0 8px 24px rgba(29, 34, 40, 0.07);
  --radius: 8px;
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--ink); }
body { font-size: 15px; line-height: 1.45; letter-spacing: 0; }
body.viewer-open,
body.intro-open { overflow: hidden; }
button, input, textarea, select { font: inherit; }
button { cursor: pointer; }

.shell { width: min(1120px, calc(100vw - 24px)); margin: 0 auto; padding: 14px 0 28px; }
.topbar { display: grid; gap: 12px; padding: 12px 0 4px; }
.brand h1 { margin: 4px 0 6px; font-size: 24px; line-height: 1.15; }
.eyebrow { margin: 0; color: var(--green); font-size: 12px; font-weight: 700; letter-spacing: 0; text-transform: none; }
.lede { margin: 0; color: var(--muted); max-width: 56ch; }
.status-strip { display: flex; flex-wrap: wrap; gap: 8px; }
.chip, .pill, .status-badge, .label-badge, .vote-badge { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); border-radius: 999px; padding: 5px 10px; background: var(--panel-strong); }
.chip strong, .pill strong { font-weight: 700; }
.chip[data-tone="green"], .vote-badge[data-label="left"], .vote-badge[data-decision="approve"] { background: var(--green-soft); border-color: #b9e0c8; color: var(--green); }
.chip[data-tone="blue"], .vote-badge[data-label="right"] { background: var(--blue-soft); border-color: #c0d5f0; color: var(--blue); }
.chip[data-tone="amber"], .vote-badge[data-label="uncertain"] { background: var(--amber-soft); border-color: #f1d79d; color: var(--amber); }
.chip[data-tone="red"], .vote-badge[data-label="discard"], .vote-badge[data-decision="reject"] { background: var(--red-soft); border-color: #edc4bf; color: var(--red); }

main { display: grid; gap: 14px; }
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
}
.panel-head { display: grid; gap: 4px; margin-bottom: 10px; }
.panel-head h2, .panel-head h3 { margin: 0; font-size: 18px; }
.panel-head p { margin: 0; color: var(--muted); font-size: 13px; }

.auth-panel { display: grid; gap: 12px; }
.auth-grid { display: grid; gap: 10px; }
.field { display: grid; gap: 6px; }
.field span { font-size: 13px; color: var(--muted); }
.field input, .field textarea, .field select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-strong);
  padding: 12px 12px;
  color: var(--ink);
}
.field textarea { min-height: 92px; resize: vertical; }
.field input:focus, .field textarea:focus, .field select:focus, button:focus { outline: 2px solid #96b8f3; outline-offset: 1px; }

.actions-row, .bottom-actions, .tab-row, .task-actions, .toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.primary-btn, .ghost-btn, .seg-btn, .tab, .task-link, .icon-btn {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-strong);
  color: var(--ink);
  padding: 10px 12px;
}
.primary-btn { background: var(--green); border-color: var(--green); color: white; font-weight: 700; }
.ghost-btn:hover, .tab:hover, .task-link:hover, .seg-btn:hover { border-color: #c8bcad; }
.tabs {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}
.tab { text-align: center; padding: 11px 10px; font-weight: 650; }
.tab.active { background: #1f2c25; color: white; border-color: #1f2c25; }

.view { display: grid; gap: 14px; }
.summary-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}
.summary {
  background: var(--panel-strong);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  display: grid;
  gap: 4px;
}
.summary strong { font-size: 20px; }
.summary span { color: var(--muted); font-size: 13px; }

.task-shell { display: grid; gap: 14px; }
.meta-line { color: var(--muted); font-size: 13px; }
.stack { display: grid; gap: 12px; }
.asset-block {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel-strong);
  overflow: hidden;
}
.asset-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: center;
  padding: 12px 12px 8px;
}
.asset-head h3 { margin: 0; font-size: 16px; }
.asset-head p { margin: 0; color: var(--muted); font-size: 12px; }
.asset-title-wrap { min-width: 0; }
.asset-title-row {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
}
.inline-task-id {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}
.crop-figure, .page-figure { margin: 0; }
.crop-figure img { width: 100%; display: block; background: #f6f1e7; }
.crop-frame { padding: 12px; display: grid; gap: 8px; }
.crop-frame img,
.fine-crop-svg {
  width: 100%;
  max-height: 320px;
  object-fit: contain;
  background: #f5efe4;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
}
.image-trigger {
  display: block;
  width: 100%;
  border: 0;
  border-radius: 8px;
  padding: 0;
  background: transparent;
}
.image-trigger img {
  display: block;
}

.crop-context {
  position: relative;
  display: block;
  width: 100%;
  overflow: hidden;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  background: #f3ecdf;
}
.crop-context img,
.fine-crop-svg {
  display: block;
  width: 100%;
  max-height: 320px;
  object-fit: contain;
  border: 0;
  border-radius: 8px;
  background: transparent;
}

.fine-box-rect {
  fill: rgba(255, 224, 89, 0.06);
  stroke: #bd650c;
  stroke-width: 1.25;
}

.fine-box-rect[data-origin="human_reviewed"] {
  stroke: #16834f;
  stroke-width: 1.45;
}

.fine-box-rect[data-origin="human_excluded"] { stroke: #62766a; stroke-dasharray: 3 2; }
.fine-box-rect[data-origin="model_prediction"] { stroke: #bd650c; }
.fine-box-rect[data-origin="crowd_edit"] { stroke: #236bc4; }

.crop-context-viewer {
  width: min(100%, 760px);
}
.crop-context-viewer img,
.crop-context-viewer .fine-crop-svg {
  max-height: min(72vh, 620px);
}

.page-box {
  position: absolute;
  border: 2px solid rgba(162, 99, 0, 0.92);
  background: rgba(255, 214, 141, 0.15);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.38);
  border-radius: 4px;
}
.page-box-label {
  position: absolute;
  left: 0;
  top: -24px;
  background: rgba(31, 35, 40, 0.85);
  color: white;
  padding: 2px 6px;
  border-radius: 999px;
  font-size: 11px;
  white-space: nowrap;
}

.vote-strip { display: flex; flex-wrap: wrap; gap: 8px; }
.vote-badge { font-size: 13px; }
.vote-badge small { color: inherit; opacity: 0.9; }
.vote-list { display: grid; gap: 8px; }
.vote-item {
  display: grid;
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel-strong);
  padding: 10px 12px;
}
.vote-item-head { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.vote-item-head strong { font-size: 14px; }
.vote-item-head span { color: var(--muted); font-size: 12px; }
.vote-note { color: #514c45; font-size: 13px; }

.prior-vote-card {
  display: grid;
  gap: 7px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fbf8f1;
  padding: 12px;
}
.prior-vote-card[data-label="left"] { border-color: #b9e0c8; background: #f3fbf6; }
.prior-vote-card[data-label="right"] { border-color: #c0d5f0; background: #f3f7fd; }
.prior-vote-card[data-label="uncertain"] { border-color: #f1d79d; background: #fff9ec; }
.prior-vote-card[data-label="discard"] { border-color: #edc4bf; background: #fff6f5; }
.prior-note-kicker {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}
.prior-vote-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
}
.prior-vote-note-main {
  color: var(--ink);
  font-size: 18px;
  font-weight: 800;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.candidate-editor {
  display: grid;
  gap: 9px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fbfaf7;
  padding: 11px;
}

.candidate-editor-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}

.candidate-editor-head strong { font-size: 14px; }
.candidate-legend { display: flex; gap: 9px; flex-wrap: wrap; }
.candidate-legend span {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: var(--muted);
  font-size: 11px;
}
.candidate-legend span::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
}
.candidate-legend [data-origin="human_reviewed"] { color: #16834f; }
.candidate-legend [data-origin="human_excluded"] { color: #62766a; }
.candidate-legend [data-origin="model_prediction"] { color: #ad5a08; }
.candidate-legend [data-origin="crowd_edit"] { color: #236bc4; }

.candidate-token-row {
  display: flex;
  align-items: stretch;
  gap: 6px;
  flex-wrap: wrap;
  min-height: 48px;
}

.candidate-token {
  --candidate-color: #ad5a08;
  display: grid;
  grid-template-columns: minmax(34px, calc(var(--candidate-chars) * 1.35em + 14px)) 28px;
  min-height: 46px;
  overflow: hidden;
  border: 1px solid #dfb98e;
  border-radius: 6px;
  background: #fff8ef;
  color: var(--candidate-color);
}
.candidate-token[data-origin="human_reviewed"] { --candidate-color: #16834f; border-color: #a9d4bc; background: #f3faf6; }
.candidate-token[data-origin="human_excluded"] { --candidate-color: #62766a; border-color: #b9c7bf; background: #f4f7f5; border-style: dashed; }
.candidate-token[data-origin="model_prediction"] { --candidate-color: #ad5a08; }
.candidate-token[data-origin="crowd_edit"] { --candidate-color: #236bc4; border-color: #acc8e9; background: #f2f7fd; }
.candidate-token[data-empty="true"] { border-style: dashed; }
.candidate-token[data-deletable="false"] { grid-template-columns: minmax(34px, calc(var(--candidate-chars) * 1.35em + 14px)); }

.candidate-token-input {
  width: 100%;
  min-width: 0;
  border: 0;
  border-radius: 0;
  padding: 7px 5px 7px 9px;
  background: transparent;
  color: inherit;
  font-size: 20px;
  font-weight: 800;
  line-height: 1.2;
  text-align: center;
}
.candidate-token-input::placeholder {
  color: inherit;
  font-size: 12px;
  font-weight: 700;
  opacity: 0.76;
}
.candidate-token-input:disabled { -webkit-text-fill-color: currentColor; opacity: 0.82; }

.candidate-token-delete {
  width: 28px;
  min-height: 44px;
  border: 0;
  border-left: 1px solid #d8d3cb;
  border-radius: 0;
  padding: 0;
  background: transparent;
  color: inherit;
  font-size: 19px;
  line-height: 1;
}
.candidate-token-delete:hover { background: rgba(0, 0, 0, 0.05); }
.candidate-token-delete:disabled { cursor: default; opacity: 0.42; }
.candidate-token-delete[hidden] { display: none; }

.segmented { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.segmented[data-cols="2"] { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.segmented[data-cols="4"] { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.seg-btn { padding: 12px 10px; font-weight: 700; min-height: 48px; overflow-wrap: anywhere; }
.seg-btn.active[data-label="left"] { background: var(--green); border-color: var(--green); color: white; }
.seg-btn.active[data-label="right"] { background: var(--blue); border-color: var(--blue); color: white; }
.seg-btn.active[data-label="uncertain"] { background: var(--amber); border-color: var(--amber); color: white; }
.seg-btn.active[data-label="discard"] { background: var(--red); border-color: var(--red); color: white; }
.seg-btn.active[data-review-decision="approve"] { background: var(--green); border-color: var(--green); color: white; }
.seg-btn.active[data-review-decision="reject"] { background: var(--red); border-color: var(--red); color: white; }
.seg-btn:disabled { cursor: not-allowed; opacity: 0.5; }
.primary-btn:disabled, .ghost-btn:disabled { opacity: 0.56; cursor: not-allowed; }
.bottom-actions { justify-content: space-between; gap: 10px; }
.bottom-actions .primary-btn { flex: 1 1 160px; }
.bottom-actions .ghost-btn { flex: 1 1 120px; }

.task-list { display: grid; gap: 10px; }
.task-row {
  display: grid;
  grid-template-columns: 92px minmax(0, 1fr);
  gap: 10px;
  align-items: start;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel-strong);
}
.task-thumb {
  display: block;
  width: 92px;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--line-soft);
  background: #f2eadf;
}
.task-row h4 { margin: 0 0 3px; font-size: 15px; }
.task-row p { margin: 0; color: var(--muted); font-size: 12px; }
.task-row .task-actions { margin-top: 10px; }

.admin-grid { display: grid; gap: 10px; }
.log-box {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel-strong);
  padding: 12px;
  min-height: 140px;
  white-space: pre-wrap;
  overflow: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
}

.empty-state {
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.45);
  padding: 20px;
  color: var(--muted);
  text-align: center;
}

.intro-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: max(18px, env(safe-area-inset-top)) 14px max(18px, env(safe-area-inset-bottom));
  background: rgba(21, 24, 22, 0.56);
  backdrop-filter: blur(5px);
}

.intro-backdrop[hidden] {
  display: none;
}

.intro-card {
  width: min(100%, 430px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel-strong);
  box-shadow: 0 22px 56px rgba(18, 20, 18, 0.24);
  padding: 18px;
  display: grid;
  gap: 14px;
}

.intro-card h2 {
  margin: 0;
  font-size: 23px;
  line-height: 1.2;
}

.intro-steps {
  margin: 0;
  padding: 0;
  display: grid;
  gap: 10px;
  list-style: none;
  counter-reset: intro-step;
}

.intro-steps li {
  counter-increment: intro-step;
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  column-gap: 10px;
  row-gap: 2px;
  align-items: start;
  padding: 11px 0;
  border-top: 1px solid var(--line-soft);
}

.intro-steps li::before {
  content: counter(intro-step);
  grid-row: 1 / span 2;
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 999px;
  background: var(--green-soft);
  color: var(--green);
  font-weight: 800;
}

.intro-steps strong {
  font-size: 16px;
}

.intro-steps span,
.intro-note {
  color: var(--muted);
}

.intro-note {
  margin: 0;
  font-size: 14px;
}

.intro-start {
  width: 100%;
  min-height: 46px;
}

.viewer-backdrop {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 10px;
  padding: max(12px, env(safe-area-inset-top)) 12px max(14px, env(safe-area-inset-bottom));
  background: rgba(18, 20, 18, 0.94);
  color: white;
}

.viewer-top {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
}

.viewer-tools {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: flex-end;
}

.viewer-mode {
  display: grid;
  grid-template-columns: repeat(2, minmax(72px, 1fr));
  gap: 8px;
}

.viewer-mode .seg-btn,
.viewer-locate,
.viewer-close {
  border-color: rgba(255, 255, 255, 0.22);
  background: rgba(255, 255, 255, 0.1);
  color: white;
}

.viewer-locate {
  border-color: rgba(255, 218, 70, 0.58);
  background: rgba(255, 218, 70, 0.16);
  color: #ffe176;
  font-weight: 700;
}

.viewer-mode .seg-btn.active {
  background: white;
  border-color: white;
  color: #1f2328;
}

.viewer-canvas {
  min-height: 0;
  overflow: auto;
  display: grid;
  align-items: start;
  justify-items: center;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 8px;
  background: #111511;
  touch-action: pan-x pan-y;
}

.viewer-image {
  display: block;
  width: min(100%, 720px);
  max-height: 100%;
  object-fit: contain;
  background: #f5efe4;
}

.viewer-page-stage {
  position: relative;
  width: min(1280px, max(100%, calc(100vw * var(--context-zoom, 1))));
  align-self: start;
  margin: 0 auto;
  background: #f3ecdf;
}

.viewer-page-stage img {
  display: block;
  width: 100%;
  height: auto;
}

.viewer-page-box {
  border-width: 3px;
  border-color: #ffe15a;
  background: rgba(255, 224, 89, 0.18);
  box-shadow:
    0 0 0 4px rgba(227, 51, 43, 0.92),
    0 0 0 11px rgba(255, 224, 89, 0.34),
    0 0 28px rgba(255, 224, 89, 0.5);
}

.viewer-page-box::after {
  content: "";
  position: absolute;
  inset: -18px;
  border: 1px dashed rgba(255, 255, 255, 0.82);
  border-radius: 7px;
  pointer-events: none;
}

.viewer-hint {
  color: rgba(255, 255, 255, 0.76);
  font-size: 13px;
  text-align: center;
}

@media (min-width: 820px) {
  .summary-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .auth-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .segmented[data-cols="4"] { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .task-shell { grid-template-columns: minmax(0, 0.95fr) minmax(320px, 0.7fr); align-items: start; }
  .stack { grid-column: 1; }
  .task-side { grid-column: 2; }
  .task-row { grid-template-columns: 108px minmax(0, 1fr); }
  .task-thumb { width: 108px; }
}

body.logged-in .brand .lede {
  display: none;
}

body.logged-in .topbar {
  gap: 8px;
}

body.logged-in .brand h1 {
  font-size: 21px;
  margin-bottom: 2px;
}

body.logged-in .status-strip {
  gap: 6px;
}

@media (max-width: 819px) {
  body.logged-in .shell {
    width: min(1120px, calc(100vw - 20px));
    padding-top: 10px;
  }

  body.logged-in .topbar {
    padding-top: 6px;
    padding-bottom: 0;
  }

  body.logged-in .tabs {
    position: sticky;
    top: 8px;
    z-index: 6;
    background: linear-gradient(to bottom, rgba(243, 239, 232, 0.98), rgba(243, 239, 232, 0.92));
    backdrop-filter: blur(8px);
    padding-top: 2px;
  }

  body.logged-in .meta-line,
  body.logged-in .asset-head p {
    display: none;
  }

  body.logged-in .asset-head {
    flex-direction: column;
    align-items: flex-start;
  }

  body.logged-in .asset-head.compact-head {
    flex-direction: row;
    align-items: center;
  }

  body.logged-in .asset-head.compact-head .chip {
    flex: 0 0 auto;
    padding: 5px 9px;
    font-size: 12px;
  }

  body.logged-in .asset-head .chip {
    max-width: 100%;
    overflow-wrap: anywhere;
  }

  body.logged-in .crop-frame img,
  body.logged-in .fine-crop-svg {
    max-height: 180px;
  }

  body.logged-in .bottom-actions {
    position: sticky;
    bottom: 8px;
    z-index: 5;
    margin-top: 4px;
    padding-top: 8px;
    background: linear-gradient(to top, rgba(243, 239, 232, 0.98), rgba(243, 239, 232, 0.75));
    backdrop-filter: blur(8px);
  }
}
