/* ===================================================================
   Character Search — Stylesheet
   Modern Heritage design system — inherits tokens from parent styles.css
   =================================================================== */

@font-face {
    font-family: 'YiClassic';
    src: url('./fonts/YiClassic.ttf') format('truetype');
    font-display: swap;
}
@font-face {
    font-family: 'YiReading';
    src: url('./fonts/YiReading.ttf') format('truetype');
    font-display: swap;
}
@font-face {
    font-family: 'YiDisplayWX9';
    src: url('./fonts/YiDisplayWX9.ttf') format('truetype');
    font-display: swap;
}

/* Map legacy var names used elsewhere in this file to new design tokens */
:root {
    --primary-color: var(--accent);
    --secondary-color: var(--surface-page);
    --accent-color: var(--accent);
    --text-color: var(--text-primary);
    --border-color: var(--border);
    --dark-burgundy: var(--accent-hover);
    --light-bg: var(--surface-alt);
    --error-color: var(--error);
}

/* ===================================================================
   MAIN CONTENT
   =================================================================== */
.main-content {
    padding: var(--s6) var(--s8);
    max-width: 1100px;
    margin: 0 auto;
}

/* ===================================================================
   BACK LINK (in-page override — header is now white, so dark text)
   =================================================================== */
.back-link {
    display: inline-flex;
    align-items: center;
    gap: var(--s2);
    color: var(--text-secondary);
    text-decoration: none;
    font-size: var(--t-body);
    transition: color 0.15s var(--ease);
}
.back-link:hover { color: var(--accent); }

/* ===================================================================
   SEARCH MODE TABS
   =================================================================== */
.search-modes {
    display: flex;
    gap: 0;
    margin-bottom: var(--s4);
    border-bottom: 1px solid var(--border);
}

.mode-btn {
    padding: var(--s2) var(--s5);
    border: 1px solid var(--border);
    border-bottom: none;
    background: var(--surface-card);
    font-family: var(--font-sans);
    font-size: var(--t-body);
    font-weight: 500;
    cursor: pointer;
    color: var(--text-secondary);
    transition: background 0.15s var(--ease), color 0.15s var(--ease), border-color 0.15s var(--ease);
    border-radius: var(--r-sm) var(--r-sm) 0 0;
    margin-right: var(--s1);
    position: relative;
    bottom: -1px;
}
.mode-btn:hover {
    background: var(--surface-alt);
    color: var(--text-primary);
}
.mode-btn.active {
    background: var(--surface-page);
    color: var(--accent);
    border-color: var(--border);
    border-bottom-color: var(--surface-page);
    font-weight: 600;
}

/* ===================================================================
   SEARCH PANELS
   =================================================================== */
.search-panel {
    display: none;
    background: var(--surface-card);
    border: 1px solid var(--border);
    border-radius: 0 var(--r-lg) var(--r-lg) var(--r-lg);
    padding: var(--s5) var(--s6);
    margin-bottom: var(--s4);
    box-shadow: var(--shadow-sm);
}
.search-panel.active { display: block; }

/* ===================================================================
   PHONETIC DROPDOWNS
   =================================================================== */
.phonetic-dropdowns {
    display: flex;
    align-items: flex-end;
    gap: var(--s3);
    flex-wrap: wrap;
    margin-bottom: var(--s3);
}

.filter-group {
    display: flex;
    flex-direction: column;
    gap: var(--s1);
}

.filter-group label {
    font-family: var(--font-serif);
    font-size: var(--t-caption);
    font-weight: 500;
    color: var(--text-secondary);
    letter-spacing: 0.02em;
}

.filter-group select {
    padding: var(--s2) var(--s3);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    font-family: var(--font-sans);
    font-size: var(--t-small);
    min-width: 100px;
    background: var(--surface-page);
    color: var(--text-primary);
    outline: none;
    transition: border-color 0.15s var(--ease);
}
.filter-group select:focus { border-color: var(--accent); }

/* ===================================================================
   PINYIN ROW
   =================================================================== */
.pinyin-row {
    display: flex;
    align-items: center;
    gap: var(--s3);
    border-top: 1px solid var(--border);
    padding-top: var(--s3);
}

.pinyin-row label {
    font-family: var(--font-serif);
    font-size: var(--t-small);
    color: var(--text-secondary);
    white-space: nowrap;
}

.pinyin-row input {
    flex: 1;
    padding: var(--s2) var(--s3);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    font-family: var(--font-sans);
    font-size: var(--t-small);
    background: var(--surface-page);
    color: var(--text-primary);
    outline: none;
    transition: border-color 0.15s var(--ease);
}
.pinyin-row input:focus { border-color: var(--accent); }

