:root {
    color-scheme: light;
    --bg: #ffffff;
    --fg: #212529;
    --muted-fg: #6c757d;
    --panel-bg: #f7f7f8;
    --input-bg: #ffffff;
    --input-fg: #212529;
    --input-border: #ced4da;
    --input-focus: rgba(52, 58, 64, 0.5);
    --btn-fg: #343a40;
    --btn-border: #343a40;
    --btn-hover-bg: #343a40;
    --btn-hover-fg: #ffffff;
    --success-fg: #28a745;
    --success-border: #28a745;
    --success-hover-bg: #28a745;
    --success-hover-fg: #ffffff;
    --secondary-fg: #6c757d;
    --secondary-border: #6c757d;
    --secondary-hover-bg: #6c757d;
    --secondary-hover-fg: #ffffff;
    --glow-color: 255, 193, 7;
    --border: #e5e7eb;
    --highlight-bg: rgba(52, 58, 64, 0.08);
    --sidebar-width: 280px;
}

[data-theme="dark"] {
    color-scheme: dark;
    --bg: #1a1a1a;
    --fg: #e8e8e8;
    --muted-fg: #9aa0a6;
    --panel-bg: #202020;
    --input-bg: #2a2a2a;
    --input-fg: #e8e8e8;
    --input-border: #3f3f3f;
    --input-focus: rgba(232, 232, 232, 0.4);
    --btn-fg: #e8e8e8;
    --btn-border: #888888;
    --btn-hover-bg: #e8e8e8;
    --btn-hover-fg: #1a1a1a;
    --success-fg: #4ade80;
    --success-border: #4ade80;
    --success-hover-bg: #4ade80;
    --success-hover-fg: #1a1a1a;
    --secondary-fg: #aaaaaa;
    --secondary-border: #666666;
    --secondary-hover-bg: #666666;
    --secondary-hover-fg: #ffffff;
    --glow-color: 147, 197, 253;
    --border: #2f2f2f;
    --highlight-bg: rgba(232, 232, 232, 0.08);
}

* {
    box-sizing: border-box;
}

html, body {
    height: 100%;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--fg);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    transition: background 0.2s ease, color 0.2s ease;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.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;
}

/* --- Topbar --- */
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--bg);
    gap: 12px;
}

.brand {
    display: inline-flex;
    align-items: center;
    text-decoration: none;
}

.brand-logo {
    height: 36px;
    width: auto;
    display: block;
}

.banner-dark {
    display: none;
}

[data-theme="dark"] .banner-light {
    display: none;
}

[data-theme="dark"] .banner-dark {
    display: block;
}

/* --- Theme toggle --- */
#themeToggle {
    width: 36px;
    height: 36px;
    border: 1px solid var(--btn-border);
    border-radius: 50%;
    background: var(--bg);
    color: var(--fg);
    font-size: 16px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.15s ease, background 0.2s ease, color 0.2s ease;
    flex-shrink: 0;
}

#themeToggle:hover {
    transform: scale(1.05);
}

#themeToggle.glow {
    animation: themeGlow 1.6s ease-in-out infinite;
}

@keyframes themeGlow {
    0%, 100% {
        box-shadow: 0 0 0 0 rgba(var(--glow-color), 0);
    }
    50% {
        box-shadow: 0 0 18px 4px rgba(var(--glow-color), 0.7);
    }
}

/* --- App layout --- */
.app {
    flex: 1;
    display: grid;
    grid-template-columns: var(--sidebar-width) 1fr;
    min-height: 0;
    transition: grid-template-columns 0.2s ease;
    position: relative;
}

.app.sidebar-collapsed {
    grid-template-columns: 0 1fr;
}

.sidebar {
    position: relative;
    background: var(--panel-bg);
    border-right: 1px solid var(--border);
    padding: 20px 18px;
    overflow-y: auto;
    overflow-x: hidden;
    min-width: 0;
    transition: padding 0.2s ease;
}

.app.sidebar-collapsed .sidebar {
    padding: 0;
    border-right: none;
    overflow: visible;
}

.app.sidebar-collapsed #userInput {
    display: none;
}

