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

:root {
    --bg: #fdfdfb;
    --text: #252b37;
    --text-muted: #6b7280;
    --placeholder: #c4c9d4;
    --border: #e5e7eb;
    --card-bg: #ffffff;
    --card-hover: #f9fafb;
    --accent: #3b82f6;
    --danger: #ef4444;
    --shadow: 0 1px 3px rgba(0,0,0,0.08);
    --shadow-hover: 0 4px 12px rgba(0,0,0,0.1);
    --radius: 12px;
    --font: system-ui, -apple-system, "Segoe UI", sans-serif;
    --font-mono: ui-monospace, "SF Mono", "Cascadia Code", "Segoe UI Mono", Menlo, Monaco, Consolas, monospace;
    --code-bg: #f3f4f6;
    --caret: #3b82f6;
    --selection-bg: var(--text);
    --selection-text: var(--bg);
}

/* Light mode explicit */
:root[data-theme="light"] {
    --bg: #fdfdfb;
    --text: #252b37;
    --text-muted: #6b7280;
    --placeholder: #c4c9d4;
    --border: #e5e7eb;
    --card-bg: #ffffff;
    --card-hover: #f9fafb;
    --accent: #3b82f6;
    --danger: #ef4444;
    --shadow: 0 1px 3px rgba(0,0,0,0.08);
    --shadow-hover: 0 4px 12px rgba(0,0,0,0.1);
    --code-bg: #f3f4f6;
    --caret: #3b82f6;
    --selection-bg: var(--text);
    --selection-text: var(--bg);
}

/* Dark mode explicit */
:root[data-theme="dark"] {
    --bg: #0f0f0f;
    --text: #e5e5e5;
    --text-muted: #9ca3af;
    --placeholder: #4b5563;
    --border: #2a2a2a;
    --card-bg: #1a1a1a;
    --card-hover: #222222;
    --accent: #60a5fa;
    --danger: #f87171;
    --shadow: 0 1px 3px rgba(0,0,0,0.3);
    --shadow-hover: 0 4px 12px rgba(0,0,0,0.4);
    --code-bg: #252525;
    --caret: #60a5fa;
    --selection-bg: var(--text);
    --selection-text: var(--bg);
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #0f0f0f;
        --text: #e5e5e5;
        --text-muted: #9ca3af;
        --placeholder: #4b5563;
        --border: #2a2a2a;
        --card-bg: #1a1a1a;
        --card-hover: #222222;
        --accent: #60a5fa;
        --danger: #f87171;
        --shadow: 0 1px 3px rgba(0,0,0,0.3);
        --shadow-hover: 0 4px 12px rgba(0,0,0,0.4);
        --code-bg: #252525;
        --caret: #60a5fa;
        --selection-bg: var(--text);
        --selection-text: var(--bg);
    }
}

/* Selection styling */
::selection {
    background: var(--selection-bg);
    color: var(--selection-text);
}

::-moz-selection {
    background: var(--selection-bg);
    color: var(--selection-text);
}

html {
    font-size: 16px;
}

body {
    margin: 0;
    padding: 0;
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
}

/* Container */
.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 2rem 1.5rem;
    padding-top: calc(2rem + env(safe-area-inset-top));
    padding-left: calc(1.5rem + env(safe-area-inset-left));
    padding-right: calc(1.5rem + env(safe-area-inset-right));
    padding-bottom: calc(2rem + env(safe-area-inset-bottom));
}

/* Header */
header {
    margin-bottom: 2rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

header h1 {
    font-size: 1.5rem;
    font-weight: 600;
    margin: 0;
    color: var(--text);
}

.logout-link {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 8px;
    color: var(--text-muted);
    text-decoration: none;
    transition: all 0.15s ease;
}

.can-hover .logout-link:hover {
    background: var(--border);
    color: var(--text);
}

/* Grid */
.grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 1rem;
}

@media (min-width: 640px) {
    .grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 1.25rem;
    }
}

