:root {
  color-scheme: light dark;
  --ink: #10151c;
  --paper: #f3f2ec;
  --paper-raised: #ffffff;
  --sidebar-bg: #171d26;
  --sidebar-fg: #e7e5dc;
  --sidebar-muted: #9aa3ad;
  --line: #dcdad0;
  --muted: #5c6570;
  --amber: #c9781f;
  --amber-soft: #f4e3c8;
  --teal: #1f7d6f;
  --red: #b3432f;
  --focus: #2563eb;
  --mono: "SFMono-Regular", "JetBrains Mono", Consolas, "Liberation Mono", Menlo, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  --sidebar-w: 232px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --ink: #eceae2;
    --paper: #0d1117;
    --paper-raised: #151b23;
    --sidebar-bg: #0a0d12;
    --line: #2a323d;
    --muted: #8b93a1;
    --amber: #e8a33d;
    --amber-soft: #3a2c15;
    --teal: #4fc7b3;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--sans);
  background: var(--paper);
  color: var(--ink);
  line-height: 1.5;
}

a { color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

h2, h3 { letter-spacing: -0.01em; }

/* Author-stylesheet `display` rules always beat the UA [hidden] rule,
   regardless of specificity — origin outranks specificity in the
   cascade. .app-shell/.auth-gate set `display` unconditionally below, so
   without this, toggling the `hidden` property from JS has no visual
   effect at all: both panels stay laid out, stacked in document flow. */
.app-shell[hidden], .auth-gate[hidden] { display: none; }

/* ---- auth gate ---- */

.auth-gate {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.auth-gate-card {
  width: 100%;
  max-width: 360px;
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 32px 28px;
  text-align: center;
}
.auth-gate-brand { justify-content: center; margin-bottom: 18px; color: var(--ink); }
.auth-gate-card h1 { font-size: 20px; margin: 0 0 6px; }
.auth-gate-card .hint { margin-bottom: 18px; }
.auth-gate-card #google-signin-btn { display: flex; justify-content: center; margin-bottom: 4px; }
.auth-gate-card #operator-login { text-align: left; margin-top: 18px; }
.auth-gate-card #operator-login input { width: 100%; margin-bottom: 8px; padding: 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--paper); color: var(--ink); }
.auth-gate-card hr { border: none; border-top: 1px solid var(--line); margin: 18px 0; }
.auth-gate-back { display: inline-block; margin-top: 20px; font-size: 13px; color: var(--muted); text-decoration: none; }
.auth-gate-back:hover { color: var(--ink); }
.auth-gate-legal { font-size: 11.5px; color: var(--muted); margin: 14px 0 0; }
.auth-gate-legal a { color: var(--muted); text-decoration: underline; }
.auth-gate-legal a:hover { color: var(--ink); }
#auth-gate-status { min-height: 1em; color: var(--red); }

/* ---- app shell ---- */

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh;
}

.sidebar {
  background: var(--sidebar-bg);
  color: var(--sidebar-fg);
  display: flex;
  flex-direction: column;
  padding: 20px 16px;
  gap: 20px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}
.sidebar-brand {
  font-family: var(--mono);
  font-weight: 600;
  font-size: 15px;
  padding: 0 4px;
}
.sidebar-brand .cursor { color: var(--amber); }

#top-nav { display: flex; flex-direction: column; gap: 2px; }
#top-nav button {
  text-align: left;
  background: transparent;
  border: none;
  color: var(--sidebar-muted);
  padding: 9px 10px;
  border-radius: 6px;
  font-size: 14px;
}
#top-nav button:hover { background: rgba(255, 255, 255, 0.06); color: var(--sidebar-fg); }
#top-nav button.active { background: rgba(255, 255, 255, 0.1); color: var(--sidebar-fg); font-weight: 500; }

.deletion-banner {
  margin: 4px 0 0;
  padding: 10px 12px;
  border-radius: 6px;
  background: color-mix(in srgb, var(--red) 20%, transparent);
  color: color-mix(in srgb, var(--red) 65%, var(--sidebar-fg) 35%);
  font-size: 12.5px;
  line-height: 1.5;
}

