* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --color-female: #c4553a;
    --color-male: #2a7f8e;
    --color-bg: #f5f0e8;
    --color-surface: #fffdf8;
    --color-text: #1c1917;
    --color-muted: #8c8478;
    --color-border: #e2ddd4;
    --color-accent: #c4553a;
    --radius: 6px;
    --font-display: "Instrument Serif", Georgia, serif;
    --font-body: "DM Sans", system-ui, sans-serif;
    --shadow-sm: 0 1px 2px rgba(28, 25, 23, 0.04);
    --shadow-md: 0 2px 8px rgba(28, 25, 23, 0.06), 0 1px 2px rgba(28, 25, 23, 0.04);
    --shadow-lg: 0 8px 24px rgba(28, 25, 23, 0.08), 0 2px 6px rgba(28, 25, 23, 0.04);
}

html {
    overflow-x: hidden;
}

body {
    font-family: var(--font-body);
    background: var(--color-bg);
    color: var(--color-text);
    line-height: 1.55;
    padding: 3rem 2rem 2rem;
    max-width: 1200px;
    margin: 0 auto;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
}

/* Subtle paper grain overlay */
.page-grain {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 9999;
    opacity: 0.3;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)'/%3E%3C/svg%3E");
    background-repeat: repeat;
    background-size: 256px;
}

/* Header */
header {
    margin-bottom: 3rem;
    max-width: 640px;
}

.header-eyebrow {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: var(--color-accent);
    margin-bottom: 0.75rem;
}

header h1 {
    font-family: var(--font-body);
    font-size: clamp(2.25rem, 5vw, 3.5rem);
    font-weight: 400;
    line-height: 1.1;
    letter-spacing: -0.01em;
    margin-bottom: 0.75rem;
}

.header-sub {
    color: var(--color-muted);
    font-size: 1.05rem;
    line-height: 1.5;
}

/* Controls */
.controls {
    display: flex;
    flex-wrap: wrap;
    gap: 1.5rem;
    align-items: flex-end;
    margin-bottom: 3rem;
    padding: 1.5rem;
    background: var(--color-surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.control-group label {
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--color-muted);
}

.search-wrapper {
    position: relative;
    width: 300px;
}

.search-icon {
    position: absolute;
    left: 0.75rem;
    top: 50%;
    transform: translateY(-50%);
    width: 16px;
    height: 16px;
    color: var(--color-muted);
    pointer-events: none;
}

#search-input {
    width: 100%;
    padding: 0.6rem 0.75rem 0.6rem 2.25rem;
    font-family: var(--font-body);
    font-size: 0.9rem;
    border: 1.5px solid var(--color-border);
    border-radius: var(--radius);
    background: var(--color-bg);
    color: var(--color-text);
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s;
}

#search-input::placeholder {
    color: var(--color-muted);
    font-style: italic;
}

#search-input:focus {
    border-color: var(--color-accent);
    box-shadow: 0 0 0 3px rgba(196, 85, 58, 0.1);
}

.typeahead-dropdown {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    background: var(--color-surface);
    border: 1.5px solid var(--color-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    max-height: 320px;
    overflow-y: auto;
    z-index: 100;
}

.typeahead-dropdown.hidden {
    display: none;
}

.typeahead-item {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.55rem 0.85rem;
    cursor: pointer;
    font-size: 0.88rem;
    transition: background 0.1s;
}

.typeahead-item:first-child {
    border-radius: var(--radius) var(--radius) 0 0;
}

.typeahead-item:last-child {
    border-radius: 0 0 var(--radius) var(--radius);
}

.typeahead-item:hover,
.typeahead-item.active {
    background: var(--color-bg);
}

.gender-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}

.gender-dot.F { background: var(--color-female); }
.gender-dot.M { background: var(--color-male); }

.typeahead-count {
    margin-left: auto;
    color: var(--color-muted);
    font-size: 0.78rem;
    font-variant-numeric: tabular-nums;
}

.typeahead-item.no-matches {
    color: var(--color-muted);
    cursor: default;
    font-style: italic;
}

/* Year range slider */
.control-group-slider {
    flex: 1;
    min-width: 220px;
}

.range-slider {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding-top: 0.15rem;
}

.range-label {
    font-size: 0.82rem;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    min-width: 2.8em;
    color: var(--color-text);
}

#range-label-end {
    text-align: right;
}

.range-track-wrapper {
    position: relative;
    flex: 1;
    height: 28px;
    display: flex;
    align-items: center;
}

.range-track-wrapper::before {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    height: 3px;
    background: var(--color-border);
    border-radius: 2px;
    pointer-events: none;
}

.range-track-fill {
    position: absolute;
    height: 3px;
    background: var(--color-accent);
    border-radius: 2px;
    pointer-events: none;
}

.range-track-wrapper input[type="range"] {
    position: absolute;
    width: 100%;
    -webkit-appearance: none;
    appearance: none;
    background: transparent;
    pointer-events: none;
    margin: 0;
    height: 28px;
}

.range-track-wrapper input[type="range"]::-webkit-slider-runnable-track {
    -webkit-appearance: none;
    height: 0;
}

