/* ------ Tokens ------ */
:root {
  --bg: #f9f6ed;
  --pane: #ffffff;
  --ink: #1f1c18;
  --muted: #6b6258;
  --border: #d9d2c5;
  --accent: #8b3a1d;
  --accent-2: #c25a3b;
  --pending: #b3a896;
  --in-review: #d8a23a;
  --reviewed: #4a8c3e;
  --locked: #4a3e8c;
  --red: #c24545;
  --blue: #4570c2;
  --green: #4a8c3e;
  --yellow: #b18a32;
  --gray: #7e7468;
}
* { box-sizing: border-box; }
[x-cloak] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font-family: 'Inter', -apple-system, system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
}
.deva { font-family: 'Noto Serif Devanagari', 'Noto Sans Devanagari', serif; line-height: 1.85; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ------ Top bar ------ */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px; background: var(--pane);
  border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 600; font-size: 16px; color: var(--accent); font-family: 'Noto Serif Devanagari', serif; }
.brand .muted { font-family: 'Inter', sans-serif; font-weight: 400; margin-left: 6px; }
.stats { display: flex; gap: 8px; }

/* ------ Layout ------ */
.layout { display: grid; grid-template-columns: 320px 1fr; min-height: calc(100vh - 50px); }
.sidebar { background: var(--pane); border-right: 1px solid var(--border); overflow-y: auto; max-height: calc(100vh - 50px); position: sticky; top: 50px; }
.main { padding: 20px 24px; max-width: 100%; overflow-x: hidden; }
.empty { padding: 60px 24px; color: var(--muted); }

