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

:root,
html {
    /* ── Paper ── */
    --bg-paper: #f4efe6;
    --bg-paper-warm: #f0ebe0;
    --surface: #faf7f0;
    --surface-elevated: #fefcf8;
    --surface-muted: #f5f1e9;

    /* ── Text ── */
    --text-primary: #1c1612;
    --text-secondary: #6b5d4f;
    --text-muted: #9b8d80;

    /* ── Terracotta ── */
    --accent: #c1664b;
    --accent-dark: #9b4a35;
    --accent-light: #f2d9cf;
    --accent-glow: rgba(193, 102, 75, 0.10);

    /* ── Borders ── */
    --border: rgba(0, 0, 0, 0.06);
    --border-strong: rgba(0, 0, 0, 0.10);

    /* ── Shadows ── */
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.03);
    --shadow-md: 0 1px 3px rgba(0, 0, 0, 0.03), 0 3px 12px rgba(0, 0, 0, 0.04);
    --shadow-lg: 0 2px 6px rgba(0, 0, 0, 0.03), 0 8px 28px rgba(0, 0, 0, 0.06);
    --shadow-xl: 0 4px 12px rgba(0, 0, 0, 0.04), 0 14px 44px rgba(0, 0, 0, 0.08);

    /* ── Radius ── */
    --radius-sm: 8px;
    --radius-md: 14px;
    --radius-lg: 22px;
    --radius-xl: 28px;
    --radius-full: 999px;

    /* ── Typography ── */
    --font-display: "Noto Serif SC", serif;
    --font-body: "Noto Sans SC", sans-serif;

    /* ── Legacy compat (keep JS references working) ── */
    --nav-gap: 0.75rem;
    --nav-margin-bottom: 1rem;
    --nav-padding-block: 0.7rem;
    --nav-padding-inline: 1rem;
    --nav-control-size: 42px;
    --glass-blur: 12px;
    --glass-blur-strong: 20px;
    --glass-saturate: 100%;
    --glass-highlight: none;
    --glass-outline: none;
    --panel-shadow: var(--shadow-lg);
}

/* ── Dark Mode: ink-on-dark-parchment ── */
html.dark,
html:has(body.dark),
body.dark {
    --bg-paper: #1a1714;
    --bg-paper-warm: #1e1b18;
    --surface: #211e1a;
    --surface-elevated: #282420;
    --surface-muted: #1c1916;
    --text-primary: #e8e0d5;
    --text-secondary: #a39888;
    --text-muted: #6b6156;
    --accent: #d48465;
    --accent-dark: #b56040;
    --accent-light: rgba(212, 132, 101, 0.14);
    --accent-glow: rgba(212, 132, 101, 0.10);
    --border: rgba(255, 255, 255, 0.06);
    --border-strong: rgba(255, 255, 255, 0.10);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.20);
    --shadow-md: 0 2px 8px rgba(0, 0, 0, 0.30);
    --shadow-lg: 0 4px 16px rgba(0, 0, 0, 0.35);
    --shadow-xl: 0 8px 32px rgba(0, 0, 0, 0.45);
}

/* ── Stable scrollbar gutter (Windows: prevent nav shift when scrollbar toggles) ── */
html {
    scrollbar-gutter: stable;
}

/* ── Body ── */
body {
    font-family: var(--font-body);
    background: var(--bg-paper);
    min-height: 100vh;
    color: var(--text-primary);
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    line-height: 1.6;
}

/* ── Paper texture overlay ── */
body::before {
    content: "";
    position: fixed;
    inset: 0;
    z-index: 0;
    pointer-events: none;
    opacity: 0.45;
    background:
        repeating-linear-gradient(
            0deg,
            transparent,
            transparent 2px,
            rgba(180, 160, 140, 0.03) 2px,
            rgba(180, 160, 140, 0.03) 3px
        ),
        repeating-linear-gradient(
            90deg,
            transparent,
            transparent 1px,
            rgba(180, 160, 140, 0.02) 1px,
            rgba(180, 160, 140, 0.02) 2px
        );
}

