:root {
  --bg: #F5F5F7;
  --surface: #FFFFFF;
  --surface-2: #EFEFF3;
  --text: #1D1D1F;
  --text-2: #5E5E66;
  --text-3: #8E8E96;
  --line: #E2E2E7;
  --line-strong: #CFCFD6;
  --accent: #0062CC;
  --accent-text: #FFFFFF;
  --now: #E5600B;
  --warn: #B4470A;
  --warn-bg: #FCEEE4;
  --danger: #C4252C;
  --ok: #138A8A;
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.14);
  --radius: 12px;
  --hour: 56px;
  --tab-h: 64px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  --font-num: ui-rounded, "SF Pro Rounded", -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1C1C1E;
    --surface: #2A2A2D;
    --surface-2: #333337;
    --text: #F2F2F5;
    --text-2: #B4B4BC;
    --text-3: #8A8A93;
    --line: #38383D;
    --line-strong: #4A4A50;
    --accent: #3D8BFF;
    --warn: #F0A06A;
    --warn-bg: #3A2A20;
    --danger: #FF6B6B;
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
.num { font-family: var(--font-num); font-variant-numeric: tabular-nums; }
.muted { color: var(--text-2); }
.faint { color: var(--text-3); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

#app { display: flex; flex-direction: column; height: 100%; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.6) blur(18px);
  -webkit-backdrop-filter: saturate(1.6) blur(18px);
  border-bottom: 1px solid var(--line);
}
.topbar-row { display: flex; align-items: center; gap: 8px; }
.topbar h1 { font-size: 22px; font-weight: 700; margin: 0; letter-spacing: -0.01em; flex: 1; }
.topbar .sub { font-size: 13px; color: var(--text-2); margin-top: 1px; }
.icon-btn {
  width: 36px; height: 36px; display: inline-grid; place-items: center;
  border: none; background: var(--surface-2); border-radius: 18px; color: var(--text);
}
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.pill-btn {
  height: 32px; padding: 0 12px; border: none; border-radius: 16px; background: var(--surface-2); font-weight: 600; font-size: 14px;
}
.pill-btn:disabled { opacity: 0.45; }

main { flex: 1; overflow-y: auto; padding: 12px 16px calc(var(--tab-h) + env(safe-area-inset-bottom) + 24px); -webkit-overflow-scrolling: touch; }

/* Tab bar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(4, 1fr);
  padding: 6px 8px calc(env(safe-area-inset-bottom) + 6px);
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: saturate(1.6) blur(18px);
  -webkit-backdrop-filter: saturate(1.6) blur(18px);
  border-top: 1px solid var(--line);
}
.tabbar button {
  border: none; background: none; display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-size: 11px; font-weight: 600; color: var(--text-3); padding: 4px 0; min-height: 48px;
}
.tabbar button svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabbar button[aria-current="page"] { color: var(--accent); }

/* Load bar */
.load { display: flex; align-items: baseline; gap: 6px; margin: 4px 0 10px; }
.load strong { font-size: 20px; font-weight: 600; }
.load .over { color: var(--warn); font-weight: 600; margin-left: auto; font-size: 13px; }
.meter { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; margin-bottom: 12px; }
.meter > div { height: 100%; border-radius: 3px; background: var(--text); }
.meter.over > div { background: var(--warn); }

