:root {
  --bg: #f6f5fb;
  --surface: #ffffff;
  --surface-2: #efeef7;
  --text: #1d1b29;
  --muted: #6b6880;
  --line: #e1dfee;
  --accent: #6c63ff;
  --accent-ink: #ffffff;
  --accent-soft: #e7e5ff;
  --danger: #d64545;
  --warn: #c2410c;
  --done: #9a97ad;
  --shadow: 0 6px 24px rgba(30, 20, 80, 0.12);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14131b;
    --surface: #1e1c28;
    --surface-2: #272533;
    --text: #ecebf5;
    --muted: #a4a1b8;
    --line: #34313f;
    --accent: #8b84ff;
    --accent-ink: #14131b;
    --accent-soft: #2c2950;
    --danger: #ff7b7b;
    --warn: #fb923c;
    --done: #6f6c80;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  min-height: 100dvh;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
input, select, textarea { font: inherit; color: inherit; }

/* ---------- PIN ---------- */
.pin-screen {
  min-height: 100dvh;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 24px 16px;
  gap: 8px;
}
.pin-logo { border-radius: 18px; box-shadow: var(--shadow); }
.pin-screen h1 { margin: 12px 0 0; font-size: 28px; }
.pin-prompt { margin: 0 0 12px; color: var(--muted); min-height: 1.4em; text-align: center; }
.pin-prompt.error { color: var(--danger); }
.pin-dots { display: flex; gap: 16px; margin-bottom: 24px; }
.pin-dots span {
  width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid var(--accent);
  transition: background 0.1s;
}
.pin-dots span.on { background: var(--accent); }
.pin-dots.shake { animation: shake 0.35s; }
@keyframes shake {
  20%, 60% { transform: translateX(-10px); }
  40%, 80% { transform: translateX(10px); }
}
.pin-pad {
  display: grid; grid-template-columns: repeat(3, 76px); gap: 14px;
}
.pin-pad button {
  height: 76px; border-radius: 50%;
  border: none; background: var(--surface);
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
  font-size: 28px; font-weight: 500;
}
.pin-pad button:active { background: var(--accent-soft); }
.pin-pad button[data-action="back"] { background: transparent; box-shadow: none; font-size: 24px; }
.pin-pad.busy { opacity: 0.5; pointer-events: none; }

/* ---------- Shell ---------- */
.app { max-width: 1100px; margin: 0 auto; padding: 0 16px 96px; }
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 12px;
  padding: 12px 0;
  background: var(--bg);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 20px; }
.brand img { border-radius: 7px; }
.tabs {
  margin-left: auto;
  display: flex; background: var(--surface-2); border-radius: 999px; padding: 3px;
}
.tabs button {
  border: none; background: transparent; padding: 6px 14px; border-radius: 999px; font-weight: 600;
  color: var(--muted);
}
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
.icon-btn {
  border: none; background: transparent; width: 40px; height: 40px; border-radius: 50%;
  font-size: 20px; display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: var(--surface-2); }
.banner {
  background: var(--accent-soft); color: var(--text);
  padding: 10px 14px; border-radius: 10px; margin-bottom: 12px; font-size: 14px;
}
.fab {
  position: fixed; right: max(20px, calc(50vw - 530px)); bottom: calc(24px + env(safe-area-inset-bottom));
  width: 60px; height: 60px; border-radius: 50%;
  border: none; background: var(--accent); color: var(--accent-ink);
  font-size: 34px; line-height: 1; box-shadow: var(--shadow);
}

