:root {
  --bg: #020d19;
  --bg2: #0a3664;
  --panel: #071a30;
  --surface2: #0c2540;
  --surface3: #103050;
  --border: #1a4060;
  --text: #e8f0f8;
  --text-muted: #7aa0be;
  --text-muted2: #4a7090;
  --accent: #a03aff;
  --accent2: #c084fc;
  --accent-dim: rgba(160,58,255,0.12);
  --green: #4ade80;
  --green-bg: rgba(74,222,128,0.15);
  --amber: #fbbf24;
  --amber-bg: rgba(251,191,36,0.15);
  --red: #f87171;
  --red-bg: rgba(248,113,113,0.12);
  --gray-bg: rgba(122,160,190,0.15);
  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 8px 32px rgba(0,0,0,0.4);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, 'Inter', BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: linear-gradient(160deg, var(--bg) 0%, var(--bg2) 100%);
  background-attachment: fixed;
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  min-height: 100vh;
}

a { color: var(--accent2); text-decoration: none; }
a:hover { text-decoration: underline; }

::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── App layout / sidebar ── */
.app-layout { display: flex; min-height: 100vh; align-items: stretch; }

.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: rgba(2,13,25,0.7);
  backdrop-filter: blur(12px);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  transition: width 0.22s ease;
  z-index: 10;
}
.sidebar.mini { width: 56px; }

