:root {
  --bg: #eef3f7;
  --panel: #ffffff;
  --panel-2: #f7fafc;
  --text: #14202b;
  --muted: #64758a;
  --border: #dce5ec;
  --primary: #066b63;
  --primary-2: #0f9c8f;
  --accent: #d97706;
  --danger: #b91c1c;
  --shadow: 0 10px 30px rgba(7, 23, 39, 0.08);
  --radius: 16px;
  --mono: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  --sans: "Manrope", "Avenir Next", "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; font-family: var(--sans); color: var(--text); background: radial-gradient(circle at 20% 0%, #d7f1ee 0%, #eef3f7 38%, #eef3f7 100%); }
a { color: inherit; }
body.ai-drawer-open { overflow: hidden; }

.page-shell { display: grid; grid-template-columns: 280px 1fr; min-height: 100vh; }
.sidebar {
  background: linear-gradient(180deg, #07151c 0%, #0a202b 100%);
  color: #d9e7f1;
  padding: 22px;
  border-right: 1px solid rgba(255,255,255,0.08);
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; gap: 12px; align-items: center; margin-bottom: 24px; }
.brand-mark {
  width: 42px; height: 42px; border-radius: 12px; display: grid; place-items: center;
  background: linear-gradient(135deg, #1cb3a6, #0f766e); font-weight: 800; color: white;
}
.brand-title { font-weight: 800; letter-spacing: 0.2px; }
.brand-sub { font-size: 12px; color: #99b2c6; }
.nav-links { display: grid; gap: 8px; margin-bottom: 22px; }
.nav-link {
  display: block; padding: 10px 12px; border-radius: 10px; text-decoration: none;
  color: #d8e4ef; border: 1px solid transparent;
}
.nav-link:hover { background: rgba(255,255,255,0.05); border-color: rgba(255,255,255,0.08); }
.nav-link.is-active {
  background: linear-gradient(180deg, rgba(28,179,166,0.14), rgba(15,118,110,0.10));
  border-color: rgba(143, 207, 200, 0.35);
  color: #effcf9;
}
.side-note {
  margin-top: auto; font-size: 12px; line-height: 1.45; color: #aec1d2; background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.07); border-radius: 12px; padding: 12px;
}
.side-note .label { text-transform: uppercase; letter-spacing: 0.08em; font-size: 11px; color: #8fcfc8; margin-bottom: 4px; }

.main-shell { padding: 22px; }
.topbar { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 16px; }
.page-title { margin: 0; font-size: 28px; font-weight: 800; line-height: 1.1; }
.page-subtitle { margin: 6px 0 0; color: var(--muted); font-size: 14px; }
.topbar-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.topbar-user-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: #f4f8fb;
  color: #173249;
  font-size: 13px;
  font-weight: 600;
}
.inline-form { margin: 0; display: inline-flex; }
.content-stack { display: grid; gap: 18px; align-content: start; }
.page-stack { display: grid; gap: 18px; align-content: start; }

.auth-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.auth-shell {
  width: min(480px, 100%);
  display: grid;
  gap: 14px;
}
.auth-card {
  padding: 20px;
}
.auth-brand {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 10px;
}
.auth-brand .brand-mark {
  width: 46px;
  height: 46px;
  border-radius: 14px;
}
.auth-meta {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}
.auth-card h1 {
  margin: 0;
  font-size: 22px;
}
.auth-card .hint {
  color: var(--muted);
  font-size: 12px;
}

.section-tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin: 0;
}
.section-tab-link {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: #f4f8fb;
  color: #15334a;
  text-decoration: none;
  font-weight: 700;
  font-size: 13px;
}
.section-tab-link:hover { background: #edf4f8; }
.section-tab-link.is-active {
  background: linear-gradient(180deg, #e7faf6, #d9f4ee);
  border-color: #a8e6d8;
  color: #075e55;
}
.section-tab-meta {
  display: inline-flex;
  min-width: 20px;
  height: 20px;
  border-radius: 999px;
  padding: 0 6px;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 800;
  background: rgba(7, 23, 39, 0.06);
  color: var(--muted);
}
.section-tab-link.is-active .section-tab-meta {
  background: rgba(6, 107, 99, 0.12);
  color: #066b63;
}
.section-intro {
  display: grid;
  gap: 10px;
}
.card.inset-card {
  margin: 0;
  background: linear-gradient(180deg, #ffffff 0%, #f8fbfe 100%);
}

.grid { display: grid; gap: 16px; margin-bottom: 16px; }
.content-stack > .grid,
.page-stack > .grid,
.stack-form > .grid,
.form-card > .grid { margin-bottom: 0; }
.card > .grid:last-child { margin-bottom: 0; }
.grid-1 { grid-template-columns: 1fr; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.compact-grid { gap: 12px; }

.card {
  background: linear-gradient(180deg, var(--panel) 0%, #fbfdff 100%);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}
.card h2 { margin: 0 0 10px; font-size: 18px; }
.card h3.subheading { margin: 14px 0 8px; font-size: 14px; color: #173249; }
.card-header-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 8px; }
.card-header-row h2 { margin: 0; }

.stat-card .label { color: var(--muted); font-size: 13px; }
.stat-card .value { font-size: 32px; font-weight: 800; margin-top: 4px; }

.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { padding: 10px 8px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.table thead th { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.table.compact th, .table.compact td { padding: 8px 6px; }
.table .num { text-align: right; white-space: nowrap; }
.table tr.total th, .table tr.total td { font-weight: 700; }
.table tbody tr:hover { background: #f8fbfd; }

.kv-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.kv-grid.small { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.kv-grid > div { background: var(--panel-2); border: 1px solid var(--border); border-radius: 12px; padding: 10px; }
.kv-grid span { display: block; color: var(--muted); font-size: 12px; }
.kv-grid strong { display: block; margin-top: 4px; font-size: 14px; word-break: break-word; }

.metric-box { background: var(--panel-2); border: 1px solid var(--border); border-radius: 12px; padding: 10px; }
.metric-box span { display: block; font-size: 12px; color: var(--muted); }
.metric-box strong { display: block; margin-top: 4px; font-size: 15px; }

.form-card, .stack-form { display: grid; gap: 12px; }
.form-grid label { display: grid; gap: 6px; }
.checkbox-inline { display: inline-flex !important; gap: 8px; align-items: center; font-weight: 600; }
.checkbox-inline input[type='checkbox'] { width: 16px; height: 16px; padding: 0; }
label { display: grid; gap: 6px; font-size: 13px; font-weight: 600; color: #173249; }
input, select, textarea {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: #fff; font: inherit; color: var(--text);
}
textarea { resize: vertical; min-height: 92px; }
input:focus, select:focus, textarea:focus {
  outline: none; border-color: #0b998b; box-shadow: 0 0 0 3px rgba(11,153,139,0.14);
}
.checkbox-row { display: flex; gap: 16px; flex-wrap: wrap; align-items: center; color: var(--muted); font-size: 13px; }
.checkbox-row label { display: inline-flex; gap: 8px; align-items: center; font-weight: 500; color: var(--muted); }
.checkbox-row input[type='checkbox'] { width: 16px; height: 16px; padding: 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 10px 14px; border-radius: 12px; border: 1px solid transparent; text-decoration: none;
  font-weight: 700; cursor: pointer; font-family: var(--sans); font-size: 14px;
}
.btn-primary { background: linear-gradient(135deg, var(--primary), var(--primary-2)); color: white; }
.btn-primary:hover { filter: brightness(1.03); }
.btn-secondary { background: #f3f7fa; border-color: var(--border); color: #163247; }
.btn-secondary:hover { background: #edf3f7; }
.btn-warning { background: #fff7ed; border-color: #fed7aa; color: #9a3412; }
.btn-warning:hover { background: #ffedd5; }
.btn-danger { background: #fef2f2; border-color: #fecaca; color: #991b1b; }
.btn-danger:hover { background: #fee2e2; }
.btn-xs { padding: 6px 10px; border-radius: 10px; font-size: 12px; font-weight: 700; }
.form-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.row-actions form { display: inline-flex; margin: 0; }
.row-actions.row-actions-tight { gap: 4px; }

.busy-overlay {
  position: fixed;
  inset: 0;
  background: rgba(9, 23, 34, 0.42);
  backdrop-filter: blur(2px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}
.busy-overlay.is-open { display: flex; }
.busy-overlay-card {
  min-width: min(420px, calc(100vw - 40px));
  max-width: 560px;
  border-radius: 16px;
  border: 1px solid rgba(255,255,255,0.25);
  background: rgba(255,255,255,0.95);
  box-shadow: 0 18px 40px rgba(13, 32, 45, 0.22);
  padding: 16px 18px;
  display: flex;
  align-items: center;
  gap: 14px;
}
.busy-overlay-title { font-weight: 800; color: #0f2230; font-size: 14px; }
.busy-overlay-subtitle { color: var(--muted); font-size: 13px; margin-top: 2px; }
.busy-spinner {
  width: 22px; height: 22px; border-radius: 999px;
  border: 3px solid #d8e6ef; border-top-color: var(--primary);
  animation: busy-spin .8s linear infinite;
  flex: 0 0 auto;
}
@keyframes busy-spin { to { transform: rotate(360deg); } }

.import-sync-table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 14px; background: #fff; }
.import-sync-table-wrap .table { margin: 0; }
.import-sync-table .row-diff { background: #fffaf0; }
.import-sync-table .row-same { background: #f7fbff; }
.import-sync-table .row-empty { opacity: 0.85; }
.import-sync-table .col-compact { white-space: nowrap; }
.sync-status-pill {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  border: 1px solid var(--border);
  color: #173249;
  background: #f7fbff;
}
.sync-status-pill.diff { background: #fff7ed; border-color: #fed7aa; color: #9a3412; }
.sync-status-pill.same { background: #eff6ff; border-color: #bfdbfe; color: #1d4ed8; }
.sync-status-pill.missing { background: #f8fafc; border-color: #e2e8f0; color: #64748b; }
.sync-compare-muted { color: var(--muted); font-size: 12px; }

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #fff;
  color: #173249;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}
.icon-btn:hover { background: #f6fafc; }

.pill {
  display: inline-flex; align-items: center; justify-content: center; padding: 4px 10px; border-radius: 999px;
  background: #edf6ff; color: #1d4f7a; border: 1px solid #d8e8f8; font-size: 12px; font-weight: 700;
}
.pill-imported, .pill-parsed { background: #eefbf7; color: #065f46; border-color: #c8eedf; }
.pill-generated, .pill-approved { background: #eefbf7; color: #065f46; border-color: #c8eedf; }
.pill-manual, .pill-simulated { background: #fff7ed; color: #9a3412; border-color: #fed7aa; }
.pill-needs_review, .pill-failed, .pill-rejected { background: #fef2f2; color: #991b1b; border-color: #fecaca; }
.pill-pending { background: #eef2ff; color: #3730a3; border-color: #c7d2fe; }

.text-link { color: #0f6a88; text-decoration: none; font-weight: 600; }
.text-link:hover { text-decoration: underline; }
.muted { color: var(--muted); }
.small-text { font-size: 12px; }
.mono { font-family: var(--mono); }
.note-box {
  background: linear-gradient(180deg, #f8fbfd, #f0f7fb);
  border: 1px solid #d8e7f1; border-radius: 12px; padding: 12px; color: #3c556b; font-size: 13px; line-height: 1.5;
}
.alert { border-radius: 12px; padding: 12px; margin-top: 10px; font-size: 13px; }
.alert-warning { background: #fff7ed; border: 1px solid #fed7aa; color: #9a3412; }
.alert-info { background: #eff6ff; border: 1px solid #bfdbfe; color: #1d4ed8; }
.alert ul { margin: 8px 0 0 16px; padding: 0; }
.number-list { margin: 8px 0 0 20px; padding: 0; line-height: 1.5; }
.number-list li + li { margin-top: 6px; }
.bullet-list { margin: 8px 0 0 16px; padding: 0; line-height: 1.5; }
.bullet-list li + li { margin-top: 6px; }
.divider { border: 0; border-top: 1px solid var(--border); margin: 16px 0; }
.inline-links { display: flex; gap: 12px; flex-wrap: wrap; }
.top-gap { margin-top: 12px; }
.pre-wrap { white-space: pre-wrap; }
.md-content { line-height: 1.5; }
.md-content > :first-child { margin-top: 0; }
.md-content > :last-child { margin-bottom: 0; }
.md-content p { margin: 0 0 8px; }
.md-content h1, .md-content h2, .md-content h3, .md-content h4, .md-content h5, .md-content h6 {
  margin: 10px 0 6px;
  line-height: 1.3;
  color: #123148;
}
.md-content h1 { font-size: 18px; }
.md-content h2 { font-size: 16px; }
.md-content h3 { font-size: 15px; }
.md-content h4, .md-content h5, .md-content h6 { font-size: 14px; }
.md-content ul, .md-content ol { margin: 6px 0 8px 18px; padding: 0; }
.md-content li + li { margin-top: 4px; }
.md-content a { color: #0f6a88; text-decoration: none; font-weight: 600; }
.md-content a:hover { text-decoration: underline; }
.md-content code {
  font-family: var(--mono);
  background: #eef4f8;
  border: 1px solid #dbe7ef;
  border-radius: 6px;
  padding: 1px 5px;
  font-size: 12px;
}
.md-content pre.md-code-block { margin: 8px 0; white-space: pre-wrap; }
.md-content .md-table-wrap {
  margin: 8px 0;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
}
.md-content table {
  width: 100%;
  min-width: 420px;
  border-collapse: collapse;
  font-size: 13px;
}
.md-content th,
.md-content td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
  text-align: left;
  white-space: normal;
  word-break: break-word;
}
.md-content thead th {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  background: #f8fbfd;
  white-space: nowrap;
}
.md-content tbody tr:last-child td { border-bottom: 0; }
.md-content blockquote {
  margin: 8px 0;
  padding: 8px 10px;
  border-left: 3px solid #b9d4e8;
  background: #f5fafe;
  border-radius: 0 10px 10px 0;
}
.md-content hr { border: 0; border-top: 1px solid var(--border); margin: 10px 0; }
.dashboard-analytics-card { display: grid; gap: 14px; align-content: start; }
.dashboard-card-header { align-items: flex-start; }
.dashboard-card-header h2 { margin-bottom: 4px; }
.dashboard-card-header p { margin: 0; }
.toolbar-form {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  flex-wrap: wrap;
}
.toolbar-control {
  display: grid;
  gap: 6px;
  min-width: 180px;
  font-size: 12px;
  font-weight: 700;
  color: #173249;
}
.toolbar-control input,
.toolbar-control select {
  min-height: 38px;
  padding: 8px 10px;
}
.analytics-period-line {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}
.code-block {
  font-family: var(--mono); font-size: 12px; background: #08141d; color: #dbe8f6; border-radius: 12px;
  border: 1px solid #132433; padding: 12px; max-height: 420px; overflow: auto;
}
.ai-msg-body.md-content p { margin-bottom: 6px; }
.ai-msg-body.md-content ul, .ai-msg-body.md-content ol { margin-left: 16px; }
.ai-msg-body.md-content .md-table-wrap { max-width: 100%; }
.ai-msg-body.md-content table { min-width: 520px; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.ai-consult-tab {
  position: fixed;
  right: 16px;
  top: 48%;
  transform: translateY(-50%);
  z-index: 1100;
  border: 1px solid rgba(6, 107, 99, 0.25);
  background: linear-gradient(180deg, #0f9c8f, #066b63);
  color: #fff;
  border-radius: 14px;
  padding: 12px 10px;
  writing-mode: vertical-rl;
  text-orientation: mixed;
  letter-spacing: 0.04em;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 12px 24px rgba(6, 107, 99, 0.25);
}
.ai-consult-tab:hover { filter: brightness(1.03); }

.ai-drawer-overlay {
  position: fixed;
  inset: 0;
  background: rgba(7, 23, 39, 0.35);
  z-index: 1098;
}

.ai-drawer {
  position: fixed;
  top: 0;
  right: 0;
  height: 100vh;
  width: min(420px, 92vw);
  background: linear-gradient(180deg, #fbfdff 0%, #f4f9fc 100%);
  border-left: 1px solid var(--border);
  box-shadow: -20px 0 50px rgba(7, 23, 39, 0.16);
  z-index: 1099;
  display: grid;
  grid-template-rows: auto auto auto 1fr auto;
  gap: 12px;
  padding: 14px;
  transform: translateX(100%);
  transition: transform 180ms ease;
}
.ai-drawer.is-open { transform: translateX(0); }

.ai-drawer-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
}
.ai-drawer-header h2 { margin: 0; font-size: 18px; }
.ai-drawer-header p {
  margin: 4px 0 0;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.4;
}

.ai-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px;
}
.ai-card .label {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 6px;
}
.ai-page-context {
  font-size: 13px;
  line-height: 1.45;
  color: #1a3247;
}

.ai-prompt-list {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.ai-prompt-chip {
  border: 1px solid #d8e7f1;
  background: #f5fafc;
  color: #133a52;
  border-radius: 999px;
  padding: 8px 10px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}
.ai-prompt-chip:hover { background: #edf6fb; }

.ai-talk-log {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fff;
  padding: 10px;
  overflow: auto;
  min-height: 220px;
  max-height: 44vh;
  display: grid;
  align-content: start;
  gap: 10px;
}
.ai-msg {
  display: grid;
  gap: 4px;
  max-width: 92%;
}
.ai-msg-role {
  font-size: 11px;
  color: var(--muted);
  font-weight: 700;
  letter-spacing: 0.05em;
}
.ai-msg-body {
  border-radius: 12px;
  padding: 9px 10px;
  font-size: 13px;
  line-height: 1.45;
  border: 1px solid var(--border);
  white-space: pre-wrap;
}
.ai-msg-user { justify-self: end; }
.ai-msg-user .ai-msg-role { text-align: right; }
.ai-msg-user .ai-msg-body {
  background: #e9faf7;
  border-color: #bdeee4;
}
.ai-msg-assistant .ai-msg-body { background: #f7fafc; }

.ai-talk-form {
  display: grid;
  gap: 8px;
}
.ai-talk-form textarea {
  min-height: 92px;
  max-height: 180px;
}
.ai-form-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  flex-wrap: wrap;
}

@media (max-width: 1100px) {
  .page-shell { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; border-right: 0; border-bottom: 1px solid rgba(255,255,255,0.08); }
  .main-shell { padding: 16px; }
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 760px) {
  .topbar { flex-direction: column; align-items: stretch; }
  .grid-2, .grid-3, .grid-4, .kv-grid { grid-template-columns: 1fr; }
  .sidebar { padding: 16px; }
  .card { padding: 14px; border-radius: 14px; }
  .toolbar-form { width: 100%; }
  .toolbar-control { min-width: 0; width: 100%; }
  .toolbar-form .btn { width: 100%; }
  .section-tabs { gap: 6px; }
  .section-tab-link { width: 100%; justify-content: space-between; border-radius: 12px; }
  .page-title { font-size: 22px; }
  .table { display: block; overflow-x: auto; white-space: nowrap; }
  .ai-consult-tab {
    top: auto;
    bottom: 14px;
    right: 14px;
    transform: none;
    writing-mode: initial;
    padding: 10px 14px;
    border-radius: 999px;
  }
  .ai-drawer {
    width: 100vw;
    max-width: 100vw;
    padding-bottom: max(14px, env(safe-area-inset-bottom));
  }
}