@media (min-width: 1024px) {
    .grid {
        grid-template-columns: repeat(4, 1fr);
        gap: 1.5rem;
    }
}

/* Cards */
.card {
    aspect-ratio: 3 / 4;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    cursor: pointer;
    text-decoration: none;
    color: inherit;
    display: block;
    box-shadow: var(--shadow);
}

.can-hover .card:hover {
    box-shadow: var(--shadow-hover);
    transform: translateY(-3px);
    border-color: #d1d5db;
}

.card:active {
    transform: scale(0.98);
}

/* New Card */
.new-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    border: 2px dashed var(--border);
    background: transparent;
    font-family: inherit;
    font-size: 0.875rem;
    color: var(--text-muted);
}

.can-hover .new-card:hover {
    border-color: var(--accent);
    color: var(--accent);
    background: rgba(59, 130, 246, 0.04);
}

.new-icon {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--card-bg);
    display: flex;
    align-items: center;
    justify-content: center;
}

.can-hover .new-card:hover .new-icon {
    background: rgba(96, 165, 250, 0.15);
}

/* Note Card Preview */
.note-card {
    position: relative;
}

/* Publishing Badges */
.publish-badge {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: #22c55e;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 5;
    color: white;
    box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.publish-badge.modified {
    background: #f59e0b;
}

.preview-container {
    width: 100%;
    height: 100%;
    overflow: hidden;
    padding: 0.75rem;
}

.preview-content {
    transform: scale(0.35);
    transform-origin: top left;
    width: 285%;
    height: 285%;
    font-size: 1rem;
    line-height: 1.6;
    pointer-events: none;
}

.preview-content h1 {
    font-size: 1.75rem;
    font-weight: 700;
    margin: 0 0 0.5rem 0;
    line-height: 1.2;
}

.preview-content .excerpt {
    color: var(--text-muted);
    font-size: 0.95rem;
    margin-bottom: 1rem;
    font-style: italic;
}

.preview-content .body {
    font-size: 0.95rem;
}

.preview-content .body p {
    margin: 0 0 1em 0;
}

.preview-content .body h1,
.preview-content .body h2,
.preview-content .body h3 {
    margin: 1.5em 0 0.5em 0;
}

.preview-content .body h1 { font-size: 1.5rem; }
.preview-content .body h2 { font-size: 1.25rem; }
.preview-content .body h3 { font-size: 1.1rem; }

.preview-content .body ul,
.preview-content .body ol {
    margin: 0 0 1em 0;
    padding-left: 1.5em;
}

.preview-content .body code {
    background: var(--code-bg);
    padding: 0.15em 0.3em;
    border-radius: 4px;
    font-size: 0.9em;
}

.preview-content .body pre {
    background: var(--code-bg);
    padding: 1em;
    border-radius: 8px;
    overflow-x: auto;
}

.preview-content .body pre code {
    background: none;
    padding: 0;
}

.preview-content .body blockquote {
    margin: 0 0 1em 0;
    padding-left: 1em;
    border-left: 3px solid var(--border);
    color: var(--text-muted);
}

.preview-content .body a,
.preview-content .body a:visited {
    color: var(--accent);
}

.preview-content.empty {
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    font-style: italic;
}

/* Edit Page */
.edit-page {
    min-height: 100vh;
    overflow-anchor: none;
}

.edit-page * {
    overflow-anchor: none;
}

.edit-container {
    max-width: 680px;
    margin: 0 auto;
    padding: 1rem 1.5rem 0;
    padding-left: calc(1.5rem + env(safe-area-inset-left));
    padding-right: calc(1.5rem + env(safe-area-inset-right));
    min-height: 100vh;
}

.edit-header {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.5rem 0 1.5rem;
    margin-top: env(safe-area-inset-top);
    position: sticky;
    top: env(safe-area-inset-top);
    background: var(--bg);
    z-index: 10;
}

/* Cover the safe area above sticky header when scrolled */
.edit-page::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: env(safe-area-inset-top);
    background: var(--bg);
    z-index: 11;
}