.range-track-wrapper input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--color-surface);
    border: 2.5px solid var(--color-accent);
    box-shadow: var(--shadow-sm);
    cursor: pointer;
    pointer-events: all;
    position: relative;
    margin-top: -9px;
    transition: transform 0.15s, box-shadow 0.15s;
}

.range-track-wrapper input[type="range"]::-webkit-slider-thumb:hover {
    transform: scale(1.15);
    box-shadow: 0 0 0 4px rgba(196, 85, 58, 0.12);
}

.range-track-wrapper input[type="range"]::-moz-range-track {
    height: 0;
    border: none;
    background: transparent;
}

.range-track-wrapper input[type="range"]::-moz-range-thumb {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--color-surface);
    border: 2.5px solid var(--color-accent);
    box-shadow: var(--shadow-sm);
    cursor: pointer;
    pointer-events: all;
}

.range-track-wrapper input[type="range"]:focus {
    outline: none;
}

.range-track-wrapper input[type="range"]:focus::-webkit-slider-thumb {
    box-shadow: 0 0 0 4px rgba(196, 85, 58, 0.15);
}

.range-track-wrapper input[type="range"]:focus::-moz-range-thumb {
    box-shadow: 0 0 0 4px rgba(196, 85, 58, 0.15);
}

/* Sections */
.section {
    margin-bottom: 3rem;
    animation: fadeUp 0.4s ease both;
}

@keyframes fadeUp {
    from { opacity: 0; transform: translateY(12px); }
    to { opacity: 1; transform: translateY(0); }
}

.section-header {
    display: flex;
    align-items: baseline;
    gap: 1rem;
    margin-bottom: 1.25rem;
    flex-wrap: wrap;
}

.section-header h2 {
    font-family: var(--font-body);
    font-size: 1.6rem;
    font-weight: 400;
}

.hidden {
    display: none;
}

/* Charts */
.chart-container {
    background: var(--color-surface);
    border-radius: var(--radius);
    padding: 1.5rem;
    box-shadow: var(--shadow-md);
    min-width: 0;
    overflow-x: auto;
}

.charts-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.25rem;
}

.chart-title {
    font-size: 0.72rem;
    font-weight: 600;
    margin-bottom: 0.85rem;
    color: var(--color-muted);
    text-transform: uppercase;
    letter-spacing: 0.08em;
}

/* D3 chart styling */
.axis text {
    font-family: var(--font-body);
    font-size: 11px;
    fill: var(--color-muted);
}

.axis path,
.axis line {
    stroke: var(--color-border);
}

.grid line {
    stroke: var(--color-border);
    stroke-opacity: 0.5;
    stroke-dasharray: 2, 3;
}

.grid path {
    stroke: none;
}

.line-path {
    fill: none;
    stroke-width: 2.5;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.tooltip-line {
    stroke: var(--color-muted);
    stroke-width: 1;
    stroke-dasharray: 3, 3;
}

.tooltip-circle {
    stroke: var(--color-surface);
    stroke-width: 2.5;
}

.chart-tooltip {
    position: absolute;
    padding: 0.4rem 0.65rem;
    background: var(--color-text);
    color: var(--color-bg);
    font-family: var(--font-body);
    font-size: 0.78rem;
    font-weight: 500;
    border-radius: 4px;
    pointer-events: none;
    white-space: nowrap;
    box-shadow: var(--shadow-md);
}

.bar-label {
    font-family: var(--font-body);
    font-size: 11px;
    fill: var(--color-muted);
}

/* Top names controls */
.top-controls {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
}

.top-controls select,
.top-controls button {
    padding: 0.4rem 0.7rem;
    font-family: var(--font-body);
    font-size: 0.82rem;
    font-weight: 500;
    border: 1.5px solid var(--color-border);
    border-radius: var(--radius);
    background: var(--color-surface);
    color: var(--color-text);
    cursor: pointer;
    transition: all 0.15s;
}

.top-controls select:hover,
.top-controls button:hover {
    border-color: var(--color-muted);
}

.top-controls button.active {
    background: var(--color-text);
    color: var(--color-surface);
    border-color: var(--color-text);
}

.range-mode-label {
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--color-muted);
    font-variant-numeric: tabular-nums;
}

.no-data-msg {
    padding: 2.5rem;
    text-align: center;
    color: var(--color-muted);
    font-size: 0.9rem;
    font-style: italic;
}

/* Footer */
footer {
    margin-top: 2rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--color-border);
}

footer p {
    font-size: 0.78rem;
    color: var(--color-muted);
    line-height: 1.6;
    max-width: 540px;
}

/* Responsive */
@media (max-width: 768px) {
    body {
        padding: 1.5rem 1rem 1rem;
    }

    header {
        margin-bottom: 2rem;
    }

    header h1 {
        font-size: 2rem;
    }

    .charts-row {
        grid-template-columns: 1fr;
    }

    .search-wrapper {
        width: 100%;
    }

    .controls {
        flex-direction: column;
        align-items: stretch;
    }

    .section-header {
        flex-direction: column;
        gap: 0.5rem;
    }
}
