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

:root {
    --bg: #f4f4f5;
    --bg-secondary: #f8f9fa;
    --text: #1a1a2e;
    --text-secondary: #555;
    --accent: #2563eb;
    --accent-hover: #1d4ed8;
    --border: #e2e8f0;
    --card-bg: #ffffff;
    --card-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
    --code-bg: #f1f5f9;
    --blockquote-border: #2563eb;
    --tag-bg: #e0e7ff;
    --tag-text: #3730a3;
    --footer-bg: #f8f9fa;
    --header-bg: #d7d7d7;
    --header-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
    --table-stripe: #f8f9fa;
    --copy-button-bg: #94a3b8;
    --copy-button-hover-bg: #8492a6;
    --copy-button-text: #0f172a;
}

[data-theme="dark"] {
    --bg: #0f172a;
    --bg-secondary: #1e293b;
    --text: #e2e8f0;
    --text-secondary: #94a3b8;
    --accent: #60a5fa;
    --accent-hover: #93bbfc;
    --border: #334155;
    --card-bg: #1e293b;
    --card-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    --code-bg: #2f3948;
    --blockquote-border: #60a5fa;
    --tag-bg: #1e3a5f;
    --tag-text: #93c5fd;
    --footer-bg: #1e293b;
    --header-bg: #1e293b;
    --header-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    --table-stripe: #1e293b;
    --copy-button-bg: #0f172a;
    --copy-button-hover-bg: #1e293b;
    --copy-button-text: #e2e8f0;
}

html {
    font-size: 16px;
    scroll-behavior: smooth;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background-color: var(--bg);
    color: var(--text);
    line-height: 1.7;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    transition: background-color 0.2s, color 0.2s;
}

a {
    color: var(--accent);
    text-decoration: none;
    transition: color 0.15s;
}

a:hover {
    color: var(--accent-hover);
}

.container {
    width: 100%;
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 1.5rem;
}

/* Header */
.site-header {
    background: var(--header-bg);
    box-shadow: var(--header-shadow);
    transition: background-color 0.2s;
    margin-bottom: 20px;
}

.site-header-inner {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem 1rem;
    min-height: 3.5rem;
    padding-top: 0.5rem;
    padding-bottom: 0.5rem;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.site-header-search {
    flex: 1 1 16rem;
    min-width: 0;
    max-width: 38rem;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 0.4rem;
}

.header-fulltext-form {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    width: 100%;
}

.header-fulltext-form .category-search-input {
    flex: 1 1 auto;
}

.category-search-keyword-row {
    width: 100%;
    min-width: 0;
}

.category-search-keyword-input {
    width: 100%;
}

.category-search-top-row {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: flex-end;
    gap: 0.5rem 0.65rem;
    min-width: 0;
}

.category-search-chips {
    display: flex;
    flex-wrap: nowrap;
    gap: 0.35rem;
    align-items: center;
    flex: 0 1 auto;
    max-width: min(20rem, 50vw);
    min-width: 0;
    overflow-x: auto;
    overflow-y: hidden;
    min-height: 2rem;
    scrollbar-width: thin;
}

.category-search-chips::-webkit-scrollbar {
    height: 4px;
}

.category-search-chips::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 2px;
}

.category-search-chip {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    max-width: 100%;
}

.category-search-chip-remove {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.25rem;
    height: 1.25rem;
    padding: 0;
    border: none;
    border-radius: 999px;
    background: var(--border);
    color: var(--text);
    font-size: 1rem;
    line-height: 1;
    cursor: pointer;
    flex-shrink: 0;
    transition: background-color 0.15s, color 0.15s;
}

.category-search-chip-remove:hover {
    background: var(--accent);
    color: var(--card-bg);
}

.category-search-controls {
    display: flex;
    flex-wrap: nowrap;
    align-items: stretch;
    gap: 0.5rem;
    flex: 0 0 auto;
}

.category-search-combo {
    position: relative;
    flex: 0 0 auto;
    width: 11rem;
    max-width: 11rem;
    min-width: 11rem;
}

.category-search-input {
    width: 100%;
    padding: 0.4rem 0.45rem;
    text-overflow: ellipsis;
    font: inherit;
    font-size: 0.9rem;
    border: 1px solid var(--border);
    border-radius: 0.35rem;
    background: var(--card-bg);
    color: var(--text);
    transition: border-color 0.15s;
}