.back-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 8px;
    color: var(--text-muted);
    text-decoration: none;
    transition: all 0.15s ease;
    -webkit-tap-highlight-color: transparent;
}

.can-hover .back-btn:hover {
    background: var(--border);
    color: var(--text);
}

.back-btn:active {
    opacity: 0.7;
}

.save-status {
    flex: 1;
    font-size: 0.8rem;
    color: var(--text-muted);
    text-align: center;
}

.save-status.saving {
    color: var(--accent);
}

.preview-btn,
.delete-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 8px;
    border: none;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    transition: all 0.15s ease;
    -webkit-tap-highlight-color: transparent;
}

.can-hover .preview-btn:hover {
    background: var(--border);
    color: var(--text);
}

.can-hover .delete-btn:hover {
    background: rgba(239, 68, 68, 0.1);
    color: var(--danger);
}

.preview-btn:active,
.delete-btn:active {
    opacity: 0.7;
}

/* Publish Button */
.publish-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 8px;
    border: none;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    transition: all 0.15s ease;
    -webkit-tap-highlight-color: transparent;
}

.can-hover .publish-btn:hover {
    background: var(--border);
    color: var(--text);
}

.publish-btn:active {
    opacity: 0.7;
}

.publish-btn.published {
    color: #22c55e;
}

.can-hover .publish-btn.published:hover {
    background: rgba(34, 197, 94, 0.1);
    color: #22c55e;
}

.publish-btn.has-changes {
    color: #f59e0b;
}

.can-hover .publish-btn.has-changes:hover {
    background: rgba(245, 158, 11, 0.1);
    color: #f59e0b;
}

/* Form Fields */
.edit-form {
    display: flex;
    flex-direction: column;
}

.field {
    width: 100%;
    border: none;
    background: transparent;
    font-family: var(--font);
    color: var(--text);
    resize: none;
    outline: none;
    overflow: hidden;
    overflow-anchor: none;
    caret-color: var(--caret);
}

.field::placeholder {
    color: var(--placeholder);
}

.field-title {
    font-size: 2rem;
    font-weight: 700;
    line-height: 1.2;
    padding: 0;
    margin-bottom: 0.5rem;
    min-height: 2.4rem;
}

.field-excerpt {
    font-size: 1.1rem;
    line-height: 1.5;
    color: var(--text-muted);
    font-style: italic;
    padding: 0;
    padding-bottom: 0.75rem;
    margin-bottom: 1rem;
    border-bottom: 1px solid var(--border);
    min-height: 1.65rem;
}

.field-body {
    font-family: var(--font-mono);
    font-size: 0.95rem;
    line-height: 1.8;
    padding: 0;
    padding-bottom: calc(60vh + env(safe-area-inset-bottom));
    min-height: 60vh;
    caret-color: var(--caret);
    letter-spacing: -0.01em;
}

/* Animation */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.card {
    animation: fadeIn 0.3s ease forwards;
}

.grid .card:nth-child(1) { animation-delay: 0s; }
.grid .card:nth-child(2) { animation-delay: 0.05s; }
.grid .card:nth-child(3) { animation-delay: 0.1s; }
.grid .card:nth-child(4) { animation-delay: 0.15s; }
.grid .card:nth-child(5) { animation-delay: 0.2s; }
.grid .card:nth-child(6) { animation-delay: 0.25s; }
.grid .card:nth-child(7) { animation-delay: 0.3s; }
.grid .card:nth-child(8) { animation-delay: 0.35s; }

/* View Transitions */
@view-transition {
    navigation: auto;
}

::view-transition-old(root),
::view-transition-new(root) {
    animation-duration: 0.3s;
    animation-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
}

