:root {
  --indigo: #6c5ffc;
  --indigo-dark: #5a4ce0;
  --pink: #d64ba0;
  --green: #18a86b;
  --amber: #f2a93b;
  --red: #e5556e;
  --blue: #3b82f6;

  --bg: #f4f5fa;
  --surface: #ffffff;
  --surface-2: #fafbfe;
  --border: #e6e8f0;
  --text: #2a2f45;
  --text-muted: #767d9c;
  --ring: rgba(108, 95, 252, 0.35);
  --shadow: 0 1px 2px rgba(29, 33, 54, 0.05), 0 8px 24px rgba(29, 33, 54, 0.06);
  --sidebar-w: 248px;
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12141d;
    --surface: #1a1d2a;
    --surface-2: #1f2231;
    --border: #2b3043;
    --text: #e7e9f3;
    --text-muted: #9aa1bd;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.35);
  }
}

:root[data-theme="dark"] {
  --bg: #12141d;
  --surface: #1a1d2a;
  --surface-2: #1f2231;
  --border: #2b3043;
  --text: #e7e9f3;
  --text-muted: #9aa1bd;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Nunito", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 { margin: 0; font-weight: 700; letter-spacing: -0.01em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.05rem; }
h3 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-muted); }

a { color: var(--indigo); }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.85em; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.shell { display: flex; min-height: 100vh; }

/* ---------- sidebar ---------- */
.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 18px 12px 28px;
  overflow-y: auto;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 0 10px 18px; text-decoration: none; color: inherit; }
.brand-mark { width: 30px; height: 30px; }
.brand-text { font-size: 1.2rem; font-weight: 600; }
.brand-text strong { color: var(--indigo); }