/* ---------- Task list ---------- */
.group { margin-top: 18px; }
.group h3 {
  margin: 0 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted);
  display: flex; align-items: center; gap: 8px;
}
.group h3 .count { font-weight: 500; }
.group.overdue h3 { color: var(--warn); }
.group h3 button { border: none; background: none; color: var(--accent); font-size: 13px; padding: 0; text-transform: none; letter-spacing: 0; }
.task-list { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: var(--radius); overflow: hidden; }
.task {
  display: flex; align-items: flex-start; gap: 12px;
  padding: 12px 14px; border-top: 1px solid var(--line);
}
.task:first-child { border-top: none; }
.check {
  flex: none; width: 26px; height: 26px; margin-top: 1px; border-radius: 50%;
  border: 2px solid var(--accent); background: transparent; padding: 0;
  display: inline-flex; align-items: center; justify-content: center; color: var(--accent-ink); font-size: 15px;
}
.check:hover { background: var(--accent-soft); }
.task.done .check { background: var(--accent); }
.task-body { flex: 1; min-width: 0; border: none; background: none; text-align: left; padding: 0; }
.task-title { display: block; font-weight: 500; overflow-wrap: anywhere; }
.task.done .task-title { color: var(--done); text-decoration: line-through; }
.task-meta { display: flex; flex-wrap: wrap; gap: 6px 10px; margin-top: 3px; font-size: 13px; color: var(--muted); }
.task-meta .late { color: var(--warn); font-weight: 600; }
.chip { display: inline-flex; align-items: center; gap: 5px; }
.chip i { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.canvas-bar {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  font-size: 13px; color: var(--muted); margin-top: 4px;
}
.canvas-bar.error { color: var(--danger); }
.canvas-bar .link { font-size: 13px; }
.editor-link { align-self: flex-start; text-decoration: none; font-weight: 600; }
.empty { text-align: center; color: var(--muted); margin-top: 48px; }

/* ---------- Calendar ---------- */
.cal-toolbar { display: flex; align-items: center; gap: 6px; margin: 4px 0 10px; }
.cal-toolbar h2 { margin: 0 0 0 8px; font-size: 18px; }
.pill { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 6px 14px; font-weight: 600; }
.cal-days, .cal-allday, .cal-grid {
  display: grid; grid-template-columns: 52px repeat(var(--cols, 7), minmax(0, 1fr));
}
.cal-days > div { text-align: center; padding: 6px 2px; font-size: 13px; color: var(--muted); }
.cal-days button {
  border: none; background: none; width: 100%; padding: 4px 0; border-radius: 10px; color: var(--muted);
}
.cal-days b { display: block; font-size: 20px; color: var(--text); }
.cal-days .today b { color: var(--accent); }
.cal-days .sel button { background: var(--accent-soft); }
.cal-allday { border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); min-height: 8px; }
.cal-allday > div { padding: 3px; display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.cal-allday .label { font-size: 11px; color: var(--muted); justify-content: center; }
.cal-scroll { height: calc(100dvh - 250px); min-height: 360px; overflow-y: auto; position: relative; }
.cal-grid { position: relative; }
.cal-hour { font-size: 11px; color: var(--muted); text-align: right; padding-right: 6px; transform: translateY(-7px); height: 48px; }
.cal-col { position: relative; border-left: 1px solid var(--line); }
.cal-slot { height: 24px; border-top: 1px solid var(--line); }
.cal-slot:nth-child(even) { border-top-style: dotted; border-top-color: color-mix(in srgb, var(--line) 60%, transparent); }
.cal-slot:hover { background: var(--accent-soft); cursor: pointer; }
.cal-now { position: absolute; left: 0; right: 0; height: 2px; background: var(--danger); z-index: 2; pointer-events: none; }
.event {
  position: absolute; left: 3px; right: 3px; z-index: 1;
  border: none; border-left: 4px solid var(--c, var(--accent)); border-radius: 6px;
  background: color-mix(in srgb, var(--c, var(--accent)) 22%, var(--surface));
  color: var(--text); text-align: left; padding: 2px 6px; font-size: 12px; overflow: hidden;
}
.event b { display: block; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.event.done { opacity: 0.5; }
.event.done b { text-decoration: line-through; }
.due-chip {
  border: none; border-radius: 6px; padding: 2px 6px; font-size: 12px; text-align: left;
  background: var(--surface-2); color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; width: 100%;
}
.due-chip.done { text-decoration: line-through; color: var(--done); }

/* ---------- Editor ---------- */
.editor {
  border: none; border-radius: 18px; padding: 0; width: min(520px, calc(100vw - 24px));
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
.editor::backdrop { background: rgba(10, 8, 30, 0.45); }
.editor form { padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.editor h2 { margin: 0; font-size: 20px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span, .field-group legend { font-size: 13px; font-weight: 600; color: var(--muted); }
.field-group { border: none; padding: 0; margin: 0; }
.field-group legend { padding: 0; margin-bottom: 6px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.editor input, .editor select, .editor textarea {
  border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 9px 10px; min-width: 0;
}
.editor input:focus, .editor select:focus, .editor textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.editor textarea { resize: vertical; }
.row input[type="date"] { flex: 1 1 140px; }
.row input[type="time"] { flex: 1 1 110px; }
.row select { flex: 1 1 100px; }
.link { border: none; background: none; color: var(--accent); padding: 4px; font-size: 14px; }
.editor-actions { display: flex; gap: 8px; align-items: center; margin-top: 4px; }
.editor-actions .spacer { flex: 1; }
.editor-actions button { border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 9px 16px; font-weight: 600; }
.editor-actions .primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.editor-actions .danger { color: var(--danger); border-color: transparent; background: transparent; padding-left: 0; }
.form-error { color: var(--danger); margin: 0; font-size: 14px; }

@media (max-width: 560px) {
  .brand { font-size: 0; }
  .brand img { width: 30px; height: 30px; }
  .tabs { margin-left: 0; }
  .topbar { justify-content: space-between; }
}