.sidebar-top { display: flex; align-items: stretch; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.sidebar-brand {
  flex: 1; padding: 18px 14px; overflow: hidden; white-space: nowrap;
  font-weight: 700; font-size: 16px; color: var(--text);
  transition: opacity 0.15s;
  display: flex; align-items: center;
}
.sidebar-brand span { color: var(--accent); }
.sidebar-logo { height: 28px; width: auto; display: block; }
.sidebar.mini .sidebar-brand { opacity: 0; width: 0; padding: 0; }

.sidebar-toggle {
  flex-shrink: 0; width: 36px;
  background: none; border: none; border-left: 1px solid var(--border);
  color: var(--text-muted); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: color 0.15s, background 0.15s;
}
.sidebar-toggle:hover { color: var(--accent); background: var(--accent-dim); }
.sidebar-toggle svg { transition: transform 0.22s ease; }
.sidebar.mini .sidebar-toggle { width: 100%; border-left: none; }
.sidebar.mini .sidebar-toggle svg { transform: rotate(180deg); }

.sidebar-nav { flex: 1; padding: 10px 6px; display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: var(--radius-sm);
  font-size: 13px; font-weight: 500; color: var(--text-muted);
  cursor: pointer; transition: all 0.15s;
  white-space: nowrap; position: relative;
}
.nav-item:hover { background: var(--surface2); color: var(--text); text-decoration: none; }
.nav-item.active { background: var(--accent-dim); color: var(--accent); font-weight: 600; }
.nav-icon { width: 18px; height: 18px; flex-shrink: 0; display: flex; align-items: center; justify-content: center; }
.nav-label { flex: 1; overflow: hidden; transition: opacity 0.15s; }

.sidebar.mini .nav-label { opacity: 0; width: 0; }
.sidebar.mini .nav-item { justify-content: center; padding: 9px 0; }

.sidebar.mini .nav-item::after {
  content: attr(data-tooltip);
  position: absolute; left: calc(100% + 8px); top: 50%; transform: translateY(-50%);
  background: var(--surface3); border: 1px solid var(--border);
  border-radius: 7px; padding: 5px 10px;
  font-size: 12px; color: var(--text); white-space: nowrap;
  opacity: 0; pointer-events: none;
  transition: opacity 0.15s; z-index: 500;
  box-shadow: var(--shadow);
}
.sidebar.mini .nav-item:hover::after { opacity: 1; }

.sidebar-bottom-row {
  margin: 8px 12px; padding-top: 12px; border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.token-card { cursor: default; }
.token-card-header {
  display: flex; align-items: center; gap: 5px;
  font-size: 11.5px; font-weight: 600; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.04em;
  margin-bottom: 6px;
}
.token-card-icon { color: var(--amber); font-size: 12px; }
.token-card-total {
  font-size: 22px; font-weight: 800; color: var(--text);
  font-variant-numeric: tabular-nums; line-height: 1.2;
  margin-bottom: 6px;
}
.token-card-line {
  font-size: 11.5px; color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  margin-bottom: 2px;
}
.token-card-budget {
  font-size: 12px; font-weight: 700; color: var(--accent2);
  font-variant-numeric: tabular-nums;
  margin-top: 6px;
}
.token-card-budget.token-card-budget-over { color: var(--red); }

.sidebar.mini .token-card { display: none; }
.sidebar.mini .sidebar-bottom-row { border-top: none; margin: 0; padding: 0; }

.app-content { flex: 1; min-width: 0; }

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
.page-header h1 { font-size: 22px; font-weight: 800; margin: 0; }
.page-header p { color: var(--text-muted); margin: 4px 0 0; }

.btn {
  display: inline-block;
  border: none;
  background: var(--accent);
  color: #020d19;
  padding: 9px 20px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s;
}
.btn:hover { background: var(--accent2); box-shadow: 0 4px 16px rgba(160,58,255,0.35); }
.btn.secondary { background: transparent; color: var(--text-muted); border: 1px solid var(--border); }
.btn.secondary:hover { border-color: var(--accent); color: var(--accent); box-shadow: none; background: transparent; }
.btn.danger { background: var(--red); color: #020d19; }
.btn.danger:hover { background: #fca5a5; box-shadow: 0 4px 16px rgba(248,113,113,0.35); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; box-shadow: none; }

.cards-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
}
.card .num { font-size: 28px; font-weight: 800; color: var(--accent); line-height: 1; }
.card .label { color: var(--text-muted); font-size: 12px; margin-top: 4px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 20px;
}
.panel h2 {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted2);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin: 0 0 16px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.panel h2::before {
  content: '';
  width: 3px;
  height: 12px;
  background: var(--accent);
  border-radius: 2px;
  box-shadow: 0 0 6px rgba(160,58,255,0.5);
}

.charts-row {
  display: grid;
  grid-template-columns: 1.3fr 1fr;
  gap: 20px;
  margin-bottom: 20px;
}
@media (max-width: 900px) {
  .charts-row { grid-template-columns: 1fr; }
}

table { width: 100%; border-collapse: collapse; }
th, td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 13.5px;
}
th { background: var(--surface3); color: var(--text-muted); font-weight: 600; font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; }
tr:hover td { background: var(--surface2); }

.filters { display: flex; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; align-items: center; }
select, input[type=text], input[type=number], input[type=file], textarea {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-size: 13.5px;
  font-family: inherit;
  background: var(--surface2);
  color: var(--text);
  outline: none;
  transition: border-color 0.2s;
}
select:focus, input[type=text]:focus, input[type=number]:focus, textarea:focus { border-color: var(--accent); }
textarea { width: 100%; resize: vertical; }
label { font-weight: 600; font-size: 13px; display: block; margin-bottom: 6px; color: var(--text); }
.field { margin-bottom: 16px; }
.field .hint { color: var(--text-muted); font-size: 12px; margin-top: 4px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}
.badge.auto_approved { background: var(--green-bg); color: var(--green); }
.badge.pending_review { background: var(--amber-bg); color: var(--amber); }
.badge.revision_requested { background: var(--red-bg); color: var(--red); }
.badge.approved_after_review { background: var(--green-bg); color: var(--green); }
.badge.scoring { background: var(--gray-bg); color: var(--text-muted); }
.badge.scoring_failed { background: var(--red-bg); color: var(--red); }

.flag-pill {
  display: inline-block;
  background: var(--red-bg);
  color: var(--red);
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 11.5px;
  font-weight: 600;
}

.score-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px;
  margin-bottom: 16px;
}
.score-item {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px;
}
.score-item .title { font-weight: 600; margin-bottom: 4px; display: flex; justify-content: space-between; }
.score-item .title .val { font-size: 18px; font-weight: 800; color: var(--accent2); }
.score-item .reason { color: var(--text-muted); font-size: 13px; }

.article-text {
  white-space: pre-wrap;
  line-height: 1.7;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 16px;
  max-height: 480px;
  overflow-y: auto;
  background: var(--surface2);
}

.flag-item {
  border-left: 3px solid var(--red);
  background: var(--red-bg);
  padding: 10px 12px;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  margin-bottom: 8px;
}
.flag-item .sentence { font-style: italic; margin-bottom: 4px; color: var(--text); }
.flag-item .reason { font-size: 12.5px; color: #fca5a5; }

.empty-state { text-align: center; color: var(--text-muted); padding: 40px 20px; }

.loading-overlay {
  position: fixed; inset: 0; background: rgba(2,13,25,0.85);
  display: flex; align-items: center; justify-content: center;
  color: var(--text); font-weight: 600; z-index: 50;
}
.loading-overlay-content { display: flex; flex-direction: column; align-items: center; gap: 16px; text-align: center; max-width: 320px; padding: 0 20px; }
.loading-spinner {
  width: 40px; height: 40px; border-radius: 50%;
  border: 3px solid var(--border); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed; bottom: 28px; right: 28px;
  background: var(--panel); color: var(--text); border: 1px solid var(--border);
  padding: 12px 18px;
  border-radius: 12px; box-shadow: var(--shadow); z-index: 60;
  font-size: 13px; font-weight: 500;
}
.toast.error { border-color: var(--red); color: var(--red); }

.pagination { display: flex; gap: 8px; align-items: center; margin-top: 14px; color: var(--text-muted); }
.pagination .btn.secondary { padding: 6px 12px; }

.pill-select { display: flex; gap: 8px; flex-wrap: wrap; }

.brief-details {
  margin-top: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface2);
}
.brief-details summary {
  cursor: pointer;
  padding: 12px 14px;
  font-weight: 600;
  font-size: 13px;
  color: var(--text-muted);
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
}
.brief-details summary::-webkit-details-marker { display: none; }
.brief-details summary::before { content: '▸'; color: var(--accent); transition: transform 0.15s; }
.brief-details[open] summary::before { transform: rotate(90deg); }
.brief-details summary:hover { color: var(--text); }
.brief-details-body { padding: 4px 16px 16px; }
.brief-section { margin-top: 18px; }
.brief-section:first-child { margin-top: 0; }
.brief-section h3 {
  font-size: 11px; font-weight: 700; color: var(--text-muted2);
  text-transform: uppercase; letter-spacing: 0.06em;
  margin: 0 0 10px; padding-bottom: 6px; border-bottom: 1px solid var(--border);
}
.brief-field { margin-bottom: 12px; }
.brief-field label { font-weight: 500; font-size: 12.5px; color: var(--text-muted); margin-bottom: 4px; }
.brief-field textarea { min-height: 44px; }

/* ── Client grid ── */
.client-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 16px;
}
.client-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  cursor: pointer;
  transition: border-color 0.2s, transform 0.15s, box-shadow 0.2s;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-decoration: none;
  color: inherit;
  min-height: 150px;
}
.client-card:hover { border-color: var(--accent); transform: translateY(-2px); box-shadow: 0 8px 32px rgba(160,58,255,0.12); text-decoration: none; }
.client-card-header { display: flex; align-items: center; gap: 12px; }
.client-avatar {
  width: 40px; height: 40px; border-radius: 12px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 16px; color: #020d19;
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent2) 100%);
}
.client-card-name { font-size: 15.5px; font-weight: 700; line-height: 1.3; }
.client-card-preview {
  font-size: 12.5px; color: var(--text-muted); line-height: 1.5;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
}
.client-card-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: auto; }

