/* ── Reset & base ─────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --navy:   #0d1b2a;
  --navy2:  #162032;
  --navy3:  #1e2d40;
  --border: #253548;
  --teal:   #0a9396;
  --teal2:  #0cb8bb;
  --green:  #52b788;
  --coral:  #e76f51;
  --amber:  #e9c46a;
  --text:   #d4e5f7;
  --muted:  #7b96b2;
  --white:  #f0f8ff;
  --red:    #e63946;
  --font:   'Segoe UI', system-ui, -apple-system, sans-serif;
}

html { font-size: 15px; }
body { background: var(--navy); color: var(--text); font-family: var(--font); line-height: 1.5; min-height: 100vh; }

a { color: var(--teal2); text-decoration: none; }
a:hover { text-decoration: underline; }

code { font-family: 'Cascadia Code', 'Consolas', monospace; font-size: 0.9em;
       background: var(--navy3); padding: 2px 6px; border-radius: 4px; }

/* ── Login ────────────────────────────────────────────────────────────────── */
.login-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; }

.login-card {
  background: var(--navy2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2.5rem;
  width: 100%;
  max-width: 380px;
}

.login-card h1 { font-size: 1.5rem; color: var(--teal2); margin-bottom: 1.5rem; text-align: center; }

.login-card label { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 0.3rem; margin-top: 1rem; }

.login-card input {
  width: 100%; padding: 0.55rem 0.75rem;
  background: var(--navy3); border: 1px solid var(--border); border-radius: 6px;
  color: var(--white); font-size: 0.95rem;
}
.login-card input:focus { outline: 2px solid var(--teal); }

.login-card button {
  margin-top: 1.5rem; width: 100%; padding: 0.65rem;
  background: var(--teal); border: none; border-radius: 6px;
  color: #fff; font-size: 1rem; font-weight: 600; cursor: pointer;
}
.login-card button:hover { background: var(--teal2); }

.qr-setup { text-align: center; margin-bottom: 1.5rem; }
.qr-setup img { border-radius: 8px; border: 4px solid white; margin: 1rem 0; }
.manual-key { font-size: 0.8rem; color: var(--muted); margin-top: 0.5rem; word-break: break-all; }
.setup-link { margin-top: 1rem; text-align: center; font-size: 0.85rem; color: var(--muted); }

/* ── Alerts ───────────────────────────────────────────────────────────────── */
.alert { padding: 0.6rem 0.9rem; border-radius: 6px; margin-bottom: 1rem; font-size: 0.9rem; }
.alert-error { background: rgba(231,63,70,.18); border: 1px solid var(--red); color: #ff8a8e; }
.alert-warn  { background: rgba(233,196,106,.15); border: 1px solid var(--amber); color: var(--amber); }
.alert-ok    { background: rgba(82,183,136,.15); border: 1px solid var(--green); color: var(--green); }

/* ── App shell ────────────────────────────────────────────────────────────── */
.app { display: grid; grid-template-rows: auto 1fr; min-height: 100vh; }

header.topbar {
  background: var(--navy2); border-bottom: 1px solid var(--border);
  padding: 0.7rem 1.5rem; display: flex; align-items: center; gap: 1rem;
}
.topbar .logo { font-size: 1.1rem; font-weight: 700; color: var(--teal2); flex: 1; }
.topbar nav { display: flex; gap: 0.75rem; align-items: center; }

main.content { padding: 1.5rem; max-width: 1400px; width: 100%; margin: 0 auto; }

/* ── Filters bar ──────────────────────────────────────────────────────────── */
.filters {
  display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center;
  margin-bottom: 1.5rem;
}
.filters .range-btn {
  padding: 0.35rem 0.8rem; border-radius: 20px; font-size: 0.85rem; cursor: pointer;
  background: var(--navy3); border: 1px solid var(--border); color: var(--muted);
}
.filters .range-btn.active,
.filters .range-btn:hover { background: var(--teal); border-color: var(--teal); color: #fff; }

.filters .date-inputs { display: flex; gap: 0.4rem; align-items: center; margin-left: auto; }
.filters .date-inputs input[type=date] {
  background: var(--navy3); border: 1px solid var(--border); border-radius: 6px;
  color: var(--text); padding: 0.3rem 0.6rem; font-size: 0.85rem;
}

/* ── Stat tiles ───────────────────────────────────────────────────────────── */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 1rem; margin-bottom: 1.5rem; }

.tile {
  background: var(--navy2); border: 1px solid var(--border); border-radius: 10px;
  padding: 1rem; display: flex; flex-direction: column; gap: 0.3rem;
}
.tile .tile-label { font-size: 0.75rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.tile .tile-value { font-size: 1.9rem; font-weight: 700; line-height: 1; }
.tile .tile-sub   { font-size: 0.78rem; color: var(--muted); }

.tile-value.good   { color: var(--green); }
.tile-value.warn   { color: var(--amber); }
.tile-value.bad    { color: var(--coral); }
.tile-value.teal   { color: var(--teal2); }

/* ── Charts grid ──────────────────────────────────────────────────────────── */
.charts { display: grid; grid-template-columns: repeat(auto-fill, minmax(460px, 1fr)); gap: 1.25rem; margin-bottom: 1.5rem; }
.chart-card {
  background: var(--navy2); border: 1px solid var(--border); border-radius: 10px;
  padding: 1.2rem;
}
.chart-card h3 { font-size: 0.85rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; margin-bottom: 0.8rem; }
.chart-card canvas { display: block; width: 100% !important; }

.chart-wide { grid-column: 1 / -1; }

/* ── Calendar heatmap ─────────────────────────────────────────────────────── */
#calendar-wrap { overflow-x: auto; }
.cal-grid { display: flex; gap: 4px; }
.cal-month { display: flex; flex-direction: column; gap: 2px; }
.cal-month-label { font-size: 0.72rem; color: var(--muted); text-align: center; margin-bottom: 4px; }
.cal-week { display: flex; flex-direction: column; gap: 2px; }
.cal-day {
  width: 13px; height: 13px; border-radius: 2px; cursor: default;
  position: relative;
}
.cal-day[data-tip]:hover::after {
  content: attr(data-tip);
  position: absolute; left: 16px; top: -4px;
  background: var(--navy3); border: 1px solid var(--border);
  color: var(--text); font-size: 0.75rem; padding: 4px 8px; border-radius: 5px;
  white-space: nowrap; z-index: 10; pointer-events: none;
}

.ahi-none   { background: var(--navy3); }
.ahi-normal { background: var(--green); }
.ahi-mild   { background: var(--amber); }
.ahi-mod    { background: var(--coral); }
.ahi-severe { background: var(--red); }
.ahi-no-data{ background: var(--border); }

.cal-legend { display: flex; gap: 1rem; align-items: center; font-size: 0.78rem; color: var(--muted); margin-top: 0.6rem; flex-wrap: wrap; }
.cal-legend span { display: flex; align-items: center; gap: 4px; }
.swatch { width: 12px; height: 12px; border-radius: 2px; }

/* ── Night table ──────────────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; background: var(--navy2); border: 1px solid var(--border); border-radius: 10px; margin-bottom: 1.5rem; }
table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
thead th {
  background: var(--navy3); color: var(--muted); text-align: left;
  padding: 0.7rem 0.8rem; white-space: nowrap; user-select: none; cursor: pointer;
}
thead th:hover { color: var(--text); }
thead th .sort-arrow { opacity: 0.4; margin-left: 4px; }
thead th.sort-asc  .sort-arrow,
thead th.sort-desc .sort-arrow { opacity: 1; color: var(--teal2); }
tbody tr { border-top: 1px solid var(--border); }
tbody tr:hover { background: var(--navy3); }
tbody td { padding: 0.55rem 0.8rem; white-space: nowrap; }

.badge {
  display: inline-block; padding: 1px 8px; border-radius: 20px; font-size: 0.78rem; font-weight: 600;
}
.badge-ok   { background: rgba(82,183,136,.2); color: var(--green); }
.badge-warn { background: rgba(233,196,106,.2); color: var(--amber); }
.badge-bad  { background: rgba(231,111,81,.2);  color: var(--coral); }
.badge-nc   { background: rgba(123,150,178,.15); color: var(--muted); }

/* ── Upload / Sync panel ──────────────────────────────────────────────────── */
.action-bar { display: flex; flex-wrap: wrap; gap: 0.75rem; margin-bottom: 1.5rem; align-items: center; }

.btn {
  padding: 0.5rem 1.1rem; border-radius: 7px; border: none; font-size: 0.9rem;
  font-weight: 600; cursor: pointer; display: inline-flex; align-items: center; gap: 0.45rem;
}
.btn-primary  { background: var(--teal);  color: #fff; }
.btn-primary:hover  { background: var(--teal2); }
.btn-success  { background: var(--green); color: #0d1b2a; }
.btn-success:hover  { background: #6dd4a2; }
.btn-outline  { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-outline:hover  { border-color: var(--teal); color: var(--teal2); }

#upload-status { font-size: 0.85rem; color: var(--muted); }

/* ── Modal ────────────────────────────────────────────────────────────────── */
.modal-backdrop {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,.6);
  z-index: 100; align-items: center; justify-content: center;
}
.modal-backdrop.open { display: flex; }
.modal {
  background: var(--navy2); border: 1px solid var(--border); border-radius: 12px;
  padding: 2rem; max-width: 480px; width: 90%; max-height: 90vh; overflow-y: auto;
}
.modal h2 { margin-bottom: 1rem; color: var(--white); }
.modal-close { float: right; background: none; border: none; color: var(--muted); font-size: 1.3rem; cursor: pointer; }

/* ── Responsive ───────────────────────────────────────────────────────────── */
@media (max-width: 720px) {
  .charts { grid-template-columns: 1fr; }
  .tiles  { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); }
  main.content { padding: 1rem; }
}
