:root {
  color-scheme: light;
  --page: #f7f7f5;
  --surface-1: #ffffff;
  --text-primary: #101014;
  --text-secondary: #4d4d55;
  --text-muted: #85858e;
  --grid: #e8e7e2;
  --baseline: #d4d3cc;
  --border: rgba(16, 16, 20, 0.08);
  --shadow: 0 1px 2px rgba(16, 16, 20, 0.04), 0 4px 12px rgba(16, 16, 20, 0.05);
  --brand: #5b54d9;
  --brand-2: #e0559e;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-5: #e87ba4;
  --good: #0ca30c;
  --warn: #c98500;
  --critical: #d03b3b;
  --radius: 14px;
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0f;
  --surface-1: #17171a;
  --text-primary: #f5f5f7;
  --text-secondary: #bdbdc4;
  --text-muted: #8b8b93;
  --grid: #26262b;
  --baseline: #35353c;
  --border: rgba(255, 255, 255, 0.08);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  --brand: #7d76f2;
  --brand-2: #f27ab8;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-5: #d55181;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font: 14.5px/1.55 "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--brand); text-decoration: none; }

#loadbar {
  position: fixed;
  top: 0;
  left: 0;
  height: 3px;
  width: 0;
  background: linear-gradient(90deg, var(--brand), var(--brand-2));
  z-index: 100;
  opacity: 0;
  transition: width 0.25s ease, opacity 0.2s ease;
}
#loadbar.on { opacity: 1; animation: loadbar 12s cubic-bezier(0.1, 0.6, 0.2, 1) forwards; }
@keyframes loadbar { 0% { width: 0; } 60% { width: 72%; } 100% { width: 92%; } }

nav {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 212px;
  z-index: 20;
  display: flex;
  flex-direction: column;
  background: var(--surface-1);
  border-right: 1px solid var(--border);
  padding: 0.9rem 0.7rem;
  overflow-y: auto;
}
.nav-top { display: flex; align-items: center; }
nav .brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  font-size: 0.95rem;
  color: var(--text-primary);
  white-space: nowrap;
  padding: 0.3rem 0.45rem 0.9rem;
}
.brand-icon { width: 22px; height: 22px; border-radius: 6px; display: block; }
/* An uploaded logo keeps its own proportions and carries the wordmark, so
   it is given more room and the text lockup steps aside. */
.brand.has-logo { padding-left: 0.15rem; }
.brand.has-logo .brand-icon {
  width: auto;
  height: 30px;
  max-width: 168px;
  border-radius: 0;
  object-fit: contain;
}
nav .brand .hq { color: var(--brand); }
.nav-panel {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}
.nav-links { display: flex; flex-direction: column; gap: 0.1rem; }
nav a {
  color: var(--text-secondary);
  padding: 0.48rem 0.65rem;
  border-radius: 9px;
  font-size: 0.9rem;
  white-space: nowrap;
}
nav a:hover { background: color-mix(in srgb, var(--brand) 9%, transparent); }
nav a.active {
  color: var(--brand);
  font-weight: 650;
  background: color-mix(in srgb, var(--brand) 13%, transparent);
}
.nav-group { margin: 0; }
.nav-group > summary.nav-glabel {
  list-style: none;
  cursor: pointer;
  user-select: none;
  display: flex;
  align-items: center;
  border-radius: 7px;
}
.nav-group > summary.nav-glabel::-webkit-details-marker { display: none; }
.nav-group > summary.nav-glabel::after {
  content: "";
  width: 6px;
  height: 6px;
  margin-left: auto;
  border-right: 1.6px solid var(--text-muted);
  border-bottom: 1.6px solid var(--text-muted);
  transform: rotate(-45deg);
  transition: transform 0.15s ease;
}
.nav-group[open] > summary.nav-glabel::after { transform: rotate(45deg); }
.nav-group > summary.nav-glabel:hover { color: var(--text-secondary); }
.nav-glabel {
  color: var(--text-muted);
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 0.9rem 0.65rem 0.25rem;
}
.nav-group a { display: block; }
.nav-side {
  margin-top: auto;
  padding-top: 0.75rem;
  border-top: 1px solid var(--grid);
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}
.nav-side .who { color: var(--text-muted); font-size: 0.78rem; padding: 0 0.65rem; }
.nav-side .logout { color: var(--text-secondary); padding: 0.35rem 0.65rem; }
.theme-btn {
  margin: 0.25rem 0.35rem 0;
  padding: 0.4rem 0.65rem;
  font-size: 0.82rem;
  text-align: left;
  color: var(--text-secondary);
  background: none;
  border: 1px solid var(--grid);
  border-radius: 9px;
}
.theme-btn:hover { border-color: var(--brand); color: var(--brand); }
.nav-toggle { display: none; }
.nav-burger { display: none; }

