/* ============================================================
   Deliveright Insights v3
   Palette tuned to Deliveright brand: black, silver, orange
============================================================ */
@import url('https://fonts.googleapis.com/css2?family=Fraunces:ital,opsz,wght@0,9..144,500;0,9..144,700&family=Inter+Tight:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap');

:root {
  /* Brand */
  --brand-orange: #f08c1f;
  --brand-orange-dark: #d97509;
  --brand-orange-soft: #fce7cc;
  --brand-silver: #a8a9a9;
  --brand-charcoal: #2a2a2a;
  --brand-black: #0e0e10;

  /* Surfaces */
  --bg: #fafaf9;
  --surface: #ffffff;
  --surface-2: #f5f5f3;
  --surface-3: #ebebe8;

  /* Ink */
  --ink: #1a1d23;
  --ink-2: #4a5161;
  --ink-3: #8b93a5;
  --ink-4: #b8bcc5;

  /* Lines */
  --line: #e8e6e1;
  --line-2: #d4d1ca;

  /* Accent & semantic */
  --accent: var(--brand-orange);
  --accent-soft: var(--brand-orange-soft);
  --accent-dark: var(--brand-orange-dark);
  --success: #2d7a5f;
  --success-soft: #d8ebe1;
  --warn: #b8860b;
  --warn-soft: #f7ecd0;
  --danger: #a8433f;
  --danger-soft: #f5dedb;
  --info: #4a6fa5;
  --info-soft: #dce5f3;

  /* Chart palette tuned to brand */
  --chart-1: #f08c1f;
  --chart-2: #a8a9a9;
  --chart-3: #2a2a2a;
  --chart-4: #d97509;
  --chart-5: #6a6e75;
  --chart-6: #8b4513;
  --chart-7: #2d7a5f;
  --chart-8: #4a6fa5;

  --radius: 10px;
  --radius-sm: 6px;
  --shadow-sm: 0 1px 2px rgba(14,14,16,0.04);
  --shadow: 0 1px 3px rgba(14,14,16,0.07), 0 4px 12px rgba(14,14,16,0.04);
  --shadow-lg: 0 4px 24px rgba(14,14,16,0.10);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: 'Inter Tight', -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

h1, h2, h3, h4 {
  font-family: 'Fraunces', Georgia, serif;
  font-weight: 600;
  letter-spacing: -0.015em;
  color: var(--ink);
}
h1 { font-size: 28px; line-height: 1.1; }
h3 { font-size: 15px; font-weight: 600; }

button { font-family: inherit; cursor: pointer; }

.layout {
  display: grid;
  grid-template-columns: 240px 1fr;
  min-height: 100vh;
}

/* ============ SIDEBAR ============ */
.sidebar {
  background: var(--brand-black);
  color: #e8e8e6;
  padding: 24px 14px 16px;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  border-right: 1px solid var(--brand-charcoal);
}

.brand {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: 0 6px 20px;
  border-bottom: 1px solid var(--brand-charcoal);
  margin-bottom: 16px;
}
.brand-logo {
  width: 100%;
  max-width: 160px;
  height: auto;
  object-fit: contain;
  background: var(--brand-black);
  padding: 8px 0;
}
.brand-sub {
  font-family: 'Fraunces', serif;
  font-style: italic;
  font-size: 11px;
  color: var(--brand-orange);
  letter-spacing: 0.05em;
  margin-top: -4px;
  padding-left: 4px;
}

.nav-section { margin-bottom: 8px; }
.nav-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: #7a7c80;
  padding: 12px 10px 6px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 10px;
  background: transparent;
  border: none;
  color: #c5c5c2;
  font-size: 13.5px;
  font-weight: 500;
  border-radius: var(--radius-sm);
  text-align: left;
  transition: background 0.15s, color 0.15s;
}
.nav-item:hover { background: var(--brand-charcoal); color: #fff; }
.nav-item.active {
  background: var(--brand-orange);
  color: var(--brand-black);
}
.nav-item .ico { font-size: 14px; }

.sidebar-footer { margin-top: auto; padding-top: 16px; border-top: 1px solid var(--brand-charcoal); }

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  font-size: 11.5px;
  color: #b0b2b5;
}
.user-info img { width: 24px; height: 24px; border-radius: 50%; object-fit: cover; }
.user-info .logout {
  margin-left: auto;
  color: #7a7c80;
  font-size: 10px;
  cursor: pointer;
  border: none;
  background: transparent;
}
.user-info .logout:hover { color: var(--brand-orange); }

