:root {
    --bg: #f5f7fb;
    --bg-elev: #ffffff;
    --bg-soft: #f0f3f9;
    --border: #e3e8f0;
    --border-strong: #d2d9e6;
    --text: #1a2233;
    --text-muted: #6b7488;
    --text-faint: #8b94a8;

    --primary: #4f6cf6;
    --primary-hover: #4159d6;
    --primary-soft: #eef1ff;
    --primary-text: #ffffff;

    --success: #19a974;
    --success-soft: #e6f7ef;
    --warning: #d97706;
    --warning-soft: #fff4e2;
    --danger: #e53e3e;
    --danger-soft: #fdecec;

    --prio-low: #38b2ac;
    --prio-low-soft: #e6f7f6;
    --prio-medium: #d97706;
    --prio-medium-soft: #fff4e2;
    --prio-high: #e53e3e;
    --prio-high-soft: #fdecec;

    --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
    --shadow-md: 0 4px 12px rgba(15, 23, 42, 0.06);
    --shadow-lg: 0 12px 28px rgba(15, 23, 42, 0.08);

    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;

    --transition: 160ms cubic-bezier(.2,.8,.2,1);
}

[data-theme="dark"] {
    --bg: #0f1320;
    --bg-elev: #181d2e;
    --bg-soft: #1f2438;
    --border: #2a3047;
    --border-strong: #3a4262;
    --text: #e8ecf6;
    --text-muted: #a0a8bd;
    --text-faint: #7c869f;

    --primary: #6b86ff;
    --primary-hover: #8298ff;
    --primary-soft: #232a4d;

    --success-soft: #133327;
    --warning-soft: #3a2a14;
    --danger-soft: #3a1818;

    --prio-low-soft: #15302e;
    --prio-medium-soft: #3a2a14;
    --prio-high-soft: #3a1818;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 12px 28px rgba(0, 0, 0, 0.45);
}

* {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background:
        radial-gradient(1000px 500px at 80% -10%, rgba(79, 108, 246, 0.12), transparent 60%),
        radial-gradient(800px 400px at -10% 0%, rgba(56, 178, 172, 0.10), transparent 60%),
        var(--bg);
    color: var(--text);
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
    line-height: 1.5;
}

.app {
    max-width: 1100px;
    margin: 0 auto;
    padding: 24px 20px 80px;
}

/* --- Header --- */
.header {
    margin-bottom: 24px;
}
.header-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}
.brand {
    display: flex;
    align-items: center;
    gap: 14px;
}
.brand-logo {
    width: 48px;
    height: 48px;
    border-radius: 14px;
    background: linear-gradient(135deg, var(--primary), #7e95ff);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-md);
}
.brand h1 {
    font-size: 22px;
    margin: 0;
    letter-spacing: -0.01em;
}
.subtitle {
    margin: 2px 0 0;
    color: var(--text-muted);
    font-size: 14px;
}

.theme-toggle {
    width: 42px;
    height: 42px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--bg-elev);
    color: var(--text);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-sm);
    transition: transform var(--transition), background var(--transition);
}
.theme-toggle:hover { transform: translateY(-1px); }
.theme-toggle .icon-moon { display: none; }
[data-theme="dark"] .theme-toggle .icon-sun { display: none; }
[data-theme="dark"] .theme-toggle .icon-moon { display: block; }

/* --- Stats --- */
.stats {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 12px;
    margin-bottom: 20px;
}
.stat-card {
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 14px 16px;
    box-shadow: var(--shadow-sm);
}
.stat-value {
    font-size: 26px;
    font-weight: 700;
    letter-spacing: -0.02em;
}
.stat-label {
    color: var(--text-muted);
    font-size: 13px;
    margin-top: 2px;
}

/* --- Cards --- */
.card {
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 20px;
    box-shadow: var(--shadow-sm);
    margin-bottom: 20px;
}
.card-title {
    font-size: 16px;
    margin: 0 0 14px;
    letter-spacing: -0.01em;
}

/* --- Form --- */
.task-form { display: flex; flex-direction: column; gap: 14px; }
.form-row { display: flex; gap: 12px; }
.form-row-multi { flex-wrap: wrap; }

.field {
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-width: 0;
    flex: 1 1 140px;
}
.field-label {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.req { color: var(--danger); }

input[type="text"],
input[type="search"],
input[type="date"],
textarea,
select {
    width: 100%;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-strong);
    background: var(--bg-elev);
    color: var(--text);
    font: inherit;
    transition: border-color var(--transition), box-shadow var(--transition), background var(--transition);
}
input:focus,
textarea:focus,
select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(79, 108, 246, 0.18);
}
textarea { resize: vertical; min-height: 60px; }

[data-theme="dark"] input[type="date"] { color-scheme: dark; }

