:root{
  color-scheme: light;
  --bg:#eef0f3;
  --surface-1:#fcfcfb;
  --surface-2:#f3f4f6;
  --border:#dcdfe4;
  --grid:#e6e8ec;
  --text-primary:#0b0b0b;
  --text-secondary:#52514e;
  --text-muted:#6f6e69;
  --brand:#05070A;
  --brand-accent:#2F63D4;
  --series-1:#2a78d6;
  --series-2:#eb6834;
  --good:#0a7a33;
  --warning:#9a6200;
  --critical:#c62828;
  --focus:#2F63D4;
}
@media (prefers-color-scheme: dark){
  :root:not([data-theme="light"]){
    color-scheme: dark;
    --bg:#0f1012;
    --surface-1:#1a1a19;
    --surface-2:#232322;
    --border:#33332f;
    --grid:#2a2a28;
    --text-primary:#ffffff;
    --text-secondary:#c3c2b7;
    --text-muted:#9a998f;
    --series-1:#3987e5;
    --series-2:#d95926;
    --good:#4cc47a;
    --warning:#e3a33a;
    --critical:#ef6b6b;
    --focus:#6da7ec;
  }
}
:root[data-theme="dark"]{
  color-scheme: dark;
  --bg:#0f1012;
  --surface-1:#1a1a19;
  --surface-2:#232322;
  --border:#33332f;
  --grid:#2a2a28;
  --text-primary:#ffffff;
  --text-secondary:#c3c2b7;
  --text-muted:#9a998f;
  --series-1:#3987e5;
  --series-2:#d95926;
  --good:#4cc47a;
  --warning:#e3a33a;
  --critical:#ef6b6b;
  --focus:#6da7ec;
}

*{ box-sizing:border-box; }
html,body{ margin:0; }
body{
  background:var(--bg); color:var(--text-primary);
  font:14px/1.45 Inter, system-ui, sans-serif;
  -webkit-font-smoothing:antialiased;
}
[hidden]{ display:none !important; }
h1,h2,h3{ margin:0; font-family:Oswald, Inter, sans-serif; font-weight:500; letter-spacing:.3px; }
h2{ font-size:22px; }
h3{ font-size:16px; margin-bottom:6px; }
.muted{ color:var(--text-secondary); }
.small{ font-size:12px; }
.error{ color:var(--critical); min-height:1.2em; margin:4px 0 0; }
:focus-visible{ outline:2px solid var(--focus); outline-offset:2px; }
.num{ font-variant-numeric:tabular-nums; }

