/* === Design Tokens === */

/* Light palette — applied by default and when explicitly selected */
:root, html[data-theme="light"] {
    /* Cool Stone — neutral zinc base, Sakura pink spark */
    --bg: #FFFFFF;
    --surface: #F7F7F8;
    --border: #E4E4E7;
    --text: #09090B;
    --text-muted: #52525B;
    --accent: #3F3F46;
    --accent-hover: #27272A;
    --accent-fg: #FFFFFF;
    --accent-subtle: #F4F4F5;
    --link: #9D174D;
    --link-hover: #831843;
    --danger: #9B2626;
    --danger-hover: #7F1D1D;
    --danger-fg: #FFFFFF;
    --success-bg: #F0FAF2;
    --success-border: #A7F3C0;
    --success-text: #2D6A3F;
    --error-bg: #FEF2F2;
    --error-border: #FECACA;
    --error-text: #991B1B;
    --spark: #FF79C6;
    --link-subtle: rgba(157, 23, 77, 0.08);
    --focus-ring: 0 0 0 3px rgba(255, 121, 198, 0.22);
    --focus-ring-danger: 0 0 0 3px rgba(155, 38, 38, 0.14);
    --shadow-dialog: 0 8px 32px rgba(9, 9, 11, 0.12);
    --backdrop: rgba(9, 9, 11, 0.25);
    --sidebar-w: 220px;
    --radius: 3px;
}

/* Dark palette — applied by the inline script when OS prefers dark or explicitly selected */
html[data-theme="dark"] {
    /* Warm Carbon — warm stone base, Sakura pink spark */
    --bg: #131110;
    --surface: #1C1917;
    --border: #292524;
    --text: #FAFAF9;
    --text-muted: #A8A29E;
    --accent: #D6D3D1;
    --accent-hover: #E8E5E3;
    --accent-fg: #131110;
    --accent-subtle: #292524;
    --link: #FF79C6;
    --link-hover: #FFB3E0;
    --danger: #E07070;
    --danger-hover: #EF8888;
    --danger-fg: #131110;
    --success-bg: #1A2E21;
    --success-border: #3D6B4A;
    --success-text: #6EAF81;
    --error-bg: #2A1515;
    --error-border: #7B2626;
    --error-text: #E07070;
    --spark: #FF79C6;
    --link-subtle: rgba(255, 121, 198, 0.11);
    --focus-ring: 0 0 0 3px rgba(255, 121, 198, 0.22);
    --focus-ring-danger: 0 0 0 3px rgba(224, 112, 112, 0.22);
    --shadow-dialog: 0 8px 32px rgba(0, 0, 0, 0.55);
    --backdrop: rgba(0, 0, 0, 0.6);
}

/* === Reset === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
button { font: inherit; }

/* === Base === */
html { font-size: 16px; }

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    line-height: 1.6;
    color: var(--text);
    background: var(--bg);
    display: flex;
    height: 100vh;
    overflow: hidden;
}

h1 { font-size: 1.375rem; font-weight: 600; line-height: 1.2; margin-bottom: 1.25rem; }
h3 { font-size: 1rem; font-weight: 600; line-height: 1.2; }
p { color: var(--text-muted); font-size: 0.9375rem; }
a { color: var(--link); text-decoration: none; }
a:hover { color: var(--link-hover); text-decoration: underline; }

:focus-visible { outline: 2px solid var(--link); outline-offset: 2px; }

/* === Sidebar === */
aside#sidebar {
    width: var(--sidebar-w);
    flex-shrink: 0;
    border-right: 1px solid var(--border);
    background: var(--surface);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: width 200ms ease-out;
}

aside#sidebar.collapsed { width: 3rem; }

#sidebar-toggle {
    background: none;
    border: none;
    cursor: pointer;
    padding: 0.75rem;
    line-height: 1;
    color: var(--text-muted);
    align-self: flex-start;
    transition: color 150ms ease-out;
}

#sidebar-toggle:hover { color: var(--text); }

#sidebar-toggle svg {
    display: block;
    transition: transform 200ms ease-out;
}

aside#sidebar.collapsed #sidebar-toggle svg { transform: rotate(180deg); }

#sidebar-inner {
    padding: 0;
    overflow: hidden;
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