/* --- Buttons --- */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    transition: background var(--transition), color var(--transition), transform var(--transition), border-color var(--transition);
}
.btn:active { transform: translateY(1px); }
.btn-primary {
    background: var(--primary);
    color: var(--primary-text);
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost {
    background: transparent;
    color: var(--text-muted);
    border-color: var(--border-strong);
}
.btn-ghost:hover { color: var(--text); background: var(--bg-soft); }
.btn-danger-ghost {
    background: transparent;
    color: var(--danger);
    border-color: var(--border-strong);
    padding: 8px 12px;
    font-size: 13px;
}
.btn-danger-ghost:hover { background: var(--danger-soft); }

/* --- List header & filters --- */
.list-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
    margin-bottom: 12px;
}
.list-controls {
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
}
.search {
    position: relative;
    display: flex;
    align-items: center;
}
.search svg {
    position: absolute;
    left: 10px;
    color: var(--text-faint);
    pointer-events: none;
}
.search input {
    padding-left: 34px;
    width: 240px;
    max-width: 100%;
}
#sortSelect { width: auto; min-width: 180px; }

.filters {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 12px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--border);
}
.filters-spacer { flex: 1; }
.filter-btn {
    border: 1px solid var(--border-strong);
    background: transparent;
    color: var(--text-muted);
    padding: 7px 12px;
    border-radius: 999px;
    cursor: pointer;
    font: inherit;
    font-size: 13px;
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    transition: all var(--transition);
}
.filter-btn:hover {
    color: var(--text);
    background: var(--bg-soft);
}
.filter-btn.active {
    background: var(--primary-soft);
    color: var(--primary);
    border-color: transparent;
}
.filter-count {
    font-size: 11px;
    background: var(--bg-soft);
    color: var(--text-muted);
    padding: 1px 7px;
    border-radius: 999px;
    min-width: 20px;
    text-align: center;
}
.filter-btn.active .filter-count {
    background: rgba(79, 108, 246, 0.15);
    color: var(--primary);
}

/* --- Task list --- */
.task-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.task-tree {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.task-tree-node {
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.nested-subtasks {
    list-style: none;
    margin: 4px 0 0 0;
    padding: 0 0 0 12px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    position: relative;
}
.nested-subtasks::before {
    content: "";
    position: absolute;
    left: -16px;
    top: 4px;
    bottom: 4px;
    width: 2px;
    background: var(--border);
    border-radius: 2px;
}
.subtask-list-collapsed {
    display: none !important;
}
.sub-toggle {
    flex-shrink: 0;
    width: 32px;
    min-height: 32px;
    align-self: stretch;
    margin: -4px 0;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: color var(--transition), background var(--transition);
}
.sub-toggle:hover {
    color: var(--text);
    background: var(--bg-soft);
}
.sub-toggle:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}
.sub-toggle-chevron {
    transition: transform var(--transition);
}
.sub-toggle[aria-expanded="false"] .sub-toggle-chevron {
    transform: rotate(-90deg);
}
.task-item {
    display: flex;
    gap: 12px;
    padding: 14px 16px;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--bg-elev);
    transition: border-color var(--transition), background var(--transition), transform var(--transition), opacity var(--transition), box-shadow var(--transition);
    position: relative;
}
.task-item:hover {
    border-color: var(--border-strong);
    box-shadow: var(--shadow-sm);
}
.task-item.subtask {
    padding: 10px 14px;
    background: var(--bg-soft);
    border-color: transparent;
    border-left: 3px solid var(--border-strong);
    border-top-left-radius: 8px;
    border-bottom-left-radius: 8px;
}
.task-item.subtask:hover {
    border-color: transparent;
    border-left-color: var(--primary);
    background: var(--bg-elev);
}
.task-item.done { opacity: 0.65; }
.task-item.done .task-title {
    text-decoration: line-through;
    color: var(--text-muted);
}
.task-item.editing {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(79, 108, 246, 0.15);
}

/* --- Drag & Drop --- */
.drag-handle {
    flex-shrink: 0;
    width: 18px;
    align-self: stretch;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-faint);
    cursor: grab;
    border-radius: 6px;
    margin: -4px 0 -4px -6px;
    opacity: 0;
    transition: opacity var(--transition), color var(--transition), background var(--transition);
    touch-action: none;
}
.task-item:hover .drag-handle { opacity: 0.55; }
.drag-handle:hover { opacity: 1 !important; color: var(--text); background: var(--bg-soft); }
.drag-handle:active { cursor: grabbing; }

.task-item.dragging {
    opacity: 0.4;
    box-shadow: var(--shadow-lg);
}
.task-item.drop-before {
    box-shadow: 0 -3px 0 0 var(--primary);
}
.task-item.drop-after {
    box-shadow: 0 3px 0 0 var(--primary);
}
.task-item.drop-into {
    border-color: var(--primary);
    background: var(--primary-soft);
}

