/*
 * MIT License
 * 
 * Copyright (c) 2025 dot-sys (https://github.com/dot-sys)
 */

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    overflow-x: hidden;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

:root {
    --diff-red: #8b0000;
}

.tool-main {
    flex: 1;
    padding: 2rem;
    max-width: 1600px;
    margin: 0 auto;
    width: 100%;
}

.tool-container {
    background-color: var(--secondary-bg);
    border: 1px solid var(--border-color);
    padding: 2rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.tool-header h3 {
    margin: 0;
    font-size: 1.5rem;
}

.controls-row {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 1rem;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--border-color);
    flex-wrap: wrap;
}

.io-grid {
    display: grid;
    grid-template-columns: 1fr 28px 1fr;
    gap: 1.5rem;
    height: calc(100vh - 350px);
    min-height: 500px;
}

/* CSRSS Tool Specific Grid: Side-by-side inputs, full-width output */
.csrss-grid {
    grid-template-columns: 1fr 1fr;
    row-gap: 1.5rem;
}


.input-window,
.output-window {
    background: #000;
    border: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    resize: none !important;
}

.output-window {
    grid-column: 1 / -1;
}

.window-header {
    background: rgba(255, 255, 255, 0.05);
    padding: 0.5rem 1rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #888;
    height: 40px;
}

.textarea-wrapper {
    flex: 1;
    position: relative;
    overflow: hidden;
    display: flex;
    flex-direction: row;
    height: 100%;
}

.text-input,
.highlight-overlay {
    flex: 1;
    font-family: 'JetBrains Mono', 'Fira Code', monospace !important;
    font-size: 13px !important;
    line-height: 20px !important;
    padding: 10px !important;
    /* removed horizontal padding to allow gutters to be flush with borders */
    margin: 0 !important;
    border: none !important;
    outline: none !important;
    white-space: pre !important;
    overflow-wrap: normal !important;
    word-break: normal !important;
    /* Custom compact dark scrollbars (Webkit/Blink) */
    scrollbar-width: thin;
    /* Firefox */
    scrollbar-color: #2a2a2a #0a0a0a;
    /* Firefox thumb / track */
}

/* Webkit / Blink – vertical */
.text-input::-webkit-scrollbar,
.highlight-overlay::-webkit-scrollbar {
    width: 5px;
    height: 5px;
}

.text-input::-webkit-scrollbar-track,
.highlight-overlay::-webkit-scrollbar-track {
    background: #0a0a0a;
}

.text-input::-webkit-scrollbar-thumb,
.highlight-overlay::-webkit-scrollbar-thumb {
    background: #2a2a2a;
    border-radius: 3px;
}

.text-input::-webkit-scrollbar-thumb:hover,
.highlight-overlay::-webkit-scrollbar-thumb:hover {
    background: #3d3d3d;
}

.text-input::-webkit-scrollbar-corner,
.highlight-overlay::-webkit-scrollbar-corner {
    background: #0a0a0a;
}

.text-input {
    z-index: 2;
    background: transparent !important;
    color: #fff !important;
    caret-color: var(--accent-color);
}

.line-numbers {
    width: 50px;
    background: #000;
    color: #555;
    padding: 10px 5px 10px 0 !important;
    /* Flush with left border */
    text-align: right;
    font-family: 'JetBrains Mono', 'Fira Code', monospace !important;
    font-size: 13px !important;
    line-height: 20px !important;
    border-right: 1px solid var(--border-color);
    user-select: none;
    /* No scrollbars – scrolling is driven purely by JS */
    overflow: hidden;
    scrollbar-width: none;
    /* Firefox */
    white-space: pre;
    /* plain-text newlines work correctly in input mode */
}

.line-numbers::-webkit-scrollbar {
    display: none;
}

/* Webkit/Blink */

/* In results mode the separate line-number gutter is HIDDEN.
   Numbers are rendered inline inside each diff-line row instead,
   which guarantees zero drift – they share the exact same DOM element. */
.results-mode .line-numbers {
    display: none !important;
}

/* ── Inline line-number gutter (inside each diff-line row) ────────────── */
/* position:sticky keeps it pinned at left:0 during horizontal scroll.    */
.ln-gutter {
    flex-shrink: 0;
    display: inline-block;
    width: 50px;
    height: 20px;
    line-height: 20px;
    text-align: right;
    padding: 0 5px 0 0;
    /* No left padding to ensure end-to-border alignment */
    box-sizing: border-box;
    background: #000;
    /* Solid black for normal rows */
    border-right: 1px solid var(--border-color);
    color: #555;
    user-select: none;
    position: sticky;
    left: 0;
    z-index: 10;
}