aside#sidebar.collapsed #sidebar-inner { display: none; }

.sidebar-header {
    padding: 0.9rem 1rem 0.7rem;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.sidebar-title {
    font-size: 0.68rem;
    font-weight: 600;
    letter-spacing: 0.09em;
    text-transform: uppercase;
    color: var(--text-muted);
    user-select: none;
}

.btn-new-inline {
    width: 22px;
    height: 22px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-muted);
    text-decoration: none;
    cursor: pointer;
    flex-shrink: 0;
    transition: background 150ms ease-out, color 150ms ease-out, border-color 150ms ease-out;
}

.btn-new-inline:hover {
    background: var(--accent-subtle);
    color: var(--text);
    border-color: var(--text-muted);
    text-decoration: none;
}

#sidebar-inner ul {
    list-style: none;
    padding: 0.5rem 0;
    overflow-y: auto;
    flex: 1;
}

.nav-link {
    display: block;
    padding: 0.4rem 1rem;
    color: var(--text-muted);
    text-decoration: none;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 0.875rem;
    cursor: pointer;
    transition: background 150ms ease-out, color 150ms ease-out;
}

.nav-link:hover { background: var(--accent-subtle); color: var(--text); text-decoration: none; }

.nav-link.active {
    background: var(--link-subtle);
    color: var(--text);
    font-weight: 500;
    border-left: 2px solid var(--spark);
    padding-left: calc(1rem - 2px);
}

.sort-row {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.5rem 1rem;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.sort-label {
    font-size: 0.6875rem;
    color: var(--text-muted);
    opacity: 0.55;
    user-select: none;
    margin-right: 0.1rem;
}

.sort-opt {
    font-size: 0.6875rem;
    font-family: inherit;
    color: var(--text-muted);
    background: none;
    border: none;
    padding: 0;
    transition: color 150ms ease-out;
    cursor: pointer;
}

.sort-opt:hover { color: var(--text); text-decoration: none; }

.sort-opt.sort-active {
    color: var(--text);
    font-weight: 500;
}

.sort-sep {
    font-size: 0.6875rem;
    color: var(--border);
    user-select: none;
}

/* === Main === */
main#main-content {
    flex: 1;
    padding: 2rem;
    overflow-y: auto;
    min-width: 0;
    max-width: 800px;
    margin-inline: auto;
}

main#main-content.empty-state {
    display: flex;
    align-items: center;
    justify-content: center;
}

.form-container {
    max-width: 400px;
    margin: auto;
}

/* === Flash === */
.flash {
    padding: 0.625rem 1rem;
    border-radius: var(--radius);
    margin-bottom: 1.5rem;
    border: 1px solid var(--success-border);
    background: var(--success-bg);
    color: var(--success-text);
    font-size: 0.875rem;
    max-height: 8rem;
    overflow: hidden;
    transition: opacity 350ms ease,
                max-height 300ms ease 100ms,
                margin-bottom 300ms ease 100ms,
                padding 300ms ease 100ms,
                border-width 300ms ease 100ms;
}

.flash.flash-out {
    opacity: 0;
    max-height: 0;
    margin-bottom: 0;
    padding-top: 0;
    padding-bottom: 0;
    border-width: 0;
}

@media (prefers-reduced-motion: reduce) {
    .flash { transition: none; }
}

.flash.flash-error {
    border-color: var(--error-border);
    background: var(--error-bg);
    color: var(--error-text);
}

/* === Forms === */
label {
    display: block;
    font-size: 0.875rem;
    font-weight: 500;
    margin-bottom: 0.375rem;
}

input[type="text"],
input[type="email"],
input[type="password"] {
    display: block;
    width: 100%;
    padding: 0.5rem 0.75rem;
    font-family: inherit;
    font-size: 0.9375rem;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    transition: border-color 150ms ease-out, box-shadow 150ms ease-out;
    margin-bottom: 1rem;
}

input[type="text"]:focus,
input[type="email"]:focus,
input[type="password"]:focus {
    outline: none;
    border-color: var(--link);
    box-shadow: var(--focus-ring);
}

input[type="text"][aria-invalid="true"],
input[type="email"][aria-invalid="true"] { border-color: var(--danger); }

