/* ── Theme Variables ──────────────────────────────────────────────── */
:root {
    /* Semantic UI Layering */
    --bg-base: #1e1e1e; /* App background, deep containers */
    --bg-surface: #252526; /* Sidebars, panels, rows */
    --bg-surface-elevated: #2d2d2d; /* Top bar, active control highlights */
    --bg-control: #333333; /* Inputs, standard button rest states */

    /* Dedicated Viewport Colors */
    --bg-viewport: #000000;
    --bg-viewport-drag: #1a3a4a;
    --bg-tile: #111111;

    /* System Borders */
    --border-subtle: #3d3d3d; /* Structural splitters & panel edges */
    --border-control: #555555; /* Form inputs and buttons */

    /* Typography System */
    --text-main: #eeeeee; /* Primary text and titles */
    --text-muted: #888888; /* Labels, headers, metadata */
    --text-dim: #cccccc; /* Secondary content text */
    --text-disabled: #666666; /* Disabled text/elements */
    --text-light: #ffffff;

    /* Semantic Accents */
    --accent-primary: #287b60;
    --accent-primary-hover: #236f55;
    --accent-success: #28a745;
    --accent-success-hover: #218838;
    --accent-danger: #c0392b;
    --accent-danger-hover: #a93226;
    --accent-subtle: #3a3a3a;
    --accent-subtle-hover: #444444;

    /* Global Radius Tokens */
    --radius-sm: 3px;
    --radius-md: 6px;
}

/* ── Reset / Base ─────────────────────────────────────────────────── */
*,
*::before,
*::after {
    box-sizing: border-box;
}

body {
    font-family:
        -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial,
        sans-serif;
    background: var(--bg-base);
    color: var(--text-main);
    margin: 0;
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

/* ── Layout & Structure ───────────────────────────────────────────── */
#top-bar {
    padding: 10px 20px;
    background: var(--bg-surface-elevated);
    border-bottom: 1px solid var(--border-subtle);
    display: flex;
    align-items: center;
    gap: 20px;
    z-index: 10;
    flex-shrink: 0;
}

#main-container {
    flex: 1;
    display: flex;
    overflow: hidden;
}

#sidebar {
    width: 270px;
    background: var(--bg-surface);
    border-right: 1px solid var(--border-subtle);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    overflow-x: hidden;
    flex-shrink: 0;
    z-index: 10;
}

h2 {
    margin: 0;
    font-size: 18px;
    font-weight: bold;
    color: var(--text-muted);
;
}

.sidebar-section {
    display: flex;
    flex-direction: column;
    gap: 14px;
    padding: 16px;
    border-bottom: 1px solid var(--border-subtle);
}

.section-header {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-main);
    text-transform: uppercase;
    letter-spacing: 0.6px;
    margin-bottom: -4px;
}

/* ── Form Controls & Buttons ───────────────────────────────────────── */
.control-group {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.control-label {
    font-size: 11px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

input[type="range"] {
    width: 100%;
    height: 22px;
    margin: 0;
    background: none;
    -webkit-appearance: none;
    appearance: none;
    cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
    height: 3px;
    background: var(--bg-control);
    border-radius: 2px;
}
input[type="range"]::-moz-range-track {
    height: 3px;
    background: var(--bg-control);
    border-radius: 2px;
}
input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 14px;
    height: 14px;
    margin-top: -6px; /* center the 14px thumb on the 3px track */
    border-radius: 50%;
    background: var(--accent-primary);
    cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--accent-primary);
    cursor: pointer;
}

input[type="checkbox"] {
    accent-color: var(--accent-primary);
}

/* Root interactive elements now cleanly bound to the primary accent color */
input[type="number"],
input[type="file"] {
    background: var(--bg-control);
    border: 1px solid var(--accent-primary);
    color: var(--text-main);
    border-radius: var(--radius-sm);
    padding: 4px 6px;
}

/* Standard Buttons */
button {
    background: var(--accent-primary);
    color: var(--text-light);
    border: none;
    padding: 8px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-weight: 600;
    font-size: 12px;
}
button:hover:not(:disabled) {
    background: var(--accent-primary-hover);
}
button:disabled {
    background: var(--accent-subtle);
    color: var(--text-disabled);
    cursor: not-allowed;
}

