:root {
    --bg-primary: #0d1117;
    --bg-secondary: #161b22;
    --bg-tertiary: #21262d;
    --border-color: #30363d;
    --text-primary: #e6edf3;
    --text-secondary: #8b949e;
    --accent-blue: #58a6ff;
    --accent-green: #3fb950;
    --accent-purple: #a371f7;
    --accent-orange: #d29922;
    --accent-red: #f85149;
    --accent-cyan: #39c5cf;
    --sidebar-width: 280px;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans', Helvetica, Arial, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.5;
    min-height: 100vh;
}

.layout {
    display: flex;
    min-height: 100vh;
}

.sidebar {
    width: var(--sidebar-width);
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    position: fixed;
    height: 100vh;
    overflow-y: auto;
}

.sidebar-header {
    padding: 1.25rem;
    border-bottom: 1px solid var(--border-color);
}

.sidebar-header h1 {
    font-size: 1.25rem;
    font-weight: 600;
    background: linear-gradient(135deg, var(--accent-blue), var(--accent-purple));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.sidebar-header .subtitle {
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-top: 0.25rem;
}

.sidebar-section {
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border-color);
}

.sidebar-section label {
    display: block;
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 0.5rem;
}

#corpus-input {
    width: 100%;
    height: 120px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    color: var(--text-primary);
    padding: 0.5rem;
    font-family: 'SF Mono', 'Consolas', monospace;
    font-size: 0.75rem;
    resize: vertical;
}

#corpus-input:focus {
    outline: none;
    border-color: var(--accent-blue);
}

.preset-buttons {
    display: flex;
    gap: 0.5rem;
    margin-top: 0.5rem;
}

.preset-buttons button {
    flex: 1;
    font-size: 0.7rem;
    padding: 0.25rem 0.5rem;
}

input[type="number"] {
    width: 100%;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    color: var(--text-primary);
    padding: 0.5rem;
    font-size: 0.875rem;
}

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

.action-buttons {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.button-row {
    display: flex;
    gap: 0.5rem;
}

.button-row button {
    flex: 1;
}

button {
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 0.5rem 0.75rem;
    border-radius: 6px;
    font-size: 0.8rem;
    cursor: pointer;
    transition: all 0.2s ease;
}

button:hover:not(:disabled) {
    background: var(--border-color);
}

button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

button.primary {
    background: var(--accent-blue);
    border-color: var(--accent-blue);
    color: #fff;
    width: 100%;
}

button.primary:hover:not(:disabled) {
    background: #4093e6;
}

button.playing {
    background: var(--accent-orange);
    border-color: var(--accent-orange);
}

/* Stats in Sidebar */
.stats {
    flex: 1;
}

.stat-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0;
}

.stat-row:not(:last-child) {
    border-bottom: 1px solid var(--bg-tertiary);
}