/* Note card to editor transition */
::view-transition-old(note-*):only-child {
    animation: scale-down 0.3s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

::view-transition-new(note-*):only-child {
    animation: scale-up 0.3s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

@keyframes scale-down {
    from {
        opacity: 1;
        transform: scale(1);
    }
    to {
        opacity: 0;
        transform: scale(0.95);
    }
}

@keyframes scale-up {
    from {
        opacity: 0;
        transform: scale(0.95);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

/* Smooth morph for matched elements */
::view-transition-group(*) {
    animation-duration: 0.35s;
    animation-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
}

::view-transition-image-pair(*) {
    isolation: isolate;
}

::view-transition-old(*),
::view-transition-new(*) {
    animation: none;
    mix-blend-mode: normal;
}

/* Crossfade the content while morphing */
::view-transition-old(*) {
    animation: fade-out 0.2s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

::view-transition-new(*) {
    animation: fade-in 0.25s cubic-bezier(0.4, 0, 0.2, 1) 0.1s forwards;
    opacity: 0;
}

@keyframes fade-out {
    from { opacity: 1; }
    to { opacity: 0; }
}

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

/* Reduce motion for accessibility */
@media (prefers-reduced-motion: reduce) {
    ::view-transition-group(*),
    ::view-transition-old(*),
    ::view-transition-new(*) {
        animation: none !important;
    }
}


/* Modal */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 100;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.modal-overlay.active {
    display: flex;
}

.modal {
    background: var(--card-bg);
    border-radius: var(--radius);
    padding: 1.5rem;
    max-width: 400px;
    width: 100%;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    animation: modal-in 0.2s ease;
}

@keyframes modal-in {
    from {
        opacity: 0;
        transform: scale(0.95) translateY(10px);
    }
    to {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

.modal h2 {
    margin: 0 0 0.5rem 0;
    font-size: 1.25rem;
    font-weight: 600;
}

.modal-description {
    margin: 0 0 1.25rem 0;
    color: var(--text-muted);
    font-size: 0.9rem;
    line-height: 1.5;
}

.slug-preview {
    display: flex;
    align-items: center;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0 0.75rem;
    margin-bottom: 0.5rem;
}

.slug-prefix,
.slug-suffix {
    color: var(--text-muted);
    font-size: 0.9rem;
    font-family: ui-monospace, monospace;
}

.slug-input {
    flex: 1;
    border: none;
    background: transparent;
    color: var(--text);
    font-size: 0.95rem;
    font-family: ui-monospace, monospace;
    padding: 0.75rem 0.25rem;
    outline: none;
    min-width: 0;
}

.slug-input::placeholder {
    color: var(--placeholder);
}

.modal-error {
    color: var(--danger);
    font-size: 0.85rem;
    min-height: 1.25rem;
    margin-bottom: 0.75rem;
}

.modal-actions {
    display: flex;
    gap: 0.75rem;
    justify-content: flex-end;
}

.modal-btn {
    padding: 0.6rem 1.25rem;
    border-radius: 8px;
    border: none;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s ease;
}

.modal-btn-cancel {
    background: var(--border);
    color: var(--text);
}

.can-hover .modal-btn-cancel:hover {
    background: var(--placeholder);
}

.modal-btn-primary {
    background: var(--accent);
    color: white;
}

.can-hover .modal-btn-primary:hover {
    background: #2563eb;
}

.modal-btn-danger {
    background: var(--danger);
    color: white;
}

.can-hover .modal-btn-danger:hover {
    background: #dc2626;
}

.modal-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}


/* Theme Toast */
.theme-toast {
    position: fixed;
    bottom: 2rem;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: var(--card-bg);
    color: var(--text);
    padding: 0.6rem 1.25rem;
    border-radius: 99px;
    font-size: 0.85rem;
    font-weight: 500;
    box-shadow: var(--shadow-hover);
    border: 1px solid var(--border);
    opacity: 0;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.3s ease;
    z-index: 1000;
    pointer-events: none;
}

.theme-toast.visible {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
}