/* buttons + inputs */
.btn{
  font:inherit; font-weight:600; cursor:pointer;
  border:1px solid var(--border); background:var(--surface-1); color:var(--text-primary);
  border-radius:8px; padding:8px 14px;
}
.btn:hover{ background:var(--surface-2); }
.btn.primary{ background:var(--brand-accent); border-color:var(--brand-accent); color:#fff; }
.btn.primary:hover{ filter:brightness(1.08); }
.btn.ghost{ background:transparent; }
.btn.small{ padding:6px 10px; font-size:13px; }
.btn.danger{ color:var(--critical); }
input,select{
  font:inherit; color:var(--text-primary); background:var(--surface-1);
  border:1px solid var(--border); border-radius:8px; padding:6px 8px; min-width:0;
}
.row-gap{ display:flex; gap:8px; flex-wrap:wrap; align-items:center; }

/* login */
.login{ min-height:100vh; display:grid; place-items:center; padding:16px; background:var(--brand); }
.login-card{
  width:100%; max-width:340px; background:var(--surface-1); border-radius:14px; padding:28px;
  display:flex; flex-direction:column; gap:10px; border-top:4px solid var(--brand-accent);
}
.login-card img{ border-radius:12px; }
.login-card h1{ font-size:24px; }
.login-card label{ font-weight:600; font-size:13px; }
.login-card input{ padding:10px; }

/* top bar */
.topbar{
  display:flex; align-items:center; gap:16px; flex-wrap:wrap;
  background:var(--brand); color:#fff; padding:10px 16px; border-bottom:3px solid var(--brand-accent);
}
.brand{ display:flex; align-items:center; gap:10px; }
.brand img{ border-radius:6px; }
.brand-name{ font-family:Oswald, sans-serif; font-weight:700; letter-spacing:1px; font-size:15px; line-height:1.1; }
.brand-sub{ font-size:11px; color:#9fb6e8; }
.tabs{ display:flex; gap:4px; margin-left:8px; }
.tabs button{
  font:inherit; font-weight:600; color:#c9d1de; background:transparent; border:0;
  padding:8px 12px; border-radius:8px; cursor:pointer;
}
.tabs button[aria-selected="true"]{ color:#fff; background:rgba(255,255,255,.12); }
.top-actions{ margin-left:auto; display:flex; gap:8px; align-items:center; }
.top-actions .btn{ background:transparent; color:#fff; border-color:rgba(255,255,255,.3); }
.icon-btn{ background:transparent; border:1px solid rgba(255,255,255,.3); border-radius:8px; padding:6px; cursor:pointer; display:flex; }
.icon-btn svg{ width:18px; height:18px; fill:#fff; }

/* filters */
.filters{
  display:flex; flex-wrap:wrap; gap:8px; align-items:center;
  padding:10px 16px; background:var(--surface-1); border-bottom:1px solid var(--border);
  position:sticky; top:0; z-index:5;
}
.filters label{ display:flex; gap:6px; align-items:center; color:var(--text-secondary); font-size:13px; }
.filters select{ max-width:180px; }
.presets{ display:flex; border:1px solid var(--border); border-radius:8px; overflow:hidden; }
.presets button{
  font:inherit; font-size:13px; background:var(--surface-1); color:var(--text-primary);
  border:0; border-right:1px solid var(--border); padding:6px 10px; cursor:pointer;
}
.presets button:last-child{ border-right:0; }
.presets button[aria-pressed="true"]{ background:var(--brand-accent); color:#fff; }

main{ padding:16px; max-width:1500px; margin:0 auto; }
.panel-head{ display:flex; justify-content:space-between; align-items:center; gap:12px; flex-wrap:wrap; margin-bottom:12px; }

/* widget grid */
.grid{ display:grid; grid-template-columns:repeat(12, minmax(0,1fr)); gap:12px; }
.w{
  background:var(--surface-1); border:1px solid var(--border); border-radius:12px;
  padding:14px 16px; min-width:0; position:relative;
}
.w.s{ grid-column:span 3; } .w.m{ grid-column:span 6; } .w.l{ grid-column:span 12; }
@media (max-width:1100px){ .w.s{ grid-column:span 6; } }
@media (max-width:720px){
  .filters{ position:static; }
  .filters select{ flex:1 1 calc(50% - 8px); max-width:none; }
  .presets{ width:100%; } .presets button{ flex:1; padding:6px 4px; }
 .w.s,.w.m,.w.l{ grid-column:span 12; } .tabs{ margin-left:0; order:3; width:100%; } }
.w-head{ display:flex; justify-content:space-between; gap:8px; align-items:flex-start; margin-bottom:8px; }
.w-title{ font-weight:600; color:var(--text-secondary); font-size:13px; }
.w-tools{ display:none; gap:4px; }
.editing .w-tools{ display:flex; }
.editing .w{ border-style:dashed; }
.w-tools button{
  font:inherit; font-size:12px; border:1px solid var(--border); background:var(--surface-2);
  color:var(--text-primary); border-radius:6px; padding:2px 7px; cursor:pointer;
}
.kpi-value{ font-size:30px; font-weight:700; line-height:1.1; letter-spacing:-.5px; }
.kpi-sub{ color:var(--text-muted); font-size:12px; margin-top:4px; }
.legend{ display:flex; gap:14px; font-size:12px; color:var(--text-secondary); margin-bottom:6px; }
.legend i{ display:inline-block; width:10px; height:10px; border-radius:3px; margin-right:5px; vertical-align:-1px; }
.w svg{ display:block; width:100%; overflow:visible; }
.w svg text{ fill:var(--text-secondary); font-size:11px; font-family:Inter, sans-serif; }
.w svg .val{ fill:var(--text-primary); font-weight:600; }
.w svg .gridline{ stroke:var(--grid); stroke-width:1; }
.w svg .axis{ stroke:var(--border); stroke-width:1; }
.w svg .hit{ fill:transparent; cursor:pointer; }
.w svg .hit:hover + .bar, .w svg g.row:hover .bar{ filter:brightness(1.12); }
.w .nodata{ color:var(--text-muted); padding:24px 0; text-align:center; }

/* tables */
.table-wrap{ overflow:auto; max-height:70vh; border:1px solid var(--border); border-radius:10px; background:var(--surface-1); }
.w .table-wrap{ max-height:360px; border:0; }
.data-table{ width:100%; border-collapse:collapse; font-size:13px; }
.data-table th,.data-table td{ padding:7px 10px; border-bottom:1px solid var(--grid); text-align:left; white-space:nowrap; }
.data-table th{ position:sticky; top:0; background:var(--surface-2); font-weight:600; color:var(--text-secondary); cursor:pointer; user-select:none; }
.data-table td.n,.data-table th.n{ text-align:right; font-variant-numeric:tabular-nums; }
.data-table tbody tr:hover{ background:var(--surface-2); }
.pill{ display:inline-flex; align-items:center; gap:4px; font-size:12px; font-weight:600; padding:1px 8px; border-radius:99px; border:1px solid var(--border); }
.pill.paid{ color:var(--good); } .pill.unpaid{ color:var(--critical); }

/* data tab */
.card{ background:var(--surface-1); border:1px solid var(--border); border-radius:12px; padding:16px; margin-bottom:14px; }
.drop{
  display:block; border:2px dashed var(--border); border-radius:12px; padding:34px 16px; text-align:center;
  color:var(--text-secondary); cursor:pointer; margin-top:10px;
}
.drop.over{ border-color:var(--brand-accent); background:var(--surface-2); }
#preview{ margin-top:14px; }
.stats{ display:grid; grid-template-columns:repeat(auto-fit, minmax(150px,1fr)); gap:10px; margin:10px 0; }
.stats div{ background:var(--surface-2); border-radius:8px; padding:10px; }
.stats b{ display:block; font-size:18px; }
.defs{ margin:0; padding-left:18px; color:var(--text-secondary); }
.defs li{ margin:4px 0; }
.empty{ text-align:center; padding:60px 16px; background:var(--surface-1); border:1px solid var(--border); border-radius:12px; }
.empty p{ color:var(--text-secondary); }

/* dialog */
dialog{ border:1px solid var(--border); border-radius:14px; background:var(--surface-1); color:var(--text-primary); padding:22px; width:min(420px, calc(100vw - 32px)); }
dialog::backdrop{ background:rgba(0,0,0,.45); }
dialog form{ display:flex; flex-direction:column; gap:10px; }
dialog label{ display:flex; flex-direction:column; gap:4px; font-size:13px; font-weight:600; color:var(--text-secondary); }
.dlg-actions{ display:flex; justify-content:flex-end; gap:8px; margin-top:6px; }

.tooltip{
  position:fixed; z-index:50; pointer-events:none; background:var(--surface-1); color:var(--text-primary);
  border:1px solid var(--border); border-radius:8px; padding:8px 10px; font-size:12px;
  box-shadow:0 6px 20px rgba(0,0,0,.18); max-width:260px;
}
.tooltip b{ display:block; margin-bottom:2px; }
.tooltip .tt-row{ display:flex; justify-content:space-between; gap:14px; }
.tooltip i{ display:inline-block; width:8px; height:8px; border-radius:2px; margin-right:5px; }
.toast{
  position:fixed; bottom:20px; left:50%; transform:translateX(-50%); z-index:60;
  background:var(--brand); color:#fff; padding:10px 16px; border-radius:10px; font-weight:600;
}

/* lifetime + profitability */
.data-table tbody tr.clickable{ cursor:pointer; }
.data-table tr.selected{ background:var(--surface-2); box-shadow:inset 3px 0 0 var(--brand-accent); }
.detail{ margin-bottom:14px; }
.detail-head{ display:flex; justify-content:space-between; align-items:center; gap:8px; margin-bottom:10px; }
.inline-form{ display:flex; flex-wrap:wrap; gap:10px; align-items:flex-end; }
.inline-form label{ display:flex; flex-direction:column; gap:4px; font-size:12px; font-weight:600; color:var(--text-secondary); }
.inline-form label.grow{ flex:1 1 180px; }
.inline-form input[type=number]{ width:120px; }
dialog label.check{ flex-direction:row; align-items:center; gap:8px; }
.neg{ color:var(--critical); }
.meter{ display:inline-block; width:90px; height:8px; border-radius:4px; background:var(--grid); vertical-align:middle; margin-right:8px; overflow:hidden; }
.meter i{ display:block; height:100%; background:var(--series-1); border-radius:4px; }
.meter i.low{ background:var(--critical); }
.w svg .avg{ fill:var(--text-muted); font-weight:400; }

/* accounts */
.login-card label{ display:flex; flex-direction:column; gap:4px; }
#credFields, #codeFields{ display:flex; flex-direction:column; gap:8px; }
.dlg-body{ display:flex; flex-direction:column; gap:10px; }
.dlg-body form{ display:flex; flex-direction:column; gap:8px; padding:12px 0; border-top:1px solid var(--border); }
.dlg-body h4{ margin:0; font-size:14px; }
#twofa{ border-top:1px solid var(--border); padding-top:12px; display:flex; flex-direction:column; gap:8px; }
#twofaBody{ display:flex; flex-direction:column; gap:8px; }
.qr{ background:#fff; padding:10px; border-radius:8px; width:max-content; }
.secret{ display:flex; gap:8px; align-items:center; background:var(--surface-2); border:1px solid var(--border); border-radius:8px; padding:10px 12px; }
.secret code{ font-size:18px; font-weight:700; letter-spacing:1px; flex:1; word-break:break-all; }
.tag{ display:inline-block; font-size:11px; font-weight:600; padding:1px 7px; border-radius:99px; border:1px solid var(--border); color:var(--text-secondary); margin-right:4px; }
.tag.warn{ color:var(--critical); }
.tag.ok{ color:var(--good); }
.user-actions{ display:flex; gap:4px; flex-wrap:wrap; }
.user-actions select{ padding:3px 6px; font-size:12px; }

/* Editing controls only for editor/admin roles. */
body:not(.can-edit) :is(#editBtn, #addWidgetBtn, #resetLayoutBtn, .upload-card, #costForm, #tireForm, [data-del], [data-del-cost], [data-del-tire], .edit-only){ display:none !important; }

/* admin workspace */
.subnav{ display:flex; gap:4px; flex-wrap:wrap; margin-bottom:14px; border-bottom:1px solid var(--border); padding-bottom:8px; }
.subnav button{ font:inherit; font-weight:600; font-size:13px; background:transparent; border:0; color:var(--text-secondary); padding:7px 11px; border-radius:8px; cursor:pointer; }
.subnav button[aria-current="true"]{ background:var(--surface-1); color:var(--text-primary); box-shadow:inset 0 -2px 0 var(--brand-accent); }
.subnav .n{ font-size:11px; color:var(--text-muted); margin-left:4px; }
.chip{ display:inline-flex; align-items:center; gap:4px; font-size:12px; font-weight:600; padding:1px 8px; border-radius:99px; border:1px solid var(--border); white-space:nowrap; }
.chip.crit{ color:var(--critical); border-color:currentColor; }
.chip.warn{ color:var(--warning); border-color:currentColor; }
.chip.ok{ color:var(--good); }
.chip.done{ color:var(--text-muted); }
.overdue{ color:var(--critical); font-weight:600; }
.count-badge{ display:inline-block; min-width:18px; padding:0 5px; border-radius:99px; background:var(--brand-accent); color:#fff; font-size:11px; line-height:18px; text-align:center; margin-left:4px; }
.ws-toolbar{ display:flex; flex-wrap:wrap; gap:8px; align-items:center; margin-bottom:10px; }
.ws-toolbar input[type=search]{ min-width:200px; }
.data-table td.wrap{ white-space:normal; min-width:240px; max-width:420px; }
.data-table tr.closed td{ color:var(--text-muted); }
.wide-dlg{ width:min(760px, calc(100vw - 32px)); }
.item-grid{ display:grid; grid-template-columns:repeat(2, minmax(0,1fr)); gap:10px; }
.item-grid .wide{ grid-column:1 / -1; }
.item-grid textarea{ font:inherit; color:var(--text-primary); background:var(--surface-1); border:1px solid var(--border); border-radius:8px; padding:6px 8px; min-height:64px; resize:vertical; }
@media (max-width:620px){ .item-grid{ grid-template-columns:1fr; } }
#itemHistory{ margin-top:8px; font-size:12px; color:var(--text-secondary); }
#itemHistory div p{ margin:4px 0; }
.tiles{ display:grid; grid-template-columns:repeat(auto-fit, minmax(170px,1fr)); gap:12px; margin-bottom:14px; }
.tile{ background:var(--surface-1); border:1px solid var(--border); border-radius:12px; padding:12px 14px; cursor:pointer; text-align:left; font:inherit; color:inherit; }
.tile:hover{ border-color:var(--brand-accent); }
.tile .t{ font-size:12px; font-weight:600; color:var(--text-secondary); }
.tile .v{ font-size:28px; font-weight:700; }
.tile .v.bad{ color:var(--critical); }
.stale{ color:var(--critical); font-weight:600; }
.towbook{ font-size:11px; color:var(--text-muted); }

.update-bar{ position:fixed; top:12px; left:50%; transform:translateX(-50%); z-index:70; background:var(--surface-1); color:var(--text-primary);
  border:2px solid var(--brand-accent); border-radius:10px; padding:8px 12px; display:flex; gap:10px; align-items:center; box-shadow:0 6px 20px rgba(0,0,0,.2); }
/* Tab row scrolls sideways on narrow screens instead of running off the edge. */
.tabs{ overflow-x:auto; scrollbar-width:none; }
.tabs button{ white-space:nowrap; flex:0 0 auto; }

/* fleet */
.item-grid h4{ grid-column:1 / -1; margin:8px 0 0; font-size:13px; color:var(--text-secondary); border-bottom:1px solid var(--border); padding-bottom:4px; }
.due-soon{ color:var(--warning); font-weight:600; }
.due-past{ color:var(--critical); font-weight:600; }
body:not(.can-edit) .edit-only-flex{ display:none !important; }
