:root {
  --sage: #8FA772;
  --sage-light: #D5E2D2;
  --olive: #3B4430;
  --olive-dark: #2C3E2C;
  --cream: #F2F2E8;
  --bg: #F4F5F0;
  --card: #FFFFFF;
  --border: #E3E6DC;
  --text: #2A2F25;
  --muted: #6B7064;
  --good: #3F8F4F;
  --bad: #B8473B;
  --warn: #C49A3A;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(40, 50, 30, 0.06), 0 2px 8px rgba(40, 50, 30, 0.05);
  font-family: "Open Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
h1, h2, h3 { color: var(--olive); margin: 0; line-height: 1.2; }
h2 { font-size: 1.35rem; font-weight: 700; }
h3 { font-size: 1rem; font-weight: 700; }
a { color: var(--olive); }
button, select, input { font: inherit; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.hidden { display: none !important; }
.mt { margin-top: 14px; }
.mt-s { margin-top: 10px; }

/* ---------- Top bar ---------- */
.topbar {
  background: var(--olive);
  color: #fff;
  padding: 12px 24px;
  display: flex;
  align-items: center;
  gap: 18px;
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 20;
}
.topbar .logo { height: 36px; width: auto; }
.topbar .title { font-weight: 700; font-size: 1.05rem; letter-spacing: 0.2px; }
.topbar .title span { display: block; font-weight: 400; font-size: 0.78rem; opacity: 0.8; }
.topbar .spacer { flex: 1; }
.controls { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.controls label { font-size: 0.78rem; opacity: 0.85; display: flex; flex-direction: column; gap: 2px; }
.controls select {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 8px;
  padding: 6px 8px;
}
.controls select option { color: var(--text); }
.check { flex-direction: row !important; align-items: center; gap: 6px !important; font-size: 0.85rem !important; }
.btn {
  border: 1px solid var(--border);
  background: #fff;
  color: var(--olive);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.88rem;
}
.btn:hover { background: var(--cream); }
.btn.primary { background: var(--sage); border-color: var(--sage); color: #fff; }
.btn.primary:hover { background: #7E9763; }
.topbar .btn { background: rgba(255, 255, 255, 0.1); color: #fff; border-color: rgba(255, 255, 255, 0.3); }
.topbar .btn:hover { background: rgba(255, 255, 255, 0.2); }

.subbar {
  background: #fff;
  border-bottom: 1px solid var(--border);
  padding: 8px 24px;
  display: flex;
  gap: 18px;
  align-items: center;
  flex-wrap: wrap;
  font-size: 0.88rem;
}
.subbar nav { display: flex; gap: 4px; flex-wrap: wrap; }
.subbar nav a { text-decoration: none; padding: 5px 10px; border-radius: 6px; color: var(--muted); font-weight: 600; }
.subbar nav a:hover { background: var(--cream); color: var(--olive); }
.subbar .asof { margin-left: auto; color: var(--muted); }

/* ---------- Layout ---------- */
main { max-width: 1280px; margin: 0 auto; padding: 24px; display: flex; flex-direction: column; gap: 28px; }
section { scroll-margin-top: 120px; }
.section-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.section-head p { margin: 0; color: var(--muted); font-size: 0.9rem; }
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}
.grid { display: grid; gap: 14px; }
.cards-6 { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); }
.cards-4 { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }
.two-col { grid-template-columns: 1.3fr 1fr; }
@media (max-width: 900px) { .two-col { grid-template-columns: 1fr; } main { padding: 16px; } .topbar, .subbar { padding: 10px 16px; } }

/* ---------- Scorecards ---------- */
.kpi .label { font-size: 0.82rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.4px; }
.kpi .value { font-size: 1.9rem; font-weight: 700; color: var(--olive); margin: 4px 0 2px; }
.delta { font-size: 0.85rem; font-weight: 600; }
.delta.up { color: var(--good); }
.delta.down { color: var(--bad); }
.delta.flat { color: var(--muted); }
.kpi .sub { font-size: 0.8rem; color: var(--muted); }
.kpi.hero { border-top: 4px solid var(--sage); }
.target { margin-top: 10px; }
.target .bar { height: 6px; background: var(--sage-light); border-radius: 4px; overflow: hidden; }
.target .bar i { display: block; height: 100%; background: var(--sage); width: 0; }
.target .bar i.done { background: var(--good); }
.target .t { font-size: 0.78rem; color: var(--muted); margin-top: 4px; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th { text-align: left; font-size: 0.76rem; text-transform: uppercase; letter-spacing: 0.4px; color: var(--muted); font-weight: 700; padding: 8px 10px; border-bottom: 2px solid var(--border); white-space: nowrap; }
td { padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tr:last-child td { border-bottom: none; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 8px; vertical-align: middle; }
.pill { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 0.76rem; font-weight: 700; background: var(--cream); color: var(--olive); }
.pill.good { background: #E3F0DF; color: var(--good); }
.pill.warn { background: #F6EBD3; color: #8A6A1E; }
.pill.bad { background: #F5DEDA; color: var(--bad); }
.rate-bar { display: inline-block; width: 60px; height: 6px; background: var(--sage-light); border-radius: 3px; margin-left: 8px; vertical-align: middle; overflow: hidden; }
.rate-bar i { display: block; height: 100%; background: var(--sage); }

/* ---------- Charts ---------- */
.chart-box { position: relative; height: 300px; }
.chart-box.tall { height: 360px; }
.chart-box.short { height: 240px; }

/* ---------- Notices ---------- */
.notice { display: flex; gap: 12px; align-items: flex-start; background: var(--cream); border: 1px dashed #C9CDBD; border-radius: var(--radius); padding: 14px 16px; font-size: 0.9rem; }
.notice strong { color: var(--olive); }
.callout { background: #FBF7EC; border-left: 4px solid var(--warn); padding: 10px 14px; border-radius: 6px; font-size: 0.88rem; margin-top: 12px; }
.empty { color: var(--muted); font-style: italic; padding: 10px 0; }

/* ---------- Goals banner ---------- */
.goals {
  display: grid; grid-template-columns: auto repeat(3, 1fr); gap: 14px; align-items: center;
  background: linear-gradient(90deg, var(--olive) 0%, #4A5640 100%); color: #fff;
  border-radius: var(--radius); padding: 14px 18px; box-shadow: var(--shadow);
}
.goals-title { font-weight: 700; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.8px; opacity: 0.85; }
.goal { display: flex; gap: 10px; align-items: center; }
.goal strong { display: block; font-size: 0.98rem; }
.goal small { display: block; font-size: 0.8rem; opacity: 0.85; }
.goal-n { width: 28px; height: 28px; border-radius: 50%; background: var(--sage); display: grid; place-items: center; font-weight: 700; flex: none; }
@media (max-width: 900px) { .goals { grid-template-columns: 1fr; } }

/* ---------- Month table ---------- */
.month-table th, .month-table td { text-align: right; }
.month-table th:first-child, .month-table td:first-child { text-align: left; }
.month-table td:first-child { font-weight: 600; color: var(--olive); white-space: nowrap; }
.month-table .partial { color: var(--muted); font-style: italic; }
.month-table .best { background: #EEF5EA; font-weight: 700; }
.month-table tr.group td { background: var(--cream); font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.4px; color: var(--muted); font-weight: 700; padding-top: 6px; padding-bottom: 6px; }
.chg { display: block; font-size: 0.74rem; font-weight: 600; }
.chg.up { color: var(--good); }
.chg.down { color: var(--bad); }
.chg.flat { color: var(--muted); }

/* ---------- How it's calculated ---------- */
.how dl { margin: 0; display: grid; grid-template-columns: 220px 1fr; gap: 10px 18px; font-size: 0.9rem; }
.how dt { font-weight: 700; color: var(--olive); }
.how dd { margin: 0; color: var(--text); }
@media (max-width: 700px) { .how dl { grid-template-columns: 1fr; } }

/* ---------- Data quality ---------- */
.dq-head { display: flex; align-items: baseline; gap: 12px; }
.dq-score { font-size: 2.4rem; font-weight: 700; color: var(--olive); }
.dq-row { display: grid; grid-template-columns: 1fr 70px 90px; gap: 10px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 0.9rem; }
.dq-row:last-child { border-bottom: none; }
.dq-row .bar { height: 8px; background: #EEE; border-radius: 4px; overflow: hidden; }
.dq-row .bar i { display: block; height: 100%; }

/* ---------- Chat ---------- */
.ask-btn {
  position: fixed; right: 22px; bottom: 22px; z-index: 30;
  background: var(--olive); color: #fff; border: none; border-radius: 999px;
  padding: 13px 20px; font-weight: 700; box-shadow: 0 6px 18px rgba(40, 50, 30, 0.3); cursor: pointer;
}
.ask-btn:hover { background: var(--olive-dark); }
.chat {
  position: fixed; right: 22px; bottom: 80px; z-index: 40; width: min(440px, calc(100vw - 32px)); height: min(620px, calc(100vh - 120px));
  background: #fff; border: 1px solid var(--border); border-radius: 16px; box-shadow: 0 12px 40px rgba(40, 50, 30, 0.25);
  display: flex; flex-direction: column; overflow: hidden;
}
.chat header { background: var(--olive); color: #fff; padding: 12px 16px; display: flex; align-items: center; gap: 10px; }
.chat header strong { flex: 1; }
.chat header button { background: none; border: none; color: #fff; font-size: 1.3rem; cursor: pointer; }
.chat .msgs { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 10px; background: var(--bg); }
.msg { padding: 10px 12px; border-radius: 12px; max-width: 92%; white-space: pre-wrap; font-size: 0.9rem; }
.msg.user { align-self: flex-end; background: var(--sage); color: #fff; }
.msg.assistant { align-self: flex-start; background: #fff; border: 1px solid var(--border); }
.msg.thinking { color: var(--muted); font-style: italic; }
.chat .suggest { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 14px 10px; background: var(--bg); }
.chat .suggest button { border: 1px solid var(--border); background: #fff; border-radius: 999px; padding: 5px 10px; font-size: 0.8rem; cursor: pointer; color: var(--olive); }
.chat form { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--border); }
.chat textarea { flex: 1; resize: none; border: 1px solid var(--border); border-radius: 10px; padding: 9px; font: inherit; height: 44px; }

/* ---------- Dialogs ---------- */
dialog { border: none; border-radius: 14px; padding: 0; width: min(560px, calc(100vw - 32px)); box-shadow: 0 12px 40px rgba(40, 50, 30, 0.3); }
dialog::backdrop { background: rgba(30, 35, 25, 0.45); }
dialog .dlg { padding: 20px 22px; display: flex; flex-direction: column; gap: 12px; }
dialog .row { display: grid; grid-template-columns: 1fr 140px; gap: 10px; align-items: center; }
dialog input, dialog select { border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; width: 100%; }
dialog .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 6px; }
.linkbox { background: var(--cream); border-radius: 8px; padding: 10px; word-break: break-all; font-size: 0.85rem; }

/* ---------- Auth pages ---------- */
.auth-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: var(--olive); padding: 16px; }
.auth-card { background: #fff; border-radius: 16px; padding: 32px 28px; width: min(400px, 100%); box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25); }
.auth-card .brand { background: var(--olive); border-radius: 10px; padding: 14px; text-align: center; margin-bottom: 18px; }
.auth-card .brand img { height: 40px; }
.auth-card form { display: flex; flex-direction: column; gap: 12px; margin-top: 14px; }
.auth-card label { font-size: 0.85rem; font-weight: 600; color: var(--olive); display: flex; flex-direction: column; gap: 4px; }
.auth-card input { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; }
.auth-card .error { color: var(--bad); font-size: 0.88rem; min-height: 1.2em; }

/* Narrow screens: let the (two-row) header scroll away instead of covering content */
@media (max-width: 1100px) { .topbar { position: static; } }
/* Leave room for the floating "Ask AI" button */
main { padding-bottom: 90px; }
.goal-link { color: #fff; text-decoration: underline; text-decoration-color: rgba(255, 255, 255, 0.4); }
.goal-link:hover { text-decoration-color: #fff; }

/* Sortable table headers */
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--olive); }
th.sorted { color: var(--olive); }

/* Feedback pop-up (ClickUp form) */
.feedback-btn { background: var(--sage) !important; border-color: var(--sage) !important; color: #fff !important; }
.feedback-btn:hover { background: #7E9763 !important; }
dialog.feedback-dlg { width: min(760px, calc(100vw - 32px)); }
.feedback-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.feedback-frame { width: 100%; height: min(720px, calc(100vh - 220px)); border: 1px solid var(--border); border-radius: 10px; background: #fff; }

/* Settings menu (header) */
.settings-wrap { position: relative; }
.settings-menu {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 50; min-width: 230px;
  background: #fff; color: var(--text); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 10px 30px rgba(30, 35, 25, 0.25); padding: 6px;
}
.settings-menu .menu-item {
  display: block; width: 100%; text-align: left; background: none; border: 0; border-radius: 6px;
  padding: 9px 12px; cursor: pointer; font-weight: 600; font-size: 0.9rem; color: var(--olive);
}
.settings-menu .menu-item:hover { background: var(--cream); }
.controls .settings-menu .menu-check {
  display: flex; flex-direction: row; align-items: center; gap: 8px; padding: 9px 12px;
  font-size: 0.9rem; opacity: 1; color: var(--text); cursor: pointer;
}
.settings-menu .menu-sep { height: 1px; background: var(--border); margin: 4px 6px; }

/* =====================================================================
   Layout: alignment + responsive (phone / tablet / desktop)
   ===================================================================== */

/* Let grid columns shrink so charts and tables never push the page wider than the screen */
.grid > * { min-width: 0; }
.card { min-width: 0; }
canvas { max-width: 100%; }

/* Header: dropdowns and buttons share one height and one bottom line */
.topbar { row-gap: 10px; }
.controls { align-items: flex-end; gap: 10px; }
.controls label { gap: 4px; }
.controls select, .topbar .btn { height: 38px; }
.controls select { min-width: 180px; padding: 0 10px; }
.topbar .btn { display: inline-flex; align-items: center; justify-content: center; padding: 0 16px; white-space: nowrap; }

/* Section menu: always one row, swipe sideways if it doesn't fit */
.subbar nav { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
.subbar nav::-webkit-scrollbar { display: none; }
.subbar nav a { white-space: nowrap; }
.subbar .asof { white-space: nowrap; }
@media (max-width: 1100px) {
  .subbar { flex-wrap: wrap; row-gap: 2px; }
  .subbar nav { width: 100%; }
  .subbar .asof { margin-left: 0; white-space: normal; }
}

/* Wide tables: swipe sideways, first column stays visible */
.table-wrap { -webkit-overflow-scrolling: touch; }
.table-wrap th:first-child, .table-wrap td:first-child { position: sticky; left: 0; background: #fff; z-index: 1; }
.table-wrap tr.group td:first-child { background: var(--cream); }
.table-wrap tfoot td:first-child { background: #fff; }

/* Loading feedback when switching period */
main.loading { opacity: 0.55; transition: opacity 0.15s; pointer-events: none; }

/* Phones */
@media (max-width: 700px) {
  :root { font-size: 14px; }
  .topbar { padding: 12px 14px; gap: 10px; }
  .topbar .logo { height: 26px; }
  .topbar .title { font-size: 0.95rem; }
  .topbar .title span { display: none; }
  .topbar .spacer { display: none; }
  .controls { width: 100%; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; align-items: end; }
  .controls label { width: 100%; }
  .controls select { width: 100%; min-width: 0; font-size: 16px; } /* 16px keeps iPhones from zooming in */
  .controls .btn, .settings-wrap, .settings-wrap > .btn { width: 100%; }
  .settings-menu { right: 0; min-width: 220px; }
  .subbar { padding: 4px 10px 6px; }
  .subbar nav a { padding: 7px 9px; }
  .subbar .asof { font-size: 0.78rem; padding: 0 9px; }
  main { padding: 14px 12px 90px; gap: 24px; }
  .card { padding: 14px; }
  h2 { font-size: 1.15rem; }
  .section-head { gap: 4px 10px; margin-bottom: 10px; }
  .section-head p { font-size: 0.85rem; }
  .cards-6, .cards-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .kpi .value { font-size: 1.45rem; }
  .kpi .label { font-size: 0.7rem; letter-spacing: 0.2px; }
  .kpi .delta, .kpi .sub { font-size: 0.75rem; }
  .chart-box { height: 260px; }
  .chart-box.tall { height: 300px; }
  .chart-box.short { height: 220px; }
  .goals { padding: 14px; gap: 12px; }
  table { font-size: 0.82rem; }
  th, td { padding: 8px 7px; }
  th { font-size: 0.68rem; }
  .dq-row { grid-template-columns: 1fr 56px 64px; gap: 8px; }
  .dq-score { font-size: 2rem; }
  dialog .row { grid-template-columns: 1fr; }
  dialog .dlg { padding: 16px; }
  .feedback-frame { height: calc(100vh - 190px); }
  .how dl { gap: 4px 0; }
  .how dd { margin-bottom: 10px; }
}

/* Phones: tables stay one line per cell and swipe; every dropdown 16px (no iOS zoom) */
@media (max-width: 700px) {
  select { font-size: 16px; }
  .table-wrap th, .table-wrap td { white-space: nowrap; }
  .table-wrap td:first-child { white-space: normal; min-width: 130px; max-width: 180px; }
  .rate-bar { width: 36px; margin-left: 5px; }
}

/* Month table group labels stay visible while the table scrolls sideways */
.table-wrap tr.group td:first-child { position: static; }
.group-label { position: sticky; left: 8px; }

/* Pop-ups never taller than the screen; buttons always visible */
dialog { max-height: calc(100vh - 32px); }
dialog .dlg { max-height: calc(100vh - 32px); overflow-y: auto; }
dialog .actions { position: sticky; bottom: -20px; background: #fff; padding: 10px 0 4px; margin-bottom: -4px; }

/* Readable first column in data tables (names / sources / metrics) */
.table-wrap td:first-child { min-width: 170px; }
.month-table td:first-child { min-width: 190px; }
@media (max-width: 700px) {
  .table-wrap td:first-child, .month-table td:first-child { min-width: 130px; }
}

/* "Connect an AI app" consent page */
.consent-list { margin: 10px 0 12px; padding-left: 18px; display: flex; flex-direction: column; gap: 6px; color: var(--text, #2b2f26); }
.consent-who { margin: 4px 0 0; }
.consent-actions { display: flex; gap: 10px; justify-content: flex-end; }
.consent-actions .btn { min-width: 110px; }
@media (max-width: 480px) { .consent-actions .btn { flex: 1; } }