body::after {
    content: "";
    position: fixed;
    inset: 0;
    z-index: 0;
    pointer-events: none;
    background:
        radial-gradient(ellipse at 15% 10%, rgba(193, 102, 75, 0.04), transparent 50%),
        radial-gradient(ellipse at 85% 90%, rgba(155, 74, 53, 0.03), transparent 50%),
        radial-gradient(ellipse at 50% 50%, rgba(180, 150, 120, 0.02), transparent 70%);
}

html.dark body::before,
html:has(body.dark) body::before,
body.dark::before {
    opacity: 0.20;
    background:
        repeating-linear-gradient(
            0deg,
            transparent,
            transparent 2px,
            rgba(255, 255, 255, 0.015) 2px,
            rgba(255, 255, 255, 0.015) 3px
        );
}

html.dark body::after,
html:has(body.dark) body::after,
body.dark::after {
    background:
        radial-gradient(ellipse at 15% 10%, rgba(212, 132, 101, 0.06), transparent 50%),
        radial-gradient(ellipse at 85% 90%, rgba(180, 100, 70, 0.04), transparent 50%);
}

/* ── App Container ── */
.app-container {
    position: relative;
    z-index: 2;
    max-width: 1280px;
    margin: 0 auto;
    padding: 1.5rem 2rem;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    transition: padding 0.2s ease;
}

body.chat-active .app-container {
    max-width: none;
    padding: 1rem 1.5rem;
}

/* ── Header ── */
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem;
    margin-bottom: var(--nav-margin-bottom);
    padding: 0.6rem 0;
    transition: padding 0.2s ease, margin-bottom 0.2s ease, gap 0.2s ease;
}

/* Slightly narrower header in chat mode */
body.chat-active .header {
    padding: 0.3rem 0;
    margin-bottom: 0.4rem;
    gap: 0.5rem;
}

body.chat-active .logo {
    font-size: 1.15rem;
}

body.chat-active .logo span {
    display: none;
}

body.chat-active .round-nav-btn,
body.chat-active .theme-toggle {
    width: 32px;
    height: 32px;
    min-height: 32px;
}

body.chat-active .theme-icon {
    width: 14px;
    height: 14px;
}

/* ── Logo ── */
.logo {
    font-family: var(--font-display);
    font-size: 1.45rem;
    font-weight: 700;
    text-decoration: none;
    color: var(--accent-dark);
    letter-spacing: 0.06em;
    line-height: 1.3;
    transition: color 0.2s ease, font-size 0.2s ease;
}

.logo:hover {
    color: var(--accent);
}

.logo span {
    font-weight: 400;
    font-size: 0.85em;
    color: var(--text-muted);
    letter-spacing: 0.03em;
}

/* ── Header actions ── */
.header-actions {
    display: flex;
    align-items: center;
    gap: 0.6rem;
}

.round-nav-btn {
    width: 40px;
    height: 40px;
    border: 1px solid var(--border);
    border-radius: 50%;
    background: var(--surface);
    color: var(--text-secondary);
    cursor: pointer;
    display: grid;
    place-items: center;
    padding: 0;
    font-size: 1rem;
    transition: width 0.2s ease, height 0.2s ease, color 0.2s ease, border-color 0.2s ease, background 0.2s ease, box-shadow 0.2s ease;
    box-shadow: var(--shadow-sm);
}

.round-nav-btn:hover {
    color: var(--accent);
    border-color: var(--accent);
    background: var(--surface-elevated);
    box-shadow: var(--shadow-md);
}

/* ── Theme Toggle ── */
.theme-toggle {
    width: 40px;
    height: 40px;
    min-height: 40px;
    border: 1px solid var(--border);
    border-radius: 50%;
    background: var(--surface);
    cursor: pointer;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    color: var(--text-secondary);
    font-size: 0;
    padding: 0;
    transition: width 0.2s ease, height 0.2s ease, min-height 0.2s ease, color 0.2s ease, border-color 0.2s ease, background 0.2s ease, box-shadow 0.2s ease;
    box-shadow: var(--shadow-sm);
}