input[type="text"][aria-invalid="true"]:focus,
input[type="email"][aria-invalid="true"]:focus {
    box-shadow: var(--focus-ring-danger);
}

.field-error {
    display: block;
    color: var(--danger);
    font-size: 0.8125rem;
    margin-top: -0.75rem;
    margin-bottom: 1rem;
}

/* === Buttons === */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.4375rem 0.875rem;
    font-family: inherit;
    font-size: 0.875rem;
    font-weight: 500;
    border-radius: var(--radius);
    border: 1px solid transparent;
    cursor: pointer;
    text-decoration: none;
    white-space: nowrap;
    background: none;
    color: var(--text);
    transition: background 150ms ease-out, border-color 150ms ease-out, color 150ms ease-out;
}

.btn-primary {
    background: var(--accent);
    color: var(--accent-fg);
    border-color: var(--accent);
}

.btn-primary:hover {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
    color: var(--accent-fg);
    text-decoration: none;
}

.btn-secondary {
    background: var(--surface);
    color: var(--text-muted);
    border-color: var(--border);
}

.btn-secondary:hover {
    background: var(--bg);
    color: var(--text);
    text-decoration: none;
}

.btn-outline-danger {
    background: var(--surface);
    color: var(--danger);
    border-color: var(--danger);
}

.btn-outline-danger:hover {
    background: var(--danger);
    color: var(--danger-fg);
    text-decoration: none;
}

.btn-danger {
    background: var(--danger);
    color: var(--danger-fg);
    border-color: var(--danger);
}

.btn-danger:hover {
    background: var(--danger-hover);
    border-color: var(--danger-hover);
    color: var(--danger-fg);
    text-decoration: none;
}

.btn-ghost,
.btn-ghost-danger {
    background: transparent;
    border-color: transparent;
    color: var(--text-muted);
    font-size: 0.75rem;
    font-weight: 400;
}

.btn-ghost:hover {
    background: var(--accent-subtle);
    border-color: var(--border);
    color: var(--text);
    text-decoration: none;
}

.btn-ghost-danger:hover {
    color: var(--link);
    border-color: var(--link);
    background: var(--link-subtle);
    text-decoration: none;
}


/* Icon-only button — used for edit/delete actions */
.btn-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0;
    background: none;
    border: none;
    border-radius: var(--radius);
    color: var(--text-muted);
    cursor: pointer;
    transition: background 150ms ease-out, color 150ms ease-out;
}

.btn-icon:hover { background: var(--accent-subtle); color: var(--text); }
.btn-icon-danger:hover { color: var(--danger); }

/* === Card === */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: calc(var(--radius) * 1.5);
    padding: 1.5rem;
    margin-bottom: 1.5rem;
}

/* On project detail pages the main area becomes a flex column so the
   header stays fixed at the top and only the content below scrolls.
   Padding is removed here and owned by .project-header / .project-scroll
   so the header border runs full-width edge to edge. */
main#main-content:has(.project-header) {
    display: flex;
    flex-direction: column;
    overflow-y: hidden;
    padding: 0;
    max-width: none;
}

/* === Project header === */
.project-header {
    display: flex;
    align-items: center;
    gap: 1rem;
    flex-shrink: 0;
    padding: 0.875rem 2rem;
    border-bottom: 1px solid var(--border);
}

.project-info { flex: 1; min-width: 0; }

.project-info h1 {
    font-size: 0.95rem;
    font-weight: 600;
    margin-bottom: 0;
    color: var(--text);
}

.project-info p {
    margin: 0.2rem 0 0;
    font-size: 0.8125rem;
    color: var(--text-muted);
}

.project-actions {
    display: flex;
    gap: 0.5rem;
    flex-shrink: 0;
    margin-left: auto;
}

/* === Dialog === */
dialog {
    margin: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: calc(var(--radius) * 1.5);
    padding: 1.75rem;
    max-width: 420px;
    width: 90%;
    box-shadow: var(--shadow-dialog);
    color: var(--text);
}

dialog::backdrop {
    background: var(--backdrop);
    backdrop-filter: blur(2px);
}

dialog h3 { margin-bottom: 0.875rem; }

dialog p { margin-bottom: 0; }

