:root {
  --primary: #3f51b5;
  --primary-dark: #303f9f;
  --primary-light: #e8eaf6;
  --accent: #00897b;
  --warn: #e65100;
  --danger: #c62828;
  --ok: #2e7d32;
  --bg: #f4f6fb;
  --surface: #fff;
  --text: #1f2937;
  --muted: #6b7280;
  --border: #e5e7eb;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .1);
  --sidebar-w: 248px;
  font-family: "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
body { font-size: 14px; line-height: 1.45; }
a { color: var(--primary); }
.ico { width: 20px; height: 20px; fill: currentColor; flex: none; }

.app { display: flex; min-height: 100vh; }

/* ---------- sidebar ---------- */
.sidebar {
  width: var(--sidebar-w); background: #1e2a5a; color: #dfe3f5; display: flex; flex-direction: column;
  position: fixed; inset: 0 auto 0 0; z-index: 30; transition: transform .2s ease;
}
.brand { display: flex; gap: 12px; align-items: center; padding: 20px 18px; border-bottom: 1px solid rgba(255,255,255,.08); }
.brand-ico { width: 34px; height: 34px; fill: #9fa8da; }
.brand strong { display: block; font-size: 16px; color: #fff; }
.brand small { color: #9fa8da; }
.sidebar nav { padding: 10px 8px; display: flex; flex-direction: column; gap: 2px; flex: 1; overflow-y: auto; }
.sidebar nav a {
  display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-radius: 8px;
  color: #c5cae9; text-decoration: none; font-weight: 500;
}
.sidebar nav a:hover { background: rgba(255,255,255,.06); color: #fff; }
.sidebar nav a.active { background: var(--primary); color: #fff; }
.badge { margin-left: auto; background: var(--warn); color: #fff; border-radius: 10px; padding: 0 7px; font-size: 12px; }
.sidebar-foot { font-size: 12px; color: #7986cb; padding: 12px 18px; margin: 0; }
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 25; }

/* ---------- main ---------- */
.main { flex: 1; margin-left: var(--sidebar-w); min-width: 0; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 20; background: var(--surface); box-shadow: var(--shadow);
  display: flex; align-items: center; gap: 16px; padding: 10px 24px; flex-wrap: wrap;
}
.topbar h1 { font-size: 20px; margin: 0; flex: 1; min-width: 140px; }
.topbar .menu-btn { display: none; }
.filters { display: flex; gap: 10px; }
.filters label { display: flex; flex-direction: column; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
select, input[type=search] {
  font: inherit; padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px; background: #fff; color: var(--text); min-width: 110px;
}
select:focus, input:focus, button:focus-visible, a:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
.sync { display: flex; align-items: center; gap: 10px; }
.sync-status { font-size: 12px; color: var(--muted); text-align: right; }
.sync-status.stale { color: var(--warn); }
.user-email { font-size: 12px; color: var(--muted); max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- sign-in screen ---------- */
body.signed-out .app { display: none; }
body:not(.signed-out) .auth { display: none; }
.auth { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; background: linear-gradient(135deg, #1e2a5a 0%, #3f51b5 100%); }
.auth-card { background: var(--surface); border-radius: 16px; box-shadow: 0 10px 40px rgba(0,0,0,.25); padding: 32px; width: 100%; max-width: 440px; text-align: center; }
.auth-card h2 { margin: 18px 0 8px; font-size: 20px; }
.auth-card p { color: var(--text); margin: 8px 0; }
.auth-card .sub { color: var(--muted); font-size: 12px; margin-top: 16px; }
.auth-card .note { text-align: left; margin: 12px 0 0; }
.auth-card .spinner { margin: 24px auto 12px; }
.auth-brand { display: flex; gap: 12px; align-items: center; justify-content: center; text-align: left; }
.auth-brand .ico { width: 40px; height: 40px; fill: var(--primary); }
.auth-brand strong { display: block; font-size: 18px; }
.auth-brand small { color: var(--muted); }
.auth-actions { display: flex; flex-direction: column; gap: 10px; margin-top: 18px; }
.auth-actions .btn { justify-content: center; }
.btn.google-btn { background: #fff; color: #1f1f1f; border: 1px solid #747775; padding: 10px 16px; }
.btn.google-btn:hover { background: #f2f2f2; }
.google-btn svg { width: 20px; height: 20px; }
code { background: #f1f3f9; padding: 1px 5px; border-radius: 4px; font-size: 12px; }

.btn {
  display: inline-flex; align-items: center; gap: 6px; font: inherit; font-weight: 600; border-radius: 8px; padding: 8px 14px;
  border: 1px solid transparent; cursor: pointer; background: var(--primary); color: #fff;
}
.btn:hover { background: var(--primary-dark); }
.btn:disabled { opacity: .6; cursor: progress; }
.btn-outline { background: #fff; color: var(--primary); border-color: #c5cae9; }
.btn-outline:hover { background: var(--primary-light); }
.icon-btn { background: none; border: 0; padding: 6px; border-radius: 50%; cursor: pointer; color: var(--text); display: inline-flex; }
.icon-btn:hover { background: var(--primary-light); }
.spin .ico { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.banner { margin: 16px 24px 0; padding: 10px 14px; border-radius: 8px; background: #fff3e0; color: #8a3c00; border: 1px solid #ffcc80; }
.banner.error { background: #ffebee; color: var(--danger); border-color: #ef9a9a; }
.view { padding: 20px 24px 40px; outline: none; }

/* ---------- cards ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 16px; margin-bottom: 20px; }
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px 18px; position: relative; overflow: hidden; }
.card::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: var(--c, var(--primary)); }
.card .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.card .value { font-size: 24px; font-weight: 700; margin-top: 6px; font-variant-numeric: tabular-nums; }
.card .value.small { font-size: 16px; font-weight: 600; color: var(--muted); }
.card .hint { font-size: 12px; color: var(--muted); margin-top: 4px; }

.panel { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px 18px; margin-bottom: 20px; min-width: 0; }
.panel h2 { font-size: 16px; margin: 0 0 4px; }
.panel .sub { color: var(--muted); font-size: 12px; margin: 0 0 12px; }
.grid-2 { display: grid; grid-template-columns: 2fr 1fr; gap: 20px; }
.grid-2.even { grid-template-columns: 1fr 1fr; }
.grid-2 > .panel { margin-bottom: 0; }
.grid-wrap { margin-bottom: 20px; }
.chart-box { position: relative; height: 300px; }

/* ---------- tables ---------- */
.table-tools { display: flex; gap: 10px; align-items: center; margin-bottom: 10px; flex-wrap: wrap; }
.table-tools input[type=search] { flex: 1; min-width: 180px; max-width: 320px; }
.table-scroll { overflow-x: auto; border: 1px solid var(--border); border-radius: 8px; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
td.wrap { white-space: normal; min-width: 220px; }
th { background: #f8f9fc; font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; position: sticky; top: 0; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--primary); }
th .arrow { font-size: 10px; margin-left: 4px; }
td.num, th.num { text-align: right; }
tbody tr:hover { background: #fafbff; }
tfoot td { font-weight: 700; background: #f8f9fc; }
.pager { display: flex; justify-content: flex-end; align-items: center; gap: 10px; padding-top: 10px; color: var(--muted); font-size: 13px; flex-wrap: wrap; }
.pager button { padding: 4px 10px; }
.pager select { min-width: 70px; padding: 4px 6px; }

.chip { display: inline-block; padding: 2px 10px; border-radius: 12px; font-size: 12px; font-weight: 600; background: #eceff1; color: #455a64; }
.chip.ok { background: #e8f5e9; color: var(--ok); }
.chip.warn { background: #fff3e0; color: var(--warn); }
.chip.bad { background: #ffebee; color: var(--danger); }
.chip.info { background: var(--primary-light); color: var(--primary-dark); }

/* ---------- states ---------- */
.state { text-align: center; padding: 48px 16px; color: var(--muted); }
.state h3 { color: var(--text); margin: 8px 0; }
.spinner { width: 42px; height: 42px; border: 4px solid var(--primary-light); border-top-color: var(--primary); border-radius: 50%; animation: spin .9s linear infinite; margin: 0 auto; }
.empty { padding: 24px; text-align: center; color: var(--muted); background: #fafbff; border: 1px dashed var(--border); border-radius: 8px; }
.note { background: #e3f2fd; color: #0d47a1; border-radius: 8px; padding: 10px 14px; margin-bottom: 16px; font-size: 13px; }
.note.warn { background: #fff3e0; color: #8a3c00; }

.report-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px; margin-bottom: 20px; }
.report-item { text-align: left; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 14px; cursor: pointer; font: inherit; color: inherit; }
.report-item:hover { border-color: var(--primary); }
.report-item.active { border-color: var(--primary); background: var(--primary-light); }
.report-item strong { display: block; margin-bottom: 4px; }
.report-item span { color: var(--muted); font-size: 12px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 600; }

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .grid-2, .grid-2.even { grid-template-columns: 1fr; }
  .grid-2 > .panel { margin-bottom: 0; }
}
@media (max-width: 860px) {
  .sidebar { transform: translateX(-100%); box-shadow: 0 0 24px rgba(0,0,0,.3); }
  .sidebar.open { transform: none; }
  .main { margin-left: 0; }
  .topbar .menu-btn { display: inline-flex; }
  .topbar { padding: 8px 12px; gap: 8px; }
  .topbar h1 { font-size: 17px; }
  .filters { order: 3; width: 100%; }
  .filters label { flex: 1; }
  .filters select { width: 100%; min-width: 0; }
  .sync-status, .user-email { display: none; }
  .refresh-label { display: none; }
  .view { padding: 14px 12px 32px; }
  .banner { margin: 12px 12px 0; }
  .cards { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .card { padding: 12px 14px; }
  .card .value { font-size: 18px; }
  .chart-box { height: 240px; }
}
@media (max-width: 420px) {
  .cards { grid-template-columns: 1fr; }
}
@media print {
  .sidebar, .topbar, .table-tools, .pager, .actions { display: none !important; }
  .main { margin: 0; }
}
