:root {
    --bg: #0f172a;
    --surface: #1f2937;
    --surface-2: #273345;
    --border: #334155;
    --text: #f1f5f9;
    --muted: #94a3b8;
    --accent: #38bdf8;
    --accent-2: #0ea5e9;
    --danger: #f87171;
    --good: #4ade80;
    --pri-low: #60a5fa;
    --pri-med: #fbbf24;
    --pri-high: #f87171;
    --overdue: #fca5a5;
    --radius: 10px;
    --tap: 44px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
    line-height: 1.4;
    padding-bottom: env(safe-area-inset-bottom);
}

button, input, select, textarea {
    font: inherit;
    color: inherit;
}

button {
    background: var(--accent);
    color: #03131c;
    border: 0;
    border-radius: var(--radius);
    padding: 10px 16px;
    min-height: var(--tap);
    cursor: pointer;
    font-weight: 600;
}
button.ghost {
    background: transparent;
    color: var(--muted);
    border: 1px solid var(--border);
}
button.danger {
    background: transparent;
    color: var(--danger);
    border: 1px solid var(--danger);
}
button:active { transform: scale(0.98); }

input, select, textarea {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px;
    width: 100%;
    min-height: var(--tap);
}
textarea { resize: vertical; min-height: 90px; }
input[type="search"]::-webkit-search-cancel-button { cursor: pointer; }

/* Login */
.login {
    max-width: 360px;
    margin: 10vh auto 0;
    padding: 24px;
}
.login h1 { margin: 0 0 20px; text-align: center; }
.login form { display: grid; gap: 14px; }
.login label { display: grid; gap: 6px; font-size: 14px; color: var(--muted); }
.error {
    background: rgba(248, 113, 113, 0.12);
    color: var(--danger);
    border: 1px solid var(--danger);
    border-radius: var(--radius);
    padding: 10px 12px;
    font-size: 14px;
}

/* App shell */
.topbar {
    position: sticky;
    top: 0;
    z-index: 5;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: calc(8px + env(safe-area-inset-top, 0px)) 12px 8px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.topbar-row { display: flex; align-items: center; gap: 8px; }
.topbar-tools { gap: 8px; }
.topbar-tools select {
    width: auto;
    min-width: 110px;
    flex: 0 0 auto;
    padding: 8px 10px;
    min-height: 38px;
    height: 38px;
}
.topbar-tools input[type="search"] {
    flex: 1;
    min-height: 38px;
    height: 38px;
    padding: 8px 12px;
}
.logout-form { margin: 0; flex: 0 0 auto; }
.logout-form button { padding: 0 12px; min-height: 38px; height: 38px; min-width: 38px; font-size: 16px; }
#select-mode-btn { padding: 0 12px; min-height: 38px; height: 38px; min-width: 38px; font-size: 16px; flex: 0 0 auto; }

/* Bulk bar */
.bulk-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 6px 8px;
}
.bulk-count { font-size: 13px; color: var(--muted); flex: 1; }
.bulk-actions { display: flex; gap: 6px; }
.bulk-actions button { padding: 6px 10px; min-height: 36px; font-size: 13px; }

/* Popover */
.popover {
    position: absolute;
    z-index: 30;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: 0 12px 30px rgba(0,0,0,0.4);
    padding: 4px;
    display: flex;
    flex-direction: column;
    min-width: 180px;
}
.popover button {
    background: transparent;
    color: var(--text);
    text-align: left;
    padding: 10px 12px;
    border-radius: 6px;
    font-weight: 500;
    min-height: 40px;
}
.popover button:hover { background: var(--surface-2); }

/* Select-mode visuals */
body.select-mode .add-sub,
body.select-mode .snooze { display: none; }
body.select-mode .check {
    border-color: var(--accent);
}
.todo.selected {
    background: rgba(56, 189, 248, 0.12);
    border-color: var(--accent);
}
body.select-mode .todo-main { pointer-events: none; }

/* Agenda groups */
.agenda-group { margin: 4px 0 10px; }
.agenda-group h3 {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--muted);
    margin: 6px 4px;
    font-weight: 600;
}
.agenda-group.overdue h3 { color: var(--overdue); }
.agenda-group.today h3 { color: var(--pri-med); }

/* Space toggle */
.space-toggle {
    flex: 1;
    display: flex;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 3px;
    gap: 2px;
}
.space-btn {
    flex: 1;
    background: transparent;
    color: var(--muted);
    border: 0;
    border-radius: 999px;
    padding: 6px 14px;
    font-size: 14px;
    font-weight: 600;
    min-height: 32px;
}
.space-btn[aria-selected="true"] {
    background: var(--accent);
    color: #03131c;
}