.category-search-input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.2);
}

[data-theme="dark"] .category-search-input:focus {
    box-shadow: 0 0 0 2px rgba(96, 165, 250, 0.25);
}

.category-search-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    z-index: 200;
    margin: 0.15rem 0 0;
    padding: 0.25rem 0;
    list-style: none;
    max-height: 12rem;
    overflow-y: auto;
    border: 1px solid var(--border);
    border-radius: 0.35rem;
    background: var(--card-bg);
    box-shadow: var(--card-shadow);
    transition: opacity 0.15s ease, transform 0.15s ease;
}

.category-search-dropdown[hidden] {
    display: none;
}

.category-search-dropdown:not([hidden]) {
    opacity: 1;
    transform: translateY(0);
}

.category-search-option {
    padding: 0.4rem 0.65rem;
    font-size: 0.9rem;
    cursor: pointer;
    color: var(--text);
}

.category-search-option:hover,
.category-search-option:focus {
    background: var(--bg-secondary);
    outline: none;
}

.category-search-option.category-search-option-highlighted {
    background: var(--bg-secondary);
    box-shadow: inset 3px 0 0 var(--accent);
}

.category-search-button {
    padding: 0.4rem 0.85rem;
    font: inherit;
    font-size: 0.9rem;
    font-weight: 600;
    border: none;
    border-radius: 0.35rem;
    background: var(--accent);
    color: #fff;
    cursor: pointer;
    white-space: nowrap;
    transition: background-color 0.15s;
}

.category-search-button:hover {
    background: var(--accent-hover);
}

.category-search-button:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.site-logo {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 1.25rem;
    line-height: 1;
    font-weight: 700;
    color: var(--text);
}

.site-logo-icon {
    height: 3em;
    width: auto;
    display: block;
    flex-shrink: 0;
}

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

.site-nav {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.theme-toggle {
    background: transparent;
    border: none;
    padding: 0;
    cursor: pointer;
    font: inherit;
    color: inherit;
}

.theme-toggle:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 3px;
    border-radius: 999px;
}

.theme-toggle-track {
    position: relative;
    display: inline-flex;
    align-items: center;
    width: 7.5rem;
    height: 2rem;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--bg-secondary);
    padding: 0 0.5rem;
    transition: border-color 0.2s, background-color 0.2s;
}

.theme-toggle:hover .theme-toggle-track {
    border-color: var(--accent);
}

.theme-toggle-label {
    position: relative;
    z-index: 1;
    width: 50%;
    font-size: 0.75rem;
    font-weight: 600;
    text-align: center;
    transition: color 0.2s;
}

.theme-toggle-label-light {
    color: #ffffff;
}

.theme-toggle-label-dark {
    color: var(--text-secondary);
}

.theme-toggle-thumb {
    position: absolute;
    top: 0.2rem;
    left: 0.2rem;
    width: calc(50% - 0.2rem);
    height: calc(100% - 0.4rem);
    border-radius: 999px;
    background: var(--accent);
    transition: transform 0.2s ease, background-color 0.2s;
}

[data-theme="dark"] .theme-toggle-label-light {
    color: var(--text-secondary);
}

[data-theme="dark"] .theme-toggle-label-dark {
    color: #ffffff;
}

[data-theme="dark"] .theme-toggle-thumb {
    transform: translateX(100%);
}

/* Main */
main {
    flex: 1;
    padding: 2rem 1.5rem;
}

/* Article list */
.home-categories {
    margin-bottom: 1.75rem;
}

.home-category-search {
    margin-bottom: 0.7rem;
}

.category-search-top-row-compact {
    justify-content: flex-start;
    gap: 0.35rem 0.5rem;
}

.category-search-chips-compact {
    min-height: 1.6rem;
    max-width: 100%;
}

.category-search-chips-compact .category-search-chip {
    font-size: 0.72rem;
}

.category-search-chips-compact .category-search-chip-remove {
    width: 1.05rem;
    height: 1.05rem;
    font-size: 0.85rem;
}

.category-search-combo-compact {
    width: 9rem;
    max-width: 9rem;
    min-width: 9rem;
}

