:root {
  --bg: #000000;
  --bg-2: #0c0c0c;
  --surface: #141414;
  --surface-2: #1c1c1c;
  --row: #161616;
  --line: #2a2a2a;
  --ink: #f4f4f4;
  --ink-dim: #a3a3a3;
  --ink-faint: #6b6b6b;
  --accent: #ff4a1c;
  --accent-2: #ff7a4d;
  --today: #ff4a1c;
  --upcoming: #5ac8fa;
  --waiting: #bf5af2;
  --logbook: #30d158;
  --danger: #ff453a;
  --flag: #ff4a1c;
  --p1: #ff3b30;
  --p2: #ffd60a;
  --p3: #0a84ff;
  --p4: #30d158;
  --radius: 16px;
  --tap: 48px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --display: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
html { background: var(--bg); color: var(--ink); }
body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input, textarea, select { font-family: inherit; color: inherit; }
button { cursor: pointer; }
img { display: block; }
a { color: var(--accent); }

.hidden { display: none !important; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

/* ---------- Gate ---------- */
#gate {
  position: fixed; inset: 0; z-index: 80;
  display: grid; place-items: center;
  padding: calc(24px + var(--safe-t)) 24px calc(24px + var(--safe-b));
  background:
    radial-gradient(900px 500px at 50% -10%, color-mix(in srgb, var(--accent) 22%, transparent), transparent 60%),
    var(--bg);
}
.gate-card {
  width: 100%; max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 36px 28px 28px;
  text-align: center;
}
.gate-icon {
  width: 84px; height: 84px; border-radius: 22px;
  margin: 0 auto 18px;
  box-shadow: 0 12px 32px rgba(255, 74, 28, 0.28);
}
.gate-h { font-family: var(--display); font-size: 22px; font-weight: 700; margin: 0 0 4px; letter-spacing: -0.02em; }
.gate-sub { margin: 0 0 22px; color: var(--ink-dim); font-size: 14px; line-height: 1.45; }
.gate-card label {
  display: block; text-align: left; font-size: 11px; letter-spacing: 0.14em;
  text-transform: uppercase; color: var(--ink-faint); margin-bottom: 8px; font-weight: 600;
}
.gate-card input {
  width: 100%; background: var(--bg); border: 1px solid var(--line); color: var(--ink);
  border-radius: 12px; padding: 14px 14px; font-size: 16px;
}
.gate-card input:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 22%, transparent);
}
.gate-err { min-height: 18px; margin: 10px 0 0; color: var(--danger); font-size: 13px; text-align: left; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid transparent; border-radius: 12px;
  min-height: var(--tap); padding: 12px 16px;
  font-size: 16px; font-weight: 650;
  transition: transform 0.08s ease, background 0.15s ease, opacity 0.15s ease;
}
.btn:active { transform: scale(0.98); }
.btn[disabled] { opacity: 0.5; pointer-events: none; }
.btn-primary { background: var(--accent); color: #fff; width: 100%; margin-top: 14px; }
.btn-primary:hover { background: var(--accent-2); }
.btn-ghost { background: transparent; border-color: var(--line); color: var(--ink-dim); }
.btn-danger { background: color-mix(in srgb, var(--danger) 16%, transparent); color: var(--danger); width: 100%; }

/* ---------- App shell ---------- */
#app {
  min-height: 100%;
  display: grid;
  grid-template-columns: 240px 1fr;
  padding-top: var(--safe-t);
  padding-bottom: calc(var(--safe-b) + 64px);
}
@media (min-width: 860px) {
  #app { padding-bottom: var(--safe-b); min-height: 100dvh; height: 100dvh; }
}

