/* HexEditorBase — format-agnostic styles */

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

:root {
    --bg-dark: #1a1a1a;
    --bg-panel: #252525;
    --bg-input: #333;
    --text-primary: #fff;
    --text-secondary: #888;
    --accent: #4a9eff;
    --accent-hover: #3a8eef;
    --danger: #ff4a4a;
    --success: #4aff7a;
    --border: #444;

    /* Hex row height — used for slider gutter alignment */
    --hex-row-height: 20px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-dark);
    color: var(--text-primary);
    min-height: 100vh;
}

.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* Header */
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 20px;
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border);
}

.header h1 {
    font-size: 18px;
    font-weight: 600;
}

.format-nav { display: flex; gap: 8px; align-items: center; }
.format-nav a { color: var(--text-secondary); text-decoration: none; font-size: 13px; padding: 2px 6px; border-radius: 3px; }
.format-nav a:hover { color: var(--text-primary); background: var(--bg-secondary); }
.format-nav a.active { color: var(--color-accent); font-weight: bold; }

.header-controls {
    display: flex;
    align-items: center;
    gap: 12px;
}

.header-btn {
    padding: 8px 14px;
    font-size: 13px;
    flex: none;
}

.view-toggle {
    display: flex;
    gap: 4px;
    background: var(--bg-input);
    border-radius: 6px;
    padding: 4px;
}

.toggle-btn {
    padding: 8px 16px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 4px;
    font-size: 14px;
    transition: all 0.2s;
}

.toggle-btn.active {
    background: var(--accent);
    color: var(--text-primary);
}

.toggle-btn:hover:not(.active) {
    color: var(--text-primary);
}

/* Main Content */
.main {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* Preview Panel */
.preview-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg-dark);
    border-right: 1px solid var(--border);
    overflow: hidden;
}

.preview-panel.popout-hidden {
    display: none;
}

.preview-container {
    width: 100%;
    flex: 1;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    position: relative;
}

.preview-container:focus {
    outline: none;
}

.preview-container:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
}

.empty-state {
    text-align: center;
    color: var(--text-secondary);
}

.empty-state p {
    margin-bottom: 16px;
    font-size: 16px;
}

.popout-placeholder[hidden] {
    display: none;
}

.popout-placeholder {
    text-align: center;
    color: var(--text-secondary);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    height: 100%;
    position: absolute;
    inset: 0;
}

.popout-placeholder p {
    font-size: 16px;
}

.preview-image {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    image-rendering: pixelated;
    transform-origin: 0 0;
    will-change: transform;
    user-select: none;
    -webkit-user-drag: none;
}

/* Block highlight overlay (hex↔pixel mapping) */
.block-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 5;
}

/* Zoom Controls */
.zoom-controls {
    position: absolute;
    bottom: 12px;
    right: 12px;
    display: flex;
    align-items: center;
    gap: 4px;
    background: rgba(0, 0, 0, 0.7);
    border-radius: 6px;
    padding: 4px 6px;
    pointer-events: auto;
    z-index: 10;
}

.zoom-btn {
    width: 28px;
    height: 28px;
    border: none;
    border-radius: 4px;
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
    font-size: 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    line-height: 1;
}

.zoom-btn:hover {
    background: rgba(255, 255, 255, 0.2);
}

.zoom-btn:active {
    background: rgba(255, 255, 255, 0.3);
}

.zoom-btn-reset {
    width: auto;
    padding: 0 8px;
    font-size: 12px;
}

.zoom-btn.active {
    background: rgba(74, 159, 255, 0.4);
    color: #fff;
}

.zoom-separator {
    width: 1px;
    height: 16px;
    background: rgba(255, 255, 255, 0.2);
}

.zoom-level {
    color: var(--text-secondary);
    font-size: 12px;
    font-family: var(--font-mono);
    min-width: 40px;
    text-align: center;
    user-select: none;
}

.file-size {
    color: var(--text-secondary);
    font-size: 11px;
    font-family: 'SF Mono', 'Monaco', 'Menlo', monospace;
    margin-right: 4px;
    user-select: none;
}

/* Controls Panel */
.controls-panel {
    width: 350px;
    min-width: 300px;
    background: var(--bg-panel);
    overflow-y: auto;
    padding: 20px;
}

.controls-panel.popout-active {
    width: auto;
    flex: 1;
    min-width: 0;
}

.controls-panel.hex-active {
    width: auto;
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    overflow-y: hidden;
}

.view {
    display: block;
}

.view.hidden {
    display: none;
}

#hex-view {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    min-width: 0;
}

#hex-view.hidden {
    display: none;
}

.view h2 {
    font-size: 16px;
    margin-bottom: 8px;
}

.description {
    color: var(--text-secondary);
    font-size: 13px;
    margin-bottom: 24px;
}