.category-search-input-compact {
    padding: 0.3rem 0.4rem;
    font-size: 0.82rem;
    border-radius: 0.3rem;
}

.category-search-button-compact {
    padding: 0.3rem 0.65rem;
    font-size: 0.82rem;
    border-radius: 0.3rem;
}

.category-search-dropdown-compact .category-search-option {
    padding: 0.32rem 0.55rem;
    font-size: 0.82rem;
}

.home-categories-title {
    font-size: 1.1rem;
    margin-bottom: 0.75rem;
}

.home-categories-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.45rem;
}

.article-list {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.article-card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 0.75rem;
    padding: 1.5rem;
    box-shadow: var(--card-shadow);
    transition: background-color 0.2s, box-shadow 0.2s;
}

.article-card:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.article-card-title {
    font-size: 1.25rem;
    line-height: 1.4;
    margin-bottom: 0.5rem;
}

.article-card-title a {
    color: var(--text);
}

.article-card-title a:hover {
    color: var(--accent);
}

.article-meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.75rem;
    font-size: 0.875rem;
    color: var(--text-secondary);
    margin-bottom: 0.75rem;
}

.article-categories {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
}

.category-tag {
    display: inline-block;
    background: var(--tag-bg);
    color: var(--tag-text);
    font-size: 0.75rem;
    padding: 0.15rem 0.55rem;
    border-radius: 999px;
    font-weight: 500;
    transition: background-color 0.15s;
}

.category-tag:hover {
    filter: brightness(0.9);
    color: var(--tag-text);
}

.article-excerpt {
    color: var(--text-secondary);
    font-size: 0.95rem;
    margin-bottom: 0.75rem;
}

.read-more {
    font-size: 0.875rem;
    font-weight: 600;
}

/* Pagination */
.pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    margin-top: 2.5rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--border);
}

.pagination-link {
    font-weight: 600;
    font-size: 0.9rem;
    padding: 0.4rem 1rem;
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    transition: background-color 0.15s, border-color 0.15s;
}

.pagination-link:hover {
    background: var(--bg-secondary);
    border-color: var(--accent);
}

.pagination-info {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

/* Article full */
.article-full {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 0.75rem;
    padding: 1.5rem;
    box-shadow: var(--card-shadow);
    transition: background-color 0.2s, box-shadow 0.2s;
    max-width: 100%;
    margin-top: 20px;
}

.article-header {
    margin-bottom: 0.4rem;
    padding-bottom: 0;
    border-bottom: none;
}

.article-full .article-header {
    margin-bottom: 0.4rem;
}

.article-full .article-meta {
    margin-bottom: 0;
}

.article-content h1 {
    font-size: 2rem;
    line-height: 1.3;
    margin-bottom: 1rem;
    margin-top: 0;
}

.article-content h2 {
    font-size: 1.5rem;
    margin-top: 2rem;
    margin-bottom: 0.75rem;
    padding-bottom: 0.3rem;
    border-bottom: 1px solid var(--border);
}

.article-content h3 {
    font-size: 1.25rem;
    margin-top: 1.5rem;
    margin-bottom: 0.5rem;
}

.article-content h4,
.article-content h5,
.article-content h6 {
    margin-top: 1.25rem;
    margin-bottom: 0.5rem;
}

.article-content p {
    margin-bottom: 1rem;
}

.article-content img {
    max-width: 100%;
    height: auto;
    border-radius: 0.5rem;
    margin: 1rem 0;
    cursor: zoom-in;
    transition: opacity 0.2s;
}

.article-content img:hover {
    opacity: 0.9;
}

/* Lightbox styles */
.lightbox-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.9);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 9999;
    opacity: 0;
    transition: opacity 0.3s ease;
    cursor: zoom-out;
}

.lightbox-overlay.active {
    display: flex;
    opacity: 1;
}

.lightbox-content {
    max-width: 90%;
    max-height: 90%;
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    cursor: default;
}

.lightbox-image {
    max-width: 100%;
    max-height: 90vh;
    object-fit: contain;
    border-radius: 4px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
    user-select: none;
    cursor: zoom-in;
}

.lightbox-close {
    position: absolute;
    top: -40px;
    right: 0;
    color: white;
    font-size: 30px;
    background: none;
    border: none;
    cursor: pointer;
    line-height: 1;
    padding: 10px;
    transition: transform 0.2s;
}