.card-add {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 20px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  cursor: pointer; transition: border-color 0.2s, background 0.2s;
  color: var(--text-muted); min-height: 150px;
  text-decoration: none;
}
.card-add:hover { border-color: var(--accent); background: var(--accent-dim); color: var(--accent); text-decoration: none; }
.card-add-label { font-size: 14px; font-weight: 600; }

/* ── Quill rich text editor (dark theme override) ── */
#articleEditor.ql-container { border-color: var(--border); border-radius: 0 0 var(--radius-sm) var(--radius-sm); background: var(--surface2); font-size: 14px; }
#articleEditor .ql-editor { min-height: 320px; color: var(--text); }
#articleEditor .ql-editor.ql-blank::before { color: var(--text-muted); font-style: normal; }
/* Pasted content (Word, Google Docs, web copy-paste) often carries inline
   color/background-color styles — e.g. explicit black text — that would
   otherwise render invisible against this dark editor. Force-normalize
   every element inside the editor to the theme's colors regardless of any
   inline style the source HTML brought in. (These never reach storage
   anyway — src/articleHtml.js strips style attributes on submit — this is
   purely about what's visible while composing.) */
#articleEditor .ql-editor * { color: var(--text) !important; background-color: transparent !important; }
#articleEditor .ql-editor a { color: var(--accent2) !important; }
.ql-toolbar.ql-snow { border-color: var(--border); border-radius: var(--radius-sm) var(--radius-sm) 0 0; background: var(--surface3); }
.ql-snow .ql-stroke { stroke: var(--text-muted); }
.ql-snow .ql-fill, .ql-snow .ql-stroke.ql-fill { fill: var(--text-muted); }
.ql-snow .ql-picker { color: var(--text-muted); }
.ql-snow .ql-picker-options { background: var(--surface3); border-color: var(--border) !important; }
.ql-toolbar.ql-snow .ql-picker-label { border-color: transparent; }
.ql-snow.ql-toolbar button:hover, .ql-snow .ql-toolbar button:hover,
.ql-snow.ql-toolbar button.ql-active, .ql-snow .ql-toolbar button.ql-active,
.ql-snow.ql-toolbar .ql-picker-label:hover, .ql-snow .ql-toolbar .ql-picker-label:hover,
.ql-snow.ql-toolbar .ql-picker-label.ql-active, .ql-snow .ql-toolbar .ql-picker-label.ql-active,
.ql-snow.ql-toolbar .ql-picker-item:hover, .ql-snow .ql-toolbar .ql-picker-item:hover {
  color: var(--accent);
}
.ql-snow.ql-toolbar button:hover .ql-stroke, .ql-snow .ql-toolbar button:hover .ql-stroke,
.ql-snow.ql-toolbar button.ql-active .ql-stroke, .ql-snow .ql-toolbar button.ql-active .ql-stroke {
  stroke: var(--accent);
}
.ql-snow .ql-tooltip { background: var(--surface3); border-color: var(--border); color: var(--text); box-shadow: var(--shadow); }
.ql-snow .ql-tooltip input[type=text] { background: var(--surface2); color: var(--text); border-color: var(--border); }

/* ── Rendered article body (article.ejs detail view) ── */
.article-text h1, .article-text h2, .article-text h3, .article-text h4 { margin: 16px 0 8px; line-height: 1.3; color: var(--text); }
.article-text h1:first-child, .article-text h2:first-child, .article-text h3:first-child { margin-top: 0; }
.article-text h1 { font-size: 20px; font-weight: 800; }
.article-text h2 { font-size: 17px; font-weight: 700; }
.article-text h3 { font-size: 15px; font-weight: 700; }
.article-text p { margin: 0 0 12px; }
.article-text ul, .article-text ol { margin: 0 0 12px; padding-left: 22px; }
.article-text blockquote { border-left: 3px solid var(--accent); margin: 0 0 12px; padding-left: 14px; color: var(--text-muted); }
.article-text a { color: var(--accent2); }

/* ── Heading structure analysis (article.ejs) ── */
.heading-ok { color: var(--green); font-weight: 600; font-size: 13.5px; }
.heading-warn { color: var(--amber); font-weight: 600; font-size: 13.5px; }
.heading-issue-list { margin: 8px 0 0; padding-left: 20px; color: var(--amber); font-size: 13px; }
.heading-issue-list li { margin-bottom: 4px; }