.sidebar {
  display: none;
  border-right: 1px solid var(--line);
  background: var(--bg-2);
  padding: 18px 12px 24px;
  overflow-y: auto;
}
.side-brand {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 10px 18px;
}
.side-brand img { width: 32px; height: 32px; border-radius: 8px; }
.side-brand .name { font-weight: 700; letter-spacing: -0.02em; }
.side-brand .sub { font-size: 11px; color: var(--ink-faint); }
.nav-btn {
  width: 100%; display: flex; align-items: center; gap: 10px;
  background: transparent; border: 0; color: var(--ink);
  border-radius: 12px; padding: 11px 12px; min-height: 44px;
  font-size: 16px; font-weight: 600; text-align: left;
}
.nav-btn .count {
  margin-left: auto; font-size: 12px; color: var(--ink-faint); font-weight: 600;
  min-width: 20px; text-align: right;
}
.nav-btn.active { background: var(--surface-2); }
.nav-btn .dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.nav-btn[data-when="today"] .dot { background: var(--today); }
.nav-btn[data-when="upcoming"] .dot { background: var(--upcoming); }
.nav-btn[data-when="waiting"] .dot { background: var(--waiting); }
.nav-btn[data-when="logbook"] .dot { background: var(--logbook); }
.side-label {
  margin: 18px 12px 8px; font-size: 11px; letter-spacing: 0.14em;
  text-transform: uppercase; color: var(--ink-faint); font-weight: 700;
}
.proj-btn {
  width: 100%; display: flex; align-items: center; gap: 10px;
  background: transparent; border: 0; color: var(--ink-dim);
  border-radius: 12px; padding: 10px 12px; min-height: 42px;
  font-size: 15px; font-weight: 550; text-align: left;
}
.proj-btn.active { background: var(--surface-2); color: var(--ink); }
.proj-btn .swatch { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); flex: none; }
.proj-btn .count { margin-left: auto; font-size: 12px; color: var(--ink-faint); }

.main {
  min-width: 0;
  display: flex; flex-direction: column;
  max-width: 720px; width: 100%;
  margin: 0 auto;
}
.top {
  position: sticky; top: 0; z-index: 10;
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  padding: 14px 18px 8px;
  display: flex; align-items: flex-end; gap: 12px;
}
.top h1 {
  font-family: var(--display);
  font-size: 32px; font-weight: 800; letter-spacing: -0.035em;
  margin: 0; line-height: 1.05;
}
.top .date { color: var(--ink-dim); font-size: 14px; font-weight: 600; margin: 0 0 4px auto; }
.sync {
  font-size: 11px; color: var(--ink-faint); font-weight: 600;
  display: inline-flex; align-items: center; gap: 6px;
}
.sync.on { color: #30d158; }
.sync.off { color: #ffd60a; }

.quick {
  padding: 8px 16px 4px;
}
.quick input {
  width: 100%; background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; padding: 14px 16px; font-size: 16px; min-height: var(--tap);
}
.quick input:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 18%, transparent);
}

.list { padding: 6px 10px 110px; }
.empty {
  margin: 28px 12px; padding: 28px 16px; text-align: center;
  color: var(--ink-faint); border: 1.5px dashed var(--line); border-radius: 16px;
  font-size: 14px; line-height: 1.5;
}

.item {
  display: grid;
  grid-template-columns: 44px 1fr;
  gap: 2px 8px;
  align-items: start;
  padding: 10px 8px;
  border-radius: 14px;
  min-height: 56px;
}
.item:active { background: var(--surface); }
.item + .item { border-top: 1px solid color-mix(in srgb, var(--line) 70%, transparent); }

.check {
  width: 44px; height: 44px; border: 0; background: transparent;
  display: grid; place-items: center; padding: 0; margin-top: 1px;
}
.check i {
  width: 24px; height: 24px; border-radius: 50%;
  border: 2px solid #5a5a5a;
  display: block;
  transition: background 0.12s ease, border-color 0.12s ease, transform 0.12s ease;
}
.item.done .check i,
.item[data-when="logbook"] .check i {
  background: var(--logbook); border-color: var(--logbook);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 12.5l4 4 8-9'/%3E%3C/svg%3E");
  background-size: 16px; background-repeat: no-repeat; background-position: center;
}
.item.flagged .check i { border-color: var(--accent); }

.body { min-width: 0; padding-top: 8px; }
.title {
  font-size: 17px; font-weight: 600; letter-spacing: -0.01em;
  line-height: 1.25; word-break: break-word;
}
.item.done .title,
.item[data-when="logbook"] .title {
  color: var(--ink-faint); text-decoration: line-through;
}
.meta {
  display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; align-items: center;
}
.pill {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; font-weight: 650; color: var(--ink-dim);
  background: var(--surface-2); border-radius: 999px; padding: 3px 9px;
}
.pill .swatch { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }
.due { font-size: 12px; font-weight: 650; color: var(--ink-faint); }
.due.today { color: var(--accent); }
.due.overdue { color: var(--danger); }
.flag {
  width: 44px; height: 44px; border: 0; background: transparent;
  color: var(--ink-faint); display: grid; place-items: center;
}
.flag.on { color: var(--accent); }
.flag svg { width: 18px; height: 18px; }