/* --- Sidebar collapse chevron --- */
.sidebar-toggle {
    position: absolute;
    top: 14px;
    left: calc(var(--sidebar-width) - 14px);
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--fg);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    z-index: 5;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12);
    transition: left 0.2s ease, background 0.15s ease, color 0.15s ease;
}

.app.sidebar-collapsed .sidebar-toggle {
    left: 6px;
}

.sidebar-toggle:hover {
    background: var(--btn-hover-bg);
    color: var(--btn-hover-fg);
}

.sidebar-toggle-glyph {
    font-size: 18px;
    line-height: 1;
    display: block;
    transition: transform 0.2s ease;
}

.app.sidebar-collapsed .sidebar-toggle-glyph {
    transform: rotate(180deg);
}

.canvas {
    padding: 24px;
    overflow: auto;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
    gap: 20px;
    min-width: 0;
}

/* --- Form controls --- */
#userInput {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

label {
    display: block;
    font-weight: 500;
    margin-top: 10px;
    margin-bottom: 4px;
    color: var(--fg);
    font-size: 0.9rem;
}

#userInput > label:first-of-type {
    margin-top: 0;
}

input[type="text"],
input[type="search"],
select {
    background: var(--input-bg);
    color: var(--input-fg);
    border: 1px solid var(--input-border);
    border-radius: 4px;
    padding: 6px 12px;
    font-family: inherit;
    font-size: 1rem;
    line-height: 1.5;
    width: 100%;
}

input[type="text"]:focus,
input[type="search"]:focus,
select:focus {
    outline: none;
    border-color: var(--btn-border);
    box-shadow: 0 0 0 0.1rem var(--input-focus);
}

input[type="color"] {
    width: 100%;
    height: 38px;
    padding: 2px;
    border: 1px solid var(--input-border);
    border-radius: 4px;
    background: var(--input-bg);
    cursor: pointer;
}

input[type="color"]:disabled {
    cursor: not-allowed;
    opacity: 0.5;
}

input[type="range"] {
    width: 100%;
    accent-color: var(--btn-border);
}

.checkbox-row {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 10px;
}

.checkbox-row label {
    margin: 0;
    font-weight: 400;
}

/* --- Shadow options --- */
.shadow-options {
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 10px 12px 14px;
    margin: 10px 0 0;
    background: var(--bg);
    display: flex;
    flex-direction: column;
    gap: 0;
}

.shadow-options[hidden] {
    display: none;
}

.shadow-options-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 6px;
}

.shadow-options-title {
    font-size: 0.8rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--muted-fg);
}

.link-button {
    background: transparent;
    border: none;
    padding: 0;
    color: var(--muted-fg);
    font: inherit;
    font-size: 0.8rem;
    cursor: pointer;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.link-button:hover {
    color: var(--fg);
}

.link-button:focus {
    outline: 1px dotted var(--fg);
    outline-offset: 2px;
}

.shadow-options label {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 8px;
    font-size: 0.85rem;
}

.range-value {
    font-variant-numeric: tabular-nums;
    color: var(--muted-fg);
    font-weight: 600;
}

/* --- Combobox --- */
.combobox {
    position: relative;
}

#fontInput {
    padding-right: 28px;
}

.combobox-clear {
    position: absolute;
    top: 50%;
    right: 6px;
    transform: translateY(-50%);
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: none;
    background: transparent;
    color: var(--muted-fg);
    font-size: 12px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.combobox-clear:hover {
    background: var(--highlight-bg);
    color: var(--fg);
}

.combobox-list {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    max-height: 220px;
    overflow-y: auto;
    background: var(--input-bg);
    border: 1px solid var(--input-border);
    border-radius: 4px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
    z-index: 10;
}

.combobox-list[hidden] {
    display: none;
}

.combobox-item {
    padding: 6px 10px;
    cursor: pointer;
    color: var(--input-fg);
    font-size: 0.95rem;
}

.combobox-item:hover,
.combobox-item.active {
    background: var(--highlight-bg);
}

.combobox-item.selected {
    font-weight: 600;
}

.combobox-empty {
    padding: 8px 10px;
    color: var(--muted-fg);
    font-style: italic;
    font-size: 0.9rem;
}

/* --- Buttons --- */
.btn {
    background: transparent;
    border: 1px solid var(--btn-border);
    color: var(--btn-fg);
    border-radius: 4px;
    padding: 6px 12px;
    font-family: inherit;
    font-size: 1rem;
    line-height: 1.5;
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease;
}

.btn:hover:not(:disabled) {
    background: var(--btn-hover-bg);
    color: var(--btn-hover-fg);
}

.btn:focus {
    outline: none;
    box-shadow: 0 0 0 0.1rem var(--input-focus);
}

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

.btn-sm {
    padding: 4px 10px;
    font-size: 0.85rem;
}

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

.btn-secondary:hover:not(:disabled) {
    background: var(--secondary-hover-bg);
    color: var(--secondary-hover-fg);
}

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

.btn-success:hover:not(:disabled) {
    background: var(--success-hover-bg);
    color: var(--success-hover-fg);
}

#buttons {
    margin-top: 18px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

#download {
    display: block;
    text-decoration: none;
}

