:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --accent: #38bdf8;

    --bg: #f5f7fb;
    --surface: #ffffff;
    --surface-alt: #f0f2f7;
    --border: #e5e8f0;
    --text: #1a1d29;
    --text-muted: #6b7280;
    --success: #16a34a;
    --danger: #dc2626;
    --radius: 14px;
    --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 4px 12px rgba(16, 24, 40, .06);
    font-size: 16px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
html { overflow-x: hidden; }
body {
    margin: 0;
    overflow-x: hidden;
    font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { margin: 0 0 .5rem; line-height: 1.25; }
p { margin: 0 0 1rem; color: var(--text-muted); }

.btn {
    display: inline-flex;
    align-items: center;
    gap: .4rem;
    border: none;
    border-radius: 10px;
    padding: .65rem 1.1rem;
    font-size: .95rem;
    font-weight: 600;
    cursor: pointer;
    touch-action: manipulation;
    transition: transform .08s ease, box-shadow .15s ease, opacity .15s ease;
}
.btn:hover { text-decoration: none; }
.btn-primary { background: linear-gradient(135deg, var(--primary), var(--primary-dark)); color: #fff; box-shadow: 0 2px 8px color-mix(in srgb, var(--primary) 40%, transparent); }
.btn-primary:hover { opacity: .92; }
.btn-outline { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-outline:hover { background: var(--surface-alt); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-sm { padding: .4rem .75rem; font-size: .82rem; }
.btn-block { width: 100%; justify-content: center; }
.btn:disabled { opacity: .35; cursor: not-allowed; }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1.5rem;
}

input[type=text], input[type=email], input[type=password], input[type=date], select, textarea {
    width: 100%;
    padding: .65rem .8rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface);
    color: var(--text);
    font-size: .95rem;
    font-family: inherit;
}
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 20%, transparent);
}
label { display: block; font-weight: 600; font-size: .85rem; margin-bottom: .35rem; color: var(--text); }
.field { margin-bottom: 1.1rem; }