/* Knobs */
.knobs-container {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.knob-group {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.knob-group label {
    font-size: 14px;
    font-weight: 500;
    display: flex;
    justify-content: space-between;
}

.knob-value {
    color: var(--accent);
    font-family: monospace;
    font-size: 13px;
}

.knob-group input[type="range"] {
    width: 100%;
    height: 6px;
    -webkit-appearance: none;
    background: var(--bg-input);
    border-radius: 3px;
    cursor: pointer;
}

.knob-group input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 16px;
    height: 16px;
    background: var(--accent);
    border-radius: 50%;
    cursor: pointer;
}

.knob-group input[type="range"]:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.knob-desc {
    color: var(--text-secondary);
    font-size: 12px;
}

.knob-offset {
    color: var(--text-secondary);
    font-family: 'SF Mono', 'Monaco', 'Menlo', monospace;
    font-size: 11px;
}

/* Actions */

.primary-btn, .secondary-btn {
    flex: 1;
    padding: 10px 16px;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s;
}

.primary-btn {
    background: var(--accent);
    color: var(--text-primary);
}

.primary-btn:hover:not(:disabled) {
    background: var(--accent-hover);
}

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

.secondary-btn {
    background: var(--bg-input);
    color: var(--text-primary);
}

.secondary-btn:hover:not(:disabled) {
    background: #444;
}

/* Hex Editor */
.hex-toolbar {
    display: flex;
    gap: 8px;
    margin-bottom: 12px;
    align-items: center;
}

.hex-toolbar input {
    flex: 1;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--bg-input);
    color: var(--text-primary);
    font-family: monospace;
    font-size: 13px;
}

.hex-toolbar button {
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--bg-input);
    color: var(--text-primary);
    cursor: pointer;
}

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

#search-status {
    color: var(--text-secondary);
    font-size: 12px;
    min-width: 60px;
    text-align: center;
}

.hex-and-gutter {
    display: flex;
    flex: 1;
    min-height: 0;
    gap: 0;
}

.hex-container {
    background: var(--bg-dark);
    border: 1px solid var(--border);
    border-radius: 4px;
    flex: 1;
    min-height: 0;
    overflow: auto;
    font-family: 'SF Mono', 'Monaco', 'Menlo', monospace;
    font-size: 12px;
}

.hex-content {
    padding: 8px;
    white-space: pre;
    line-height: var(--hex-row-height);
}

.hex-row {
    display: flex;
    gap: 8px;
    height: var(--hex-row-height);
    align-items: center;
    overflow: hidden;
}

.hex-row:nth-child(even) {
    background: rgba(255,255,255,0.02);
}

.hex-offset {
    color: #aaa;
    min-width: 60px;
    font-size: 12px;
}

.hex-bytes {
    display: flex;
    gap: 4px;
}

.hex-byte {
    width: 24px;
    text-align: center;
    cursor: pointer;
    border-radius: 2px;
}

.hex-byte:hover {
    background: var(--accent);
}

.hex-byte.selected {
    background: var(--accent);
    color: #fff;
    outline: 1px solid #fff;
    outline-offset: -1px;
}

.hex-byte.pending {
    background: var(--accent);
    color: #fff;
    outline: 1px solid var(--success);
    outline-offset: -1px;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.hex-byte.flash-copy {
    background: var(--success);
    color: #000;
}

.hex-byte.search-match {
    background: #ffa64a;
    color: #000;
}

.hex-byte.search-current {
    background: var(--success);
    color: #000;
}

/* Segment divider rows */
.hex-divider {
    height: var(--hex-row-height);
    line-height: var(--hex-row-height);
}

.hex-divider-text {
    font-size: 11px;
    color: var(--text-secondary);
    letter-spacing: 0.5px;
}

.hex-ascii {
    color: var(--text-secondary);
    margin-left: 16px;
}

.hex-info {
    display: flex;
    justify-content: space-between;
    margin-top: 8px;
    font-size: 12px;
    color: var(--text-secondary);
}

/* Status Bar */
.status-bar {
    display: flex;
    justify-content: space-between;
    padding: 8px 20px;
    background: var(--bg-panel);
    border-top: 1px solid var(--border);
    font-size: 12px;
    color: var(--text-secondary);
}

/* Slider highlight on hex bytes controlled by gutter sliders */
.hex-byte.slider-highlight {
    background: rgba(74, 255, 122, 0.25);
}

/* Gutter panel — scroll-synced slider panel beside hex editor */
.panel-gutter {
    flex: 0 0 200px;
    min-width: 160px;
    background: var(--bg-panel);
    overflow: hidden;
    border-left: 1px solid var(--border);
}

.gutter-scroll-wrapper {
    height: 100%;
    overflow-y: auto;
    overflow-x: hidden;
}

.gutter-content {
    position: relative;
}

.gutter-slider {
    position: absolute;
    left: 0;
    right: 0;
    padding: 2px 8px;
    display: flex;
    flex-direction: column;
    gap: 0;
}

.gutter-slider-label {
    font-size: 10px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: flex;
    justify-content: space-between;
    align-items: baseline;
}

.gutter-slider-offset {
    font-weight: 400;
    font-family: 'SF Mono', 'Monaco', 'Menlo', monospace;
    color: var(--text-secondary);
    font-size: 9px;
}

.gutter-slider-desc {
    font-size: 9px;
    color: var(--text-secondary);
    line-height: 1.2;
    margin-bottom: 1px;
}

.gutter-slider-row {
    display: flex;
    align-items: center;
    gap: 4px;
    height: var(--hex-row-height);
}

.gutter-slider-row input[type="range"] {
    flex: 1;
    height: 4px;
    -webkit-appearance: none;
    background: var(--bg-input);
    border-radius: 2px;
    cursor: pointer;
}

.gutter-slider-row input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--slider-color, var(--accent));
    cursor: pointer;
}