.lightbox-close:hover {
    transform: scale(1.1);
}

.lightbox-nav {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(255, 255, 255, 0.1);
    color: white;
    border: none;
    padding: 20px 15px;
    cursor: pointer;
    font-size: 24px;
    border-radius: 4px;
    transition: background 0.2s;
    user-select: none;
}

.lightbox-nav:hover {
    background: rgba(255, 255, 255, 0.2);
}

.lightbox-prev {
    left: -60px;
}

.lightbox-next {
    right: -60px;
}

@media (max-width: 768px) {
    .lightbox-prev {
        left: 10px;
        background: rgba(0, 0, 0, 0.3);
    }
    .lightbox-next {
        right: 10px;
        background: rgba(0, 0, 0, 0.3);
    }
    .lightbox-close {
        top: 10px;
        right: 10px;
        background: rgba(0, 0, 0, 0.3);
        border-radius: 50%;
        width: 40px;
        height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
}

.article-content a {
    text-decoration: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 2px;
}

.article-content ul,
.article-content ol {
    margin-bottom: 1rem;
    padding-left: 1.5rem;
}

.article-content li {
    margin-bottom: 0.25rem;
}

.article-content blockquote {
    border-left: 3px solid var(--blockquote-border);
    padding: 0.5rem 1rem;
    margin: 1rem 0;
    background: var(--bg-secondary);
    border-radius: 0 0.5rem 0.5rem 0;
}

.article-content blockquote p:last-child {
    margin-bottom: 0;
}

.article-content pre {
    background: var(--code-bg);
    padding: 0.5rem 0.5rem;
    overflow-x: auto;
    scrollbar-width: thin;
    scrollbar-color: #94a3b8 #e2e8f0;
    margin: 0;
    font-family: "SF Mono", "Fira Code", "Fira Mono", "Roboto Mono", Consolas, monospace;
    font-size: 0.875rem;
    line-height: 1.5;
    position: relative;
}

.article-content pre::-webkit-scrollbar,
.article-content pre code::-webkit-scrollbar {
    height: 0.35rem;
}

.article-content pre::-webkit-scrollbar-track,
.article-content pre code::-webkit-scrollbar-track {
    background: #e2e8f0;
}

.article-content pre::-webkit-scrollbar-thumb,
.article-content pre code::-webkit-scrollbar-thumb {
    background: #94a3b8;
    border: 1px solid #e2e8f0;
    border-radius: 999px;
}

.article-content pre code {
    scrollbar-width: thin;
    scrollbar-color: #94a3b8 #e2e8f0;
}

[data-theme="dark"] .article-content pre {
    scrollbar-color: #64748b var(--code-bg);
}

[data-theme="dark"] .article-content pre code {
    scrollbar-color: #64748b var(--code-bg);
}

[data-theme="dark"] .article-content pre::-webkit-scrollbar-track,
[data-theme="dark"] .article-content pre code::-webkit-scrollbar-track {
    background: var(--code-bg);
}

[data-theme="dark"] .article-content pre::-webkit-scrollbar-thumb,
[data-theme="dark"] .article-content pre code::-webkit-scrollbar-thumb {
    background: #64748b;
    border-color: var(--code-bg);
}

.code-container {
    margin: 1.5rem 0;
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    background: var(--code-bg);
    overflow: hidden;
    position: relative;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: stretch;
}

.code-container.has-line-numbers {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-areas: "numbers code";
}

.code-container .copy-code-button {
    grid-column: 2;
    grid-row: 1;
    justify-self: end;
    align-self: start;
}

.code-container .line-numbers {
    grid-area: numbers;
    padding: 0.5rem 0.75rem;
    border-right: 1px solid var(--border);
    background: var(--bg-secondary);
    color: var(--text-secondary);
    font-family: "SF Mono", "Fira Code", "Fira Mono", "Roboto Mono", Consolas, monospace;
    font-size: 0.875rem;
    line-height: 1.5;
    text-align: right;
    user-select: none;
    display: flex;
    flex-direction: column;
}

.code-container pre {
    grid-column: 1;
    grid-row: 1;
    border: none;
    border-radius: 0;
    min-width: 0;
}

.code-container.has-line-numbers .copy-code-button {
    grid-column: auto;
    grid-row: auto;
    position: absolute;
    top: 0;
    right: 0;
}

.code-container.has-line-numbers pre {
    grid-area: code;
    grid-column: auto;
    grid-row: auto;
}

.copy-code-button {
    margin: 0.25rem 0.25rem;
    padding: 0.25rem 0.75rem;
    font-size: 0.75rem;
    font-family: inherit;
    color: var(--copy-button-text);
    background: var(--copy-button-bg);
    border: 1px solid var(--border);
    border-radius: 0.25rem;
    cursor: pointer;
    opacity: 0.8;
    z-index: 10;
}

.copy-code-button:hover {
    opacity: 1;
    background: var(--copy-button-hover-bg);
    color: var(--copy-button-text);
}

.copy-code-button.copied {
    color: #10b981;
    border-color: #10b981;
    opacity: 1;
}

.article-content code {
    font-family: "SF Mono", "Fira Code", "Fira Mono", "Roboto Mono", Consolas, monospace;
    font-size: 0.875rem;
}

.article-content pre code {
    font-family: inherit;
    font-size: 0.875rem;
}

.article-content p code,
.article-content li code {
    background: var(--code-bg);
    padding: 0.15rem 0.35rem;
    border-radius: 0.25rem;
    border: 1px solid var(--border);
}

.article-content hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 2rem 0;
}