/* Diff rows – gutter turns red for any detected difference, with white numbers for contrast */
.diff-line[data-type="change"] .ln-gutter,
.diff-line[data-type="delete"] .ln-gutter,
.diff-line[data-type="insert"] .ln-gutter,
.diff-line.diff-row .ln-gutter {
    background-color: var(--diff-red) !important;
    /* Solid red to maintain opacity and indicate difference */
    color: #fff !important;
    /* White numbers for better readability on red background */
}

/* Match rows – gutter turns green when similarity highlighting is active */
.diff-line.match-row .ln-gutter {
    background-color: #004400 !important;
    /* Solid green to match row background */
    color: #fff !important;
    /* White numbers for better readability on green background */
}

/* The text content cell inside each result row */
.diff-text {
    flex-shrink: 0;
    /* don't shrink – white-space:pre sets the width */
    white-space: pre;
    padding: 0 10px;
    height: 20px;
    line-height: 20px;
    box-sizing: border-box;
}

/* (old .ln-row kept for potential fallback; effectively unused in results mode) */
.ln-row {
    display: none;
}

.highlight-overlay {
    position: absolute;
    top: 0;
    left: 0; /* Default to zero for tools like CSRSS */
    right: 0;
    bottom: 0;
    pointer-events: none;
    color: transparent;
    z-index: 1;
    overflow: hidden;
}

/* Offset overlay if a line-number gutter is present (for Compare Tool) */
.line-numbers + .highlight-overlay {
    left: 50px;
}


.highlight-overlay .diff-line {
    display: flex;
    align-items: stretch;
    width: max-content;
    min-width: 100%;
    height: 20px;
    white-space: nowrap;
    box-sizing: border-box;
}

.match-row {
    background-color: #004400;
    color: #fff !important;
    /* White font for better contrast on green background */
}

.diff-row {
    background-color: var(--diff-red) !important;
    /* Matches gutter background for a seamless full-width highlight */
    color: #fff !important;
    /* White font for better contrast on red background as requested */
}

.exact-diff-row {
    /* Base props inherited from .diff-line */
}

.non-match-row {
    min-height: 20px;
    line-height: 20px;
}

/* Gap row - alignment filler where the opposite side has a line */
.gap-row {
    min-height: 20px;
    line-height: 20px;
    background-color: rgba(255, 255, 255, 0.03);
}

/* Character diffing: inline span highlights for changed chars within a paired change row */
.char-dec {
    background-color: var(--diff-red) !important;
    /* Solid background for removed chars */
    color: #fff !important;
}

.char-inc {
    background-color: var(--diff-red) !important;
    /* Solid background for inserted chars */
    color: #fff !important;
}

.char-diff {
    display: inline !important;
    background-color: var(--diff-red) !important;
    color: #fff !important;
    font-weight: bold;
}

.search-match {
    background-color: DARKGOLDENROD !important;
    color: #fff !important;
    border-radius: 2px;
}

/* CSRSS Search Highlights */
mark.highlight {
    background-color: rgba(255, 255, 255, 0.2);
    color: inherit;
}

mark.highlight-active {
    background-color: var(--accent-color);
    color: var(--bg-color);
}

.highlighted-content {
    white-space: pre-wrap !important;
    overflow-y: auto !important;
}


/* Settings Dropdown */
.custom-dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-trigger {
    background: #000;
    border: 1px solid var(--border-color);
    color: var(--text-color);
    padding: 0.5rem 1rem;
    font-family: var(--font-mono);
    font-size: 0.9rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 180px;
    justify-content: space-between;
}

.dropdown-trigger:after {
    content: '▼';
    font-size: 0.6rem;
    color: #888;
}

.dropdown-content {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    background: #0a0a0a;
    border: 1px solid var(--border-color);
    z-index: 100;
    min-width: 250px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    padding: 0.5rem 0;
}

.dropdown-content.show {
    display: block;
}

.dropdown-item {
    position: relative;
    /* For tooltip positioning */
    padding: 0.8rem 1rem;
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: pointer;
    transition: background 0.2s;
    user-select: none;
}

.dropdown-item.disabled {
    opacity: 0.4;
    cursor: not-allowed;
    pointer-events: none;
}

.dropdown-item.disabled * {
    pointer-events: none;
}

/* Tooltip */
.tooltip {
    display: none;
    position: absolute;
    left: 105%;
    top: 50%;
    transform: translateY(-50%);
    background: #000;
    border: 1px solid #fff;
    color: #fff;
    padding: 5px 10px;
    font-size: 0.75rem;
    white-space: nowrap;
    z-index: 1000;
    pointer-events: none;
}

.dropdown-item:hover .tooltip {
    display: block;
}