.status {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  font-size: 10.5px;
  color: #7a7c80;
  font-family: 'JetBrains Mono', monospace;
}
.status .dot { width: 6px; height: 6px; border-radius: 50%; background: #7a7c80; }
.status.ok .dot { background: var(--success); box-shadow: 0 0 0 3px rgba(45,122,95,0.2); }
.status.bad .dot { background: var(--danger); }

.version {
  padding: 4px 10px 0;
  font-size: 10px;
  color: #555;
  font-family: 'JetBrains Mono', monospace;
  letter-spacing: 0.04em;
}

/* ============ DATE RANGE PICKER ============ */
.daterange {
  display: flex;
  align-items: flex-end;
  gap: 6px;
}
.daterange-preset {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.daterange-preset > label {
  font-size: 10.5px;
  color: var(--ink-3);
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.daterange-preset select {
  background: var(--surface);
  border: 1px solid var(--line-2);
  color: var(--ink);
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-family: inherit;
  min-width: 160px;
  outline: none;
  cursor: pointer;
}
.daterange-preset select:focus { border-color: var(--accent); }
.daterange-dates {
  display: none;
  gap: 4px;
}
.daterange-dates.show { display: flex; }
.daterange-dates .field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.daterange-dates label {
  font-size: 10.5px;
  color: var(--ink-3);
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.daterange-dates input[type="date"] {
  background: var(--surface);
  border: 1px solid var(--line-2);
  color: var(--ink);
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  font-size: 12.5px;
  font-family: inherit;
  outline: none;
  width: 130px;
}
.daterange-dates input[type="date"]:focus { border-color: var(--accent); }

/* ============ MAIN ============ */
.main { padding: 28px 36px; max-width: 1600px; width: 100%; }

.view { display: none; animation: fade-in 0.25s ease; }
.view.active { display: block; }
@keyframes fade-in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }

.page-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 24px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--line);
}
.page-header h1 { margin-bottom: 4px; }
.subtitle { color: var(--ink-3); font-size: 14px; max-width: 600px; }
.subtitle-sm { color: var(--ink-3); font-size: 12px; margin-top: 2px; }

.global-filters {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}
.global-filters label, .field label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 10.5px;
  color: var(--ink-3);
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.field { display: flex; flex-direction: column; gap: 4px; }

.select, input.text {
  background: var(--surface);
  border: 1px solid var(--line-2);
  color: var(--ink);
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-family: inherit;
  min-width: 140px;
  outline: none;
  transition: border-color 0.15s;
}
.select:focus, input.text:focus { border-color: var(--accent); }

.btn {
  background: var(--surface);
  border: 1px solid var(--line-2);
  color: var(--ink);
  padding: 7px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  transition: all 0.15s;
}
.btn:hover { background: var(--surface-2); border-color: var(--ink-3); }
.btn.primary {
  background: var(--brand-black);
  color: #fff;
  border-color: var(--brand-black);
}
.btn.primary:hover { background: var(--brand-charcoal); }
.btn.accent {
  background: var(--accent);
  color: var(--brand-black);
  border-color: var(--accent);
}
.btn.accent:hover { background: var(--accent-dark); }
.btn-link {
  background: transparent; border: none;
  color: var(--accent);
  font-size: 12px; font-weight: 600;
  padding: 4px 8px; cursor: pointer; border-radius: 4px;
}
.btn-link:hover { background: var(--accent-soft); }

.segmented {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-sm);
  padding: 2px;
}
.segmented button {
  background: transparent;
  border: none;
  padding: 5px 12px;
  font-size: 12px;
  color: var(--ink-2);
  border-radius: 4px;
  font-weight: 500;
}
.segmented button.active { background: var(--brand-black); color: #fff; }

.kbd {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  color: var(--ink-4);
  margin-left: 6px;
}

/* ============ COMBOBOX (searchable dropdown) ============ */
.combobox {
  position: relative;
  min-width: 260px;
}
.combobox-input {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-sm);
  padding: 7px 28px 7px 10px;
  font-size: 13px;
  font-family: inherit;
  color: var(--ink);
  outline: none;
  cursor: text;
  transition: border-color 0.15s;
}
.combobox-input:focus { border-color: var(--accent); }
.combobox-input::placeholder { color: var(--ink-4); }
.combobox-clear {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  background: transparent;
  border: none;
  color: var(--ink-3);
  font-size: 14px;
  cursor: pointer;
  padding: 2px 6px;
  line-height: 1;
}
.combobox-clear:hover { color: var(--ink); }
.combobox-list {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  z-index: 30;
  max-height: 320px;
  overflow-y: auto;
  display: none;
}
.combobox.open .combobox-list { display: block; }
.combobox-item {
  padding: 7px 10px;
  font-size: 13px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  gap: 8px;
  border-bottom: 1px solid var(--surface-2);
}
.combobox-item:hover, .combobox-item.highlight {
  background: var(--accent-soft);
  color: var(--brand-black);
}
.combobox-item .count {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10.5px;
  color: var(--ink-3);
}
.combobox-item .name { font-weight: 500; }
.combobox-item.all {
  font-style: italic;
  color: var(--ink-3);
}

