/* Jarvis — Design System "Essencial". Neutral, honest, accessible.
   Tokens from docs/03_DESIGN_SYSTEM_ESSENCIAL. Light default; dark via media query and an
   explicit [data-theme] override. Colors always paired with text labels (never color-only). */

:root {
  --background: #f8fafb;
  --surface: #ffffff;
  --text: #202936;
  --text-secondary: #5c6878;
  --border: #dce2e8;
  --surface-subtle: #eef2f5;
  --action: #30383f;
  --on-action: #ffffff;
  --action-subtle: #edf1f3;
  /* Restrained semantic tints (base stays neutral; always used with a label). */
  --pos: #2f6f5e;
  --neg: #9a3b36;
  --pos-subtle: #e7f1ee;
  --neg-subtle: #f6e9e8;

  --radius-card: 20px;
  --radius-dialog: 18px;
  --radius-mono: 10px;
  --radius-btn: 9px;
  --radius-field: 8px;

  --s4: 4px; --s8: 8px; --s12: 12px; --s16: 16px;
  --s20: 20px; --s24: 24px; --s28: 28px; --s32: 32px;

  --maxw: 1120px; /* analytical content width */
  --shadow: 0 1px 2px rgba(20, 25, 31, .04), 0 2px 8px rgba(20, 25, 31, .04);
  --font: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --background: #14191f; --surface: #202730; --text: #edf1f5; --text-secondary: #b6c1cd;
    --border: #3b4654; --surface-subtle: #293440; --action: #d6e0eb; --on-action: #202830;
    --action-subtle: #303b45; --pos: #7fc8b2; --neg: #e39a94;
    --pos-subtle: #22302c; --neg-subtle: #332524;
    --shadow: 0 1px 2px rgba(0,0,0,.25), 0 2px 10px rgba(0,0,0,.25);
  }
}
:root[data-theme="dark"] {
  --background: #14191f; --surface: #202730; --text: #edf1f5; --text-secondary: #b6c1cd;
  --border: #3b4654; --surface-subtle: #293440; --action: #d6e0eb; --on-action: #202830;
  --action-subtle: #303b45; --pos: #7fc8b2; --neg: #e39a94;
  --pos-subtle: #22302c; --neg-subtle: #332524;
  --shadow: 0 1px 2px rgba(0,0,0,.25), 0 2px 10px rgba(0,0,0,.25);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font); font-size: 14px; line-height: 1.5;
  color: var(--text); background: var(--background);
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; }
h1, h2, h3 { font-weight: 500; margin: 0; }
button, input, select { font-family: inherit; font-size: inherit; }
:focus-visible { outline: 2px solid var(--action); outline-offset: 2px; border-radius: 4px; }

/* ---- App shell ---------------------------------------------------------- */
.app { display: grid; grid-template-columns: 236px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--surface); border-right: 1px solid var(--border);
  padding: var(--s20) var(--s16); display: flex; flex-direction: column; gap: var(--s24);
  position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: var(--s12); padding: 0 var(--s8); }
.monogram {
  width: 34px; height: 34px; border-radius: var(--radius-mono); background: var(--action);
  color: var(--on-action); display: grid; place-items: center; font-weight: 600; font-size: 18px;
}
.brand-name { font-size: 18px; font-weight: 600; letter-spacing: -.01em; }
.brand-sub { font-size: 11px; color: var(--text-secondary); }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-group-label {
  font-size: 11px; color: var(--text-secondary); text-transform: uppercase;
  letter-spacing: .04em; padding: var(--s12) var(--s8) var(--s4);
}
.nav-item {
  display: flex; align-items: center; gap: var(--s12); padding: var(--s8) var(--s12);
  border-radius: var(--radius-btn); color: var(--text); text-decoration: none; cursor: pointer;
  border: none; background: none; width: 100%; text-align: left; font-size: 14px;
}
.nav-item:hover { background: var(--surface-subtle); }
.nav-item[aria-current="page"] { background: var(--action-subtle); font-weight: 500; }
.nav-item svg { width: 18px; height: 18px; flex: none; color: var(--text-secondary); }
.nav-item[aria-current="page"] svg { color: var(--text); }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: var(--s8); }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: var(--s16); padding: var(--s16) var(--s28);
  border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--background);
  z-index: 5;
}
.topbar h1 { font-size: 22px; }
.topbar .spacer { flex: 1; }
.content { padding: var(--s28); max-width: var(--maxw); width: 100%; margin: 0 auto; }
.page-intro { color: var(--text-secondary); margin: calc(-1 * var(--s8)) 0 var(--s20); font-size: 13px; }

/* ---- Cards & grid ------------------------------------------------------- */
.grid { display: grid; gap: var(--s16); }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.card {
  background: var(--surface); border-radius: var(--radius-card); padding: var(--s20);
  box-shadow: var(--shadow); border: 1px solid transparent;
}
.card-title { font-size: 15px; font-weight: 500; }
.card-head { display: flex; align-items: center; gap: var(--s12); margin-bottom: var(--s16); }
.card-head .spacer { flex: 1; }
.section-title { font-size: 22px; margin: var(--s28) 0 var(--s16); }
.section-title:first-child { margin-top: 0; }