.article-content table {
    width: 100%;
    border-collapse: collapse;
    margin: 1rem 0;
    font-size: 0.9rem;
}

.article-content th,
.article-content td {
    border: 1px solid var(--border);
    padding: 0.5rem 0.75rem;
    text-align: left;
}

.article-content th {
    background: var(--bg-secondary);
    font-weight: 600;
}

.article-content tr:nth-child(even) td {
    background: var(--table-stripe);
}

/* Category heading */
.category-heading,
.search-heading {
    font-size: 1.75rem;
    margin-bottom: 1.5rem;
    padding-bottom: 0.75rem;
    border-bottom: 2px solid var(--accent);
}

.search-selected-intro {
    margin-bottom: 1.25rem;
    color: var(--text-secondary);
    line-height: 1.6;
}

.search-selected-intro .category-tag {
    margin-left: 0.25rem;
}

/* 404 */
.error-page {
    text-align: center;
    padding: 4rem 1rem;
}

.error-page h1 {
    font-size: 6rem;
    font-weight: 800;
    color: var(--text-secondary);
    line-height: 1;
    margin-bottom: 1rem;
}

.error-page p {
    font-size: 1.25rem;
    color: var(--text-secondary);
    margin-bottom: 2rem;
}

.back-home {
    font-weight: 600;
    font-size: 1rem;
}

.no-results {
    text-align: center;
    color: var(--text-secondary);
    padding: 3rem 1rem;
    font-size: 1.1rem;
}

/* Footer */
.site-footer {
    background: var(--footer-bg);
    border-top: 1px solid var(--border);
    padding: 1.5rem 0;
    text-align: center;
    font-size: 0.875rem;
    color: var(--text-secondary);
    transition: background-color 0.2s;
}

/* Responsive */
@media (max-width: 900px) {
    .site-header-search {
        flex: 1 1 100%;
        max-width: none;
        order: 3;
        width: 100%;
    }

    .site-logo {
        order: 1;
    }

    .site-nav {
        order: 2;
    }
}

@media (max-width: 640px) {
    .site-logo-icon {
        height: 2em;
    }

    .article-card {
        padding: 1rem;
    }

    .article-content h1 {
        font-size: 1.5rem;
    }

    .article-content h2 {
        font-size: 1.25rem;
    }

    .article-content pre {
        font-size: 0.8rem;
        padding: 0.75rem;
    }

    .error-page h1 {
        font-size: 4rem;
    }

    .pagination {
        flex-direction: column;
        gap: 0.5rem;
    }

    .category-search-controls {
        flex-wrap: wrap;
    }

    .category-search-combo-compact {
        width: min(16rem, 100%);
        max-width: 100%;
        min-width: 0;
    }
}

/* highlight.js overrides to respect theme */
[data-theme="dark"] .hljs {
    background: var(--code-bg) !important;
}

:root .hljs {
    background: var(--code-bg) !important;
    padding: 0 !important;
}