/* ===================================================================
   SEARCH ROW
   =================================================================== */
.search-row {
    display: flex;
    gap: var(--s3);
}

.search-row input {
    flex: 1;
    padding: var(--s2) var(--s4);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    font-family: var(--font-sans);
    font-size: var(--t-body);
    background: var(--surface-page);
    color: var(--text-primary);
    outline: none;
    transition: border-color 0.15s var(--ease);
}
.search-row input:focus { border-color: var(--accent); }

/* ===================================================================
   YI FONT
   =================================================================== */
.yi-font {
    font-family: 'YiClassic', 'YiReading', 'YiDisplayWX9', sans-serif;
    font-size: var(--t-lead);
}

.yi-code-display {
    margin-top: var(--s2);
    font-family: var(--font-mono);
    font-size: var(--t-caption);
    color: var(--text-muted);
}

/* ===================================================================
   DRAW SECTION
   =================================================================== */
.draw-section {
    margin-bottom: var(--s4);
    background: var(--surface-card);
    border: 1px solid var(--border);
    border-radius: var(--r-md);
    padding: var(--s3) var(--s4);
    box-shadow: var(--shadow-sm);
}

.draw-section summary {
    cursor: pointer;
    font-family: var(--font-serif);
    font-size: var(--t-body);
    font-weight: 500;
    color: var(--accent);
    padding: var(--s1) 0;
    user-select: none;
    list-style: none;
}
.draw-section summary::-webkit-details-marker { display: none; }
.draw-section summary::before {
    content: '▸ ';
    font-size: var(--t-caption);
    transition: transform 0.2s var(--ease);
}
.draw-section[open] summary::before { content: '▾ '; }
.draw-section summary:hover { color: var(--accent-hover); }

.canvas-container {
    display: flex;
    align-items: flex-start;
    gap: var(--s3);
    margin-top: var(--s3);
    flex-wrap: wrap;
}

#drawCanvas {
    border: 1px solid var(--border);
    border-radius: var(--r-md);
    background: var(--surface-card);
    cursor: crosshair;
    touch-action: none;
}

.canvas-actions {
    display: flex;
    flex-direction: column;
    gap: var(--s2);
}

/* ===================================================================
   STATUS BAR
   =================================================================== */
.status-bar {
    font-family: var(--font-sans);
    font-size: var(--t-small);
    color: var(--text-muted);
    margin-bottom: var(--s3);
    min-height: 1.2em;
    padding: var(--s1) 0;
}
.status-bar.error { color: var(--error); }

/* ===================================================================
   CS MAP SECTION
   =================================================================== */
.cs-map-section {
    margin: var(--s4) 0;
}

.cs-map-toggle {
    display: inline-flex;
    align-items: center;
    gap: var(--s2);
    background: none;
    border: none;
    font-family: var(--font-serif);
    font-size: var(--t-body);
    font-weight: 500;
    color: var(--accent);
    cursor: pointer;
    padding: var(--s1) 0;
    user-select: none;
    transition: color 0.15s var(--ease);
}
.cs-map-toggle:hover { color: var(--accent-hover); }

.cs-map-toggle .toggle-arrow {
    display: inline-block;
    transition: transform 0.2s var(--ease);
    font-size: var(--t-caption);
}
.cs-map-toggle.open .toggle-arrow {
    transform: rotate(90deg);
}

.cs-map-container {
    margin-top: var(--s2);
    border: 1px solid var(--border);
    border-radius: var(--r-md);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
}

/* Map legend (Leaflet control) */
.cs-map-legend {
    background: rgba(255, 255, 255, 0.94);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    padding: var(--s2) var(--s3);
    font-family: var(--font-sans);
    font-size: var(--t-caption);
    box-shadow: var(--shadow-sm);
    max-height: 200px;
    overflow-y: auto;
}
.cs-map-legend-item {
    display: flex;
    align-items: center;
    gap: var(--s1);
    padding: 2px 0;
}
.cs-map-legend-color {
    display: inline-block;
    width: 14px;
    height: 14px;
    border-radius: var(--r-sm);
    flex-shrink: 0;
}
.cs-map-legend-label {
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 150px;
}

/* Map pin / locality tag markers */
.map-loc-tag-icon {
    background: none !important;
    border: none !important;
}
.map-loc-tag {
    display: inline-flex;
    align-items: center;
    gap: var(--s1);
    padding: 3px var(--s1);
    border-radius: var(--r-sm);
    box-shadow: var(--shadow-sm);
    white-space: nowrap;
}
.map-loc-tag-img {
    width: 20px;
    height: 20px;
    object-fit: contain;
    border-radius: 2px;
    mix-blend-mode: multiply;
}
.map-loc-tag-label {
    font-family: var(--font-sans);
    font-size: 10px;
    font-weight: 600;
    color: var(--text-primary);
}
.map-loc-tag-count {
    font-family: var(--font-mono);
    font-weight: 700;
    font-size: 12px;
    color: var(--text-primary);
}