/* ---------- Auth pages ---------- */
.auth-shell {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: radial-gradient(circle at top left, color-mix(in srgb, var(--primary) 18%, var(--bg)), var(--bg) 60%);
    padding: 1.5rem;
}
.auth-box {
    width: 100%;
    max-width: 400px;
    background: var(--surface);
    border-radius: 20px;
    box-shadow: var(--shadow);
    padding: 2.5rem 2.25rem;
    border: 1px solid var(--border);
}
.auth-logo {
    display: flex;
    align-items: center;
    gap: .6rem;
    font-weight: 800;
    font-size: 1.25rem;
    margin-bottom: 1.75rem;
}
.auth-logo .dot {
    width: 34px; height: 34px; border-radius: 9px;
    background: linear-gradient(135deg, var(--primary), var(--accent));
}
.alert {
    padding: .75rem 1rem;
    border-radius: 10px;
    font-size: .88rem;
    margin-bottom: 1rem;
}
.alert-error { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }
.alert-success { background: #f0fdf4; color: #166534; border: 1px solid #bbf7d0; }
.alert-info { background: color-mix(in srgb, var(--primary) 10%, white); color: var(--primary-dark); border: 1px solid color-mix(in srgb, var(--primary) 30%, white); }

/* ---------- App shell ---------- */
.app-shell { display: flex; min-height: 100vh; }
.sidebar {
    width: 240px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 1.5rem 1rem;
    display: flex;
    flex-direction: column;
    gap: .25rem;
    position: sticky;
    top: 0;
    height: 100vh;
}
.sidebar .auth-logo { padding: 0 .5rem; margin-bottom: 2rem; }
.nav-link {
    display: flex;
    align-items: center;
    gap: .65rem;
    padding: .6rem .75rem;
    border-radius: 10px;
    color: var(--text-muted);
    font-weight: 600;
    font-size: .9rem;
}
.nav-link:hover { background: var(--surface-alt); text-decoration: none; color: var(--text); }
.nav-link.active { background: color-mix(in srgb, var(--primary) 12%, white); color: var(--primary-dark); }
.sidebar-footer { margin-top: auto; padding-top: 1rem; border-top: 1px solid var(--border); }

.main { flex: 1; padding: 2rem 2.5rem; max-width: 1100px; }
.topbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1.75rem; flex-wrap: wrap; gap: 1rem; }
.topbar h1 { font-size: 1.5rem; }
.badge { display: inline-block; padding: .2rem .6rem; border-radius: 999px; font-size: .72rem; font-weight: 700; }
.badge-admin { background: color-mix(in srgb, var(--primary) 15%, white); color: var(--primary-dark); }
.badge-open { background: #eef2ff; color: #4338ca; }
.badge-progress { background: #fff7ed; color: #c2410c; }
.badge-done { background: #f0fdf4; color: #15803d; }

.grid { display: grid; gap: 1.25rem; }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }

.stat { text-align: left; }
.stat .value { font-size: 2rem; font-weight: 800; }
.stat .label { color: var(--text-muted); font-size: .85rem; font-weight: 600; }

.list-item { display: flex; align-items: center; justify-content: space-between; padding: .75rem 0; border-bottom: 1px solid var(--border); gap: 1rem; }
.list-item:last-child { border-bottom: none; }

.checkbox-row { display: flex; align-items: center; gap: .65rem; padding: .5rem 0; }
.checkbox-row input { width: 20px; height: 20px; accent-color: var(--primary); }

.routine-edit-panel {
    display: none;
    margin: -.35rem 0 .6rem 2.2rem;
    padding: .75rem;
    background: var(--surface-alt);
    border-radius: 10px;
}
.routine-edit-toggle:checked ~ .routine-edit-panel { display: block; }
.routine-edit-form { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.routine-edit-form input[type=text] { flex: 1; min-width: 140px; }
.routine-edit-form select { max-width: 180px; }
label.btn { user-select: none; }

.progress-bar { height: 8px; background: var(--surface-alt); border-radius: 999px; overflow: hidden; }
.progress-bar > span { display: block; height: 100%; background: linear-gradient(90deg, var(--primary), var(--accent)); }

.theme-swatch {
    width: 100%;
    aspect-ratio: 1.6;
    border-radius: 12px;
    border: 2px solid transparent;
    cursor: pointer;
    display: flex;
    align-items: flex-end;
    padding: .5rem;
    color: #fff;
    font-weight: 700;
    font-size: .8rem;
}
.theme-swatch.selected { border-color: var(--text); box-shadow: 0 0 0 3px color-mix(in srgb, var(--text) 15%, transparent); }

table { width: 100%; border-collapse: collapse; }
table th, table td { text-align: left; padding: .65rem .5rem; border-bottom: 1px solid var(--border); font-size: .9rem; white-space: nowrap; }
table th { color: var(--text-muted); font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; }
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -.25rem; padding: 0 .25rem; }

.heatmap-wrap { display: inline-block; min-width: 100%; }
.heatmap-months {
    display: flex;
    font-size: .72rem;
    color: var(--text-muted);
    margin-bottom: .3rem;
    padding-left: 2px;
    gap: 17px;
}
.heatmap-months span { width: 14px; }
.heatmap {
    display: grid;
    grid-auto-flow: column;
    grid-template-rows: repeat(7, 14px);
    gap: 3px;
}
.heatmap-cell {
    width: 14px;
    height: 14px;
    border-radius: 3px;
    background: var(--surface-alt);
    border: 1px solid var(--border);
}
.heatmap-empty { background: transparent; border-color: transparent; }
.heatmap-l0 { background: var(--surface-alt); }
.heatmap-l1 { background: color-mix(in srgb, var(--primary) 25%, var(--surface-alt)); border-color: transparent; }
.heatmap-l2 { background: color-mix(in srgb, var(--primary) 50%, var(--surface-alt)); border-color: transparent; }
.heatmap-l3 { background: color-mix(in srgb, var(--primary) 75%, var(--surface-alt)); border-color: transparent; }
.heatmap-l4 { background: var(--primary); border-color: transparent; }
.heatmap-legend { display: flex; align-items: center; gap: .3rem; }
.heatmap-legend .heatmap-cell { width: 12px; height: 12px; }

.empty-state { text-align: center; padding: 2.5rem 1rem; color: var(--text-muted); }

.flex-between { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; }
.muted { color: var(--text-muted); font-size: .85rem; }
.mt-2 { margin-top: 1.5rem; }
.mb-0 { margin-bottom: 0; }

code.token {
    display: block;
    background: #0f172a;
    color: #e2e8f0;
    padding: .8rem 1rem;
    border-radius: 10px;
    font-size: .82rem;
    word-break: break-all;
}

.mobile-topbar { display: none; }
.hamburger-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface);
    color: var(--text);
    font-size: 1.2rem;
    cursor: pointer;
    touch-action: manipulation;
    flex-shrink: 0;
}
.hamburger-btn:hover { background: var(--surface-alt); }

.habit-check {
    width: 36px;
    height: 36px;
    border-radius: 9px;
    border: 2px solid var(--border);
    background: var(--surface);
    color: #fff;
    font-weight: 800;
    font-size: 1rem;
    cursor: pointer;
    touch-action: manipulation;
    display: flex;
    align-items: center;
    justify-content: center;
}
.habit-check.checked {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    border-color: var(--primary-dark);
}

@media (max-width: 800px) {
    .app-shell { flex-direction: column; }

    .mobile-topbar {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 1rem;
        padding: .85rem 1.1rem;
        background: var(--surface);
        border-bottom: 1px solid var(--border);
        position: sticky;
        top: 0;
        z-index: 30;
    }
    .mobile-topbar .auth-logo { margin-bottom: 0; }

    .sidebar {
        display: none;
        width: 100%;
        height: auto;
        position: static;
        flex-direction: column;
        border-right: none;
        border-bottom: 1px solid var(--border);
        box-shadow: var(--shadow);
    }
    .sidebar.open { display: flex; }
    .sidebar .auth-logo { display: none; }
    .sidebar-footer { display: block; margin-top: 1rem; padding-top: 1rem; border-top: 1px solid var(--border); }
    .main { padding: 1.25rem; }
}