.nav-section { margin: 16px 0 6px; padding: 0 12px; font-size: 0.68rem; font-weight: 700; letter-spacing: 0.1em; text-transform: uppercase; color: var(--text-muted); }
.nav-list, .nav-sub { list-style: none; margin: 0; padding: 0; }
.nav-link {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 9px 12px; border-radius: 9px; text-decoration: none;
  color: var(--text); font: inherit; font-weight: 600; font-size: 0.92rem;
  background: none; border: 0; cursor: pointer; text-align: left;
}
.nav-link:hover { background: var(--surface-2); }
.nav-link.active { background: color-mix(in srgb, var(--indigo) 12%, transparent); color: var(--indigo); }
.nav-link .icon { width: 19px; height: 19px; flex: 0 0 19px; }
.icon { fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.nav-toggle .chev { width: 16px; height: 16px; margin-left: auto; transition: transform 0.15s ease; }
li.open .nav-toggle .chev { transform: rotate(90deg); }
.nav-sub { display: none; margin: 2px 0 6px 30px; }
li.open .nav-sub { display: block; }
.nav-sub .nav-link { padding: 7px 12px; font-weight: 500; font-size: 0.88rem; }

/* ---------- main ---------- */
.main { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 20px; background: var(--surface);
  border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5;
}
.topbar-spacer { flex: 1 1 auto; }
.icon-btn { display: grid; place-items: center; width: 38px; height: 38px; border: 0; border-radius: 9px; background: none; color: var(--text); cursor: pointer; }
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn:hover { background: var(--surface-2); }
.user { display: flex; align-items: center; gap: 10px; }
.avatar { display: grid; place-items: center; width: 36px; height: 36px; border-radius: 50%; background: var(--indigo); color: #fff; font-weight: 700; }
.user-text { display: flex; flex-direction: column; line-height: 1.2; }
.user-text small { color: var(--text-muted); }

.content { flex: 1 1 auto; padding: 22px; display: flex; flex-direction: column; gap: 18px; max-width: 100%; }
.page-head { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.page-head h1 { margin-right: auto; }
.plan-chip { padding: 7px 14px; border-radius: 999px; background: var(--surface); border: 1px solid var(--border); font-size: 0.85rem; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.card-title { margin-bottom: 14px; }
.grid-2 { display: grid; gap: 18px; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); }

/* ---------- tiles ---------- */
.tiles { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.tiles-wide { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.tile { position: relative; padding: 14px 16px 14px 20px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2); }
.tile::before { content: ""; position: absolute; inset: 10px auto 10px 8px; width: 4px; border-radius: 3px; background: var(--indigo); }
.tile h2, .tile h4 { font-size: 0.9rem; font-weight: 700; }
.tile-value { margin: 6px 0 0; font-size: 1.6rem; font-weight: 800; color: var(--indigo); }
.tile .muted { margin: 2px 0 0; }
.accent-indigo::before { background: var(--indigo); }
.accent-green::before { background: var(--green); }
.accent-red::before { background: var(--red); }
.accent-amber::before { background: var(--amber); }
.accent-blue::before { background: var(--blue); }
.accent-pink::before { background: var(--pink); }
.tile.current { border-color: var(--indigo); }
.opr-grid { display: grid; gap: 20px; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.opr-col { display: flex; flex-direction: column; gap: 10px; }

/* ---------- tables ---------- */
.table-toolbar, .filter-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.table-toolbar .search { margin-left: auto; }
.field select, .input, input[type="search"], input[type="text"], input[type="email"], input[type="password"] {
  padding: 8px 12px; border: 1px solid var(--border); border-radius: 9px;
  background: var(--surface); color: var(--text); font: inherit; min-width: 160px;
}
:focus-visible { outline: 2px solid var(--indigo); outline-offset: 2px; }
.field select:focus, .input:focus, input:focus { border-color: var(--indigo); box-shadow: 0 0 0 3px var(--ring); outline: none; }

.table-scroll { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
.table th, .table td { padding: 11px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
.table thead th { white-space: nowrap; }
/* Cells wrap instead of forcing a wide table, so no column is cut off at
   narrow widths; the scroller stays for genuinely wide tables. */
.table td { white-space: normal; }
/* Values that must never break mid-token keep their own line. */
.table td.cell-money, .table td.cell-date, .table td.cell-status { white-space: nowrap; }
.table thead th { background: var(--surface-2); font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); }
.table tbody tr:hover { background: var(--surface-2); }
.th-sort { background: none; border: 0; font: inherit; color: inherit; cursor: pointer; padding: 0; display: inline-flex; gap: 6px; align-items: center; text-transform: inherit; letter-spacing: inherit; }
.sort-mark::after { content: "↕"; opacity: 0.4; }
.th-sort[data-dir="asc"] .sort-mark::after { content: "↑"; opacity: 1; }
.th-sort[data-dir="desc"] .sort-mark::after { content: "↓"; opacity: 1; }
td.empty { text-align: center; color: var(--text-muted); padding: 28px 12px; white-space: normal; }
.table-footer { display: flex; align-items: center; gap: 12px; margin-top: 12px; flex-wrap: wrap; }
.pager { margin-left: auto; display: flex; gap: 8px; }

.pill { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 0.78rem; font-weight: 700; background: color-mix(in srgb, var(--text-muted) 16%, transparent); }
.pill-active, .pill-accepted, .pill-done, .pill-won { background: color-mix(in srgb, var(--green) 18%, transparent); color: var(--green); }
.pill-inactive, .pill-declined, .pill-lost { background: color-mix(in srgb, var(--red) 18%, transparent); color: var(--red); }
.pill-pending, .pill-draft { background: color-mix(in srgb, var(--amber) 20%, transparent); color: #a9741a; }
.pill-inprogress, .pill-open { background: color-mix(in srgb, var(--blue) 18%, transparent); color: var(--blue); }

/* ---------- misc ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 9px 16px; border-radius: 9px; border: 1px solid transparent; font: inherit; font-weight: 700; cursor: pointer; }
.btn-primary { background: var(--indigo); color: #fff; }
.btn-primary:hover { background: var(--indigo-dark); }
.btn-ghost { background: var(--surface); border-color: var(--border); color: var(--text); }
.btn-ghost:hover { background: var(--surface-2); }
.btn[disabled] { opacity: 0.55; cursor: default; }
.btn-block { width: 100%; }
.muted { color: var(--text-muted); margin: 0; }
.small { font-size: 0.85rem; }
.empty-state { text-align: center; padding: 48px 20px; }
.empty-state h1 { font-size: 2.6rem; }
.chart { display: flex; align-items: flex-end; gap: 8px; height: 180px; padding-top: 10px; }
.bar { flex: 1 1 0; background: color-mix(in srgb, var(--indigo) 75%, transparent); border-radius: 6px 6px 0 0; min-height: 3px; position: relative; }
.bar span { position: absolute; bottom: -22px; left: 0; right: 0; text-align: center; font-size: 0.7rem; color: var(--text-muted); }
.settings-layout { display: grid; gap: 18px; grid-template-columns: minmax(200px, 260px) 1fr; }
.settings-nav { display: flex; flex-direction: column; gap: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; height: fit-content; }
.settings-tab { padding: 10px 14px; border: 0; border-radius: 9px; background: none; font: inherit; font-weight: 600; text-align: left; cursor: pointer; color: var(--text); }
.settings-tab.active { background: var(--indigo); color: #fff; }
.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.setting-row:last-child { border-bottom: 0; }
.switch { appearance: none; width: 44px; height: 24px; border-radius: 999px; background: var(--border); position: relative; cursor: pointer; flex: 0 0 44px; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: left 0.15s ease; }
.switch:checked { background: var(--indigo); }
.switch:checked::after { left: 23px; }
.plan-points { list-style: none; margin: 10px 0; padding: 0; font-size: 0.88rem; color: var(--text-muted); display: grid; gap: 4px; }
.footer { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; padding: 16px 22px; border-top: 1px solid var(--border); color: var(--text-muted); font-size: 0.85rem; }
.footer-links { margin-left: auto; display: flex; gap: 16px; flex-wrap: wrap; }
.footer-links a { color: var(--text-muted); text-decoration: none; }
.footer-links a:hover { color: var(--indigo); }
.alert { padding: 10px 14px; border-radius: 9px; background: color-mix(in srgb, var(--red) 14%, transparent); color: var(--red); font-weight: 600; }
.toast { position: fixed; right: 16px; bottom: 16px; z-index: 50; padding: 12px 16px; border-radius: 10px; background: var(--text); color: var(--bg); box-shadow: var(--shadow); font-weight: 600; }

/* ---------- auth ---------- */
.auth-body { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.auth-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; box-shadow: var(--shadow); padding: 30px; }
.auth-brand { font-size: 1.7rem; font-weight: 600; margin-bottom: 4px; }
.auth-brand strong { color: var(--indigo); }
.auth-form { display: grid; gap: 14px; margin: 18px 0 12px; }
.field.block { display: grid; gap: 6px; font-weight: 600; font-size: 0.88rem; }
.field.block input { width: 100%; }

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 20;
    transform: translateX(-100%); transition: transform 0.2s ease;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .settings-layout { grid-template-columns: 1fr; }
}
@media (max-width: 560px) {
  .content { padding: 16px; gap: 14px; }
  .card { padding: 14px; }
  h1 { font-size: 1.25rem; }
  .table-toolbar .search { margin-left: 0; width: 100%; }
  .field select, .input, input[type="search"] { width: 100%; min-width: 0; }
  .footer-links { margin-left: 0; }
  .user-text { display: none; }
}

/* ---------- record editor ---------- */
.col-actions, .cell-actions { white-space: nowrap; width: 1%; }
.cell-actions { display: flex; gap: 6px; }
.btn-sm { padding: 5px 10px; font-size: 0.82rem; }
.btn-danger { background: var(--red); color: #fff; }
.btn-danger:hover { background: #d13c58; }
.btn-danger-text { color: var(--red); }
.btn-danger-text:hover { background: color-mix(in srgb, var(--red) 12%, transparent); }

.dialog {
  width: min(560px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 24px 60px rgba(20, 23, 40, 0.28);
}
.dialog-sm { width: min(420px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(20, 23, 40, 0.45); }
.dialog-head { display: flex; align-items: center; gap: 12px; padding: 16px 18px; border-bottom: 1px solid var(--border); }
.dialog-head h2 { margin-right: auto; }
.dialog-body { padding: 18px; display: grid; gap: 14px; max-height: min(60vh, 520px); overflow-y: auto; }
.dialog-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 14px 18px; border-top: 1px solid var(--border); }
.dialog .alert { margin: 14px 18px 0; }

.field-row { display: flex; align-items: center; justify-content: space-between; gap: 14px; font-weight: 600; font-size: 0.88rem; }
.field.block textarea, .field.block select { width: 100%; font: inherit; }
.field.block textarea {
  padding: 8px 12px; border: 1px solid var(--border); border-radius: 9px;
  background: var(--surface); color: var(--text); resize: vertical;
}
.field.block small.muted { font-weight: 400; }
.field-error { color: var(--red); font-weight: 600; font-size: 0.8rem; }
[aria-invalid="true"] { border-color: var(--red) !important; }
input[type="color"] { padding: 2px; height: 38px; min-width: 60px; }

@media (max-width: 560px) {
  .dialog-body { max-height: 65vh; }
  .cell-actions { flex-direction: column; }
}
.stacked-form { display: grid; gap: 14px; max-width: 520px; }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; padding-top: 14px; }
.alert-ok { background: color-mix(in srgb, var(--green) 14%, transparent); color: var(--green); word-break: break-all; }
a.settings-tab { text-decoration: none; display: block; }
input[type="file"] {
  padding: 7px 10px; border: 1px dashed var(--border); border-radius: 9px;
  background: var(--surface-2); color: var(--text); font: inherit; width: 100%;
}
input[type="file"]::file-selector-button {
  margin-right: 10px; padding: 5px 12px; border: 1px solid var(--border);
  border-radius: 7px; background: var(--surface); color: var(--text);
  font: inherit; font-weight: 600; cursor: pointer;
}
.send-results { list-style: none; margin: 12px 0 0; padding: 0; display: grid; gap: 6px; font-size: 0.88rem; }
.send-results li { padding: 8px 12px; border-radius: 8px; }
.send-results li.ok { background: color-mix(in srgb, var(--green) 14%, transparent); color: var(--green); }
.send-results li.bad { background: color-mix(in srgb, var(--red) 14%, transparent); color: var(--red); }
.send-results li.warn { background: color-mix(in srgb, var(--amber) 18%, transparent); color: #8a5d0f; }
.field-row.sub { padding-left: 14px; border-left: 2px solid var(--border); }
.field-row.sub small { display: block; font-weight: 400; }
.spec-grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); }
.spec-grid .field.block span small { font-weight: 400; }

/* `hidden` must win over the display rules set on form rows and sections. */
[hidden] { display: none !important; }
.transcript { display: grid; gap: 10px; max-height: 520px; overflow-y: auto; padding-right: 4px; }
.bubble { padding: 10px 14px; border-radius: 12px; max-width: 86%; }
.bubble p { margin: 0; white-space: pre-wrap; }
.bubble small { display: block; margin-top: 4px; font-size: .72rem; }
.bubble.in { background: var(--surface-2); border: 1px solid var(--border); justify-self: start; border-bottom-left-radius: 4px; }
.bubble.out { background: color-mix(in srgb, var(--indigo) 12%, transparent); justify-self: end; border-bottom-right-radius: 4px; }

/* Plan and usage, shown beside the dashboard title. */
.plan-usage {
  display: inline-block;
  margin-left: 10px;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.18);
  font-size: 0.78rem;
  text-transform: capitalize;
}
.plan-usage-over { background: #b42318; color: #fff; font-weight: 600; }
.tile-value .over-limit, .tile-value.over-limit { color: #b42318; }