/* ============ KPI CARDS ============ */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 14px;
  margin-bottom: 24px;
}
@media (max-width: 1200px) { .kpi-grid { grid-template-columns: repeat(3, 1fr); } }
@media (max-width: 700px)  { .kpi-grid { grid-template-columns: repeat(2, 1fr); } }

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow-sm);
  position: relative;
  overflow: hidden;
  transition: box-shadow 0.2s;
}
.kpi-card:hover { box-shadow: var(--shadow); }
.kpi-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0;
  width: 3px; height: 100%;
  background: var(--accent);
  opacity: 0.6;
}
.kpi-label {
  font-size: 10.5px;
  font-weight: 600;
  color: var(--ink-3);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  margin-bottom: 10px;
}
.kpi-value {
  font-family: 'Fraunces', serif;
  font-size: 28px;
  font-weight: 600;
  line-height: 1;
  letter-spacing: -0.02em;
  color: var(--ink);
}
.kpi-sub {
  font-size: 11px;
  color: var(--ink-3);
  margin-top: 6px;
  font-family: 'JetBrains Mono', monospace;
}
.kpi-card.loading .kpi-value { color: var(--ink-4); }

/* ============ CARDS ============ */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin-bottom: 20px;
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  gap: 12px;
}
.card-header h3 {
  font-family: 'Fraunces', serif;
  font-weight: 600;
  font-size: 14px;
}

