:root {
  --bg: #f7f6f2;
  --fg: #1f1f1d;
  --muted: #6b6a66;
  --card: #ffffff;
  --line: #e4e2dc;
  --track: #f0eee8;
  --accent: #2f5bea;
  --danger: #c93a3a;
  --dev0: #2f6fe0;
  --dev1: #e07a2f;
  --dev2: #8553e0;
  --dev3: #2f9e6a;
  --today: rgba(47, 91, 234, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15161a;
    --fg: #ececea;
    --muted: #9b9a95;
    --card: #1e2027;
    --line: #2c2f38;
    --track: #262932;
    --accent: #7c9bff;
    --danger: #ff7b7b;
    --dev0: #5b93f5;
    --dev1: #f09552;
    --dev2: #a37cf2;
    --dev3: #4fc08d;
    --today: rgba(124, 155, 255, 0.12);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

body {
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

[hidden] { display: none !important; }

button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); min-height: 1.5em; margin: 0; }
.center { min-height: 60vh; display: flex; align-items: center; justify-content: center; color: var(--muted); }
.link { background: none; border: none; padding: 0; color: var(--accent); text-decoration: underline; }

/* ---------- 登录 ---------- */
.auth { max-width: 360px; margin: 18vh auto 0; padding: 0 16px; display: flex; flex-direction: column; gap: 12px; }
.auth h1 { margin: 0; font-size: 26px; }
.auth p { margin: 0; }
.auth input { font: inherit; font-size: 17px; padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--card); color: var(--fg); }
.primary { padding: 12px 14px; border: none; border-radius: 10px; background: var(--accent); color: #fff; font-size: 17px; font-weight: 600; }

/* ---------- 主界面 ---------- */
main { max-width: 760px; margin: 0 auto; padding: 12px 16px 32px; }

.top h1 { margin: 8px 0 2px; font-size: 24px; }
.top p { margin: 0 0 4px; }

.card {
  margin-top: 16px;
  padding: 16px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
}
.card h2 { margin: 0 0 8px; font-size: 17px; }

.week-bar { display: flex; align-items: center; justify-content: space-between; }
.week-bar h2 { margin: 0; }
.week-bar button { width: 40px; height: 40px; border: 1px solid var(--line); border-radius: 10px; background: transparent; font-size: 22px; line-height: 1; }
.week-bar button:disabled { opacity: 0.3; }

.totals { display: flex; gap: 20px; margin: 12px 0 8px; flex-wrap: wrap; }
.stat { display: flex; flex-direction: column; }
.stat span { font-size: 12px; }
.stat strong { font-size: 24px; font-variant-numeric: tabular-nums; line-height: 1.2; }

.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 13px; margin-bottom: 8px; }
.legend .item { display: flex; align-items: center; gap: 6px; }
.legend .dot, .tag .dot { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

/* ---------- 时间线 ---------- */
.timeline { --label-w: 64px; --total-w: 52px; }

.hours {
  position: relative;
  height: 16px;
  margin-left: var(--label-w);
  margin-right: var(--total-w);
  font-size: 11px;
  color: var(--muted);
}
.hours span { position: absolute; transform: translateX(-50%); }

.day-row {
  display: flex;
  align-items: center;
  gap: 0;
  padding: 3px 0;
  border-radius: 8px;
  cursor: pointer;
}
.day-row:hover { background: var(--track); }
.day-row.selected { background: var(--today); }
.day-row .label { flex: 0 0 var(--label-w); font-size: 13px; padding-left: 6px; }
.day-row .label b { display: block; font-size: 12px; color: var(--muted); font-weight: 500; }
.day-row.is-today .label { color: var(--accent); }
.day-row .total { flex: 0 0 var(--total-w); text-align: right; font-size: 13px; font-variant-numeric: tabular-nums; padding-right: 6px; }

.track {
  flex: 1;
  position: relative;
  min-height: 22px;
  background: var(--track);
  border-radius: 5px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.track .grid { position: absolute; top: 0; bottom: 0; width: 1px; background: var(--line); }
.lane { position: relative; flex: 1; min-height: 9px; }
.lane + .lane { border-top: 1px solid var(--card); }
.block { position: absolute; top: 1px; bottom: 1px; border-radius: 2px; opacity: 0.9; }

/* ---------- 明细 ---------- */
.block-row { padding: 10px 0; border-top: 1px solid var(--line); }
.block-row:first-child { border-top: none; }
.block-head { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.block-head .time { font-variant-numeric: tabular-nums; font-weight: 600; font-size: 16px; }
.block-head .dur { font-variant-numeric: tabular-nums; color: var(--muted); }
.tag { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; color: var(--muted); }
.ongoing { color: var(--dev3); font-size: 12px; }
.tops { margin: 4px 0 0; padding: 0; list-style: none; font-size: 13px; color: var(--muted); }
.tops li { display: flex; gap: 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tops .d { flex: 0 0 48px; font-variant-numeric: tabular-nums; text-align: right; }
.tops .n { overflow: hidden; text-overflow: ellipsis; }

/* ---------- 四周柱状图 ---------- */
.chart { display: flex; align-items: flex-end; gap: 3px; height: 120px; padding-top: 14px; }
.chart .col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; height: 100%; cursor: pointer; min-width: 0; }
.chart .bar { width: 100%; background: var(--dev0); border-radius: 3px 3px 0 0; opacity: 0.55; min-height: 1px; }
.chart .col.selected .bar { opacity: 1; }
.chart .col.is-today .bar { background: var(--accent); }
.chart .col .v { font-size: 10px; color: var(--muted); font-variant-numeric: tabular-nums; margin-bottom: 2px; }
.chart .col .d { font-size: 10px; color: var(--muted); margin-top: 3px; }
.chart .col:not(.labeled) .d { visibility: hidden; }

/* ---------- 设备 ---------- */
.device-row { display: flex; justify-content: space-between; gap: 10px; padding: 6px 0; border-top: 1px solid var(--line); font-size: 14px; }
.device-row:first-child { border-top: none; }
.device-row .stale { color: var(--danger); }

.foot { margin-top: 24px; display: flex; justify-content: space-between; font-size: 14px; }
.foot a { color: var(--accent); }

.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  max-width: calc(100vw - 32px); padding: 12px 16px; border-radius: 12px;
  background: #24262b; color: #fff; font-size: 15px; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25); white-space: nowrap;
}
.toast.error-toast { background: var(--danger); }

@media (max-width: 480px) {
  .timeline { --label-w: 50px; --total-w: 44px; }
  .day-row .label b { display: none; }
  .stat strong { font-size: 20px; }
}