dialog footer {
    display: flex;
    gap: 0.5rem;
    justify-content: flex-end;
    margin-top: 1.5rem;
}

/* === Sidebar footer === */
#sidebar-footer {
    margin-top: auto;
    padding: 0.5rem 0.75rem;
    border-top: 1px solid var(--border);
    flex-shrink: 0;
    transition: border-color 150ms ease-out;
}

/* Hide label text when collapsed; keep icon visible */
aside#sidebar.collapsed .sidebar-label { display: none; }

aside#sidebar.collapsed #sidebar-footer {
    padding-left: 0;
    padding-right: 0;
}

aside#sidebar.collapsed #settings-btn {
    justify-content: center;
    padding: 0.75rem;
}

#settings-btn {
    display: flex;
    align-items: center;
    gap: 0.375rem;
    width: 100%;
    padding: 0.25rem 0.5rem;
    background: none;
    border: none;
    border-radius: var(--radius);
    color: var(--text-muted);
    font-size: 0.75rem;
    cursor: pointer;
    text-align: left;
    transition: background 150ms ease-out, color 150ms ease-out;
}

#settings-btn:hover,
#settings-btn[aria-expanded="true"] {
    background: var(--accent-subtle);
    color: var(--text);
}

/* === Settings panel === */
#settings-panel {
    position: fixed;
    z-index: 50;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.75rem 0.875rem;
    min-width: 152px;
    box-shadow: var(--shadow-dialog);
}

.settings-label {
    font-size: 0.6875rem;
    font-weight: 600;
    letter-spacing: 0.07em;
    text-transform: uppercase;
    color: var(--text-muted);
    margin-bottom: 0.375rem;
}

.theme-options {
    display: flex;
    flex-direction: column;
    gap: 0.125rem;
}

.theme-opt {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    width: 100%;
    padding: 0.3rem 0.5rem;
    background: none;
    border: none;
    border-radius: var(--radius);
    color: var(--text-muted);
    font-size: 0.8125rem;
    font-family: inherit;
    cursor: pointer;
    text-align: left;
    transition: background 150ms ease-out, color 150ms ease-out;
}

.theme-opt:hover {
    background: var(--accent-subtle);
    color: var(--text);
}

.theme-opt[aria-checked="true"] {
    color: var(--text);
    font-weight: 500;
}

/* === Welcome card (empty state) === */
.welcome-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: calc(var(--radius) * 4);
    padding: 2.5rem;
    max-width: 400px;
    width: 100%;
    margin: 0 auto;
    box-shadow: 0 4px 20px rgba(9, 9, 11, 0.06), 0 1px 3px rgba(9, 9, 11, 0.04);
    animation: welcome-enter 420ms cubic-bezier(0.16, 1, 0.3, 1) both;
}

html[data-theme="dark"] .welcome-card {
    box-shadow: 0 4px 28px rgba(0, 0, 0, 0.32), 0 1px 4px rgba(0, 0, 0, 0.18);
}

@keyframes welcome-enter {
    from { opacity: 0; transform: translateY(10px); }
    to   { opacity: 1; transform: translateY(0); }
}

.welcome-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 1.25rem;
}

.welcome-mark {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    flex-shrink: 0;
    background: var(--link-subtle);
    border-radius: calc(var(--radius) * 3);
    color: var(--spark);
}

/* standalone mark (home page) keeps its bottom margin */
.welcome-card > .welcome-mark {
    margin-bottom: 1.25rem;
}

.welcome-header .welcome-title {
    margin-bottom: 0;
}

.welcome-title {
    font-size: 1.25rem;
    font-weight: 600;
    line-height: 1.2;
    margin-bottom: 0.4rem;
}

.welcome-desc {
    font-size: 0.875rem;
    color: var(--text-muted);
    line-height: 1.65;
    margin-bottom: 0;
}

.welcome-rule {
    border: 0;
    border-top: 1px solid var(--border);
    margin: 1.75rem 0 1.5rem;
}

.welcome-form .field-group {
    margin-bottom: 1rem;
}

.welcome-form label {
    display: flex;
    align-items: baseline;
    gap: 0.4rem;
    margin-bottom: 0.3rem;
}

.field-optional {
    font-size: 0.75rem;
    font-weight: 400;
    color: var(--text-muted);
    opacity: 0.65;
}

