/* ---------- Tokens ---------- */
:root {
  color-scheme: light;
  /* Leotech logo: navy #07223F, orange #FF8C15 -> #E7532A, sky blue #98DCF5 -> #37AAEC, slate #79838D */
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f7f9fc;
  --border: #e1e6ee;
  --border-strong: #cbd3de;
  --text: #0d1b2e;
  --text-2: #475569;
  --text-3: #6b7785;
  --brand: #1668b8;          /* links, focus, highlights */
  --brand-strong: #07223f;   /* navy */
  --brand-soft: #e6f3fc;
  --primary: #0b2a4d;        /* primary buttons */
  --primary-hover: #07223f;
  --on-primary: #ffffff;
  --accent: #f57921;         /* orange */
  --accent-soft: #fff1e6;
  --accent-strong: #c2410c;
  --brand-gradient: linear-gradient(90deg, #ff8c15 0%, #e7532a 34%, #37aaec 66%, #98dcf5 100%);
  --focus: #2a8fe0;

  --good: #0a7d0a;       --good-bg: #e5f5e5;
  --warn: #8a5a00;       --warn-bg: #fdf1d6;
  --critical: #b42c2c;   --critical-bg: #fbe7e7;
  --info: #1f5fae;       --info-bg: #e3eefb;
  --muted-bg: #eef1f4;

  /* chart marks (status palette + series blue) */
  --chart-good: #0ca30c;
  --chart-critical: #d03b3b;
  --chart-series-1: #2a78d6;
  --chart-grid: #e6eaee;
  --chart-text: #4b5966;

  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(16, 32, 48, .05), 0 1px 1px rgba(16, 32, 48, .03);
  --sidenav-w: 232px;
  --topbar-h: 56px;
  --tabbar-h: 62px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0c1522;
    --surface: #132033;
    --surface-2: #18273d;
    --border: #233650;
    --border-strong: #31476a;
    --text: #edf2f8;
    --text-2: #b6c2d2;
    --text-3: #8b9aae;
    --brand: #5db8f2;
    --brand-strong: #a6dcf8;
    --brand-soft: #16324f;
    --primary: #37aaec;
    --primary-hover: #5db8f2;
    --on-primary: #07223f;
    --accent: #ff9a45;
    --accent-soft: #3a2414;
    --accent-strong: #ffb27a;
    --focus: #5db8f2;
    --good: #4cc94c;       --good-bg: #16301a;
    --warn: #f0b84a;       --warn-bg: #3a2d10;
    --critical: #f07b7b;   --critical-bg: #3d1c1c;
    --info: #7fb2f0;       --info-bg: #182a40;
    --muted-bg: #262d32;
    --chart-good: #0ca30c;
    --chart-critical: #d03b3b;
    --chart-series-1: #3987e5;
    --chart-grid: #233650;
    --chart-text: #b9c2ca;
    --shadow: none;
  }
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
}
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { line-height: 1.25; margin: 0; }
h1 { font-size: 1.4rem; font-weight: 700; color: var(--brand-strong); }
h2 { font-size: 1.05rem; font-weight: 650; }
h3 { font-size: .95rem; font-weight: 600; }
p { margin: 0 0 .75rem; }
hr { border: 0; border-top: 1px solid var(--border); margin: .5rem 0; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.i { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.muted { color: var(--text-3); }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }
.num { font-variant-numeric: tabular-nums; text-align: right; }
.pre { white-space: pre-wrap; }
[hidden] { display: none !important; }

/* ---------- Shell ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30;
  height: var(--topbar-h);
  display: flex; align-items: center; gap: .5rem;
  padding: 0 12px;
  padding-top: env(safe-area-inset-top);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar::after { content: ""; position: absolute; left: 0; right: 0; bottom: -1px; height: 3px; background: var(--brand-gradient); }
.brand { display: flex; align-items: center; gap: .6rem; color: var(--text); font-weight: 650; min-width: 0; }
.brand:hover { text-decoration: none; }
.brand img { flex: none; }
.brand-text { display: flex; flex-direction: column; line-height: 1.05; min-width: 0; }
.brand-text b { font-size: 1.12rem; font-weight: 800; letter-spacing: -.01em; color: var(--brand-strong); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-text small { font-size: .6rem; font-weight: 600; letter-spacing: .32em; text-transform: uppercase; color: var(--text-3); white-space: nowrap; }
.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: .35rem; }
.icon-btn {
  position: relative; display: inline-grid; place-items: center;
  width: 40px; height: 40px; border-radius: 10px; border: 0; background: none; color: var(--text-2); cursor: pointer;
}
.icon-btn:hover { background: var(--muted-bg); text-decoration: none; }
.bell .dot {
  position: absolute; top: 4px; right: 3px; min-width: 18px; height: 18px; padding: 0 4px;
  border-radius: 9px; background: var(--critical); color: #fff; font-size: 11px; font-weight: 700; line-height: 18px; text-align: center;
}
.avatar {
  width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent-strong); font-size: .8rem; font-weight: 700;
}
.avatar:hover { text-decoration: none; }

.sidenav {
  position: fixed; z-index: 40; top: 0; bottom: 0; left: 0; width: min(280px, 82vw);
  padding: calc(env(safe-area-inset-top) + 12px) 10px 16px;
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 2px; overflow-y: auto;
  transform: translateX(-102%); transition: transform .2s ease;
}
.sidenav.open { transform: none; }
.sidenav a, .sidenav button {
  display: flex; align-items: center; gap: .75rem; width: 100%;
  padding: .6rem .75rem; border-radius: 9px; border: 0; background: none;
  color: var(--text-2); font: inherit; font-weight: 500; text-align: left; cursor: pointer;
}
.sidenav a:hover, .sidenav button:hover { background: var(--muted-bg); text-decoration: none; }
.sidenav a.active { background: var(--brand-soft); color: var(--brand-strong); font-weight: 600; box-shadow: inset 3px 0 0 var(--accent); }
.scrim { position: fixed; inset: 0; z-index: 35; background: rgba(10, 20, 30, .4); }

.tabbar {
  position: fixed; z-index: 30; left: 0; right: 0; bottom: 0;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: flex; background: var(--surface); border-top: 1px solid var(--border);
}
.tabbar a, .tabbar button {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  border: 0; background: none; color: var(--text-3); font: inherit; font-size: .7rem; font-weight: 500; cursor: pointer;
}
.tabbar a:hover { text-decoration: none; }
.tabbar a.active { color: var(--brand-strong); position: relative; }
.tabbar a.active::before { content: ""; position: absolute; top: 0; left: 30%; right: 30%; height: 3px; border-radius: 0 0 3px 3px; background: var(--accent); }
.tabbar .i { width: 22px; height: 22px; }

.main {
  max-width: 1180px; margin: 0 auto;
  padding: 16px 16px calc(var(--tabbar-h) + 28px + env(safe-area-inset-bottom));
}
.main.bare { max-width: none; padding: 0; }

@media (min-width: 900px) {
  .menu-btn, .tabbar, .scrim { display: none !important; }
  .sidenav { transform: none; top: var(--topbar-h); width: var(--sidenav-w); padding-top: 14px; z-index: 20; }
  .main { margin-left: var(--sidenav-w); max-width: calc(1180px + var(--sidenav-w)); padding: 24px 32px 48px; }
  .main.bare { margin-left: 0; }
}

/* ---------- Page header ---------- */
.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; gap: .75rem 1rem; margin-bottom: 18px; }
.page-head .grow { flex: 1 1 240px; min-width: 0; }
.page-head .sub { color: var(--text-3); margin-top: .2rem; }
.crumbs { font-size: .85rem; color: var(--text-3); margin-bottom: .3rem; }
.actions { display: flex; flex-wrap: wrap; gap: .5rem; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .45rem;
  min-height: 42px; padding: .5rem 1rem; border-radius: 10px;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 550; cursor: pointer; white-space: nowrap;
}
.btn:hover { text-decoration: none; background: var(--surface-2); }
.btn .i { width: 18px; height: 18px; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-good { background: var(--good); border-color: var(--good); color: #fff; }
.btn-danger { color: var(--critical); }
.btn-danger:hover { background: var(--critical-bg); }
.btn-sm { min-height: 34px; padding: .3rem .7rem; font-size: .875rem; }
.btn-block { width: 100%; }
.link-btn { border: 0; background: none; color: var(--brand); font: inherit; padding: 0; cursor: pointer; }
.inline { display: inline; }

/* ---------- Cards & layout ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; min-width: 0;
}
.card + .card { margin-top: 16px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: .75rem; margin-bottom: 12px; }
.card-head a { font-size: .875rem; font-weight: 550; }
.card.flush { padding: 0; }
.card.flush > .card-head { padding: 14px 16px 0; }
.grid { display: grid; gap: 16px; }
.grid > .card + .card { margin-top: 0; }
@media (min-width: 760px) { .grid-2 { grid-template-columns: 1fr 1fr; } }
@media (min-width: 1000px) { .grid-3-1 { grid-template-columns: 2fr 1fr; } }
.stack > * + * { margin-top: 16px; }
.section-gap { margin-top: 16px; }

/* ---------- KPI tiles ---------- */
.kpis { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
@media (min-width: 760px) { .kpis { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.kpi {
  display: block; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px 16px; color: var(--text); min-width: 0;
}
a.kpi:hover { text-decoration: none; border-color: var(--border-strong); }
.kpi-label { display: flex; align-items: center; gap: .4rem; font-size: .82rem; color: var(--text-2); font-weight: 500; }
.kpi-label .i { width: 16px; height: 16px; }
.kpi-value { font-size: 1.9rem; font-weight: 650; line-height: 1.15; margin-top: .3rem; }
.kpi-note { font-size: .8rem; color: var(--text-3); margin-top: .15rem; }
.kpi.is-critical .kpi-label { color: var(--critical); }
.kpi.is-good .kpi-label { color: var(--good); }
.meter { height: 6px; border-radius: 3px; background: var(--good-bg); margin-top: .5rem; overflow: hidden; }
.meter > span { display: block; height: 100%; border-radius: 3px; background: var(--chart-good); }

/* ---------- Badges ---------- */
.badge {
  display: inline-flex; align-items: center; gap: .3rem; padding: .12rem .55rem; border-radius: 999px;
  font-size: .76rem; font-weight: 600; white-space: nowrap; background: var(--muted-bg); color: var(--text-2);
}
.badge .i { width: 13px; height: 13px; stroke-width: 2.4; }
.s-completed, .s-active { background: var(--good-bg); color: var(--good); }
.s-overdue, .s-missed { background: var(--critical-bg); color: var(--critical); }
.s-in_progress, .s-expiring { background: var(--warn-bg); color: var(--warn); }
.s-scheduled { background: var(--info-bg); color: var(--info); }
.s-cancelled, .s-expired { background: var(--muted-bg); color: var(--text-3); }

/* ---------- Lists ---------- */
.list { list-style: none; margin: 0; padding: 0; }
.list > li + li { border-top: 1px solid var(--border); }
.row-link {
  display: flex; align-items: center; gap: 12px; padding: 12px 16px; color: var(--text);
}
.row-link:hover { background: var(--surface-2); text-decoration: none; }
.row-main { flex: 1; min-width: 0; }
.row-title, .row-sub { display: block; }
.row-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub { font-size: .85rem; color: var(--text-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-end { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex: none; }
.row-chev { color: var(--text-3); width: 16px; height: 16px; }
.date-chip {
  flex: none; width: 48px; text-align: center; border-radius: 10px; padding: 4px 0; background: var(--brand-soft); color: var(--brand-strong);
}
.date-chip b { display: block; font-size: 1.15rem; line-height: 1.1; }
.date-chip small { display: block; font-size: .68rem; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.date-chip.is-overdue { background: var(--critical-bg); color: var(--critical); }
.date-chip.is-done { background: var(--good-bg); color: var(--good); }
.empty { padding: 28px 16px; text-align: center; color: var(--text-3); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: .78rem; font-weight: 600; color: var(--text-3); text-transform: uppercase; letter-spacing: .03em; background: var(--surface-2); white-space: nowrap; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; }

/* ---------- Detail lists ---------- */
.dl { display: grid; grid-template-columns: minmax(110px, 38%) 1fr; gap: 8px 12px; margin: 0; }
.dl dt { color: var(--text-3); font-size: .875rem; }
.dl dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.contact-links { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: 12px; }

/* ---------- Forms ---------- */
.form { max-width: 720px; }
.field { margin-bottom: 14px; }
.field label, .label { display: block; font-weight: 550; font-size: .9rem; margin-bottom: 5px; }
.field .hint { font-size: .8rem; color: var(--text-3); margin-top: 4px; }
.req::after { content: " *"; color: var(--critical); }
input[type=text], input[type=email], input[type=password], input[type=tel], input[type=date], input[type=number], input[type=search], select, textarea {
  width: 100%; min-height: 44px; padding: .55rem .75rem; border-radius: 10px;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
  font: inherit; font-size: 16px; /* 16px stops iOS zooming on focus */
}
textarea { min-height: 96px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--focus); box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 22%, transparent); }
.check { display: flex; align-items: center; gap: .55rem; font-weight: 500; cursor: pointer; }
.check input { width: 20px; height: 20px; accent-color: var(--brand); }
.fields-2 { display: grid; gap: 0 14px; }
@media (min-width: 600px) { .fields-2 { grid-template-columns: 1fr 1fr; } .fields-3 { grid-template-columns: 1fr 1fr 1fr; } }
.form-actions { display: flex; flex-wrap: wrap; gap: .6rem; margin-top: 6px; }
.form-error { background: var(--critical-bg); color: var(--critical); border-radius: 10px; padding: .7rem .9rem; margin-bottom: 14px; font-weight: 500; }
fieldset { border: 0; padding: 0; margin: 0; }
details.more > summary { cursor: pointer; font-weight: 600; padding: .5rem 0; color: var(--brand); list-style: none; }
details.more > summary::-webkit-details-marker { display: none; }
details.more > summary::before { content: "+ "; }
details.more[open] > summary::before { content: "− "; }

.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-end; margin-bottom: 16px; }
.filters .field { margin: 0; flex: 1 1 140px; min-width: 0; }
.filters .field label { font-size: .78rem; color: var(--text-3); }
.filters .btn { flex: none; }
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 14px; scrollbar-width: none; }
.chips a {
  flex: none; padding: .38rem .85rem; border-radius: 999px; border: 1px solid var(--border-strong);
  color: var(--text-2); font-size: .875rem; font-weight: 550; background: var(--surface);
}
.chips a:hover { text-decoration: none; background: var(--surface-2); }
.chips a.active { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.search { position: relative; flex: 1 1 220px; }
.search .i { position: absolute; left: 11px; top: 12px; color: var(--text-3); width: 18px; height: 18px; }
.search input { padding-left: 36px; }

/* ---------- Upload ---------- */
.dropzone {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: .4rem;
  border: 2px dashed var(--border-strong); border-radius: var(--radius); padding: 22px 16px; text-align: center;
  background: var(--surface-2); cursor: pointer; color: var(--text-2);
}
.dropzone:hover, .dropzone.has-files { border-color: var(--brand); color: var(--brand-strong); }
.dropzone .i { width: 30px; height: 30px; }
.dropzone input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.file-list { font-size: .85rem; color: var(--text-2); margin-top: 6px; }
.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 10px; }
.thumb { position: relative; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; background: var(--surface-2); }
.thumb a.preview { display: grid; place-items: center; aspect-ratio: 3 / 4; color: var(--text-2); }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb .pdf { font-weight: 700; font-size: .9rem; display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 8px; text-align: center; }
.thumb .pdf .i { width: 30px; height: 30px; }
.thumb form { position: absolute; top: 4px; right: 4px; }
.thumb .del { width: 28px; height: 28px; border-radius: 50%; border: 0; background: rgba(0,0,0,.6); color: #fff; font-size: 16px; line-height: 1; cursor: pointer; }
.readings { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 12px; }
@media (min-width: 600px) { .readings { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.readings .field label { font-size: .8rem; font-weight: 500; color: var(--text-2); }
.reading-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.reading-chips span { background: var(--muted-bg); border-radius: 8px; padding: .25rem .55rem; font-size: .85rem; }

/* ---------- Visit header ---------- */
.visit-hero { display: flex; gap: 14px; align-items: flex-start; }
.visit-hero .date-chip { width: 58px; padding: 6px 0; }
.visit-hero .date-chip b { font-size: 1.4rem; }
.meta { display: flex; flex-wrap: wrap; gap: 6px 14px; color: var(--text-2); font-size: .875rem; margin-top: 6px; }
.meta span { display: inline-flex; align-items: center; gap: 5px; }
.meta .i { width: 15px; height: 15px; }
.sticky-actions {
  position: sticky; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 8px); z-index: 10;
  display: flex; gap: 8px; padding: 10px; margin: 16px -4px 0;
  background: color-mix(in srgb, var(--surface) 92%, transparent); backdrop-filter: blur(6px);
  border: 1px solid var(--border); border-radius: 14px; box-shadow: 0 6px 20px rgba(0,0,0,.08);
}
.sticky-actions .btn { flex: 1; }
@media (min-width: 900px) { .sticky-actions { bottom: 12px; } }

/* ---------- Flash ---------- */
.flash { border-radius: 10px; padding: .75rem 1rem; margin-bottom: 16px; font-weight: 500; }
.flash-success { background: var(--good-bg); color: var(--good); }
.flash-error { background: var(--critical-bg); color: var(--critical); }
.flash-info { background: var(--info-bg); color: var(--info); }

/* ---------- Charts ---------- */
.chart-box { position: relative; height: 260px; }
.chart-box.tall { height: 300px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: .82rem; color: var(--text-2); margin-bottom: 10px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.legend .l-good { background: var(--chart-good); }
.legend .l-critical { background: var(--chart-critical); }
.legend .l-series-1 { background: var(--chart-series-1); }

/* ---------- Login ---------- */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; background: var(--bg); }
.auth-card { width: 100%; max-width: 380px; }
.auth-card .brand { flex-direction: column; justify-content: center; gap: .7rem; margin-bottom: 22px; text-align: center; }
.auth-card .brand-text { align-items: center; }
.auth-card .brand-text b { font-size: 2rem; }
.auth-card .brand-text small { font-size: .78rem; margin-top: .25rem; padding-left: .32em; }
.auth-card .card { position: relative; overflow: hidden; }
.auth-card .card::before { content: ""; position: absolute; top: 0; left: 0; right: 0; height: 4px; background: var(--brand-gradient); }

/* ---------- Notifications ---------- */
.notif.unread .row-title::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--brand); margin-right: 8px; vertical-align: middle; }

@media print {
  .topbar, .sidenav, .tabbar, .actions, .filters, .chips, .sticky-actions, form { display: none !important; }
  .main { margin: 0; padding: 0; }
  body { background: #fff; }
  .card { box-shadow: none; }
}
