* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px;
  color: #e5e5e5;
  background: #0f0f10;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.muted { color: #888; font-size: 13px; }

/* ── GATE ── */
.gate {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; padding: 24px;
  background: linear-gradient(135deg, #0f0f10 0%, #1a1410 100%);
}
.gate-box {
  background: #1a1a1b; border: 1px solid #2a2a2b;
  padding: 48px; border-radius: 12px; width: 100%; max-width: 440px;
}
.gate-box h2 { margin: 0 0 8px; }
.gate-copy { color: #aaa; margin: 20px 0 24px; }
.gate-box input {
  width: 100%; padding: 12px 14px; background: #0f0f10;
  border: 1px solid #333; color: #fff; border-radius: 6px;
  font-size: 14px; margin-bottom: 12px; font-family: monospace;
}
.gate-box input:focus { outline: none; border-color: #B8860B; }
.gate-advanced { margin-top: 8px; margin-bottom: 16px; }
.gate-advanced label { font-size: 12px; color: #888; display: block; margin-bottom: 4px; }
.gate-box button.primary {
  width: 100%; padding: 12px; background: #B8860B; color: #0f0f10;
  border: none; border-radius: 6px; font-size: 14px; font-weight: 600;
  cursor: pointer;
}
.gate-box button.primary:hover { background: #d09a16; }
.gate-status { margin-top: 12px; min-height: 20px; font-size: 13px; }
.gate-status.ok { color: #27AE60; }
.gate-status.err { color: #C0392B; }
.gate-footer { margin-top: 24px; font-size: 11px; color: #666; line-height: 1.5; }

/* ── BRAND ── */
.brand { display: flex; align-items: center; gap: 14px; margin-bottom: 16px; }
.brand-mark {
  width: 40px; height: 40px; background: #B8860B; color: #0f0f10;
  border-radius: 8px; display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 16px;
}
.brand.small .brand-mark { width: 32px; height: 32px; font-size: 13px; }
.brand-title { font-weight: 700; font-size: 16px; line-height: 1.2; }
.brand-sub { color: #888; font-size: 11px; }

/* ── APP LAYOUT ── */
.app { display: flex; min-height: 100vh; }
.sidebar {
  width: 240px; background: #141414; border-right: 1px solid #222;
  padding: 24px 16px; display: flex; flex-direction: column;
}
.sidebar nav { flex: 1; margin-top: 24px; display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: block; padding: 10px 14px; color: #bbb;
  text-decoration: none; border-radius: 6px; font-size: 13px;
  transition: background 0.15s;
}
.nav-item:hover { background: #1e1e1e; color: #fff; }
.nav-item.active { background: rgba(184, 134, 11, 0.15); color: #d09a16; border-left: 2px solid #B8860B; padding-left: 12px; }
.sidebar-footer { border-top: 1px solid #222; padding-top: 16px; margin-top: 16px; }
.sidebar-footer a { color: #888; text-decoration: none; font-size: 12px; }
.sidebar-footer a:hover { color: #C0392B; }
.sidebar-hint { color: #555; font-size: 11px; margin-bottom: 6px; }

/* ── MAIN ── */
.main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
.main-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 20px 32px; border-bottom: 1px solid #222;
}
.main-header h1 { margin: 0; font-size: 20px; font-weight: 600; }
.main-header-actions { display: flex; align-items: center; gap: 16px; }
.view { padding: 24px 32px 48px; overflow: auto; flex: 1; }

button.ghost {
  background: transparent; border: 1px solid #333; color: #bbb;
  padding: 7px 14px; border-radius: 6px; cursor: pointer; font-size: 13px;
}
button.ghost:hover { border-color: #B8860B; color: #d09a16; }

.loading { color: #666; padding: 40px; text-align: center; }

/* ── KPI CARDS ── */
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; margin-bottom: 32px; }
.kpi-card {
  background: #1a1a1b; border: 1px solid #252525; padding: 20px; border-radius: 10px;
}
.kpi-label { color: #888; font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; }
.kpi-value { font-size: 28px; font-weight: 600; margin: 8px 0 4px; }
.kpi-delta { font-size: 12px; }
.kpi-delta.positive { color: #27AE60; }
.kpi-delta.negative { color: #C0392B; }

/* ── TABLES ── */
.table-wrap { background: #1a1a1b; border: 1px solid #252525; border-radius: 10px; overflow: hidden; }
.table-title { padding: 16px 20px; border-bottom: 1px solid #252525; font-weight: 600; font-size: 14px; }
.table-title .subtle { font-weight: 400; color: #888; font-size: 12px; margin-left: 8px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
  text-align: left; padding: 12px 16px; background: #141414; color: #888;
  font-weight: 500; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px;
  border-bottom: 1px solid #252525;
}
td { padding: 12px 16px; border-bottom: 1px solid #222; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: #1e1e1e; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

.status-pill {
  display: inline-block; padding: 3px 10px; border-radius: 10px;
  font-size: 11px; font-weight: 600; text-transform: uppercase;
}
.status-pill.HEALTHY { background: rgba(39, 174, 96, 0.15); color: #27AE60; }
.status-pill.WATCH   { background: rgba(230, 126, 34, 0.15); color: #E67E22; }
.status-pill.CONCERN { background: rgba(192, 57, 43, 0.15); color: #C0392B; }

.alert {
  background: rgba(192, 57, 43, 0.10); border: 1px solid rgba(192, 57, 43, 0.3);
  padding: 14px 18px; border-radius: 8px; margin-bottom: 16px;
  color: #E8B4B0;
}
.alert.warn { background: rgba(230, 126, 34, 0.10); border-color: rgba(230, 126, 34, 0.3); color: #E8C4A0; }
.alert strong { display: block; color: #fff; margin-bottom: 4px; }

.section { margin-top: 32px; }
.section h2 { font-size: 16px; margin: 0 0 16px; color: #e5e5e5; font-weight: 600; }

/* Back button for drill-down */
.back-btn {
  background: none; border: none; color: #d09a16; cursor: pointer;
  font-size: 13px; padding: 0; margin-bottom: 16px;
}
.back-btn:hover { text-decoration: underline; }

/* Detail rows */
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 16px; }

.clickable { cursor: pointer; }
.clickable:hover td:first-child { color: #d09a16; }

/* Spinner */
.spinner {
  display: inline-block; width: 16px; height: 16px; border: 2px solid #333;
  border-top-color: #B8860B; border-radius: 50%; animation: spin 0.8s linear infinite;
  vertical-align: middle; margin-right: 8px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* scrollbar */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: #0f0f10; }
::-webkit-scrollbar-thumb { background: #2a2a2b; border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: #3a3a3b; }

/* Charts */
.charts-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 24px; }
.chart-box { background: #1a1a1b; border: 1px solid #252525; border-radius: 10px; padding: 16px 20px 20px; }
.chart-title { font-size: 13px; color: #aaa; margin-bottom: 12px; font-weight: 500; }
.chart-wrap { height: 240px; position: relative; }
.chart-wrap.tall { height: 480px; }
@media (max-width: 900px) { .charts-row { grid-template-columns: 1fr; } }

/* Chat launcher + panel */
.chat-fab {
    position: fixed; bottom: 24px; right: 24px;
    width: 56px; height: 56px; border-radius: 50%;
    background: #B8860B; color: #0f0f10;
    display: flex; align-items: center; justify-content: center;
    font-size: 24px; cursor: pointer;
    box-shadow: 0 4px 16px rgba(0,0,0,0.4);
    z-index: 100; border: none;
    transition: transform 0.2s;
}
.chat-fab:hover { transform: scale(1.05); background: #d09a16; }

.chat-panel {
    position: fixed; top: 0; right: 0; bottom: 0;
    width: 460px; max-width: 95vw;
    background: #141414; border-left: 1px solid #222;
    display: flex; flex-direction: column;
    transform: translateX(100%); transition: transform 0.25s ease-out;
    z-index: 200;
}
.chat-panel.open { transform: translateX(0); }
.chat-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 18px 20px; border-bottom: 1px solid #222;
}
.chat-title { font-weight: 600; font-size: 15px; display: flex; align-items: center; gap: 10px; }
.chat-title .dot { width: 8px; height: 8px; border-radius: 50%; background: #27AE60; }
.chat-close { background: none; border: none; color: #888; font-size: 24px; cursor: pointer; }
.chat-close:hover { color: #fff; }

.chat-messages {
    flex: 1; overflow-y: auto; padding: 16px;
    display: flex; flex-direction: column; gap: 12px;
}
.chat-msg { max-width: 90%; padding: 10px 14px; border-radius: 10px; font-size: 13px; line-height: 1.5; }
.chat-msg.user {
    background: #B8860B; color: #0f0f10; align-self: flex-end; font-weight: 500;
}
.chat-msg.assistant {
    background: #1e1e1f; border: 1px solid #2a2a2b; color: #ddd; align-self: flex-start;
}
.chat-msg.tool {
    align-self: flex-start; background: transparent; border: 1px dashed #333;
    color: #888; font-size: 11px; font-family: monospace; padding: 6px 10px;
}
.chat-msg.assistant p { margin: 0 0 8px; }
.chat-msg.assistant p:last-child { margin: 0; }
.chat-msg.assistant pre, .chat-msg.assistant code {
    background: #0f0f10; padding: 2px 5px; border-radius: 3px; font-size: 12px;
}
.chat-msg.assistant pre { padding: 10px; overflow-x: auto; margin: 8px 0; }
.chat-msg.assistant table { font-size: 12px; margin: 8px 0; border-collapse: collapse; width: 100%; }
.chat-msg.assistant table th, .chat-msg.assistant table td { padding: 6px 8px; border-bottom: 1px solid #2a2a2b; text-align: left; }
.chat-msg.assistant ul { padding-left: 20px; margin: 8px 0; }
.chat-msg.assistant strong { color: #fff; }

.chat-input-wrap {
    padding: 12px; border-top: 1px solid #222;
}
.chat-suggestions { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.chat-chip {
    background: #1e1e1f; border: 1px solid #2a2a2b; color: #aaa;
    padding: 6px 12px; border-radius: 14px; cursor: pointer;
    font-size: 11px; transition: all 0.15s;
}
.chat-chip:hover { border-color: #B8860B; color: #d09a16; }
.chat-input-row { display: flex; gap: 8px; }
.chat-input {
    flex: 1; background: #0f0f10; border: 1px solid #333; border-radius: 8px;
    padding: 10px 12px; color: #fff; font-size: 13px; font-family: inherit; resize: none;
}
.chat-input:focus { outline: none; border-color: #B8860B; }
.chat-send {
    background: #B8860B; color: #0f0f10; border: none; border-radius: 8px;
    padding: 0 16px; cursor: pointer; font-weight: 600; font-size: 13px;
}
.chat-send:disabled { opacity: 0.5; cursor: not-allowed; }

.chat-thinking { color: #888; font-size: 12px; display: flex; align-items: center; gap: 8px; padding: 4px; }

/* AI-branded nav item */
.nav-item.ai {
    background: linear-gradient(90deg, rgba(184,134,11,0.08), rgba(184,134,11,0));
    color: #d09a16;
    font-weight: 500;
}
.nav-item.ai:hover { background: linear-gradient(90deg, rgba(184,134,11,0.15), rgba(184,134,11,0)); color: #e0aa26; }

/* Report view styling */
.report-actions { display: flex; gap: 12px; margin-bottom: 24px; flex-wrap: wrap; }
.report-action {
    background: linear-gradient(135deg, #B8860B 0%, #8b6308 100%);
    color: #0f0f10; border: none; border-radius: 10px;
    padding: 16px 20px; cursor: pointer; font-weight: 600; font-size: 14px;
    display: flex; align-items: center; gap: 10px; flex-direction: column;
    text-align: left; min-width: 200px;
}
.report-action:hover:not(:disabled) { transform: translateY(-2px); box-shadow: 0 4px 16px rgba(184,134,11,0.3); }
.report-action:disabled { opacity: 0.6; cursor: not-allowed; }
.report-action .ra-title { font-size: 14px; }
.report-action .ra-sub { font-size: 11px; opacity: 0.7; font-weight: normal; }

.report-output {
    background: #1a1a1b; border: 1px solid #252525; border-radius: 10px;
    padding: 24px 28px; margin-top: 20px;
    font-size: 14px; line-height: 1.7; color: #ddd;
    min-height: 100px;
}
.report-output h1, .report-output h2, .report-output h3 { color: #fff; margin: 16px 0 10px; }
.report-output h1 { font-size: 20px; }
.report-output h2 { font-size: 16px; margin-top: 24px; }
.report-output ul { padding-left: 20px; margin: 10px 0; }
.report-output li { margin-bottom: 4px; }
.report-output strong { color: #d09a16; }
.report-output code { background: #0f0f10; padding: 1px 5px; border-radius: 3px; font-size: 13px; }

.report-meta { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; color: #888; font-size: 12px; }
.copy-btn { background: transparent; border: 1px solid #333; color: #bbb; padding: 6px 12px; border-radius: 6px; cursor: pointer; font-size: 12px; }
.copy-btn:hover { border-color: #B8860B; color: #d09a16; }

.msg-actions { display: flex; gap: 6px; margin-top: 8px; justify-content: flex-end; }
.msg-actions button {
    background: transparent; border: 1px solid #333; color: #888;
    padding: 4px 10px; border-radius: 5px; font-size: 11px; cursor: pointer;
    font-family: inherit;
}
.msg-actions button:hover { border-color: #B8860B; color: #d09a16; }

/* LIGHT THEME */
body.light { background: #f5f5f3; color: #222; }
body.light .sidebar { background: #ffffff; border-right-color: #e4e4e4; }
body.light .brand-title { color: #111; }
body.light .brand-sub { color: #888; }
body.light .nav-item { color: #555; }
body.light .nav-item:hover { background: #f0ebd8; color: #222; }
body.light .nav-item.active { background: rgba(184,134,11,0.12); color: #8b6308; }
body.light .main-header { border-bottom-color: #e4e4e4; }
body.light .main-header h1 { color: #111; }
body.light button.ghost { border-color: #d8d8d5; color: #555; }
body.light button.ghost:hover { border-color: #B8860B; color: #8b6308; }
body.light .kpi-card { background: #fff; border-color: #e4e4e4; }
body.light .kpi-label { color: #888; }
body.light .kpi-value { color: #111; }
body.light .table-wrap, body.light .chart-box { background: #fff; border-color: #e4e4e4; }
body.light .table-title { border-bottom-color: #e4e4e4; color: #111; }
body.light th { background: #faf8f2; color: #555; border-bottom-color: #e4e4e4; }
body.light td { border-bottom-color: #f0efed; color: #222; }
body.light tr:hover td { background: #faf8f2; }
body.light .chart-title { color: #555; }
body.light .section h2 { color: #111; }
body.light .muted { color: #888; }
body.light .sidebar-footer { border-top-color: #e4e4e4; }
body.light .sidebar-hint { color: #aaa; }
body.light .alert { background: rgba(192,57,43,0.08); border-color: rgba(192,57,43,0.25); color: #7a1d15; }
body.light .alert.warn { background: rgba(230,126,34,0.08); border-color: rgba(230,126,34,0.25); color: #6b3a0c; }
body.light .alert strong { color: #111; }
body.light ::-webkit-scrollbar-track { background: #f5f5f3; }
body.light ::-webkit-scrollbar-thumb { background: #d8d8d5; }
body.light .report-output { background: #fff; border-color: #e4e4e4; color: #222; }
body.light .report-output h1, body.light .report-output h2, body.light .report-output h3 { color: #111; }
body.light .chat-panel { background: #ffffff; border-left-color: #e4e4e4; }
body.light .chat-header { border-bottom-color: #e4e4e4; }
body.light .chat-title { color: #111; }
body.light .chat-msg.assistant { background: #f7f5ee; border-color: #e4e4e4; color: #222; }
body.light .chat-msg.tool { color: #888; border-color: #d8d8d5; }
body.light .chat-input { background: #faf8f2; border-color: #d8d8d5; color: #222; }
body.light .chat-input-wrap { border-top-color: #e4e4e4; }
body.light .chat-chip { background: #faf8f2; border-color: #e4e4e4; color: #555; }
body.light .gate { background: linear-gradient(135deg, #f5f5f3 0%, #fdf5e5 100%); }
body.light .gate-box { background: #fff; border-color: #e4e4e4; }
body.light .gate-box input { background: #faf8f2; border-color: #d8d8d5; color: #222; }
body.light .gate-copy { color: #555; }
body.light .gate-footer { color: #aaa; }
body.light .msg-actions button { border-color: #d8d8d5; color: #888; }

/* Correction FAB */
.correction-fab {
    position: fixed; bottom: 24px; right: 96px;
    width: 48px; height: 48px; border-radius: 50%;
    background: #E67E22; color: #0f0f10;
    display: flex; align-items: center; justify-content: center;
    font-size: 20px; cursor: pointer;
    box-shadow: 0 4px 16px rgba(0,0,0,0.4); z-index: 100; border: none;
}
.correction-fab:hover { transform: scale(1.05); background: #f39136; }

.modal {
    position: fixed; inset: 0; background: rgba(0,0,0,0.7);
    display: flex; align-items: center; justify-content: center;
    z-index: 300;
}
.modal-box {
    background: #1a1a1b; border: 1px solid #2a2a2b; border-radius: 12px;
    padding: 24px 28px; width: 540px; max-width: 92vw;
}
.modal-header {
    display: flex; justify-content: space-between; align-items: center;
    font-weight: 600; font-size: 16px; margin-bottom: 8px;
}
body.light .modal-box { background: #fff; border-color: #e4e4e4; color: #222; }
body.light .modal-box textarea { background: #faf8f2; color: #222; border-color: #d8d8d5; }

.date-range { display: flex; gap: 6px; align-items: center; }
.date-range select, .date-input {
    background: #0f0f10; border: 1px solid #333; color: #fff;
    padding: 7px 10px; border-radius: 6px; font-size: 12px; font-family: inherit;
}
.date-range select:focus, .date-input:focus { outline: none; border-color: #B8860B; }
body.light .date-range select, body.light .date-input { background: #faf8f2; color: #222; border-color: #d8d8d5; }
.date-input { width: 130px; }

.table-filter {
    margin-left: auto;
    background: #0f0f10; border: 1px solid #333; color: #fff;
    padding: 5px 10px; border-radius: 5px; font-size: 12px;
    width: 180px;
    font-family: inherit;
}
.table-title { display: flex; align-items: center; gap: 8px; }
.table-filter:focus { outline: none; border-color: #B8860B; }
body.light .table-filter { background: #faf8f2; border-color: #d8d8d5; color: #222; }

th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: #B8860B; }
th.sort-asc::after { content: ' ▲'; color: #B8860B; font-size: 10px; }
th.sort-desc::after { content: ' ▼'; color: #B8860B; font-size: 10px; }