@media (max-width: 720px) {
    .drag-handle { opacity: 0.5; }
}

.task-checkbox {
    margin-top: 2px;
    width: 22px;
    height: 22px;
    flex-shrink: 0;
    border-radius: 7px;
    border: 2px solid var(--border-strong);
    background: var(--bg-elev);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition);
}
.task-checkbox:hover { border-color: var(--primary); }
.task-checkbox.checked {
    background: var(--success);
    border-color: var(--success);
    color: #fff;
}
.task-checkbox svg { display: none; }
.task-checkbox.checked svg { display: block; }

.task-body { flex: 1; min-width: 0; }
.task-title {
    font-weight: 600;
    color: var(--text);
    word-wrap: break-word;
    margin-bottom: 4px;
    cursor: text;
    border-radius: var(--radius-sm);
    padding: 2px 4px;
    margin: -2px -4px 2px;
    width: fit-content;
    max-width: 100%;
    transition: background var(--transition);
}
.task-title:hover:not(.is-inline-editing) {
    background: var(--bg-soft);
}
.task-title.is-inline-editing {
    width: 100%;
    max-width: none;
    padding: 0;
    margin: 0 0 6px;
    cursor: default;
}
.task-title-inline-input {
    display: block;
    width: 100%;
    box-sizing: border-box;
    font: inherit;
    font-weight: 600;
    color: var(--text);
    padding: 6px 8px;
    margin: 0;
    border: 2px solid var(--primary);
    border-radius: var(--radius-sm);
    background: var(--bg-elev);
    outline: none;
    box-shadow: 0 0 0 3px rgba(79, 108, 246, 0.2);
}
.task-description[data-action="edit-title"] {
    cursor: pointer;
    border-radius: var(--radius-sm);
    margin: -2px -4px 6px;
    padding: 2px 4px;
    width: fit-content;
    max-width: 100%;
    text-align: left;
    transition: background var(--transition), color var(--transition);
}
.task-description[data-action="edit-title"]:hover {
    background: var(--bg-soft);
    color: var(--text);
}
.task-description[data-action="edit-title"]:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}
.task-description {
    color: var(--text-muted);
    font-size: 14px;
    white-space: pre-wrap;
    word-wrap: break-word;
    margin-bottom: 8px;
}
.task-meta {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    font-size: 12px;
    color: var(--text-muted);
}
.tag {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 3px 9px;
    border-radius: 999px;
    background: var(--bg-soft);
    color: var(--text-muted);
    font-size: 12px;
    font-weight: 600;
}
.tag svg { width: 12px; height: 12px; }

.tag-priority.priority-low { background: var(--prio-low-soft); color: var(--prio-low); }
.tag-priority.priority-medium { background: var(--prio-medium-soft); color: var(--prio-medium); }
.tag-priority.priority-high { background: var(--prio-high-soft); color: var(--prio-high); }

.tag-due.overdue { background: var(--danger-soft); color: var(--danger); }
.tag-due.today { background: var(--warning-soft); color: var(--warning); }

.task-actions {
    display: flex;
    gap: 4px;
    align-items: flex-start;
    opacity: 0.65;
    transition: opacity var(--transition);
}
.task-item:hover .task-actions { opacity: 1; }

.icon-btn {
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: background var(--transition), color var(--transition);
}
.icon-btn:hover { background: var(--bg-soft); color: var(--text); }
.icon-btn.danger:hover { background: var(--danger-soft); color: var(--danger); }
.icon-btn.subtask-add:hover { background: var(--primary-soft); color: var(--primary); }

/* --- Empty state --- */
.empty-state {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-muted);
}
.empty-illustration {
    width: 88px;
    height: 88px;
    border-radius: 50%;
    background: var(--bg-soft);
    color: var(--text-faint);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 14px;
}
.empty-state h3 { margin: 0 0 6px; color: var(--text); font-size: 17px; }
.empty-state p { margin: 0; font-size: 14px; }

/* --- Footer --- */
.footer {
    text-align: center;
    color: var(--text-muted);
    font-size: 13px;
    margin-top: 24px;
}
.footer a {
    color: var(--text-muted);
    text-decoration: none;
    border-bottom: 1px dotted var(--border-strong);
    cursor: pointer;
}
.footer a:hover { color: var(--primary); border-color: var(--primary); }
.footer a.danger-link:hover { color: var(--danger); border-color: var(--danger); }