#connection-bar {
  margin-top: auto;
  padding-top: 16px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 13px;
}
#connection-bar input {
  padding: 7px 9px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.04);
  color: var(--sidebar-fg);
  font-size: 13px;
  width: 100%;
}
#connection-status { font-size: 12px; color: var(--sidebar-muted); }
#signed-in-as { font-size: 13px; color: var(--sidebar-fg); word-break: break-word; }
#operator-login { font-size: 12px; color: var(--sidebar-muted); }
#operator-login summary { cursor: pointer; padding: 4px 0; }
#operator-login input { margin-top: 6px; }
#sign-out-btn { align-self: flex-start; }

/* google's rendered button ignores our color-scheme — give it breathing room */
#google-signin-btn { min-height: 40px; }

main#app { max-width: 1040px; margin: 0 auto; padding: clamp(20px, 3vw, 40px); width: 100%; }

/* ---- drawer toggle (mobile only) ---- */

.drawer-toggle {
  display: none;
  position: fixed;
  top: 14px;
  left: 14px;
  z-index: 40;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  width: 38px;
  height: 38px;
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}
.drawer-toggle span { display: block; height: 2px; background: var(--ink); margin: 0 8px; }
.drawer-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 29;
}

/* ---- generic controls ---- */

.panel-header { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.panel-header h2 { margin: 0; font-size: 20px; }

button {
  cursor: pointer;
  border: 1px solid var(--line);
  background: var(--paper-raised);
  color: var(--ink);
  border-radius: 6px;
  padding: 7px 13px;
  font-size: 13px;
  transition: border-color 0.15s;
}
button:hover { border-color: var(--ink); }
button.active { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }

/* ---- projects list ---- */

#project-list { list-style: none; padding: 0; margin: 0; }
#project-list li {
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  margin-bottom: 8px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  background: var(--paper-raised);
}
#project-list li:hover { border-color: var(--amber); }
#project-list li span:last-child { color: var(--muted); font-size: 12px; font-family: var(--mono); }

.empty-state {
  color: var(--muted);
  font-size: 13.5px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  padding: 20px;
  text-align: center;
  margin: 4px 0;
}

/* ---- project view ---- */

#project-view .panel-header { margin-bottom: 4px; }
#back-btn, #back-to-reports-btn { margin-bottom: 16px; background: transparent; }
#project-name { margin: 0 0 16px; font-size: 22px; }

#project-tabs { display: flex; gap: 4px; margin: 0 0 20px; border-bottom: 1px solid var(--line); overflow-x: auto; }
#project-tabs button {
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  padding: 10px 4px;
  margin-right: 20px;
  color: var(--muted);
  white-space: nowrap;
}
#project-tabs button:hover { color: var(--ink); border-color: var(--line); }
#project-tabs button.active { color: var(--ink); border-bottom-color: var(--amber); font-weight: 500; }

.table-scroll { overflow-x: auto; border: 1px solid var(--line); border-radius: 8px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; min-width: 560px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td:nth-child(2) { white-space: normal; min-width: 220px; }
th { color: var(--muted); font-weight: 500; background: var(--paper-raised); }
tbody tr:hover { background: var(--paper-raised); }
tbody tr:last-child td { border-bottom: none; }

.filters { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
select {
  padding: 7px 9px; border: 1px solid var(--line); border-radius: 6px;
  background: var(--paper-raised); color: var(--ink); font-size: 13px;
}

textarea {
  width: 100%; min-height: 70px; border: 1px solid var(--line); border-radius: 6px;
  padding: 9px; background: var(--paper-raised); color: var(--ink); font-family: inherit; font-size: 13px;
  margin-bottom: 10px;
}

.settings-group {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 18px 20px;
  margin-bottom: 16px;
  background: var(--paper-raised);
}

.danger-zone { border-color: color-mix(in srgb, var(--red) 40%, var(--line)); }
.danger-zone h3 { color: var(--red); }
.danger-zone .btn-danger {
  border-color: var(--red); color: var(--red); background: transparent;
}
.danger-zone .btn-danger:hover { background: color-mix(in srgb, var(--red) 10%, transparent); }
.danger-zone-confirm { margin-top: 12px; padding-top: 12px; border-top: 1px dashed var(--line); }

/* ---- billing ---- */

#billing-plans.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-top: 12px;
}
#billing-plans .plan {
  position: relative;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 20px;
  background: var(--paper-raised);
}
#billing-plans .plan-featured { background: var(--amber-soft); }
#billing-plans .plan h3 { margin: 0; font-size: 14px; color: var(--muted); }
#billing-plans .plan-featured h3 { color: var(--ink); }
#billing-plans .price { font-family: var(--mono); font-size: 26px; font-weight: 600; margin: 10px 0 14px; }
#billing-plans ul { list-style: none; padding: 0; margin: 0 0 16px; font-size: 13px; color: var(--muted); flex-grow: 1; }
#billing-plans ul li { padding: 4px 0 4px 18px; position: relative; }
#billing-plans ul li::before { content: "→"; color: var(--amber); position: absolute; left: 0; }
#billing-plans .badge {
  position: absolute; top: 14px; right: 14px;
  background: var(--amber); color: var(--paper); font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.03em;
}
#billing-plans button { width: 100%; }
#billing-current-plan { font-size: 14px; color: var(--ink); font-weight: 500; }
.settings-group h3 { margin: 0 0 6px; font-size: 15px; }
.settings-group:first-child { padding: 14px 20px; }