/* ------ Sidebar ------ */
.filterbar { padding: 10px; display: flex; gap: 6px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--pane); }
.filterbar input, .filterbar select { padding: 6px 8px; border: 1px solid var(--border); border-radius: 4px; flex: 1; font-size: 13px; }
.pagelist { list-style: none; padding: 0; margin: 0; }
.pagelist li { padding: 10px 12px; border-bottom: 1px solid var(--border); cursor: pointer; }
.pagelist li:hover { background: #fdfaf2; }
.pagelist li.active { background: #f3e8d3; border-left: 3px solid var(--accent); padding-left: 9px; }
.pagelist .row { display: flex; align-items: center; gap: 8px; }
.pagelist .meta { display: flex; gap: 4px; margin-top: 4px; }
.page-id { font-family: monospace; font-size: 12px; color: var(--muted); min-width: 56px; }
.label-snippet { font-size: 13px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.status-dot {
  display: inline-block; width: 10px; height: 10px; border-radius: 50%;
  background: var(--pending);
}
.status-dot.in_review { background: var(--in-review); }
.status-dot.reviewed { background: var(--reviewed); }
.status-dot.locked { background: var(--locked); }

/* ------ Page header ------ */
.page-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 16px; }
.page-header h1 { margin: 0 0 4px; font-size: 20px; }
.page-header .sub { color: var(--muted); font-size: 13px; }
.page-header code { background: #efe9d8; padding: 2px 6px; border-radius: 3px; font-size: 12px; }

/* ------ Main grid ------ */
.grid-main { display: grid; grid-template-columns: 1fr 1.1fr 1fr; gap: 14px; margin-bottom: 24px; }
@media (max-width: 1500px) { .grid-main { grid-template-columns: 1fr 1fr; } .grid-main .preview-pane { grid-column: 1 / -1; } }
@media (max-width: 1100px) { .grid-main { grid-template-columns: 1fr; } }

.scan-pane, .canonical-pane, .preview-pane { background: var(--pane); padding: 12px; border: 1px solid var(--border); border-radius: 6px; }
.scan-pane h3 { margin: 0 0 10px; }
.scan-pane img { width: 100%; height: auto; border: 1px solid var(--border); display: block; }
.preview-frame { width: 100%; height: 580px; border: 1px solid var(--border); background: #faf6ee; }
a.button {
  display: inline-block; padding: 5px 11px; border: 1px solid var(--border);
  background: white; color: var(--ink); border-radius: 4px;
  font-size: 13px; text-decoration: none;
}
a.button.primary { background: var(--accent); color: white; border-color: var(--accent); }
a.button:hover { background: #fdfaf2; }
a.button.primary:hover { background: var(--accent-2); }
.pane-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.pane-header h3 { margin: 0; }

textarea.canonical {
  width: 100%; min-height: 500px;
  padding: 14px; font-size: 16px;
  font-family: 'Noto Serif Devanagari', serif; line-height: 1.95;
  border: 2px solid var(--accent); border-radius: 4px; background: #fffdf7;
  resize: vertical;
}
.selection-bar {
  margin-top: 8px; padding: 8px 10px; background: #fff3a8; border-radius: 4px;
  display: flex; gap: 8px; align-items: center; font-size: 13px;
}

/* ------ OCR passes ------ */
.passes { background: var(--pane); padding: 12px; border: 1px solid var(--border); border-radius: 6px; margin-bottom: 24px; }
.passes h3 { margin: 0 0 10px; display: flex; gap: 12px; align-items: center; }
.pass-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
@media (max-width: 1300px) { .pass-grid { grid-template-columns: 1fr; } }
.pass-col { background: #fdfaf2; padding: 10px; border-radius: 4px; max-height: 600px; overflow-y: auto; }
.pass-col h4 { margin: 0 0 8px; text-transform: uppercase; font-size: 11px; letter-spacing: 0.05em; color: var(--muted); }
.region-card { background: white; padding: 8px 10px; border: 1px solid var(--border); border-radius: 4px; margin-bottom: 6px; }
.region-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
.role-tag { background: var(--accent); color: white; padding: 1px 6px; border-radius: 3px; font-size: 10px; text-transform: uppercase; letter-spacing: 0.05em; }
.region-text { font-size: 14px; }
.ocr-text { font-size: 14px; white-space: pre-wrap; }

/* ------ Issues ------ */
.issues, .comments { background: var(--pane); padding: 12px; border: 1px solid var(--border); border-radius: 6px; margin-bottom: 24px; }
.issue-list, .comment-list { list-style: none; padding: 0; margin: 12px 0; }
.issue { background: #fdf6f6; border: 1px solid #f0d8d4; padding: 10px 12px; margin-bottom: 8px; border-radius: 4px; }
.issue.resolved { background: #f3faf2; border-color: #c5e2bf; opacity: 0.8; }
.issue.dismissed { background: #f5f5f5; border-color: var(--border); opacity: 0.6; text-decoration: line-through; }
.issue-header { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 12px; }
.issue-cat { background: var(--red); color: white; padding: 1px 8px; border-radius: 3px; font-weight: 600; }
.issue.resolved .issue-cat { background: var(--green); }
.issue.dismissed .issue-cat { background: var(--gray); }
.grow { flex: 1; }
.status-tag { padding: 1px 6px; border-radius: 3px; font-size: 11px; }
.status-tag.open { background: #f0d8d4; }
.status-tag.resolved { background: #c5e2bf; }
.status-tag.dismissed { background: #e0e0e0; }
.issue-quote { background: #fff3a8; padding: 4px 8px; margin-top: 6px; border-radius: 3px; font-size: 13px; }
.issue-note { margin-top: 6px; font-size: 13px; }

/* ------ Comments ------ */
.comment { background: #fdfaf2; padding: 8px 12px; border-radius: 4px; margin-bottom: 6px; }
.comments form { display: flex; gap: 8px; align-items: flex-start; }
.comments textarea { flex: 1; padding: 8px; border: 1px solid var(--border); border-radius: 4px; font-family: inherit; font-size: 13px; }

/* ------ Buttons / forms ------ */
button {
  padding: 6px 12px; border: 1px solid var(--border); background: white;
  cursor: pointer; border-radius: 4px; font-size: 13px;
}
button:hover { background: #fdfaf2; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.primary { background: var(--accent); color: white; border-color: var(--accent); }
button.primary:hover { background: var(--accent-2); }
button.link { border: none; background: none; color: var(--blue); padding: 2px 4px; }
button.link:hover { text-decoration: underline; background: none; }
button.small { padding: 4px 8px; font-size: 12px; }
.actions { display: flex; gap: 8px; }
input, select, textarea { font-family: inherit; }

/* ------ Badges/pills ------ */
.badge {
  background: var(--gray); color: white;
  padding: 2px 8px; border-radius: 10px; font-size: 11px;
  display: inline-flex; align-items: center; gap: 4px;
}
.badge.green { background: var(--green); }
.badge.red { background: var(--red); }
.badge.blue { background: var(--blue); }
.badge.yellow { background: var(--yellow); }
.badge.gray { background: var(--gray); }
.badge.pending { background: var(--pending); }
.badge.in_review { background: var(--in-review); }
.badge.reviewed { background: var(--reviewed); }
.badge.locked { background: var(--locked); }
.badge b { font-weight: 700; }

.pill { font-size: 10px; padding: 1px 6px; border-radius: 8px; background: var(--gray); color: white; }
.pill.red { background: var(--red); }
.pill.blue { background: var(--blue); }
.pill.gray { background: var(--gray); }

/* ------ Modal ------ */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(31,28,24,0.5);
  display: flex; align-items: center; justify-content: center; z-index: 100;
}
.modal {
  background: var(--pane); padding: 20px; border-radius: 6px;
  width: 90%; max-width: 520px; max-height: 90vh; overflow-y: auto;
  border: 1px solid var(--border);
}
.modal h3 { margin-top: 0; }
.modal label { display: block; margin: 12px 0 4px; font-weight: 500; }
.modal select, .modal textarea, .modal input { width: 100%; padding: 8px; border: 1px solid var(--border); border-radius: 4px; }
.modal .quote { background: #fff3a8; padding: 6px 10px; border-radius: 4px; margin: 10px 0; }
.modal .actions { justify-content: flex-end; margin-top: 16px; }

/* ------ Global issues table ------ */
.issues-table { width: 100%; background: var(--pane); border: 1px solid var(--border); border-radius: 6px; border-collapse: collapse; }
.issues-table th, .issues-table td { padding: 8px 10px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border); font-size: 13px; }
.issues-table th { background: #fdfaf2; font-weight: 600; color: var(--muted); position: sticky; top: 50px; z-index: 1; }
.issues-table tr.row-resolved { background: #f7fcf6; }
.issues-table tr.row-dismissed { color: var(--muted); }
.issues-table td.deva { font-family: 'Noto Serif Devanagari', serif; }
.issues-table a { color: var(--blue); text-decoration: none; }
.issues-table a:hover { text-decoration: underline; }

/* ------ Mobile drawer + topbar ------ */
.menu-toggle {
  display: none;
  background: none; border: 1px solid var(--border);
  font-size: 18px; padding: 4px 10px; line-height: 1;
  margin-right: 8px;
}
.sidebar-backdrop {
  display: none;
  position: fixed; inset: 0; background: rgba(31,28,24,0.4); z-index: 9;
}
@media (max-width: 900px) {
  .sidebar-backdrop.open { display: block; }
}

/* ------ Mobile / tablet (≤ 900px) ------ */
@media (max-width: 900px) {
  .menu-toggle { display: inline-block; }
  .topbar { padding: 8px 12px; gap: 8px; flex-wrap: wrap; }
  .brand { font-size: 14px; flex: 1; }
  .stats { gap: 4px; flex-wrap: wrap; }
  .badge { font-size: 10px; padding: 2px 6px; }
  .hide-sm { display: none !important; }

  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0;
    width: 88vw; max-width: 360px; z-index: 10;
    transform: translateX(-100%); transition: transform 200ms ease;
    box-shadow: 2px 0 12px rgba(0,0,0,0.15);
    max-height: 100vh;
  }
  .sidebar.open { transform: translateX(0); }

  .main { padding: 12px; }
  .grid-main { grid-template-columns: 1fr; gap: 12px; }
  .pass-grid { grid-template-columns: 1fr; }

  /* Larger tap targets */
  button, a.button { min-height: 38px; padding: 8px 14px; }
  button.link, button.small, .button.small { min-height: 32px; padding: 4px 10px; }
  .pagelist li { padding: 14px 12px; }

  /* Canonical editor: shorter on mobile */
  textarea.canonical { min-height: 320px; font-size: 17px; }
  .preview-frame { height: 420px; }

  /* Page header stacks */
  .page-header { flex-direction: column; align-items: stretch; gap: 10px; }
  .page-header h1 { font-size: 17px; }

  /* Issues table → card layout */
  .issues-table thead { display: none; }
  .issues-table tr { display: block; padding: 10px 0; border-bottom: 1px solid var(--border); }
  .issues-table th, .issues-table td { display: block; padding: 4px 12px; border: none; position: static; }
  .issues-table td::before {
    content: attr(data-label);
    display: inline-block; font-weight: 600; color: var(--muted);
    text-transform: uppercase; font-size: 10px; letter-spacing: 0.05em;
    margin-right: 8px; min-width: 70px;
  }

  /* OCR pass cards: cap height so they don't dominate */
  .pass-col { max-height: 320px; }

  /* Modal full-width on phones */
  .modal { width: 96vw; padding: 16px; }
}

/* ------ Phone-only (≤ 480px) ------ */
@media (max-width: 480px) {
  .topbar { padding: 6px 8px; }
  .brand { font-size: 13px; }
  .brand .muted { display: none; }
  .stats .badge { font-size: 9px; }
  .actions { flex-wrap: wrap; }
  .scan-pane img { max-height: 60vh; object-fit: contain; }
}