#buttons .btn {
    width: 100%;
}

/* --- Featured preview --- */
#featuredPreview {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 16px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--panel-bg);
}

#featuredPreview[hidden],
#fontGrid[hidden] {
    display: none;
}

.featured-header {
    display: flex;
    align-items: center;
    gap: 8px;
}

.featured-label {
    flex: 1;
    font-size: 1rem;
    font-weight: 600;
    color: var(--fg);
    text-align: center;
}

.featured-nav {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 1px solid var(--input-border);
    background: transparent;
    color: var(--fg);
    font-size: 16px;
    line-height: 1;
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease;
    flex-shrink: 0;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

.featured-nav:hover {
    background: var(--btn-hover-bg);
    color: var(--btn-hover-fg);
}

.featured-save {
    text-decoration: none;
    flex-shrink: 0;
}

.featured-image {
    position: relative;
    min-height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: auto;
}

#result {
    max-width: 100%;
    height: auto;
    display: block;
}

.spinner-container {
    display: flex;
    justify-content: center;
    align-items: center;
    visibility: hidden;
    position: absolute;
    inset: 0;
}

.spinner-container.show {
    visibility: visible;
}

.spinner {
    width: 2rem;
    height: 2rem;
    border: 0.25em solid var(--input-border);
    border-top-color: var(--fg);
    border-radius: 50%;
    animation: spin 0.75s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* --- Font compare grid --- */
#fontGrid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 16px;
    width: 100%;
}

.font-card {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 12px;
    border: 1px solid var(--input-border);
    border-radius: 6px;
    background: var(--input-bg);
    cursor: pointer;
    transition: border-color 0.15s ease, transform 0.15s ease, box-shadow 0.15s ease;
}

.font-card:hover {
    border-color: var(--btn-border);
    transform: translateY(-2px);
}

.font-card.active {
    border-color: var(--success-border);
    box-shadow: 0 0 0 2px var(--success-border);
}

.font-card-label {
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--muted-fg);
    text-align: center;
}

.font-card-preview {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100px;
    max-height: 140px;
    overflow: hidden;
    border-radius: 4px;
}

.font-card-preview img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    display: block;
}

.font-card-preview.error {
    color: var(--muted-fg);
    font-size: 0.8rem;
    font-style: italic;
}

.font-card-preview.loading {
    position: relative;
}

.font-card-preview.loading::after {
    content: "";
    width: 1.25rem;
    height: 1.25rem;
    border: 0.2em solid var(--input-border);
    border-top-color: var(--fg);
    border-radius: 50%;
    animation: spin 0.75s linear infinite;
    position: absolute;
}

/* --- Mobile --- */
@media (max-width: 720px) {
    .app {
        grid-template-columns: 1fr;
    }
    .app.sidebar-collapsed {
        grid-template-columns: 1fr;
    }
    .sidebar {
        border-right: none;
        border-bottom: 1px solid var(--border);
        padding: 16px;
    }
    .app.sidebar-collapsed .sidebar {
        padding: 0;
        border-bottom: none;
        height: 0;
        overflow: hidden;
    }
    .sidebar-toggle {
        top: 8px;
        left: auto;
        right: 12px;
    }
    .app.sidebar-collapsed .sidebar-toggle {
        left: auto;
        right: 12px;
    }
    .canvas {
        padding: 16px;
    }
    .topbar {
        padding: 8px 12px;
    }
    .brand-logo {
        height: 28px;
    }
}