@media (max-width: 900px) {
  nav {
    position: sticky;
    inset: auto;
    width: auto;
    flex-direction: column;
    border-right: none;
    border-bottom: 1px solid var(--border);
    background: color-mix(in srgb, var(--surface-1) 92%, transparent);
    backdrop-filter: blur(10px);
    padding: 0.35rem 1rem;
    overflow: visible;
  }
  .nav-top { width: 100%; }
  nav .brand { padding: 0.45rem 0; }
  .nav-burger {
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 4px;
    margin-left: auto;
    padding: 0.55rem;
    border: none;
    background: none;
    cursor: pointer;
  }
  .nav-burger span {
    display: block;
    width: 20px;
    height: 2px;
    border-radius: 2px;
    background: var(--text-primary);
    transition: transform 0.18s ease, opacity 0.18s ease;
  }
  .nav-toggle:checked ~ .nav-burger span:nth-child(1) { transform: translateY(6px) rotate(45deg); }
  .nav-toggle:checked ~ .nav-burger span:nth-child(2) { opacity: 0; }
  .nav-toggle:checked ~ .nav-burger span:nth-child(3) { transform: translateY(-6px) rotate(-45deg); }
  .nav-panel { display: none; }
  .nav-top:has(.nav-toggle:checked) + .nav-panel,
  nav:has(.nav-toggle:checked) .nav-panel {
    display: flex;
    padding: 0.4rem 0 0.3rem;
    border-top: 1px solid var(--grid);
    margin-top: 0.3rem;
  }
  nav a { padding: 0.6rem 0.65rem; font-size: 0.95rem; }
  .nav-side { margin-top: 0.4rem; flex-direction: row; justify-content: space-between; align-items: center; }
  .theme-btn { margin: 0; }
}

main { max-width: 1160px; margin: 0 auto; padding: 1.5rem 1.25rem 3rem; }
@media (min-width: 901px) {
  body.with-nav main { margin-left: 232px; margin-right: auto; max-width: 1080px; }
}
h1 { font-size: 1.45rem; font-weight: 750; letter-spacing: -0.015em; margin: 0.25rem 0 0.9rem; }
h2 {
  font-size: 0.82rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-muted);
  margin: 2rem 0 0.6rem;
}

.alert {
  border-radius: 11px;
  padding: 0.65rem 0.95rem;
  margin: 0 0 0.9rem;
  font-size: 0.9rem;
  font-weight: 550;
  border: 1px solid;
  transition: opacity 0.5s ease;
}
.alert.success {
  color: var(--good);
  border-color: color-mix(in srgb, var(--good) 35%, transparent);
  background: color-mix(in srgb, var(--good) 9%, var(--surface-1));
}
.alert.error {
  color: var(--critical);
  border-color: color-mix(in srgb, var(--critical) 35%, transparent);
  background: color-mix(in srgb, var(--critical) 9%, var(--surface-1));
}
.alert.fade { opacity: 0; }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(195px, 1fr)); gap: 0.7rem; }
.tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.95rem 1.05rem;
  box-shadow: var(--shadow);
}
.tile .label { color: var(--text-muted); font-size: 0.76rem; font-weight: 550; }
.tile .value { font-size: 1.55rem; font-weight: 700; letter-spacing: -0.02em; margin-top: 0.2rem; }
.tile .unit { color: var(--text-muted); font-size: 0.78rem; font-weight: 450; }

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.2rem;
  margin-top: 0.7rem;
  box-shadow: var(--shadow);
}
.chart-wrap { position: relative; height: 300px; }

table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; font-size: 0.92rem; }
th, td { text-align: left; padding: 0.5rem 0.65rem; border-bottom: 1px solid var(--grid); }
th { color: var(--text-muted); font-weight: 650; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.06em; }
td.num, th.num { text-align: right; }
td.neg { color: var(--critical); }
td.pos { color: var(--good); }
tbody tr:hover td { background: color-mix(in srgb, var(--brand) 5%, transparent); }
tbody tr:last-child td { border-bottom: none; }
.table-scroll { overflow-x: auto; }

form.filters { display: flex; gap: 0.5rem; flex-wrap: wrap; margin: 0.75rem 0; }
input, select, button {
  font: inherit;
  color: var(--text-primary);
  background: var(--surface-1);
  border: 1px solid var(--baseline);
  border-radius: 9px;
  padding: 0.45rem 0.65rem;
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}
input:focus, select:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 18%, transparent);
}
input, select { max-width: 100%; min-width: 0; }
button { cursor: pointer; }
button:hover { border-color: var(--brand); }
button.primary {
  background: var(--brand);
  color: #fff;
  border-color: var(--brand);
  font-weight: 600;
}
button.primary:hover { filter: brightness(1.08); }
button.loading {
  position: relative;
  color: transparent !important;
  pointer-events: none;
}
button.loading::after {
  content: "";
  position: absolute;
  inset: 0;
  margin: auto;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  border: 2px solid color-mix(in srgb, currentColor 0%, rgba(128, 128, 128, 0.35));
  border-top-color: var(--brand);
  animation: spin 0.7s linear infinite;
}
button.primary.loading::after { border-top-color: #fff; border-color: rgba(255, 255, 255, 0.35); }
@keyframes spin { to { transform: rotate(360deg); } }

.entry-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 0.5rem; }
.entry-grid .wide { grid-column: 1 / -1; }