/* --- Modal --- */
.modal {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    visibility: hidden;
    opacity: 0;
    transition: opacity var(--transition), visibility 0s linear var(--transition);
}
.modal.open {
    visibility: visible;
    opacity: 1;
    transition: opacity var(--transition);
}
.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(15, 19, 32, 0.55);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}
[data-theme="dark"] .modal-backdrop {
    background: rgba(0, 0, 0, 0.65);
}
.modal-dialog {
    position: relative;
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    width: 100%;
    max-width: 560px;
    max-height: calc(100vh - 48px);
    display: flex;
    flex-direction: column;
    transform: translateY(12px) scale(0.98);
    transition: transform var(--transition);
}
.modal.open .modal-dialog {
    transform: translateY(0) scale(1);
}
.modal-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    padding: 18px 22px;
    border-bottom: 1px solid var(--border);
}
.modal-heading {
    flex: 1;
    min-width: 0;
}
.modal-title {
    font-size: 17px;
    margin: 0;
    letter-spacing: -0.01em;
}
.modal-subtitle {
    margin: 8px 0 0;
    font-size: 13px;
    line-height: 1.45;
    color: var(--text-muted);
    font-weight: 400;
}
.modal-subtitle.hidden {
    display: none !important;
}
.modal-header .modal-close {
    flex-shrink: 0;
    margin-top: 2px;
}
.modal-body {
    padding: 20px 22px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 14px;
}
.modal-footer {
    padding: 14px 22px;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    background: var(--bg-soft);
    border-bottom-left-radius: var(--radius-lg);
    border-bottom-right-radius: var(--radius-lg);
}

body.modal-open {
    overflow: hidden;
}

/* --- Floating Action Button (mobile) --- */
.fab {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    border: none;
    background: var(--primary);
    color: var(--primary-text);
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-lg);
    z-index: 50;
    transition: transform var(--transition), background var(--transition);
}
.fab:hover { background: var(--primary-hover); }
.fab:active { transform: scale(0.95); }

/* --- Toast --- */
.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: var(--text);
    color: var(--bg-elev);
    padding: 12px 18px;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg);
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition), transform var(--transition);
    font-size: 14px;
    font-weight: 500;
    z-index: 100;
    max-width: 90vw;
}
.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* --- Login-Gate (Passwortschutz) --- */
.login-gate {
    position: fixed;
    inset: 0;
    z-index: 400;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    background: color-mix(in srgb, var(--bg) 88%, var(--text));
    backdrop-filter: blur(10px);
}
.login-gate.hidden { display: none !important; }
.login-panel {
    width: 100%;
    max-width: 400px;
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    padding: 28px 26px 24px;
}
.login-panel h2 {
    margin: 0 0 6px;
    font-size: 1.35rem;
    font-weight: 600;
    color: var(--text);
}
.login-panel .login-hint {
    margin: 0 0 20px;
    font-size: 14px;
    color: var(--text-muted);
    line-height: 1.45;
}
.login-panel form { display: flex; flex-direction: column; gap: 16px; }
.login-panel label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 500; color: var(--text); }
.login-panel input[type="password"] {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-soft);
    color: var(--text);
    font-size: 16px;
    box-sizing: border-box;
}
.login-panel input[type="password"]:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 28%, transparent);
}
.login-error {
    margin: 0;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    background: var(--danger-soft);
    color: var(--danger);
    border: 1px solid color-mix(in srgb, var(--danger) 35%, var(--border));
}
.login-panel button[type="submit"] {
    width: 100%;
    padding: 12px 16px;
    border: none;
    border-radius: var(--radius-sm);
    background: var(--primary);
    color: var(--primary-text);
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: background var(--transition);
}
.login-panel button[type="submit"]:hover:not(:disabled) { background: var(--primary-hover); }
.login-panel button[type="submit"]:disabled { opacity: 0.65; cursor: not-allowed; }

/* --- Utilities --- */
.hidden { display: none !important; }

/* --- Responsive --- */
@media (max-width: 720px) {
    .stats { grid-template-columns: repeat(2, 1fr); }
    .form-row-multi .field { flex: 1 1 calc(50% - 12px); }
    .list-header { flex-direction: column; align-items: stretch; }
    .list-controls { width: 100%; }
    .search { flex: 1; }
    .search input { width: 100%; }
    #sortSelect { flex: 1; }
    #openNewTaskBtn { display: none; }
    .fab { display: flex; }
    .brand h1 { font-size: 20px; }
    .subtitle { display: none; }
    .task-actions { opacity: 1; }
    .modal { padding: 0; align-items: flex-end; }
    .modal-dialog {
        max-width: 100%;
        max-height: 92vh;
        border-radius: 18px 18px 0 0;
        transform: translateY(20px);
    }
    .modal.open .modal-dialog { transform: translateY(0); }
    .modal-footer { border-bottom-left-radius: 0; border-bottom-right-radius: 0; }
}

@media (max-width: 420px) {
    .form-row-multi .field { flex: 1 1 100%; }
}