.app {
    max-width: 720px;
    margin: 0 auto;
    padding: 14px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.quick-add { display: flex; gap: 8px; }
.quick-add input { flex: 1; }
.quick-add button[type="submit"] { flex: 0 0 auto; padding: 0 18px; }
#qa-details { flex: 0 0 auto; padding: 0; min-width: var(--tap); width: var(--tap); font-size: 22px; line-height: 1; }

.tag-bar { display: flex; flex-wrap: wrap; gap: 6px; }
.tag-chip {
    background: var(--surface-2);
    color: var(--muted);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 6px 12px;
    font-size: 13px;
    cursor: pointer;
    min-height: 32px;
}
.tag-chip.active { background: var(--accent); color: #03131c; border-color: var(--accent); font-weight: 600; }

/* Todo list */
.todo-list, .children {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.children {
    margin-top: 6px;
    padding-left: 24px;
    border-left: 2px solid var(--border);
    margin-left: 16px;
}
.todo {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 4px 6px;
}
.todo.done > .todo-row .title { color: var(--muted); text-decoration: line-through; }
.todo-row {
    display: flex;
    align-items: center;
    gap: 4px;
    min-height: var(--tap);
}
.check {
    background: transparent;
    border: 2px solid var(--muted);
    border-radius: 6px;
    width: 28px;
    height: 28px;
    min-height: 28px;
    padding: 0;
    flex: 0 0 auto;
    display: grid;
    place-items: center;
    color: transparent;
    margin-left: 4px;
}
.todo.done .check {
    background: var(--good);
    border-color: var(--good);
    color: #03131c;
}
.todo.done .check::after { content: "✓"; font-weight: 700; }

.todo-main {
    flex: 1;
    background: transparent;
    color: var(--text);
    text-align: left;
    padding: 8px 4px;
    min-height: var(--tap);
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
    border-radius: 6px;
    font-weight: 500;
    min-width: 0;
}
.todo-main:hover { background: var(--surface-2); }
.title-line { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.todo-main .title { word-break: break-word; }
.todo-main .progress {
    font-size: 11px;
    color: var(--muted);
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 1px 8px;
    font-weight: 500;
}
.todo-main .meta {
    font-size: 12px;
    color: var(--muted);
    font-weight: 400;
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    align-items: center;
}
.meta .pill {
    background: var(--surface-2);
    padding: 2px 8px;
    border-radius: 999px;
    border: 1px solid var(--border);
}
.meta .due.overdue { color: var(--overdue); border-color: var(--overdue); }
.meta .due.today { color: var(--pri-med); border-color: var(--pri-med); }
.meta .pri-1 { border-color: var(--pri-low); color: var(--pri-low); }
.meta .pri-2 { border-color: var(--pri-med); color: var(--pri-med); }
.meta .pri-3 { border-color: var(--pri-high); color: var(--pri-high); }
.meta .recur::before { content: "↻ "; }

.row-action {
    background: transparent;
    color: var(--muted);
    line-height: 1;
    padding: 0;
    width: var(--tap);
    height: var(--tap);
    min-height: var(--tap);
    border: 1px solid transparent;
    flex: 0 0 auto;
    font-size: 14px;
}
.row-action.add-sub { font-size: 22px; }
.row-action:hover { color: var(--accent); border-color: var(--border); }

/* Drag-and-drop */
.todo.sortable-ghost { opacity: 0.4; }
.todo.sortable-chosen { background: var(--surface-2); }
.todo.sortable-drag { box-shadow: 0 12px 30px rgba(0,0,0,0.45); }

.empty {
    color: var(--muted);
    text-align: center;
    padding: 30px 10px;
}

/* Drawer */
.drawer {
    position: fixed;
    inset: 0;
    z-index: 20;
    display: flex;
    align-items: flex-end;
    justify-content: center;
}
.drawer-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
}
.drawer-panel {
    position: relative;
    background: var(--surface);
    width: 100%;
    max-width: 600px;
    max-height: 92vh;
    overflow-y: auto;
    border-radius: 16px 16px 0 0;
    border: 1px solid var(--border);
    border-bottom: 0;
    padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
    animation: slide-up 0.18s ease-out;
}
@keyframes slide-up {
    from { transform: translateY(20%); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}
.drawer-head {
    display: flex; align-items: center; justify-content: space-between;
    margin-bottom: 12px;
}
.drawer-head h2 { margin: 0; font-size: 16px; }
.edit-form { display: grid; gap: 12px; }
.edit-form label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.row { display: grid; gap: 12px; grid-template-columns: 1fr 1fr; }
.form-actions { display: flex; justify-content: space-between; gap: 12px; margin-top: 4px; }

.date-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 4px; }
.date-chips .chip {
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 4px 10px;
    font-size: 12px;
    min-height: 30px;
    font-weight: 500;
}
.date-chips .chip:hover { border-color: var(--accent); color: var(--accent); }

@media (min-width: 720px) {
    .drawer { align-items: center; }
    .drawer-panel { border-radius: 16px; border-bottom: 1px solid var(--border); max-height: 85vh; }
}