.risk-card {
  border: 1px solid var(--warn-soft);
  background: linear-gradient(180deg, #fffdf7 0%, #ffffff 100%);
}
.alert-card {
  border: 1px solid var(--danger-soft);
  background: linear-gradient(180deg, #fff9f8 0%, #ffffff 100%);
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 20px;
}
@media (max-width: 960px) { .grid-2 { grid-template-columns: 1fr; } }

.chart-wrap {
  padding: 14px 18px 18px;
  height: 280px;
  position: relative;
}
.chart-wrap.large { height: 360px; }

/* ============ TABLES ============ */
.table-wrap {
  overflow-x: auto;
  max-height: 600px;
  overflow-y: auto;
}
table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
table.data th, table.data td {
  padding: 10px 14px;
  text-align: left;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
table.data th {
  font-size: 10.5px;
  font-weight: 600;
  color: var(--ink-3);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  background: var(--surface-2);
  position: sticky;
  top: 0;
  user-select: none;
}
table.data td { font-family: 'JetBrains Mono', monospace; font-size: 12.5px; color: var(--ink-2); }
table.data td.text { font-family: 'Inter Tight', sans-serif; color: var(--ink); font-weight: 500; font-size: 13px; }
table.data tr:hover td { background: var(--surface-2); }
table.data td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data td.null { color: var(--ink-4); font-style: italic; }
table.data a { color: var(--accent); text-decoration: none; font-weight: 600; }
table.data a:hover { text-decoration: underline; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
  font-family: 'JetBrains Mono', monospace;
}
.badge.good { background: var(--success-soft); color: var(--success); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad  { background: var(--danger-soft); color: var(--danger); }
.badge.info { background: var(--info-soft); color: var(--info); }
.badge.critical {
  background: var(--danger);
  color: #fff;
  animation: pulse-crit 2s ease-in-out infinite;
}
@keyframes pulse-crit {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.75; }
}

/* ============ QUERY VIEW ============ */
.query-layout {
  display: grid;
  grid-template-columns: 1fr 300px;
  gap: 20px;
}
@media (max-width: 1100px) { .query-layout { grid-template-columns: 1fr; } }

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
}
.toolbar .spacer { flex: 1; }

#q-editor {
  width: 100%;
  height: 180px;
  background: var(--surface);
  border: none;
  padding: 14px 18px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 13px;
  color: var(--ink);
  resize: vertical;
  outline: none;
  line-height: 1.6;
}

#q-result-header { font-size: 12px; color: var(--ink-3); font-family: 'JetBrains Mono', monospace; padding: 14px 18px; border-bottom: 1px solid var(--line); }
#q-result-header.ok { color: var(--success); }
#q-result-header.err { color: var(--danger); }
.err-box { padding: 16px; color: var(--danger); font-family: 'JetBrains Mono', monospace; font-size: 12px; white-space: pre-wrap; }
.empty-state { padding: 50px 20px; text-align: center; color: var(--ink-3); }
.empty-state-good {
  padding: 30px 20px;
  text-align: center;
  color: var(--success);
  font-weight: 500;
  background: var(--success-soft);
  border-radius: var(--radius-sm);
  margin: 10px;
}

.views-pane .card { margin-bottom: 14px; }
.views-list {
  max-height: 560px;
  overflow-y: auto;
  padding: 6px;
}
.view-entry {
  padding: 8px 10px;
  border-radius: 4px;
  cursor: pointer;
  border-bottom: 1px solid var(--line);
}
.view-entry:hover { background: var(--surface-2); }
.view-entry-name { font-family: 'JetBrains Mono', monospace; font-size: 12px; color: var(--ink); font-weight: 500; }
.view-entry-label { font-size: 11px; color: var(--ink-3); margin-top: 2px; }
.view-entry-cols {
  font-size: 10px;
  color: var(--ink-3);
  font-family: 'JetBrains Mono', monospace;
  margin-top: 4px;
  line-height: 1.4;
  display: none;
}
.view-entry.open .view-entry-cols { display: block; }

/* ============ DRAWER ============ */
.drawer {
  position: fixed;
  top: 0; right: -460px;
  width: 440px;
  height: 100vh;
  background: var(--surface);
  border-left: 1px solid var(--line);
  box-shadow: -4px 0 24px rgba(0,0,0,0.08);
  z-index: 50;
  transition: right 0.25s;
  display: flex;
  flex-direction: column;
}
.drawer.open { right: 0; }
.drawer-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.drawer-body { padding: 12px; flex: 1; overflow-y: auto; }
.drawer-item {
  padding: 10px 12px;
  border-radius: 4px;
  cursor: pointer;
  border-bottom: 1px solid var(--line);
  font-size: 12px;
}
.drawer-item:hover { background: var(--surface-2); }
.drawer-item .meta { color: var(--ink-3); font-size: 10px; margin-bottom: 4px; font-family: 'JetBrains Mono', monospace; }
.drawer-item .sql { font-family: 'JetBrains Mono', monospace; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ============ TOAST ============ */
#toast-host { position: fixed; bottom: 20px; right: 20px; z-index: 100; display: flex; flex-direction: column; gap: 8px; }
.toast {
  padding: 10px 16px;
  background: var(--brand-black);
  color: #fff;
  border-radius: var(--radius-sm);
  font-size: 12.5px;
  font-weight: 500;
  box-shadow: var(--shadow);
  animation: slide-in 0.25s ease;
}
.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }
@keyframes slide-in { from { opacity: 0; transform: translateX(20px); } to { opacity: 1; transform: translateX(0); } }

.spinner {
  width: 14px; height: 14px;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  display: inline-block;
  vertical-align: middle;
  margin-right: 6px;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-state { padding: 40px; text-align: center; color: var(--ink-3); font-size: 13px; }

/* ============ LOGIN PAGE ============ */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--brand-black);
  padding: 20px;
}
.login-card {
  background: var(--surface);
  border-radius: 12px;
  padding: 40px;
  max-width: 400px;
  width: 100%;
  text-align: center;
  box-shadow: var(--shadow-lg);
}
.login-card img { width: 140px; margin-bottom: 20px; }
.login-card h1 {
  font-size: 22px;
  margin-bottom: 10px;
}
.login-card p {
  color: var(--ink-3);
  margin-bottom: 24px;
  font-size: 13px;
}
.login-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: 100%;
  padding: 12px;
  background: #fff;
  border: 1px solid var(--line-2);
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  color: var(--ink);
  text-decoration: none;
  transition: all 0.15s;
}
.login-btn:hover {
  background: var(--surface-2);
  border-color: var(--brand-orange);
}
.login-btn img { width: 20px; height: 20px; margin: 0; }