.dropdown-item input {
    cursor: pointer;
}

/* Navigator buttons */
.nav-btn {
    background: transparent;
    border: none;
    border-left: 1px solid var(--border-color);
    color: #888;
    padding: 0 15px;
    cursor: pointer;
    font-size: 14px;
    height: 100%;
    display: flex;
    align-items: center;
    transition: all 0.3s;
}

.nav-btn:hover {
    background: rgba(255, 255, 255, 0.05);
    color: var(--accent-color);
}

.navigator-btns {
    display: flex;
    height: 100%;
    margin-right: -1rem;
}

/* ── Static Heatmap Column ─────────────────────────────────────────────── */
.heatmap-column {
    display: flex;
    flex-direction: column;
    background: rgba(255, 255, 255, 0.02);
    border-left: 1px solid var(--border-color);
    border-right: 1px solid var(--border-color);
    overflow: hidden;
    user-select: none;
}

.heatmap-header {
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.55rem;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: #444;
    border-bottom: 1px solid var(--border-color);
    background: rgba(255, 255, 255, 0.025);
    flex-shrink: 0;
}

.heatmap-track {
    flex: 1;
    position: relative;
    overflow: hidden;
}

/* Individual red diff markers on the static heatmap */
.heatmap-marker {
    position: absolute;
    left: 3px;
    right: 3px;
    height: 2px;
    border-radius: 1px;
    background: var(--diff-red);
    pointer-events: none;
}

/* Semi-transparent gray viewport indicator that moves with scroll */
.heatmap-viewport {
    position: absolute;
    left: 0;
    right: 0;
    background: rgba(200, 200, 200, 0.12);
    border: 1px solid rgba(200, 200, 200, 0.25);
    border-radius: 1px;
    pointer-events: none;
    display: none;
    box-sizing: border-box;
    transition: top 0.05s linear, height 0.05s linear;
}

/* Results Mode */
.results-mode .text-input {
    opacity: 0;
    pointer-events: none;
}

.results-mode .highlight-overlay {
    left: 0;
    /* extend over the now-hidden .line-numbers area */
    color: #fff !important;
    pointer-events: auto;
    overflow-y: auto;
    overflow-x: auto;
    z-index: 3;
    background: var(--bg-color);
    padding: 10px 0;
    /* top/bottom spacing; no horizontal padding – gutter handles it */
}

.results-mode .match-row,
.results-mode .diff-row,
.results-mode .exact-diff-row,
.results-mode .non-match-row,
.results-mode .diff-line {
    /* Base color is white for results mode, overridden by diff-row specific white if needed */
    color: #e0e0e0;
}

/* Basic Controls */
.search-input,
.output-select {
    background: #000;
    border: 1px solid var(--border-color);
    color: var(--text-color);
    padding: 0.5rem 1rem;
    font-family: var(--font-mono);
    font-size: 0.9rem;
    outline: none;
}

.search-input {
    flex: 1;
    min-width: 200px;
}

.search-input:focus,
.output-select:focus {
    border-color: var(--accent-color);
}

.create-button,
.reset-button {
    padding: 0.5rem 1.5rem;
    font-family: var(--font-mono);
    font-weight: 700;
    text-transform: uppercase;
    cursor: pointer;
    transition: all 0.3s;
}

.create-button {
    background: var(--accent-color);
    color: var(--bg-color);
    border: 1px solid var(--accent-color);
}

.create-button:hover {
    background: transparent;
    color: var(--accent-color);
}

.reset-button {
    background: transparent;
    color: #888;
    border: 1px solid var(--border-color);
}

.reset-button:hover {
    border-color: var(--diff-red);
    color: var(--diff-red);
}

.tool-disclaimer {
    margin-top: 2rem;
    text-align: center;
    font-size: 0.85rem;
    color: #666;
    margin-left: auto;
    margin-right: auto;
    line-height: 1.6;
    font-style: italic;
}

.tool-disclaimer strong {
    color: var(--accent-color);
}

.tool-disclaimer a {
    color: #888;
}

.tool-disclaimer a:hover {
    color: var(--accent-color);
}

/* Responsive */
@media (max-width: 1024px) {
    .io-grid {
        grid-template-columns: 1fr;
        grid-template-rows: 1fr auto 1fr;
    }

    .heatmap-column {
        flex-direction: row;
        height: 28px;
        border-left: none;
        border-right: none;
        border-top: 1px solid var(--border-color);
        border-bottom: 1px solid var(--border-color);
    }

    .heatmap-header {
        width: 40px;
        height: 100%;
        border-bottom: none;
        border-right: 1px solid var(--border-color);
    }
}
