@layer reset, base, layout, components, utilities;

@layer reset {
    *,
    *::before,
    *::after {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
}

@layer base {
    :root {
        --font:
            -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui,
            sans-serif;
        --font-mono: "SF Mono", Menlo, Consolas, monospace;

        --bg: light-dark(#fafafa, #0d0d0d);
        --bg-panel: light-dark(#f5f5f7, #1a1a1a);
        --bg-hover: light-dark(rgba(0, 0, 0, 0.04), rgba(255, 255, 255, 0.06));
        --bg-active: light-dark(rgba(0, 0, 0, 0.08), rgba(255, 255, 255, 0.1));

        --text: light-dark(#1d1d1f, #f5f5f7);
        --text-secondary: light-dark(#86868b, #a1a1a6);
        --text-tertiary: light-dark(#c7c7cc, #48484a);

        --border: light-dark(rgba(0, 0, 0, 0.06), rgba(255, 255, 255, 0.08));

        --accent: #0071e3;

        --radius-sm: 6px;
        --radius-md: 10px;
        --radius-lg: 14px;

        --ease-out: cubic-bezier(0.25, 0.46, 0.45, 0.94);
    }

    html {
        color-scheme: light dark;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }

    body {
        font-family: var(--font);
        font-size: 15px;
        font-weight: 400;
        letter-spacing: -0.01em;
        background: var(--bg);
        color: var(--text);
        line-height: 1.5;
        min-height: 100dvh;
        overflow-x: hidden;
    }

    ::selection {
        background: color-mix(in srgb, var(--accent) 25%, transparent);
    }
}

@layer layout {
    body {
        display: flex;
        flex-direction: column;
    }

    /* Sidebar - right side on desktop */
    aside {
        order: 1;
        flex-shrink: 0;
        background: var(--bg-panel);
        padding: 16px;
        border-bottom: 1px solid var(--border);

        header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 12px;

            strong {
                font-size: 13px;
                font-weight: 600;
                color: var(--text-secondary);
                text-transform: uppercase;
                letter-spacing: 0.02em;
            }
        }

        nav {
            display: none;
            flex-direction: column;
            gap: 2px;
        }

        ul {
            list-style: none;
            margin-top: 8px;
            display: flex;
            flex-direction: column;
            gap: 2px;
        }

        a {
            display: block;
            padding: 10px 12px;
            color: var(--text);
            text-decoration: none;
            font-size: 14px;
            border-radius: var(--radius-md);
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            transition: background 0.15s var(--ease-out);

            &:hover {
                background: var(--bg-hover);
            }

            &:active {
                background: var(--bg-active);
            }

            &.active {
                background: var(--bg-active);
                font-weight: 500;
            }

            &.new {
                color: var(--accent);
                font-weight: 500;

                &:hover {
                    background: color-mix(
                        in srgb,
                        var(--accent) 10%,
                        transparent
                    );
                }
            }
        }

        /* Hide sidebar on login page */
        body:has(main.login) aside {
            display: none;
        }

        body:has(main.login) {
            display: block;
        }
    }

    .toggle {
        cursor: pointer;
        padding: 8px;
        border-radius: var(--radius-sm);
        color: var(--text-secondary);
        transition:
            background 0.15s var(--ease-out),
            color 0.15s var(--ease-out);
        display: flex;
        align-items: center;
        justify-content: center;

        &:hover {
            background: var(--bg-hover);
            color: var(--text);
        }

        input {
            display: none;
        }

        svg {
            width: 18px;
            height: 18px;
        }
    }

    body:has(#sidebar-toggle:checked) aside nav {
        display: flex;
    }

    /* Main content - centered */
    main {
        order: 0;
        flex: 1;
        padding: 32px 20px;
        display: flex;
        flex-direction: column;

        header {
            display: flex;
            justify-content: flex-end;
            align-items: center;
            gap: 8px;
            margin-bottom: 32px;
            max-width: 680px;
            width: 100%;
            margin-left: auto;
            margin-right: auto;
        }

        h1 {
            font-size: 32px;
            font-weight: 700;
            line-height: 1.15;
            letter-spacing: -0.025em;
            margin-bottom: 24px;
            max-width: 680px;
            width: 100%;
            margin-left: auto;
            margin-right: auto;
        }

        article {
            max-width: 680px;
            width: 100%;
            margin-left: auto;
            margin-right: auto;
        }
    }

    main.editor {
        #title {
            font-family: var(--font);
            font-size: 32px;
            font-weight: 700;
            letter-spacing: -0.025em;
            border: none;
            outline: none;
            background: transparent;
            color: var(--text);
            width: 100%;
            max-width: 680px;
            margin: 0 auto 20px;
            display: block;

            &::placeholder {
                color: var(--text-tertiary);
            }
        }

        #content {
            flex: 1;
            min-height: 60vh;
            font-family: var(--font);
            font-size: 16px;
            line-height: 1.7;
            letter-spacing: -0.01em;
            border: none;
            outline: none;
            resize: none;
            background: transparent;
            color: var(--text);
            width: 100%;
            max-width: 680px;
            margin: 0 auto;
            display: block;

            &::placeholder {
                color: var(--text-tertiary);
            }
        }
    }

    /* Desktop */
    @media (min-width: 768px) {
        body {
            flex-direction: row;
        }

        aside {
            order: 1;
            width: 260px;
            height: 100dvh;
            border-bottom: none;
            border-left: 1px solid var(--border);
            padding: 20px 12px;
            overflow-y: auto;
            position: sticky;
            top: 0;
            transition:
                width 0.2s var(--ease-out),
                opacity 0.2s var(--ease-out);

            header {
                padding: 0 8px;
                margin-bottom: 16px;
            }

            nav {
                display: flex;
            }

            a {
                padding: 8px 12px;
            }
        }

        body:has(#sidebar-toggle:checked) {
            aside {
                width: 56px;
                padding: 20px 8px;

                header {
                    justify-content: center;
                    padding: 0;
                }

                header strong,
                nav {
                    display: none;
                }
            }
        }

        main {
            order: 0;
            flex: 1;
            padding: 56px 40px;
            height: 100dvh;
            overflow-y: auto;

            header {
                margin-bottom: 40px;
            }

            h1 {
                font-size: 42px;
                margin-bottom: 32px;
            }
        }

        main.editor {
            #title {
                font-size: 42px;
                margin-bottom: 24px;
            }

            #content {
                font-size: 17px;
            }
        }
    }

    @media (min-width: 1200px) {
        main {
            padding: 64px 60px;
        }
    }
}

@layer components {
    /* Login */
    main.login {
        display: flex;
        align-items: center;
        justify-content: center;
        min-height: 100dvh;
        padding: 20px;

        form {
            width: 100%;
            max-width: 320px;
            display: flex;
            flex-direction: column;
            gap: 16px;
        }

        h1 {
            font-size: 32px;
            font-weight: 700;
            text-align: center;
            margin-bottom: 8px;
        }

        .error {
            color: #ff3b30;
            font-size: 14px;
            text-align: center;
            margin: 0;
        }

        input {
            font-family: var(--font);
            font-size: 16px;
            padding: 14px 16px;
            border: 1px solid var(--border);
            border-radius: var(--radius-md);
            background: var(--bg);
            color: var(--text);
            outline: none;
            transition: border-color 0.15s var(--ease-out);

            &:focus {
                border-color: var(--accent);
            }

            &::placeholder {
                color: var(--text-tertiary);
            }
        }

        button {
            font-family: var(--font);
            font-size: 16px;
            font-weight: 500;
            padding: 14px 16px;
            border: none;
            border-radius: var(--radius-md);
            background: var(--accent);
            color: white;
            cursor: pointer;
            transition: opacity 0.15s var(--ease-out);

            &:hover {
                opacity: 0.9;
            }

            &:active {
                opacity: 0.8;
            }
        }
    }

    #status {
        font-size: 12px;
        font-weight: 500;
        color: var(--text-secondary);

        &:empty {
            display: none;
        }
    }

    .edit-btn,
    .view-btn {
        font-size: 13px;
        font-weight: 500;
        color: var(--accent);
        text-decoration: none;
        padding: 8px 16px;
        border-radius: 100px;
        background: color-mix(in srgb, var(--accent) 10%, transparent);
        transition: background 0.15s var(--ease-out);

        &:hover {
            background: color-mix(in srgb, var(--accent) 18%, transparent);
        }
    }

    article {
        font-size: 16px;
        line-height: 1.75;

        h2 {
            font-size: 26px;
            font-weight: 700;
            letter-spacing: -0.02em;
            margin-top: 48px;
            margin-bottom: 16px;
        }

        h3 {
            font-size: 20px;
            font-weight: 600;
            margin-top: 36px;
            margin-bottom: 12px;
        }

        p {
            margin-bottom: 20px;
        }

        ul,
        ol {
            padding-left: 28px;
            margin: 16px 0 24px;
        }

        li {
            margin-bottom: 8px;
        }

        code {
            font-family: var(--font-mono);
            font-size: 0.9em;
            background: var(--bg-panel);
            padding: 3px 7px;
            border-radius: var(--radius-sm);
            color: var(--accent);
        }

        pre {
            background: var(--bg-panel);
            padding: 20px 24px;
            border-radius: var(--radius-lg);
            overflow-x: auto;
            margin: 24px 0;

            code {
                background: none;
                padding: 0;
                color: var(--text);
                font-size: 14px;
                line-height: 1.6;
            }
        }

        blockquote {
            border-left: 3px solid var(--accent);
            padding-left: 20px;
            margin: 24px 0;
            color: var(--text-secondary);
            font-style: italic;
        }

        a {
            color: var(--accent);
            text-decoration: none;

            &:hover {
                text-decoration: underline;
            }
        }

        strong {
            font-weight: 600;
        }

        @media (min-width: 768px) {
            font-size: 17px;

            h2 {
                font-size: 30px;
            }

            h3 {
                font-size: 22px;
            }
        }
    }
}

@layer utilities {
    main {
        view-transition-name: main-content;
    }

    ::view-transition-old(main-content) {
        animation: none;
        opacity: 0;
    }

    ::view-transition-new(main-content) {
        animation: 150ms ease-out fade-in;
    }

    ::view-transition-old(root),
    ::view-transition-new(root) {
        animation: none;
    }

    @keyframes fade-in {
        from {
            opacity: 0;
        }
    }

    @media (prefers-reduced-motion: reduce) {
        *,
        *::before,
        *::after {
            animation-duration: 0.01ms !important;
            transition-duration: 0.01ms !important;
        }
    }

    ::-webkit-scrollbar {
        width: 0;
        height: 0;
    }
}