/* Tag click popup */
.map-tag-popup {
    font-family: var(--font-sans);
    font-size: var(--t-small);
    line-height: 1.4;
    max-width: 240px;
}
.map-tag-popup-section {
    margin-bottom: var(--s1);
}
.map-tag-popup-section strong {
    display: block;
    font-family: var(--font-serif);
    font-size: var(--t-caption);
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 2px;
}
.map-tag-pron {
    display: inline-block;
    background: var(--accent-faint);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    padding: 1px var(--s1);
    margin: 1px 2px;
    font-size: var(--t-small);
}
.map-tag-meaning {
    display: block;
    padding: 1px 0;
    color: var(--text-primary);
}
.map-tag-meaning small {
    color: var(--text-muted);
    margin-left: var(--s1);
}

/* ===================================================================
   RESULTS GRID
   =================================================================== */
.results-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
    gap: var(--s3);
}

.result-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    background: var(--surface-card);
    border: 1px solid var(--border);
    border-radius: var(--r-md);
    padding: var(--s2) var(--s1);
    cursor: pointer;
    transition: box-shadow 0.15s var(--ease), border-color 0.15s var(--ease), transform 0.15s var(--ease);
    box-shadow: var(--shadow-sm);
}
.result-card:hover {
    border-color: var(--accent);
    box-shadow: var(--shadow-md);
    transform: translateY(-1px);
}
.result-card.selected {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent);
}

.result-card img {
    width: 64px;
    height: 64px;
    object-fit: contain;
    border-radius: var(--r-sm);
    background: var(--surface-alt);
}

.card-label {
    font-family: var(--font-sans);
    font-size: var(--t-caption);
    color: var(--text-muted);
    margin-top: var(--s1);
    text-align: center;
    word-break: break-all;
}

.card-count {
    font-family: var(--font-mono);
    font-size: var(--t-caption);
    color: var(--text-muted);
    margin-top: 2px;
    text-align: center;
}

.card-placeholder {
    width: 64px;
    height: 64px;
    background: var(--surface-alt);
    border-radius: var(--r-sm);
}

.card-similarity {
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 2px;
}

/* Multi-char word cards */
.word-card {
    grid-column: span var(--word-span, 2);
}
.word-card .word-images {
    display: flex;
    flex-direction: row;
    gap: var(--s1);
    align-items: center;
    justify-content: center;
}
.word-card .word-images img {
    width: 64px;
    height: 64px;
    object-fit: contain;
    border-radius: var(--r-sm);
    background: var(--surface-alt);
}

/* ===================================================================
   DETAIL PANEL
   =================================================================== */
.detail-panel {
    background: var(--surface-card);
    border: 1px solid var(--border);
    border-top: 2px solid var(--accent);
    border-radius: var(--r-md) var(--r-md) 0 0;
    box-shadow: var(--shadow-md);
    margin-top: var(--s4);
    padding: var(--s5) var(--s6);
    position: relative;
}

.detail-close {
    position: absolute;
    top: var(--s3);
    right: var(--s4);
    background: none;
    border: none;
    font-size: var(--t-h3);
    cursor: pointer;
    color: var(--text-muted);
    line-height: 1;
    transition: color 0.15s var(--ease);
}
.detail-close:hover { color: var(--text-primary); }

.detail-content {
    margin-top: var(--s2);
}

/* Stats bar */
.detail-stats {
    display: flex;
    align-items: flex-start;
    gap: var(--s6);
    flex-wrap: wrap;
    padding-bottom: var(--s4);
    border-bottom: 1px solid var(--border);
    margin-bottom: var(--s4);
}

.detail-stat-item {
    display: flex;
    flex-direction: column;
    gap: var(--s1);
    min-width: 0;
}
.detail-stat-item.stat-image { flex-shrink: 0; }