.login-box { max-width: 370px; margin: 10vh auto; }
.login-box .card { padding: 1.75rem; }
.login-box label { display: block; margin: 0.7rem 0 0.25rem; color: var(--text-secondary); font-size: 0.85rem; }
.login-box input { width: 100%; }
.login-box button { width: 100%; margin-top: 1.1rem; }
.lockup { text-align: center; margin-bottom: 1.2rem; }
.lockup img { width: 52px; height: 52px; border-radius: 13px; display: inline-block; }
.lockup.has-logo img {
  width: auto;
  height: auto;
  max-width: 240px;
  max-height: 96px;
  border-radius: 0;
  object-fit: contain;
}
.lockup .name {
  display: block;
  font-weight: 800;
  font-size: 1.35rem;
  letter-spacing: 0.22em;
  margin-top: 0.55rem;
  color: var(--brand);
}
.lockup .tagline {
  display: block;
  font-family: "Dancing Script", cursive;
  font-size: 1.15rem;
  color: var(--brand-2);
  margin-top: 0.05rem;
}
.error { color: var(--critical); font-size: 0.85rem; margin-top: 0.5rem; }
[dir="auto"] { unicode-bidi: plaintext; }

.rangebar { display: flex; gap: 0.4rem; flex-wrap: wrap; align-items: center; margin: 0.5rem 0 1.1rem; }
.rangebar .chip {
  padding: 0.34rem 0.8rem;
  border-radius: 999px;
  font-size: 0.83rem;
  font-weight: 550;
  border: 1px solid var(--baseline);
  color: var(--text-secondary);
  background: var(--surface-1);
  transition: all 0.12s ease;
}
.rangebar .chip:hover { border-color: var(--brand); color: var(--brand); }
.rangebar .chip.active {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
  font-weight: 650;
}
.rangebar .custom { display: flex; gap: 0.3rem; align-items: center; margin-left: 0.4rem; flex-wrap: wrap; }
.rangebar input[type="date"] { padding: 0.28rem 0.45rem; font-size: 0.83rem; max-width: 40vw; min-width: 0; }
.rangebar button { padding: 0.28rem 0.75rem; font-size: 0.83rem; }

.tile.health-good { border-left: 4px solid var(--good); }
.tile.health-warn { border-left: 4px solid var(--warn); }
.tile.health-bad { border-left: 4px solid var(--critical); }

.pager { display: flex; gap: 0.75rem; align-items: center; margin-top: 0.85rem; font-size: 0.85rem; }
.pager .muted { color: var(--text-muted); }
tr.totals td { font-weight: 700; border-top: 2px solid var(--baseline); background: color-mix(in srgb, var(--brand) 4%, transparent); }
.rowactions a { font-size: 0.78rem; color: var(--text-muted); }
.rowactions a:hover { color: var(--brand); }

a.tile { display: block; color: inherit; transition: transform 0.12s ease, border-color 0.12s ease; }
a.tile:hover { border-color: var(--brand); transform: translateY(-1px); }

.logo-row { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.logo-preview {
  max-width: 190px;
  max-height: 64px;
  object-fit: contain;
  padding: 0.5rem 0.7rem;
  border: 1px solid var(--grid);
  border-radius: 11px;
  background: var(--page);
}
.logo-row form { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }

/* Explain a number where it is shown. Works on hover and on tap, since a
   title attribute never appears on a phone. */
.info {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 15px;
  height: 15px;
  margin-left: 0.3rem;
  border-radius: 50%;
  border: 1px solid var(--baseline);
  color: var(--text-muted);
  font-size: 10px;
  font-weight: 700;
  font-style: normal;
  cursor: help;
  position: relative;
  vertical-align: middle;
}
.info:hover, .info:focus { border-color: var(--brand); color: var(--brand); outline: none; }
.info::after {
  content: attr(data-tip);
  position: absolute;
  bottom: calc(100% + 7px);
  left: 50%;
  transform: translateX(-50%);
  width: max-content;
  max-width: 260px;
  padding: 0.5rem 0.65rem;
  border-radius: 9px;
  background: var(--text-primary);
  color: var(--page);
  font-size: 0.76rem;
  font-weight: 450;
  line-height: 1.45;
  text-align: left;
  white-space: normal;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.12s ease;
  z-index: 40;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.18);
}
.info:hover::after, .info:focus::after { opacity: 1; visibility: visible; }
/* Near the right edge the bubble would leave the card. */
.tiles > *:nth-child(4n) .info::after,
.tiles > *:last-child .info::after { left: auto; right: 0; transform: none; }

#lines-table input, #lines-table select { width: 100%; padding: 0.32rem 0.45rem; font-size: 0.86rem; }
#lines-table td { padding: 0.3rem 0.4rem; }
.line-drop { border: none; background: none; color: var(--text-muted); cursor: pointer; padding: 0 0.3rem; }
.line-drop:hover { color: var(--critical); }
.kind-tag { font-size: 0.7rem; font-weight: 700; padding: 0.1rem 0.4rem; border-radius: 999px;
  border: 1px solid var(--baseline); color: var(--text-muted); white-space: nowrap; }
.kind-tag.invoice { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 40%, transparent); }
