:root {
  --bg: #0b1220;
  --bg-raised: #121b2e;
  --bg-hover: #17233c;
  --line: #22304f;
  --text: #e8eef9;
  --text-dim: #93a3c0;
  --accent: #3a8dff;
  --accent-strong: #6fb1ff;
  --warn: #ffb454;
  --bad: #ff6b6b;
  --good: #4cd69c;
  --radius: 14px;
  --shadow: 0 10px 30px rgb(0 0 0 / 0.35);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: radial-gradient(1200px 500px at 20% -10%, #14213a 0%, var(--bg) 55%);
  color: var(--text);
  min-height: 100vh;
  padding-bottom: 40px;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 18px 20px 10px;
  position: sticky;
  top: 0;
  background: rgb(11 18 32 / 0.85);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
  z-index: 20;
}

.brand { display: flex; align-items: center; gap: 12px; }

.brand-mark {
  width: 40px; height: 40px; border-radius: 11px;
  background: linear-gradient(135deg, var(--accent), #7f5dff);
  box-shadow: 0 0 18px rgb(58 141 255 / 0.5);
  position: relative;
  flex: none;
}
.brand-mark::after {
  content: "";
  position: absolute; inset: 9px;
  border-radius: 50%;
  border: 3px solid rgb(255 255 255 / 0.9);
  background: radial-gradient(circle at 35% 35%, rgb(255 255 255 / 0.85) 0 8%, transparent 60%);
}

h1 { font-size: 18px; margin: 0; letter-spacing: 0.3px; }
.subtitle { margin: 2px 0 0; font-size: 13px; color: var(--text-dim); }

.actions { display: flex; gap: 10px; align-items: center; }

button {
  border: 1px solid var(--line);
  background: var(--bg-raised);
  color: var(--text);
  font-size: 14px;
  padding: 9px 14px;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.15s, transform 0.05s;
}
button:hover { background: var(--bg-hover); }
button:active { transform: translateY(1px); }
button.primary {
  background: linear-gradient(135deg, var(--accent), #6a5cff);
  border-color: transparent;
  font-weight: 600;
  box-shadow: 0 6px 18px rgb(58 141 255 / 0.35);
  color: #fff;
}
button.primary:hover { filter: brightness(1.08); }
button:disabled { opacity: 0.5; cursor: default; }
button.ghost {
  background: transparent;
  border: 1px solid var(--secondary);
  color: var(--text);
}
button.small { padding: 6px 10px; font-size: 12px; }

main { max-width: 720px; margin: 0 auto; padding: 16px; }

.spinner {
  display: inline-block; width: 14px; height: 14px; margin-right: 6px;
  border: 2px solid rgb(255 255 255 / 0.35); border-top-color: #fff;
  border-radius: 50%; animation: spin 0.8s linear infinite;
  vertical-align: -2px;
}
@keyframes spin { to { transform: rotate(360deg); } }

[hidden] { display: none !important; }

.kpis {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px;
  margin: 16px 0;
}
.kpi {
  background: var(--bg-raised); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px 10px; text-align: center;
}
.kpi-value { display: block; font-size: 22px; font-weight: 700; }
.kpi-label { display: block; font-size: 11px; color: var(--text-dim); margin-top: 4px; }

.toolbar {
  display: flex; flex-wrap: wrap; gap: 12px; align-items: center;
  margin: 6px 0 14px;
}
.segmented {
  display: inline-flex; background: var(--bg-raised);
  border: 1px solid var(--line); border-radius: 10px; overflow: hidden;
}
.seg {
  background: transparent; border: none; border-radius: 0;
  font-size: 13px; padding: 8px 12px; color: var(--text-dim);
}
.seg.active { background: var(--accent); color: #fff; font-weight: 600; }

.chart-wrap {
  background: var(--bg-raised); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px 10px 8px; margin-bottom: 18px;
}
#chart { width: 100%; display: block; }

.night-card {
  background: var(--bg-raised); border: 1px solid var(--line);
  border-radius: var(--radius); margin: 12px 0; overflow: hidden;
  box-shadow: var(--shadow);
}
.night-head {
  display: flex; justify-content: space-between; align-items: baseline;
  padding: 12px 16px 6px; flex-wrap: wrap; gap: 6px;
}
.night-date { font-size: 15px; font-weight: 650; }
.night-date small { color: var(--text-dim); font-weight: 400; margin-left: 8px; }
.night-score { font-size: 12px; color: var(--text-dim); }

.night-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 0;
  padding: 4px 8px 10px;
}
.night-cell { padding: 8px 8px; }
.night-cell .k { font-size: 11px; color: var(--text-dim); }
.night-cell .v { font-size: 16px; font-weight: 600; margin-top: 2px; }
.night-cell .u { font-size: 11px; color: var(--text-dim); }

.detail {
  background: var(--bg-raised); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px 16px; margin: 12px 0;
}
.detail h3 { margin: 10px 0 8px; font-size: 14px; color: var(--text-dim); }
.detail table { width: 100%; border-collapse: collapse; font-size: 13px; }
.detail td { padding: 5px 6px; border-bottom: 1px solid var(--line); }
.detail td:first-child { color: var(--text-dim); }

.empty { text-align: center; color: var(--text-dim); padding: 34px 12px; }
.empty p { margin: 8px 0; }

.logs {
  margin-top: 24px; border: 1px solid var(--line); border-radius: var(--radius);
  background: #070b14;
}
.logs-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 14px; border-bottom: 1px solid var(--line);
}
.logs-head h2 { font-size: 13px; color: var(--text-dim); margin: 0; }

#log-output {
  margin: 0; padding: 12px 14px; font-size: 11.5px; line-height: 1.55;
  color: #9db4d8; overflow-x: auto; max-height: 260px;
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  white-space: pre-wrap; word-break: break-word;
}

.modal {
  position: fixed; inset: 0; background: rgb(0 0 0 / 0.6);
  display: flex; align-items: center; justify-content: center; z-index: 50;
  padding: 20px;
}
.modal-box {
  background: var(--bg-raised); border: 1px solid var(--line);
  border-radius: 16px; padding: 22px; width: 100%; max-width: 320px;
  box-shadow: var(--shadow);
}
.modal-box h2 { margin: 0 0 8px; font-size: 17px; }
.modal-box p { margin: 0 0 14px; font-size: 13px; color: var(--text-dim); }
.modal-error { color: var(--bad); font-size: 13px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; }
#passkey {
  width: 130px; text-align: center; font-size: 26px; letter-spacing: 8px;
  padding: 10px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--text);
}

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #1a2744; border: 1px solid var(--line); color: var(--text);
  padding: 12px 18px; border-radius: 12px; box-shadow: var(--shadow);
  font-size: 13px; z-index: 60; max-width: min(92vw, 480px);
}
.toast.error { border-color: var(--bad); background: #3a1820; }

.night-score.good { color: var(--good); }
.night-score.warn { color: var(--warn); }
.night-score.bad { color: var(--bad); }

@media (max-width: 460px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .app-header { flex-wrap: wrap; }
  .actions { width: 100%; justify-content: space-between; }
}