/* ---------- FAB + tabbar ---------- */
.fab {
  position: fixed;
  right: calc(18px + var(--safe-r));
  bottom: calc(78px + var(--safe-b));
  width: 58px; height: 58px; border-radius: 50%; border: 0;
  background: var(--accent); color: #fff;
  box-shadow: 0 10px 28px rgba(255, 74, 28, 0.45);
  display: grid; place-items: center; z-index: 20;
}
.fab svg { width: 26px; height: 26px; }
@media (min-width: 860px) {
  .fab { bottom: calc(24px + var(--safe-b)); }
}

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 25;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border-top: 1px solid var(--line);
  padding: 6px 6px calc(6px + var(--safe-b));
}
.tab {
  background: transparent; border: 0; color: var(--ink-faint);
  min-height: 52px; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 3px;
  font-size: 11px; font-weight: 700;
}
.tab.active { color: var(--ink); }
.tab svg { width: 22px; height: 22px; opacity: 0.72; }
.tab.active svg { opacity: 1; }
.tab[data-when="today"].active { color: var(--today); }
.tab[data-when="upcoming"].active { color: var(--upcoming); }
.tab[data-when="waiting"].active { color: var(--waiting); }
.tab[data-when="logbook"].active { color: var(--logbook); }

@media (min-width: 860px) {
  .sidebar { display: block; }
  .tabbar { display: none; }
  #app { padding-left: var(--safe-l); }
}