#activity-feed, #ops-activity-feed { list-style: none; padding: 0; margin: 0; }
#activity-feed li, #ops-activity-feed li {
  padding: 12px 14px; border-left: 3px solid var(--amber); background: var(--amber-soft);
  border-radius: 6px; margin-bottom: 8px; font-size: 13.5px;
}
#ops-activity-feed li { border-left-color: var(--teal); background: color-mix(in srgb, var(--teal) 12%, var(--paper-raised)); }
.meta { color: var(--muted); font-size: 12px; margin-top: 4px; }

#keys-list, #rules-list { list-style: none; padding: 0; margin: 0 0 6px; }
#keys-list li, #rules-list li {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding: 10px 12px; border: 1px solid var(--line);
  border-radius: 6px; margin-bottom: 6px; font-size: 13px; flex-wrap: wrap;
}

#new-rule-form { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
#new-rule-form input, #new-rule-form select { flex: 1 1 140px; }
#new-rule-form input[type="number"] { flex: 0 1 100px; }

pre {
  background: color-mix(in srgb, var(--ink) 6%, var(--paper-raised));
  padding: 14px; border-radius: 8px; overflow-x: auto; font-size: 12px; font-family: var(--mono);
}

dialog {
  border: 1px solid var(--line); border-radius: 10px; padding: 22px; background: var(--paper-raised); color: var(--ink);
  max-width: 480px; width: 90vw;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog h3 { margin-top: 0; }
dialog input, dialog textarea { width: 100%; margin-bottom: 10px; padding: 9px; border: 1px solid var(--line); border-radius: 6px; background: var(--paper); color: var(--ink); font: inherit; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }

.copy-btn { font-size: 12px; }

#report-detail-view { max-width: 760px; }
#report-detail h2 { font-size: 18px; word-break: break-all; }
#report-detail .description { white-space: pre-wrap; }
#report-detail pre { white-space: pre-wrap; }

.badge {
  display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 11px;
  background: color-mix(in srgb, var(--muted) 15%, transparent); font-weight: 500;
}
.badge.status-categorized { background: color-mix(in srgb, var(--teal) 20%, transparent); color: var(--teal); }
.badge.status-failed { background: color-mix(in srgb, var(--red) 18%, transparent); color: var(--red); }
.badge.status-queued, .badge.status-processing { background: var(--amber-soft); color: var(--amber); }
.badge.status-escalated { background: color-mix(in srgb, var(--red) 12%, transparent); color: var(--red); }

.hint { color: var(--muted); font-size: 12.5px; margin: 0 0 4px; }

#support-inbox { list-style: none; padding: 0; margin: 0; }
.support-item {
  border: 1px solid var(--line); border-radius: 8px; padding: 14px; margin-bottom: 10px; font-size: 13px;
  background: var(--paper-raised);
}
.support-item .support-message { color: var(--muted); margin: 8px 0; white-space: pre-wrap; }
.support-item textarea { width: 100%; min-height: 60px; margin-bottom: 6px; }

/* ---- responsive ---- */

@media (max-width: 900px) {
  .app-shell { grid-template-columns: 1fr; }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    width: min(280px, 82vw);
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    z-index: 30;
    padding-top: 64px;
  }
  .sidebar.open { transform: translateX(0); }

  .drawer-toggle { display: flex; }
  .drawer-backdrop:not([hidden]) { display: block; }

  main#app { padding-top: 72px; }
}

@media (max-width: 560px) {
  main#app { padding: 72px 16px 24px; }
  .panel-header { flex-direction: column; align-items: flex-start; }
  .settings-group { padding: 16px; }
  dialog { padding: 18px; }
}