/* Metric card */
.metric-name { font-size: 12px; color: var(--text-secondary); }
.metric-value { font-size: 28px; font-weight: 500; letter-spacing: -.02em; margin-top: var(--s4); }
.metric-value.small { font-size: 22px; }
.metric-meta { font-size: 11px; color: var(--text-secondary); margin-top: var(--s4); }
.metric-sub { display: flex; gap: var(--s12); margin-top: var(--s12); flex-wrap: wrap; }
.metric-sub span { font-size: 11px; color: var(--text-secondary); }

.pos { color: var(--pos); }
.neg { color: var(--neg); }

/* ---- Buttons ------------------------------------------------------------ */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s8);
  padding: var(--s8) var(--s16); border-radius: var(--radius-btn); border: 1px solid var(--border);
  background: var(--surface); color: var(--text); cursor: pointer; font-size: 14px; min-height: 38px;
}
.btn:hover { background: var(--surface-subtle); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--action); color: var(--on-action); border-color: var(--action); }
.btn-primary:hover { opacity: .92; background: var(--action); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--surface-subtle); }
.btn-sm { min-height: 30px; padding: var(--s4) var(--s12); font-size: 13px; }

/* ---- Fields ------------------------------------------------------------- */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: var(--s16); }
.label { font-size: 12px; color: var(--text-secondary); }
.input, .select {
  padding: 10px var(--s12); border: 1px solid var(--border); border-radius: var(--radius-field);
  background: var(--surface); color: var(--text); min-height: 42px; width: 100%;
}
.field-row { display: grid; grid-template-columns: repeat(2, 1fr); gap: var(--s12); }
.field-hint { font-size: 11px; color: var(--text-secondary); }
.field-error { font-size: 12px; color: var(--neg); }
.check { display: flex; align-items: center; gap: var(--s8); font-size: 13px; }
.check input { width: 18px; height: 18px; }

/* ---- Tables ------------------------------------------------------------- */
.table-wrap { overflow-x: auto; border-radius: var(--radius-field); }
table.data { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data th, table.data td { padding: 10px var(--s12); text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.data th { font-weight: 500; color: var(--text-secondary); font-size: 11px; text-transform: uppercase; letter-spacing: .03em; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data tr:last-child td { border-bottom: none; }

/* ---- State chips -------------------------------------------------------- */
.chip {
  display: inline-flex; align-items: center; gap: 6px; font-size: 11px; padding: 2px 8px;
  border-radius: 999px; background: var(--surface-subtle); color: var(--text-secondary);
  border: 1px solid var(--border); white-space: nowrap;
}
.chip.pos { background: var(--pos-subtle); color: var(--pos); border-color: transparent; }
.chip.neg { background: var(--neg-subtle); color: var(--neg); border-color: transparent; }
.chip.entity { background: var(--action-subtle); color: var(--text); border-color: transparent; }

/* ---- Empty / states ----------------------------------------------------- */
.empty { text-align: center; color: var(--text-secondary); padding: var(--s32) var(--s16); font-size: 13px; }
.muted { color: var(--text-secondary); }
.toast {
  position: fixed; bottom: var(--s24); left: 50%; transform: translateX(-50%);
  background: var(--action); color: var(--on-action); padding: var(--s12) var(--s20);
  border-radius: var(--radius-btn); box-shadow: var(--shadow); z-index: 50; font-size: 13px;
}

/* ---- Charts ------------------------------------------------------------- */
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart-legend { display: flex; gap: var(--s16); flex-wrap: wrap; margin-top: var(--s12); font-size: 11px; color: var(--text-secondary); }
.legend-dot { width: 10px; height: 10px; border-radius: 3px; display: inline-block; margin-right: 6px; vertical-align: -1px; }
.bar-label { font-size: 11px; fill: var(--text-secondary); }
.axis-line { stroke: var(--border); }
.grid-line { stroke: var(--border); stroke-dasharray: 2 4; opacity: .7; }

/* ---- Login -------------------------------------------------------------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: var(--s24); }
.login-card { width: 100%; max-width: 380px; }
.login-card .brand { justify-content: center; margin-bottom: var(--s20); }

/* ---- Dialog ------------------------------------------------------------- */
.dialog-backdrop { position: fixed; inset: 0; background: rgba(20,25,31,.4); display: grid; place-items: center; z-index: 40; padding: var(--s16); }
.dialog { background: var(--surface); border-radius: var(--radius-dialog); padding: var(--s24); width: 100%; max-width: 520px; box-shadow: var(--shadow); max-height: 90vh; overflow:auto; }
.dialog h2 { font-size: 22px; margin-bottom: var(--s16); }
.dialog-actions { display: flex; justify-content: flex-end; gap: var(--s12); margin-top: var(--s20); }

/* ---- Responsive --------------------------------------------------------- */
.menu-btn { display: none; }
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; z-index: 30; width: 236px; transform: translateX(-110%); transition: transform .2s; }
  .sidebar.open { transform: none; }
  .menu-btn { display: inline-flex; }
  .grid-4 { grid-template-columns: repeat(2, 1fr); }
  .grid-3 { grid-template-columns: 1fr; }
  .grid-2 { grid-template-columns: 1fr; }
  .content { padding: var(--s16); }
  .topbar { padding: var(--s12) var(--s16); }
  .field-row { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