.theme-toggle:hover {
    color: var(--accent);
    border-color: var(--accent);
    background: var(--surface-elevated);
}

.theme-icon {
    width: 17px;
    height: 17px;
    fill: currentColor;
    display: block;
    transition: width 0.2s ease, height 0.2s ease;
}

/* ── Site Dialog / Modal ── */
.site-dialog {
    position: fixed;
    inset: 0;
    z-index: 1800;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 1.2rem;
}

.site-dialog.is-open {
    display: flex;
}

.site-dialog-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(12, 10, 8, 0.45);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
}

.site-dialog-panel {
    position: relative;
    z-index: 1;
    width: min(440px, calc(100vw - 2rem));
    padding: 1.6rem 1.4rem;
    border-radius: var(--radius-xl);
    border: 1px solid var(--border);
    background: var(--surface-elevated);
    box-shadow: var(--shadow-xl);
}

.site-dialog-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.45rem;
    padding: 0.3rem 0.75rem;
    border-radius: var(--radius-full);
    border: 1px solid var(--accent-light);
    background: var(--accent-glow);
    color: var(--accent-dark);
    font-size: 0.82rem;
    font-weight: 500;
}

.site-dialog-title {
    margin-top: 0.9rem;
    font-family: var(--font-display);
    font-size: 1.3rem;
    line-height: 1.4;
    color: var(--text-primary);
}

.site-dialog-message {
    margin-top: 0.65rem;
    color: var(--text-secondary);
    line-height: 1.75;
    font-size: 0.95rem;
}

.site-dialog-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.7rem;
    margin-top: 1.2rem;
}

.site-dialog-btn {
    -webkit-appearance: none;
    appearance: none;
    min-width: 108px;
    min-height: 44px;
    padding: 0.7rem 1.1rem;
    border-radius: var(--radius-md);
    cursor: pointer;
    font: inherit;
    font-weight: 500;
    font-size: 0.94rem;
    transition: all 0.2s ease;
}

.site-dialog-btn-secondary {
    border: 1px solid var(--border);
    color: var(--text-primary);
    background: var(--surface);
}

.site-dialog-btn-secondary:hover {
    background: var(--surface-elevated);
    border-color: var(--border-strong);
}

.site-dialog-btn-primary {
    border: 0;
    color: #fff;
    background: var(--accent);
    box-shadow: 0 4px 14px rgba(193, 102, 75, 0.25);
}

.site-dialog-btn-primary:hover {
    background: var(--accent-dark);
    box-shadow: 0 6px 18px rgba(155, 74, 53, 0.30);
}

/* ── Seal mark ── */
.seal-mark {
    position: fixed;
    bottom: 24px;
    right: 24px;
    font-family: var(--font-display);
    font-size: 0.7rem;
    opacity: 0.35;
    pointer-events: none;
    color: var(--accent);
    z-index: 1;
}

/* ── Animations ── */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(12px); }
    to   { opacity: 1; transform: translateY(0); }
}

@keyframes fadeInOpacity {
    from { opacity: 0; }
    to   { opacity: 1; }
}

@keyframes fadeInUp {
    from { opacity: 0; transform: translateY(28px); }
    to   { opacity: 1; transform: translateY(0); }
}

@keyframes fadeInScale {
    from { opacity: 0; transform: scale(0.96) translateY(16px); }
    to   { opacity: 1; transform: scale(1) translateY(0); }
}

@keyframes shimmer {
    0%   { background-position: -200% 0; }
    100% { background-position: 200% 0; }
}

/* ── Reveal (scroll-triggered, see main.js) ── */
.reveal {
    opacity: 0;
    transform: translateY(32px);
    transition: opacity 0.7s cubic-bezier(0.22, 0.61, 0.36, 1),
                transform 0.7s cubic-bezier(0.22, 0.61, 0.36, 1);
}