.banner {
  display: flex; gap: 10px; align-items: center; padding: 10px 12px; border-radius: var(--radius);
  background: var(--warn-bg); color: var(--text); margin-bottom: 12px; font-size: 14px;
}
.banner button { margin-left: auto; flex-shrink: 0; height: 32px; padding: 0 12px; border-radius: 16px; border: none; background: var(--warn); color: #fff; font-weight: 600; }

/* Timeline */
.timeline { position: relative; margin-left: 46px; border-left: 1px solid var(--line); touch-action: pan-y; }
.hour { position: absolute; left: -46px; right: 0; height: 0; border-top: 1px solid var(--line); }
.hour span { position: absolute; left: 0; width: 40px; text-align: right; top: -8px; font-size: 11px; color: var(--text-3); font-family: var(--font-num); }
.half { position: absolute; left: 0; right: 0; border-top: 1px dashed color-mix(in srgb, var(--line) 70%, transparent); }
.now-line { position: absolute; left: -6px; right: 0; height: 2px; background: var(--now); z-index: 5; pointer-events: none; }
.now-line::before { content: ""; position: absolute; left: -4px; top: -4px; width: 10px; height: 10px; border-radius: 5px; background: var(--now); }

.block {
  position: absolute; left: 6px; right: 0; border-radius: 9px; overflow: hidden;
  background: var(--surface); border: 1px solid var(--line-strong);
  padding: 5px 8px 5px 12px; text-align: left; display: block; width: calc(100% - 6px);
  font-size: 13px; line-height: 1.25; color: var(--text);
}
.block::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: var(--c, #4F5BD5); }
.block.work { background: color-mix(in srgb, var(--c) 15%, var(--surface)); border-color: color-mix(in srgb, var(--c) 35%, transparent); }
.block.fixed { background: repeating-linear-gradient(135deg, var(--surface-2) 0 6px, color-mix(in srgb, var(--surface-2) 80%, var(--line-strong)) 6px 12px); }
.block.fixed::before, .block.rest::before { display: none; }
.block.rest { background: transparent; border-style: dashed; }
.block.past { opacity: 0.55; }
.block.dragging { box-shadow: var(--shadow); z-index: 10; opacity: 0.92; }
.block .t { font-weight: 600; display: flex; gap: 6px; align-items: center; position: relative; z-index: 1; }
.block .t span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.block .time { margin-left: auto; font-weight: 500; color: var(--text-2); font-size: 11px; flex-shrink: 0; }
.block .s { color: var(--text-2); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; position: relative; z-index: 1; }
.band { position: absolute; left: 4px; right: 0; background: repeating-linear-gradient(135deg, color-mix(in srgb, var(--surface) 80%, transparent) 0 4px, color-mix(in srgb, var(--c) 30%, transparent) 4px 6px); }
.done-mark { color: var(--c); }

.ghost { position: absolute; left: 6px; right: 0; border: 1.5px dashed var(--accent); border-radius: 9px; background: color-mix(in srgb, var(--accent) 10%, transparent); pointer-events: none; }

/* Cards & lists */
.section-title { font-size: 13px; font-weight: 600; color: var(--text-2); margin: 18px 4px 8px; }
.card { background: var(--surface); border-radius: var(--radius); border: 1px solid var(--line); }
.list { list-style: none; margin: 0; padding: 0; }
.row {
  display: flex; align-items: center; gap: 10px; padding: 11px 12px; border-bottom: 1px solid var(--line);
  background: none; border-left: none; border-right: none; border-top: none; width: 100%; text-align: left;
}
.list > li:last-child .row, .list > .row:last-child { border-bottom: none; }
.dot { width: 9px; height: 9px; border-radius: 5px; flex-shrink: 0; background: var(--c, #8E8E93); }
.row .main { flex: 1; min-width: 0; }
.row .title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .meta { font-size: 12px; color: var(--text-2); }
.row .end { font-size: 13px; color: var(--text-2); flex-shrink: 0; }
.check {
  width: 24px; height: 24px; border-radius: 12px; border: 2px solid var(--line-strong); background: none; flex-shrink: 0;
  display: grid; place-items: center; padding: 0;
}
.check.on { background: var(--c, var(--accent)); border-color: var(--c, var(--accent)); color: #fff; }
.check svg { width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 3; }
.done .title { text-decoration: line-through; color: var(--text-3); }

.quick-add { display: flex; gap: 8px; margin-bottom: 12px; }
.quick-add input { flex: 1; }

input[type="text"], input[type="time"], input[type="date"], input[type="number"], input[type="password"], select, textarea {
  height: 44px; padding: 0 12px; border-radius: 10px; border: 1px solid var(--line-strong); background: var(--surface); width: 100%;
}
textarea { height: auto; padding: 10px 12px; min-height: 70px; }
.btn {
  height: 44px; padding: 0 16px; border-radius: 12px; border: none; background: var(--surface-2); font-weight: 600; flex-shrink: 0;
}
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; vertical-align: middle; }
.btn.danger { background: none; color: var(--danger); }
.btn:disabled { opacity: 0.5; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }
.btn-row .btn { flex: 1; }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); border-radius: 10px; padding: 2px; }
.seg button { border: none; background: none; height: 34px; border-radius: 8px; font-weight: 600; font-size: 14px; color: var(--text-2); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12); }
.field { display: flex; flex-direction: column; gap: 6px; margin-top: 12px; }
.field > span { font-size: 13px; font-weight: 600; color: var(--text-2); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* Week */
.day-card { padding: 12px; margin-bottom: 10px; }
.day-card header { display: flex; align-items: baseline; gap: 8px; margin-bottom: 6px; }
.day-card h2 { font-size: 16px; margin: 0; }
.day-card .today-tag { font-size: 11px; font-weight: 700; color: #fff; background: var(--now); padding: 1px 6px; border-radius: 8px; }
.day-card .load-tag { margin-left: auto; font-size: 12px; color: var(--text-2); }
.day-card .load-tag.over { color: var(--warn); font-weight: 600; }
.mini { display: flex; gap: 8px; align-items: center; font-size: 13px; padding: 3px 0; }
.mini .time { width: 84px; color: var(--text-2); flex-shrink: 0; }
.mini .bar { width: 3px; align-self: stretch; border-radius: 2px; background: var(--c); }
.mini .fixed-bar { background: var(--text-3); }

/* Sheet */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.35); z-index: 50; animation: fade 0.18s ease-out; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 51; max-height: 88vh; overflow-y: auto;
  background: var(--bg); border-radius: 18px 18px 0 0; padding: 8px 16px calc(env(safe-area-inset-bottom) + 18px);
  box-shadow: var(--shadow); animation: rise 0.22s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.sheet .grabber { width: 36px; height: 5px; border-radius: 3px; background: var(--line-strong); margin: 0 auto 10px; }
.sheet h2 { margin: 0 0 2px; font-size: 19px; }
@media (min-width: 700px) {
  .sheet { left: 50%; width: 520px; transform: translateX(-50%); bottom: 24px; border-radius: 18px; animation: none; }
  main, .topbar-row, .topbar .sub { max-width: 720px; margin-left: auto; margin-right: auto; }
}
@keyframes rise { from { transform: translateY(30px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .sheet, .sheet-backdrop { animation: none; } }

/* Toast */
#toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 14px);
  z-index: 60; display: none; align-items: center; gap: 12px; max-width: calc(100% - 32px);
  background: #1D1D1F; color: #fff; padding: 10px 14px; border-radius: 14px; font-size: 14px; box-shadow: var(--shadow);
}
#toast.show { display: flex; }
#toast.error { background: var(--danger); }
#toast button { border: none; background: rgba(255, 255, 255, 0.18); color: #fff; height: 30px; padding: 0 12px; border-radius: 15px; font-weight: 600; }

/* Login */
.login { max-width: 380px; margin: 12vh auto 0; text-align: center; }
.login img { width: 72px; height: 72px; border-radius: 16px; }
.login h1 { font-size: 26px; margin: 12px 0 4px; }
.login form { display: flex; flex-direction: column; gap: 10px; margin-top: 18px; text-align: left; }
.empty { text-align: center; color: var(--text-2); padding: 28px 12px; }
.option { width: 100%; text-align: left; border: 1px solid var(--line-strong); background: var(--surface); border-radius: 12px; padding: 12px; margin-top: 8px; }
.option strong { display: block; margin-bottom: 4px; }
.option ul { margin: 0; padding-left: 18px; font-size: 13px; color: var(--text-2); }
.link-box { word-break: break-all; font-size: 13px; padding: 10px 12px; background: var(--surface-2); border-radius: 10px; margin-top: 8px; }