.welcome-form input[type="text"],
.welcome-form input[type="email"],
.welcome-form input[type="password"] {
    margin-bottom: 0;
}

.welcome-form .field-error {
    margin-top: 0.3rem;
    margin-bottom: 0;
}

.welcome-form input::placeholder {
    color: var(--text-muted);
    opacity: 0.45;
}

.welcome-submit {
    width: 100%;
    justify-content: center;
    padding: 0.5625rem 1rem;
    margin-top: 0.375rem;
    font-size: 0.9375rem;
}

/* === Login page === */
.login-error {
    margin-bottom: 1rem;
}

.login-form .field-group:last-of-type input {
    margin-bottom: 0;
}

/* === Logout button (settings panel) === */
.logout-form {
    margin-top: 0.5rem;
    padding-top: 0.5rem;
    border-top: 1px solid var(--border);
}

.logout-btn {
    display: flex;
    align-items: center;
    width: 100%;
    padding: 0.25rem 0.5rem;
    font-family: inherit;
    font-size: 0.8125rem;
    font-weight: 400;
    color: var(--text-muted);
    background: none;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    transition: color 150ms ease-out, background 150ms ease-out;
    text-align: left;
}

.logout-btn:hover {
    color: var(--danger);
    background: var(--error-bg);
}

/* === Reduced motion === */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        transition-duration: 0.01ms !important;
        animation-duration: 0.01ms !important;
    }
    .welcome-card { animation: none; }
}

/* === Mobile burger & overlay (hidden on desktop) === */
#mobile-burger,
#sidebar-overlay { display: none; }

/* === Mobile === */
@media (max-width: 768px) {
    body { height: 100dvh; }

    /* ── Sidebar: fixed overlay, hidden off-screen ── */
    aside#sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        z-index: 200;
        width: 260px;
        transform: translateX(-100%);
        transition: transform 250ms ease-out;
    }

    aside#sidebar.mobile-open { transform: translateX(0); }

    /* Override collapsed state on mobile so content is always visible */
    aside#sidebar.collapsed { width: 260px; }

    aside#sidebar.collapsed #sidebar-inner { display: flex; }

    aside#sidebar.collapsed .sidebar-label { display: inline; }

    aside#sidebar.collapsed #sidebar-footer {
        padding-left: 0.75rem;
        padding-right: 0.75rem;
    }

    aside#sidebar.collapsed #settings-btn {
        justify-content: flex-start;
        padding: 0.25rem 0.5rem;
    }

    /* Hide desktop sidebar toggle chevron */
    #sidebar-toggle { display: none; }

    /* ── Overlay backdrop ── */
    #sidebar-overlay {
        display: block;
        position: fixed;
        inset: 0;
        z-index: 199;
        background: var(--backdrop);
        opacity: 0;
        pointer-events: none;
        transition: opacity 250ms ease-out;
    }

    #sidebar-overlay.active {
        opacity: 1;
        pointer-events: auto;
    }

    /* ── Burger button ── */
    #mobile-burger {
        display: flex;
        align-items: center;
        justify-content: center;
        position: fixed;
        top: 0;
        left: 0;
        z-index: 10;
        width: 44px;
        height: 44px;
        background: none;
        border: none;
        color: var(--text-muted);
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
    }

    #mobile-burger:hover { color: var(--text); }

    /* ── Main content adjustments ── */
    main#main-content {
        padding: 1rem;
        padding-top: 3.5rem;
        max-width: 100%;
        overflow-x: hidden;
    }

    main#main-content.empty-state { padding-top: 3.5rem; }

    /* On project detail, main has padding:0 and is a flex column.
       Reorder so .project-header sits at the top (sharing the burger row),
       then the flash appears below it, then the scrollable content. */
    main#main-content:has(.project-header) .project-header { order: 0; }
    main#main-content:has(.project-header) .flash {
        order: 1;
        margin: 0.5rem 1rem;
    }
    main#main-content:has(.project-header) .project-scroll { order: 2; }

    /* ── Welcome card: tighter on mobile ── */
    .welcome-card {
        padding: 1.75rem;
        border-radius: calc(var(--radius) * 3);
    }
}