.btn-danger {
    background: var(--accent-danger);
}
.btn-danger:hover:not(:disabled) {
    background: var(--accent-danger-hover);
}
.btn-subtle {
    background: var(--accent-subtle);
}
.btn-subtle:hover:not(:disabled) {
    background: var(--accent-subtle-hover);
}
.btn-success {
    background: var(--accent-success);
}
.btn-success:hover:not(:disabled) {
    background: var(--accent-success-hover);
}

/* Grid/Toolbar Specific Buttons */
.tool-btn {
    background: var(--bg-control);
    color: var(--text-main);
    border: 1px solid var(--border-control);
    padding: 4px 0;
    font-size: 16px;
    border-radius: var(--radius-sm);
    flex: 1;
    cursor: pointer;
    transition: background 0.15s;
    display: flex;
    justify-content: center;
    align-items: center;
}
.tool-btn.active {
    background: var(--accent-primary);
    border-color: var(--accent-primary);
}
.tool-btn:hover:not(.active) {
    background: var(--accent-subtle-hover);
}

/* ── Image Ingest Modules ─────────────────────────────────────────── */
#image-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.img-row {
    background: var(--bg-surface-elevated);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    overflow: hidden;
}

.img-row-header {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 8px;
}

.img-reorder {
    display: flex;
    flex-direction: column;
    gap: 1px;
    flex-shrink: 0;
}
.img-reorder button {
    background: none;
    border: none;
    color: var(--text-disabled);
    padding: 0;
    font-size: 10px;
    line-height: 1;
    cursor: pointer;
    font-weight: normal;
}
.img-reorder button:hover {
    color: var(--text-dim);
    background: none;
}

.img-name {
    flex: 1;
    font-size: 12px;
    color: var(--text-dim);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.img-delete {
    flex-shrink: 0;
    background: none;
    border: none;
    color: var(--text-disabled);
    font-size: 13px;
    cursor: pointer;
    padding: 2px 4px;
    border-radius: var(--radius-sm);
    line-height: 1;
}
/* :not(:disabled) matches the specificity of the global button:hover rule so this
   red actually wins (a plain .img-delete:hover loses to button:hover:not(:disabled)). */
.img-delete:hover:not(:disabled) {
    background: var(--accent-danger);
    color: var(--text-light);
}

.img-contrast {
    flex-shrink: 0;
    background: none;
    border: none;
    color: var(--text-disabled);
    font-size: 13px;
    cursor: pointer;
    padding: 2px 4px;
    border-radius: var(--radius-sm);
    line-height: 1;
}
.img-contrast:hover:not(:disabled) {
    background: var(--accent-subtle-hover);
    color: var(--text-light);
}

/* Async Processing states */
.img-row.loading .img-row-header {
    opacity: 0.5;
    pointer-events: none;
}
.img-loading-bar {
    height: 2px;
    background: var(--accent-primary);
    animation: indeterminate 1.2s linear infinite;
    display: none;
}
.img-row.loading .img-loading-bar {
    display: block;
}

@keyframes indeterminate {
    0% {
        margin-left: 0%;
        width: 30%;
    }
    50% {
        margin-left: 50%;
        width: 30%;
    }
    100% {
        margin-left: 100%;
        width: 0%;
    }
}

#img-empty {
    font-size: 12px;
    color: var(--text-muted);
    text-align: center;
    padding: 20px 0;
    line-height: 1.6;
}

/* ── Train Phase Items ────────────────────────────────────────────── */
.class-selector {
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.class-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px;
    background: var(--bg-control);
    border-radius: var(--radius-sm);
    cursor: pointer;
    border: 2px solid transparent;
}
.class-item.active {
    border-color: var(--accent-primary);
    background: var(--bg-surface-elevated);
}
.color-box {
    width: 14px;
    height: 14px;
    border-radius: var(--radius-sm);
    flex-shrink: 0;
}
.class-stat-badge {
    margin-left: auto;
    font-size: 11px;
    color: #888;
    background: rgba(0,0,0,0.05);
    padding: 2px 6px;
    border-radius: 10px;
    font-family: monospace;
    min-width: 14px;
    text-align: center;
}

/* ── Composite Structural Panels ──────────────────────────────────── */
.heavy-panel {
    background: var(--bg-base);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    padding: 14px;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.2);
}

.heavy-panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

.heavy-panel-title {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-main);
    letter-spacing: 0.5px;
}

