:root {
  --bg: #F4F6F9; --card: #FFFFFF; --text: #14202E; --sub: #5B6878; --line: #E3E8EF;
  --teal: #0A84A6; --teal-dark: #076F8C; --navy: #27324C; --danger: #D93A3A; --ok: #1F9D55; --warn: #C98A00;
  --radius: 14px; --shadow: 0 1px 2px rgba(20,32,46,.06), 0 4px 16px rgba(20,32,46,.06);
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0F141B; --card: #171E28; --text: #EEF2F6; --sub: #93A0AF; --line: #263040; --shadow: none; }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; background: var(--bg); color: var(--text); }
a { color: var(--teal); text-decoration: none; }
.hidden { display: none !important; }
h1 { font-size: 24px; margin: 0 0 4px; }
h2 { font-size: 17px; margin: 0 0 12px; }
.sub { color: var(--sub); }
.small { font-size: 13px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; word-break: break-all; }
code { background: var(--bg); padding: 1px 6px; border-radius: 6px; font-size: 13px; }

/* Layout */
.app { display: grid; grid-template-columns: 232px minmax(0, 1fr); min-height: 100vh; }
.side { background: var(--navy); color: #fff; padding: 20px 14px; display: flex; flex-direction: column; gap: 18px; position: sticky; top: 0; height: 100vh; }
.side nav { display: flex; flex-direction: column; gap: 2px; flex: 1; overflow: auto; }
.side nav a { color: rgba(255,255,255,.82); padding: 9px 12px; border-radius: 10px; display: flex; justify-content: space-between; align-items: center; }
.side nav a.active, .side nav a:hover { background: rgba(255,255,255,.12); color: #fff; }
.main { padding: 28px 32px 60px; max-width: 1240px; width: 100%; min-width: 0; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 38px; height: 38px; border-radius: 10px; background: var(--teal); color: #fff; font-weight: 800; display: grid; place-items: center; font-size: 15px; }
.brand-title { font-weight: 700; }
.brand-sub { font-size: 12px; opacity: .75; }
/* Phone: sidebar becomes a compact top bar with a swipeable row of links */
@media (max-width: 800px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .side { position: sticky; top: 0; z-index: 20; height: auto; padding: 10px 12px; gap: 10px; flex-direction: row; flex-wrap: wrap; align-items: center; box-shadow: 0 2px 8px rgba(0,0,0,.25); }
  .side .brand { flex: 1; }
  .side .brand-mark { width: 30px; height: 30px; font-size: 12px; }
  .side .brand-sub { display: none; }
  .side nav { order: 3; flex-basis: 100%; min-width: 0; flex-direction: row; flex-wrap: nowrap; overflow-x: auto; gap: 4px; padding-bottom: 2px; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
  .side nav::-webkit-scrollbar { display: none; }
  .side nav a { white-space: nowrap; padding: 7px 11px; font-size: 14px; gap: 6px; }
  .side #logout { order: 2; }
  .main { padding: 14px 12px 40px; }
  h1 { font-size: 21px; }
  .page-head { margin-bottom: 14px; }
  .page-head .row .btn { padding: 7px 11px; font-size: 14px; }
  .card { padding: 14px; border-radius: 12px; }
  .grid.stats { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .stat .n { font-size: 22px; }
  th, td { padding: 8px 8px; }
  .modal-card { padding: 16px; border-radius: 14px; }
  .tabs { max-width: 100%; overflow-x: auto; flex-wrap: nowrap; scrollbar-width: none; }
  .tabs::-webkit-scrollbar { display: none; }
  .tabs button { white-space: nowrap; }
  input, select, textarea { font-size: 16px; } /* stops iOS zooming into fields */
  .row.spread > input { max-width: 100% !important; flex-basis: 100%; }
  #tabs { min-width: 0; max-width: 100%; }
}
/* Tables scroll sideways on narrow screens instead of squeezing */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -4px; padding: 0 4px; }
.table-wrap table { min-width: 560px; }
@media (min-width: 801px) { .table-wrap table { min-width: 0; } }
.row .btn { flex-shrink: 0; }

/* Cards, grids, tables */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px 20px; border: 1px solid var(--line); min-width: 0; }
.grid { display: grid; gap: 14px; }
.grid.stats { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
.grid.two { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
.grid > * { min-width: 0; }
@media (max-width: 900px) { .grid.two { grid-template-columns: minmax(0, 1fr); } }
.stat .n { font-size: 28px; font-weight: 800; letter-spacing: -.5px; }
.stat .l { color: var(--sub); font-size: 13px; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; min-width: 0; }
.spread { justify-content: space-between; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
td { overflow-wrap: anywhere; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .4px; color: var(--sub); font-weight: 600; }
tr:hover td { background: rgba(10,132,166,.04); }
tr.clickable { cursor: pointer; }
.wrap { white-space: pre-wrap; word-break: break-word; }
.avatar { width: 34px; height: 34px; border-radius: 50%; object-fit: cover; background: var(--line); }
.tag { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--bg); color: var(--sub); border: 1px solid var(--line); }
.tag.ok { color: var(--ok); border-color: var(--ok); }
.tag.warn { color: var(--warn); border-color: var(--warn); }
.tag.bad { color: var(--danger); border-color: var(--danger); }
.tag.teal { color: var(--teal); border-color: var(--teal); }
.pill { background: var(--danger); color: #fff; font-size: 11px; padding: 1px 7px; border-radius: 999px; font-weight: 700; }
.pill:empty { display: none; }
.empty { color: var(--sub); padding: 24px; text-align: center; }
.notice { background: rgba(10,132,166,.08); border: 1px solid rgba(10,132,166,.25); padding: 12px 14px; border-radius: 10px; font-size: 14px; }
.notice.warn { background: rgba(201,138,0,.08); border-color: rgba(201,138,0,.35); }

/* Forms & buttons */
label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--sub); font-weight: 600; }
input, select, textarea { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; min-width: 0; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--teal); outline-offset: 0; border-color: transparent; }
textarea { min-height: 90px; resize: vertical; }
.btn { font: inherit; font-weight: 600; border: 1px solid var(--line); background: var(--card); color: var(--text); padding: 8px 14px; border-radius: 10px; cursor: pointer; }
.btn:hover { border-color: var(--teal); color: var(--teal); }
.btn.primary { background: var(--teal); border-color: var(--teal); color: #fff; }
.btn.primary:hover { background: var(--teal-dark); }
.btn.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn.danger:hover { background: var(--danger); color: #fff; }
.btn.ghost { background: transparent; border-color: rgba(255,255,255,.25); color: #fff; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
.error { color: var(--danger); font-size: 14px; min-height: 20px; }
.tabs { display: flex; gap: 4px; background: var(--bg); padding: 4px; border-radius: 10px; width: fit-content; min-width: 0; flex-shrink: 1; }
.tabs button { border: 0; background: transparent; padding: 6px 12px; border-radius: 8px; font: inherit; cursor: pointer; color: var(--sub); }
.audit-layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 14px; align-items: start; }
.legend-item { padding: 10px 0; border-top: 1px solid var(--line); }
.legend-item:first-of-type { border-top: 0; padding-top: 0; }
.legend-term { margin-bottom: 4px; font-weight: 600; }
.legend-desc { font-size: 13px; color: var(--sub); line-height: 1.4; }
@media (max-width: 1000px) { .audit-layout { grid-template-columns: minmax(0, 1fr); } }
.tabs button.active { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: linear-gradient(160deg, var(--navy), #0A84A6 140%); }
.login-card { width: 360px; max-width: 100%; display: flex; flex-direction: column; gap: 14px; }

/* Chart */
.chart { width: 100%; height: 160px; display: block; }
.chart rect { fill: var(--teal); opacity: .85; }
.chart rect.alt { fill: var(--navy); opacity: .5; }
.chart text { font-size: 10px; fill: var(--sub); }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--sub); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }

/* Toast & modal */
.toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); background: var(--navy); color: #fff; padding: 10px 16px; border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.25); z-index: 50; max-width: 90vw; }
.toast.bad { background: var(--danger); }
.modal { position: fixed; inset: 0; background: rgba(10,16,24,.55); display: grid; place-items: center; z-index: 40; padding: 16px; }
.modal-card { background: var(--card); border-radius: 16px; padding: 22px; width: 520px; max-width: calc(100vw - 32px); min-width: 0; box-sizing: border-box; max-height: 90vh; overflow: auto; display: flex; flex-direction: column; gap: 14px; }