.reveal.revealed {
    opacity: 1;
    transform: translateY(0);
}

.reveal-stagger > * {
    opacity: 0;
    transform: translateY(28px);
    transition: opacity 0.6s cubic-bezier(0.22, 0.61, 0.36, 1),
                transform 0.6s cubic-bezier(0.22, 0.61, 0.36, 1);
}

.reveal-stagger.revealed > *:nth-child(1) { transition-delay: 0.00s; }
.reveal-stagger.revealed > *:nth-child(2) { transition-delay: 0.10s; }
.reveal-stagger.revealed > *:nth-child(3) { transition-delay: 0.20s; }
.reveal-stagger.revealed > *:nth-child(4) { transition-delay: 0.30s; }
.reveal-stagger.revealed > *:nth-child(5) { transition-delay: 0.40s; }
.reveal-stagger.revealed > *:nth-child(6) { transition-delay: 0.50s; }
.reveal-stagger.revealed > * {
    opacity: 1;
    transform: translateY(0);
}

/* ── Responsive ── */
@media (max-width: 768px) {
    .app-container {
        padding: 1rem 1.2rem;
    }

    body.chat-active .app-container {
        padding: 0.45rem 0.6rem max(0.3rem, env(safe-area-inset-bottom));
    }

    .logo {
        font-size: 1.25rem;
    }

    .logo span {
        display: inline;
        font-size: 0.8rem;
    }

    .header {
        gap: 0.6rem;
        padding: 0.4rem 0;
    }

    body.chat-active .header {
        padding: 0.35rem 0;
        margin-bottom: 0.45rem;
        gap: 0.55rem;
    }

    body.chat-active .logo {
        font-size: 1.15rem;
    }

    body.chat-active .round-nav-btn,
    body.chat-active .theme-toggle {
        width: 36px;
        height: 36px;
        min-height: 36px;
    }

    body.chat-active .theme-icon {
        width: 15px;
        height: 15px;
    }
}

@media (max-width: 420px) {
    .app-container {
        padding: 0.85rem;
    }

    .logo {
        font-size: 1.15rem;
    }

    .round-nav-btn,
    .theme-toggle {
        width: 36px;
        height: 36px;
        min-height: 36px;
    }

    body.chat-active .app-container {
        padding: 0.4rem 0.55rem max(0.3rem, env(safe-area-inset-bottom));
    }

    body.chat-active .header {
        padding: 0.3rem 0;
        margin-bottom: 0.4rem;
        gap: 0.5rem;
    }

    body.chat-active .logo {
        font-size: 1.08rem;
    }

    body.chat-active .round-nav-btn,
    body.chat-active .theme-toggle {
        width: 32px;
        height: 32px;
        min-height: 32px;
    }

    body.chat-active .theme-icon {
        width: 14px;
        height: 14px;
    }

    .site-dialog {
        align-items: flex-end;
        padding: 0.6rem;
    }

    .site-dialog-panel {
        width: 100%;
        border-bottom-left-radius: var(--radius-lg);
        border-bottom-right-radius: var(--radius-lg);
        padding: 1.2rem 1rem calc(1rem + env(safe-area-inset-bottom));
    }

    .site-dialog-title {
        font-size: 1.15rem;
    }

    .site-dialog-actions {
        flex-direction: column-reverse;
    }

    .site-dialog-btn {
        width: 100%;
    }
}

/* ── Chat-active override ── */
body.chat-active {
    height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
    position: fixed;
    inset: 0;
    width: 100%;
}

body.chat-active .app-container {
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
}

body.dialog-open {
    overflow: hidden;
}

html.view-switching .app-container,
html.view-switching .header,
html.view-switching .logo,
html.view-switching .round-nav-btn,
html.view-switching .theme-toggle,
html.view-switching .theme-icon,
body.view-switching .app-container,
body.view-switching .header,
body.view-switching .logo,
body.view-switching .round-nav-btn,
body.view-switching .theme-toggle,
body.view-switching .theme-icon {
    transition: none !important;
}