.heavy-panel-desc {
    font-size: 11px;
    line-height: 1.4;
    color: var(--text-muted);
    margin: 0;
}

/* ── Per-image Contrast Popover ───────────────────────────────────── */
.contrast-popover {
    position: fixed;
    z-index: 1000;
    width: 200px;
    display: none;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.45);
}
/* Caret pointing at the row button. Default: popover sits to the button's right,
   so the caret points left off the popover's left edge. --caret-top is set in JS
   to line the tip up with the button's center. */
.contrast-popover::before,
.contrast-popover::after {
    content: "";
    position: absolute;
    top: var(--caret-top, 16px);
    width: 0;
    height: 0;
    border-top: 8px solid transparent;
    border-bottom: 8px solid transparent;
}
.contrast-popover::before { /* border-colored triangle */
    left: -8px;
    border-right: 8px solid var(--border-subtle);
}
.contrast-popover::after {  /* fill triangle, inset 1px to reveal the border */
    left: -7px;
    border-right: 8px solid var(--bg-base);
}
/* Flipped: popover placed to the button's left, so the caret points right. */
.contrast-popover.on-left::before {
    left: auto;
    right: -8px;
    border-right: none;
    border-left: 8px solid var(--border-subtle);
}
.contrast-popover.on-left::after {
    left: auto;
    right: -7px;
    border-right: none;
    border-left: 8px solid var(--bg-base);
}

/* Dual-thumb "contrast limits" slider: two range inputs overlaid on one track.
   Visuals come from the shared input[type=range] rule above; here we only stack
   the two inputs and make just their thumbs (not the tracks) interactive so both
   handles stay draggable. */
.contrast-slider {
    position: relative;
    height: 22px;
    margin: 2px 0;
}
.contrast-slider input[type="range"] {
    position: absolute;
    top: 0;
    left: 0;
    pointer-events: none; /* track ignores clicks; only thumbs are interactive */
}
.contrast-slider input[type="range"]::-webkit-slider-thumb {
    pointer-events: auto;
}
.contrast-slider input[type="range"]::-moz-range-thumb {
    pointer-events: auto;
}
/* The two overlaid tracks coincide; hide the upper one so it doesn't double up. */
.contrast-slider input[type="range"].contrast-hi::-webkit-slider-runnable-track {
    background: none;
}
.contrast-slider input[type="range"].contrast-hi::-moz-range-track {
    background: none;
}

.contrast-readout {
    display: flex;
    justify-content: space-between;
    font-size: 11px;
    color: var(--text-muted);
}
.contrast-readout b {
    color: var(--text-dim);
    font-weight: 600;
}

/* ── Viewport Canvas Board ────────────────────────────────────────── */
#viewport {
    flex: 1;
    position: relative;
    background: var(--bg-viewport);
    overflow: hidden;
    transition: background 0.2s;
}
#viewport.drag-active {
    background: var(--bg-viewport-drag);
}

#canvas-board {
    display: flex;
    gap: 20px;
    padding: 20px;
    transform-origin: 0 0;
    width: max-content;
    align-items: flex-start;
}

.image-container {
    position: relative;
    flex-shrink: 0;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.5);
    background: var(--bg-tile);
    display: flex;
    flex-direction: column;
}

.image-tile-label {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0, 0, 0, 0.6);
    color: var(--text-dim);
    font-size: 10px;
    padding: 3px 6px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    pointer-events: none;
    z-index: 5;
}

canvas {
    position: absolute;
    top: 0;
    left: 0;
    image-rendering: pixelated;
}
.gpu-canvas {
    z-index: 1;
}
.label-canvas {
    z-index: 2;
    cursor: crosshair;
}

/* Dynamic Interactivity states */
body.mode-grab .label-canvas,
body.space-down .label-canvas {
    cursor: grab !important;
}

body.mode-grab.panning .label-canvas,
body.space-down.panning .label-canvas {
    cursor: grabbing !important;
}

/* ── Utilities & Cleanups ─────────────────────────────────────────── */
#status {
    font-size: 12px;
    color: var(--text-muted);
}
.hint {
    font-size: 11px;
    color: var(--text-muted);
    line-height: 1.5;
    margin: 0;
}
hr.divider {
    border: none;
    border-top: 1px solid var(--border-subtle);
    margin: 0;
    width: 100%;
}