.gutter-slider-value {
    font-family: 'SF Mono', 'Monaco', 'Menlo', monospace;
    font-size: 10px;
    color: var(--text-secondary);
    min-width: 36px;
    text-align: right;
}

/* Filmstrip — thumbnail bar for multi-image toggle */
.filmstrip {
    display: none;
    overflow-x: auto;
    gap: 4px;
    padding: 4px 8px;
    background: #1e1e1e;
    border-top: 1px solid var(--border);
    height: 70px;
    align-items: center;
    flex-shrink: 0;
}

.filmstrip.visible {
    display: flex;
}

.filmstrip-thumb {
    flex: 0 0 auto;
    width: 60px;
    height: 60px;
    border-radius: 4px;
    overflow: hidden;
    cursor: pointer;
    border: 2px solid transparent;
    transition: border-color 0.15s;
}

.filmstrip-thumb:hover {
    border-color: var(--text-secondary);
}

.filmstrip-thumb.active {
    border-color: var(--accent);
}

.filmstrip-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

/* Image switch indicator */
.image-indicator {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0, 0, 0, 0.75);
    color: #fff;
    padding: 8px 16px;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s ease;
    z-index: 20;
}

.image-indicator.visible {
    opacity: 1;
}

/* Drag-and-drop feedback */
.preview-container.drag-active {
    outline: 2px dashed var(--accent);
    outline-offset: -4px;
    background: rgba(74, 159, 255, 0.05);
}

/* Matrix View — 8x8 heatmap grids */
#matrix-content {
    display: flex;
    flex-wrap: wrap;
    gap: 32px;
    padding: 16px;
    outline: none;
}

.matrix-table {
    flex: 0 0 auto;
}

.matrix-table-label {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 4px;
}

.matrix-axis-label {
    font-size: 10px;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.matrix-grid {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    gap: 2px;
}

.matrix-cell {
    width: 46px;
    height: 46px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;
    cursor: pointer;
    border-radius: 2px;
    transition: outline 0.1s;
    user-select: none;
}

.matrix-cell:hover {
    outline: 2px solid var(--accent);
    outline-offset: -1px;
}

.matrix-cell.selected {
    outline: 2px solid #4aff7a;
    outline-offset: -1px;
    box-shadow: 0 0 8px rgba(74, 255, 122, 0.5);
}

.matrix-cell.pending {
    outline: 2px solid #ff9f43;
    outline-offset: -1px;
    box-shadow: 0 0 8px rgba(255, 159, 67, 0.5);
}

/* Matrix grid wrapper with row labels */
.matrix-grid-wrapper {
    display: flex;
    gap: 4px;
}

.matrix-row-labels {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 0 2px;
    font-size: 9px;
    color: var(--text-secondary);
    height: calc(46px * 8 + 2px * 7); /* 8 cells + 7 gaps */
}

.matrix-col-labels {
    display: flex;
    justify-content: space-between;
    margin-left: calc(20px + 4px); /* row label width + gap */
    font-size: 9px;
    color: var(--text-secondary);
    width: calc(46px * 8 + 2px * 7); /* 8 cells + 7 gaps */
    margin-top: 2px;
}

.matrix-info {
    padding: 8px 16px;
    font-size: 11px;
    font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;
    color: var(--text-secondary);
    border-top: 1px solid var(--border);
    min-height: 20px;
}

/* Responsive */
@media (max-width: 768px) {
    .main {
        flex-direction: column;
    }

    .preview-panel {
        border-right: none;
        border-bottom: 1px solid var(--border);
        height: 40vh;
    }

    .controls-panel {
        width: 100%;
    }
}