.stat-label {
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.stat-value {
    font-size: 1rem;
    font-weight: 600;
    color: var(--accent-cyan);
}

.sidebar-footer {
    padding: 1rem 1.25rem;
    border-top: 1px solid var(--border-color);
    text-align: center;
}

.sidebar-footer p {
    font-size: 0.7rem;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
}

.social-links {
    display: flex;
    justify-content: center;
    gap: 0.75rem;
}

.social-links a {
    color: var(--text-secondary);
    transition: color 0.2s ease, transform 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.social-links a:hover {
    color: var(--accent-blue);
    transform: scale(1.1);
}

/* Main Content */
.main-content {
    flex: 1;
    margin-left: var(--sidebar-width);
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

/* Merge Banner */
.merge-banner {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 1rem 1.5rem;
}

.merge-banner h2 {
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 0.75rem;
}

.merge-display {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 50px;
}

.merge-animation {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
    justify-content: center;
}

.merge-token {
    background: var(--bg-tertiary);
    border: 2px solid var(--accent-blue);
    padding: 0.5rem 1rem;
    border-radius: 6px;
    font-family: 'SF Mono', 'Consolas', monospace;
    font-size: 1.25rem;
    animation: pulse 0.5s ease;
}

.merge-plus, .merge-arrow {
    color: var(--text-secondary);
    font-size: 1.25rem;
}

.merge-result {
    background: var(--accent-green);
    color: #fff;
    padding: 0.5rem 1rem;
    border-radius: 6px;
    font-family: 'SF Mono', 'Consolas', monospace;
    font-size: 1.25rem;
    font-weight: 600;
    animation: pop 0.3s ease;
}

.merge-id, .merge-count {
    color: var(--text-secondary);
    font-size: 0.875rem;
}

@keyframes pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

@keyframes pop {
    0% { transform: scale(0.8); opacity: 0; }
    100% { transform: scale(1); opacity: 1; }
}

/* Visualization Grid */
.viz-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
    flex: 1;
}

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

/* Panels */
.panel {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 1rem 1.25rem;
    display: flex;
    flex-direction: column;
}

.panel h2 {
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 0.75rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.badge {
    background: var(--accent-purple);
    color: #fff;
    font-size: 0.7rem;
    padding: 0.125rem 0.5rem;
    border-radius: 10px;
    font-weight: 500;
}

/* Pair Chart */
.pair-chart {
    display: flex;
    flex-direction: column;
    gap: 0.375rem;
    max-height: 350px;
    overflow-y: auto;
    flex: 1;
}

.pair-bar {
    display: grid;
    grid-template-columns: 100px 1fr 45px;
    gap: 0.5rem;
    align-items: center;
    padding: 0.25rem 0;
}

.pair-bar.top-pair .pair-label {
    color: var(--accent-green);
    font-weight: 600;
}

.pair-label {
    font-family: 'SF Mono', 'Consolas', monospace;
    font-size: 0.8rem;
    text-align: right;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.pair-bar-container {
    height: 18px;
    background: var(--bg-tertiary);
    border-radius: 4px;
    overflow: hidden;
}

.pair-bar-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--accent-blue), var(--accent-purple));
    border-radius: 4px;
    transition: width 0.3s ease;
}

.top-pair .pair-bar-fill {
    background: linear-gradient(90deg, var(--accent-green), var(--accent-cyan));
}

.pair-count {
    font-size: 0.7rem;
    color: var(--text-secondary);
    text-align: right;
}

/* Base Vocabulary (collapsible) */
.base-vocab-panel {
    padding: 0;
}

.panel-header-collapsible {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.25rem;
    cursor: pointer;
    transition: background 0.2s ease;
}

.panel-header-collapsible:hover {
    background: var(--bg-tertiary);
}

.panel-header-collapsible h2 {
    margin: 0;
}

.toggle-icon {
    color: var(--text-secondary);
    font-size: 0.75rem;
    transition: transform 0.2s ease;
}

.base-vocab-display {
    border-top: 1px solid var(--border-color);
    max-height: 400px;
    overflow-y: auto;
    transition: max-height 0.3s ease, padding 0.3s ease;
    padding: 1rem 1.25rem;
}

.base-vocab-display.collapsed {
    max-height: 0;
    padding: 0 1.25rem;
    overflow: hidden;
    border-top: none;
}

.base-vocab-grid {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.base-vocab-category {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.category-label {
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.category-tokens {
    display: flex;
    flex-wrap: wrap;
    gap: 3px;
}

.base-token {
    font-family: 'SF Mono', 'Consolas', monospace;
    font-size: 0.7rem;
    padding: 0.2rem 0.35rem;
    border-radius: 3px;
    cursor: default;
}

.base-token.control {
    background: var(--bg-tertiary);
    color: var(--accent-orange);
    border: 1px solid var(--accent-orange);
    opacity: 0.7;
}

.base-token.printable {
    background: var(--bg-tertiary);
    color: var(--accent-green);
    border: 1px solid var(--border-color);
}

.base-token.extended {
    background: var(--bg-tertiary);
    color: var(--accent-purple);
    border: 1px solid var(--accent-purple);
    opacity: 0.7;
}

/* Vocabulary Display (Merged tokens) */
.vocab-display {
    display: flex;
    flex-wrap: wrap;
    gap: 0.375rem;
    max-height: 350px;
    overflow-y: auto;
    flex: 1;
}

.vocab-entry {
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    padding: 0.25rem 0.5rem;
    display: flex;
    align-items: center;
    gap: 0.375rem;
    font-size: 0.8rem;
    transition: all 0.3s ease;
}

.vocab-entry.new-entry {
    background: var(--accent-green);
    border-color: var(--accent-green);
    animation: highlight 0.5s ease;
}

@keyframes highlight {
    0% { transform: scale(1.1); }
    100% { transform: scale(1); }
}

.vocab-id {
    color: var(--text-secondary);
    font-size: 0.65rem;
}

.vocab-token {
    font-family: 'SF Mono', 'Consolas', monospace;
    font-weight: 500;
}

/* Corpus Display */
.corpus-display {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    max-height: 300px;
    overflow-y: auto;
    flex: 1;
    padding: 0.25rem;
}

.token-word {
    display: flex;
    align-items: center;
    gap: 1px;
    background: var(--bg-tertiary);
    border-radius: 4px;
    padding: 0.25rem 0.375rem;
}

.token {
    font-family: 'SF Mono', 'Consolas', monospace;
    font-size: 0.75rem;
    padding: 0.125rem 0.2rem;
    border-radius: 2px;
}

.byte-token {
    background: var(--bg-primary);
    color: var(--text-secondary);
}

.merged-token {
    background: var(--accent-purple);
    color: #fff;
}

.word-freq {
    font-size: 0.65rem;
    color: var(--text-secondary);
    margin-left: 0.25rem;
}

.show-more-btn {
    width: 100%;
    margin-top: 0.5rem;
    padding: 0.5rem;
    background: var(--bg-tertiary);
    border: 1px dashed var(--border-color);
    color: var(--accent-blue);
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.75rem;
    transition: all 0.2s ease;
}

.show-more-btn:hover {
    background: var(--border-color);
    border-style: solid;
}

/* Info Panel */
.info-panel {
    margin-top: 1rem;
}

.info-content {
    display: flex;
    gap: 1.5rem;
}

.info-step {
    flex: 1;
    display: flex;
    gap: 0.75rem;
    padding: 1rem;
    background: var(--bg-tertiary);
    border-radius: 8px;
}

.step-num {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    min-width: 28px;
    background: var(--accent-blue);
    color: #fff;
    border-radius: 50%;
    font-weight: 600;
    font-size: 0.875rem;
}

.info-step strong {
    display: block;
    color: var(--text-primary);
    margin-bottom: 0.25rem;
    font-size: 0.85rem;
}

.info-step p {
    color: var(--text-secondary);
    font-size: 0.75rem;
    line-height: 1.5;
    margin: 0;
}

@media (max-width: 900px) {
    .info-content {
        flex-direction: column;
    }
}

/* Merges History */
.merges-display {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    max-height: 250px;
    overflow-y: auto;
    flex: 1;
}

.merge-entry {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.375rem 0.5rem;
    background: var(--bg-tertiary);
    border-radius: 4px;
    font-size: 0.75rem;
}

.merge-entry.latest {
    background: var(--accent-green);
    color: #fff;
}

.merge-num {
    font-weight: 600;
    min-width: 28px;
}

.merge-content {
    font-family: 'SF Mono', 'Consolas', monospace;
    flex: 1;
}

.merge-count-small {
    color: var(--text-secondary);
    font-size: 0.7rem;
}

.merge-entry.latest .merge-count-small {
    color: rgba(255, 255, 255, 0.8);
}

/* Placeholder */
.placeholder {
    color: var(--text-secondary);
    font-style: italic;
    font-size: 0.875rem;
}

/* Scrollbar styling */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: var(--bg-tertiary);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}

/* Tooltips */
.tooltip {
    cursor: help;
    border-bottom: 1px dotted var(--accent-blue);
}

.info-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--accent-blue);
    color: #fff;
    font-size: 0.6rem;
    font-weight: bold;
    margin-left: 4px;
    cursor: help;
    border-bottom: none;
}

#tooltip-box {
    position: fixed;
    background: var(--bg-primary);
    border: 1px solid var(--accent-blue);
    color: var(--text-primary);
    padding: 0.75rem 1rem;
    border-radius: 8px;
    font-size: 0.75rem;
    line-height: 1.5;
    width: 280px;
    white-space: normal;
    z-index: 10000;
    pointer-events: none;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.15s ease, visibility 0.15s ease;
}

#tooltip-box.visible {
    opacity: 1;
    visibility: visible;
}

@media (max-width: 1200px) {
    .viz-grid {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 768px) {
    .layout {
        flex-direction: column;
    }

    .sidebar {
        position: relative;
        width: 100%;
        height: auto;
    }

    .main-content {
        margin-left: 0;
    }
}