/* ---------- Editor sheet ---------- */
#sheet {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(0,0,0,0.55);
  display: grid; align-items: end;
}
#sheet.hidden { display: none; }
.sheet-card {
  background: var(--surface);
  border-top: 1px solid var(--line);
  border-radius: 22px 22px 0 0;
  padding: 10px 18px calc(18px + var(--safe-b));
  max-height: min(92dvh, 820px);
  overflow: auto;
}
.grab {
  width: 42px; height: 5px; border-radius: 99px; background: #3a3a3a;
  margin: 4px auto 12px;
}
.sheet-top { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.sheet-top h2 { margin: 0; font-size: 15px; font-weight: 700; color: var(--ink-dim); flex: 1; }
.icon-btn {
  width: 44px; height: 44px; border-radius: 12px; border: 0;
  background: var(--surface-2); color: var(--ink); display: grid; place-items: center;
}
.title-input {
  width: 100%; background: transparent; border: 0; font-size: 22px; font-weight: 750;
  letter-spacing: -0.03em; padding: 8px 0 14px; outline: none;
}
.field { margin: 0 0 14px; }
.field label {
  display: block; font-size: 11px; letter-spacing: 0.12em; text-transform: uppercase;
  color: var(--ink-faint); font-weight: 700; margin-bottom: 8px;
}
.seg {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px;
}
.seg button {
  min-height: 42px; border-radius: 11px; border: 1px solid var(--line);
  background: var(--bg); color: var(--ink-dim); font-weight: 700; font-size: 13px;
}
.seg button.on { color: #fff; border-color: transparent; }
.seg button[data-when="today"].on { background: var(--today); }
.seg button[data-when="upcoming"].on { background: #0a84ff; }
.seg button[data-when="waiting"].on { background: var(--waiting); }
.seg button[data-when="logbook"].on { background: var(--logbook); }

.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.field input[type="text"],
.field input[type="date"],
.field textarea {
  width: 100%; background: var(--bg); border: 1px solid var(--line);
  border-radius: 12px; padding: 12px 12px; font-size: 16px; min-height: 46px;
}
.field textarea { min-height: 96px; resize: vertical; line-height: 1.45; }
.field input:focus, .field textarea:focus {
  outline: none; border-color: var(--accent);
}
.toggle {
  width: 100%; min-height: 46px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg); display: flex; align-items: center; justify-content: space-between;
  padding: 0 14px; font-weight: 650;
}
.toggle.on { border-color: var(--accent); color: var(--accent); }

@media (min-width: 720px) {
  #sheet { place-items: center; }
  .sheet-card {
    width: min(520px, 92vw); border-radius: 22px; max-height: 88dvh;
    padding: 16px 22px 22px;
  }
  .grab { display: none; }
}

.toast {
  position: fixed; left: 50%; bottom: calc(96px + var(--safe-b));
  transform: translateX(-50%);
  background: #222; color: #fff; border-radius: 999px;
  padding: 10px 16px; font-size: 13px; font-weight: 650; z-index: 60;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  pointer-events: none;
}
.toast.hidden { display: none; }

@media (min-width: 860px) {
  .toast { bottom: 28px; }
}

/* Layout: fab/tabbar are position:fixed inside #app; pin them so they
   don't create extra grid tracks on desktop. */
#app > .fab,
#app > .tabbar {
  grid-column: 1 / -1;
  grid-row: 1;
}
.top > div { min-width: 0; }
.top .date { margin: 6px 0 0; }
.top #sync { margin: 8px 0 0 auto; flex: none; }
@media (max-width: 859px) {
  #app { display: block; }
}

.day-group { margin: 6px 4px 18px; }
.day-h {
  display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap;
  margin: 18px 10px 4px;
  font-size: 13px; font-weight: 800; letter-spacing: 0.04em;
  text-transform: uppercase; color: var(--ink);
}
.day-h span { font-weight: 600; letter-spacing: 0; text-transform: none; color: var(--ink-faint); font-size: 12px; }
.day-group .item + .item { border-top: 1px solid color-mix(in srgb, var(--line) 70%, transparent); }
.day-group.overdue .day-h { color: var(--danger); }

.confetti {
  position: fixed; inset: 0; width: 100%; height: 100%;
  pointer-events: none; z-index: 70;
}

.item.p1 .check i { border-color: var(--p1); }
.item.p2 .check i { border-color: var(--p2); }
.item.p3 .check i { border-color: var(--p3); }
.item.p4 .check i { border-color: var(--p4); }
.item.p1.flagged .check i { border-color: var(--p1); }
.item.p2.flagged .check i { border-color: var(--p2); }
.item.p3.flagged .check i { border-color: var(--p3); }
.item.p4.flagged .check i { border-color: var(--p4); }
.pri-seg {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px;
}
.pri-seg button {
  min-height: 44px; border-radius: 11px; border: 2px solid var(--line);
  background: var(--bg); color: var(--ink-dim); font-weight: 800; font-size: 13px;
}
.pri-seg button.p1 { color: var(--p1); }
.pri-seg button.p2 { color: var(--p2); }
.pri-seg button.p4 { color: var(--p4); }
.pri-seg button.p3 { color: var(--p3); }
.pri-seg button.on.p1 { background: var(--p1); color: #fff; border-color: var(--p1); }
.pri-seg button.on.p2 { background: var(--p2); color: #111; border-color: var(--p2); }
.pri-seg button.on.p3 { background: var(--p3); color: #fff; border-color: var(--p3); }
.pri-seg button.on.p4 { background: var(--p4); color: #fff; border-color: var(--p4); }

.list, .day-group { display: flex; flex-direction: column; }
.day-h { order: 0; }
.item.p1 { order: 1; }
.item.p2 { order: 2; }
.item.p3 { order: 3; }
.item.p4 { order: 4; }

.swipe-wrap { position: relative; overflow: hidden; border-radius: 14px; }
.swipe-actions {
  position: absolute; inset: 0; display: flex;
  pointer-events: none;
}
.swipe-resched, .swipe-done {
  flex: 1; display: flex; align-items: center;
  padding: 0 18px; font-size: 13px; font-weight: 800; letter-spacing: 0.04em;
  text-transform: uppercase; color: #fff;
}
.swipe-resched { background: #0a84ff; justify-content: flex-start; }
.swipe-done { background: #30d158; justify-content: flex-end; }
.swipe-wrap .item {
  position: relative; z-index: 1; background: var(--bg);
  touch-action: pan-y; user-select: none;
}
.swipe-wrap .item.dragging, .item.dragging { opacity: 0.4; }
.day-group.drop {
  background: color-mix(in srgb, var(--accent) 10%, transparent);
  outline: 1.5px dashed color-mix(in srgb, var(--accent) 70%, transparent);
  border-radius: 16px;
}
.resched-actions {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 8px 0 16px;
}
.resched-actions button {
  min-height: 48px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg); color: var(--ink); font-weight: 700;
}
#resched {
  position: fixed; inset: 0; z-index: 41;
  background: rgba(0,0,0,0.55);
  display: grid; align-items: end;
}
#resched.hidden { display: none; }

.wait-pill {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 12px; font-weight: 700; color: var(--waiting);
  background: color-mix(in srgb, var(--waiting) 16%, transparent);
  border-radius: 999px; padding: 3px 9px 3px 7px;
}
.wait-pill svg { width: 13px; height: 13px; }