.detail-stat-item .stat-label {
    font-family: var(--font-serif);
    font-size: var(--t-caption);
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.detail-stat-item .stat-value {
    font-family: var(--font-sans);
    font-size: var(--t-small);
    color: var(--text-primary);
}
.detail-stat-item .stat-value.stat-number {
    font-family: var(--font-mono);
    font-size: var(--t-lead);
    font-weight: 600;
    color: var(--accent);
}

.detail-stat-image {
    width: 64px;
    height: 64px;
    object-fit: contain;
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    background: var(--surface-alt);
}

/* Section title row */
.detail-section-title-row {
    display: flex;
    align-items: center;
    gap: var(--s2);
    margin-bottom: var(--s2);
    position: relative;
}

.detail-section-title {
    font-family: var(--font-serif);
    font-size: var(--t-h3);
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: var(--s2);
}

/* Filter row inside detail panel */
.detail-filter-row {
    display: flex;
    align-items: center;
    gap: var(--s3);
    margin-bottom: var(--s3);
}
.detail-filter-row label {
    font-family: var(--font-serif);
    font-size: var(--t-small);
    color: var(--text-secondary);
    white-space: nowrap;
}
.detail-filter-row select {
    padding: var(--s1) var(--s3);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    font-family: var(--font-sans);
    font-size: var(--t-small);
    background: var(--surface-page);
    color: var(--text-primary);
    outline: none;
    min-width: 180px;
    transition: border-color 0.15s var(--ease);
}
.detail-filter-row select:focus { border-color: var(--accent); }

/* Appearances container */
.detail-appearances {
    max-height: 400px;
    overflow-y: auto;
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
}

/* Network graph */
.detail-network {
    margin: var(--s3) 0;
    border: 1px solid var(--border);
    border-radius: var(--r-md);
    overflow: hidden;
}

/* ===================================================================
   APPEARANCES TABLE
   =================================================================== */
.appearances-table {
    width: 100%;
    border-collapse: collapse;
    font-family: var(--font-sans);
    font-size: var(--t-caption);
}
.appearances-table th,
.appearances-table td {
    padding: var(--s1) var(--s3);
    border-bottom: 1px solid var(--border);
    text-align: left;
    vertical-align: top;
}
.appearances-table th {
    font-family: var(--font-serif);
    font-weight: 600;
    color: var(--text-muted);
    font-size: var(--t-caption);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    position: sticky;
    top: 0;
    background: var(--surface-alt);
    z-index: 1;
}
.appearances-table tbody tr:hover {
    background: var(--accent-faint);
}

/* Group headers above table sections */
.appearance-group-header {
    display: flex;
    align-items: center;
    gap: var(--s2);
    padding: var(--s2) var(--s3);
    margin-top: var(--s3);
    margin-bottom: 0;
    border-radius: var(--r-sm) var(--r-sm) 0 0;
    background: var(--gold-faint);
    border-bottom: 1px solid var(--border);
}
.appearance-group-header:first-of-type { margin-top: 0; }

.appearance-group-label {
    font-family: var(--font-serif);
    font-weight: 600;
    font-size: var(--t-small);
    color: var(--text-primary);
}
.appearance-group-count {
    font-family: var(--font-mono);
    font-size: var(--t-caption);
    color: var(--text-muted);
}

.appearance-group-header + .detail-appearances {
    border-radius: 0 0 var(--r-sm) var(--r-sm);
    margin-bottom: var(--s1);
}

/* Appearances settings gear */
.appearances-settings-btn {
    background: none;
    border: none;
    font-size: var(--t-lead);
    cursor: pointer;
    color: var(--text-muted);
    padding: 2px var(--s1);
    border-radius: var(--r-sm);
    transition: color 0.15s var(--ease), background 0.15s var(--ease);
    line-height: 1;
}
.appearances-settings-btn:hover {
    color: var(--accent);
    background: var(--accent-faint);
}

.appearances-settings-dropdown {
    position: absolute;
    top: 100%;
    left: 80px;
    background: var(--surface-card);
    border: 1px solid var(--border);
    border-radius: var(--r-md);
    box-shadow: var(--shadow-md);
    padding: var(--s3) var(--s4);
    z-index: 50;
    min-width: 200px;
}

.settings-radio-label {
    display: flex;
    align-items: center;
    gap: var(--s2);
    padding: var(--s1) 0;
    font-family: var(--font-sans);
    font-size: var(--t-small);
    cursor: pointer;
    color: var(--text-primary);
}
.settings-radio-label input[type="radio"] {
    accent-color: var(--accent);
}

.settings-sub-dropdown {
    padding: var(--s1) 0 var(--s1) var(--s6);
}
.settings-sub-dropdown select {
    padding: var(--s1) var(--s2);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    font-family: var(--font-sans);
    font-size: var(--t-caption);
    background: var(--surface-page);
    color: var(--text-primary);
    outline: none;
}
.settings-sub-dropdown select:focus { border-color: var(--accent); }

/* ===================================================================
   CLUSTER BLOCKS
   =================================================================== */
.cluster-block {
    grid-column: 1 / -1;
    background: var(--surface-card);
    border: 1px solid var(--border);
    border-radius: var(--r-md);
    padding: var(--s3) var(--s4);
    margin-bottom: var(--s2);
    box-shadow: var(--shadow-sm);
}

.cluster-title {
    font-family: var(--font-serif);
    font-size: var(--t-h3);
    font-weight: 700;
    color: var(--accent);
    margin-bottom: var(--s1);
}

.cluster-subtitle {
    font-family: var(--font-sans);
    font-size: var(--t-small);
    color: var(--text-secondary);
    margin-bottom: var(--s2);
}

.cluster-chars {
    display: flex;
    flex-wrap: wrap;
    gap: var(--s2);
}
.cluster-chars .result-card {
    width: 72px;
    margin: 0;
}

/* ===================================================================
   PRONUNCIATION / MEANING / LOCALITY COMPACTS
   =================================================================== */
.pron-compact {
    display: flex;
    flex-wrap: wrap;
    gap: var(--s1);
}
.pron-tag {
    background: var(--accent-faint);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    padding: var(--s1) var(--s3);
    font-family: var(--font-sans);
    font-size: var(--t-body);
    white-space: nowrap;
}

.meaning-compact {
    display: flex;
    flex-wrap: wrap;
    gap: var(--s1);
}
.meaning-tag {
    background: var(--gold-faint);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    padding: 2px var(--s2);
    font-family: var(--font-sans);
    font-size: var(--t-small);
}
.meaning-tag .meaning-pct {
    font-family: var(--font-mono);
    font-size: var(--t-caption);
    color: var(--text-muted);
    margin-left: var(--s1);
}

.locality-compact {
    display: flex;
    flex-wrap: wrap;
    gap: var(--s1);
}
.locality-tag {
    background: var(--accent-subtle);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    padding: 2px var(--s2);
    font-family: var(--font-sans);
    font-size: var(--t-caption);
    color: var(--text-primary);
}

/* ===================================================================
   YI LINE TOKEN IMAGES
   =================================================================== */
.yi-line-tokens {
    display: flex;
    gap: 1px;
    align-items: center;
    flex-wrap: wrap;
}
.yi-line-tokens img {
    width: 24px;
    height: 24px;
    object-fit: contain;
}
.yi-line-tokens img.match-token {
    outline: 2px solid var(--accent);
    border-radius: 2px;
}

/* ===================================================================
   MISC INLINE
   =================================================================== */
.error {
    color: var(--error);
    font-family: var(--font-sans);
    font-size: var(--t-small);
}

/* ===================================================================
   NETWORK GRAPH (detail panel)
   =================================================================== */
.network-controls {
    padding: var(--s2) var(--s3);
    background: var(--surface-alt);
    border-bottom: 1px solid var(--border);
    text-align: center;
    font-family: var(--font-sans);
    font-size: var(--t-small);
    color: var(--text-secondary);
}
.network-controls input[type="range"] {
    width: 200px;
    vertical-align: middle;
    accent-color: var(--accent);
}
.network-controls #networkDepthVal {
    font-family: var(--font-mono);
    font-weight: 700;
    color: var(--accent);
}
.network-legend {
    position: absolute;
    top: var(--s2);
    left: var(--s2);
    background: rgba(255, 255, 255, 0.96);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    padding: var(--s2) var(--s3);
    font-family: var(--font-sans);
    font-size: var(--t-caption);
    line-height: 1.8;
    z-index: 10;
}
.network-legend-item {
    display: flex;
    align-items: center;
    gap: var(--s1);
}
.network-legend-swatch {
    width: 14px;
    height: 14px;
    border-radius: var(--r-sm);
    display: inline-block;
    border: 2px solid;
}
.network-legend-section {
    margin-top: var(--s1);
    padding-top: var(--s1);
    border-top: 1px solid var(--border);
    font-weight: 700;
    color: var(--text-muted);
}

/* ===================================================================
   RESPONSIVE
   =================================================================== */
@media (max-width: 768px) {
    .main-content {
        padding: var(--s4) var(--s3);
    }
    .phonetic-dropdowns {
        flex-direction: column;
        align-items: stretch;
    }
    .pinyin-row {
        flex-wrap: wrap;
    }
    .search-row {
        flex-wrap: wrap;
    }
    .search-row input {
        width: 100%;
    }
    .results-grid {
        grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
        gap: var(--s2);
    }
    .detail-stats {
        flex-direction: column;
        gap: var(--s3);
    }
    .detail-appearances {
        max-height: 300px;
    }
    .detail-panel {
        padding: var(--s4) var(--s3);
    }
}
