/* ================================================================
   GrainSight - Dark theme with amber/gold accent
   3D Particle Size & Granulometry Analyzer
   ================================================================ */

:root {
    --bg-dark:       #0d1117;
    --bg-panel:      #161b22;
    --bg-input:      #0d1117;
    --border:        #30363d;
    --text-primary:  #e6edf3;
    --text-secondary:#8b949e;
    --accent:        #d29922;
    --accent-hover:  #e3b341;
    --accent-dim:    #3d2e0a;
    --success:       #3fb950;
    --warning:       #d29922;
    --danger:        #f85149;
    --info:          #58a6ff;
    --font-mono:     'Consolas', 'Courier New', monospace;
}

/* ---- Reset ---- */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
                 Oxygen, Ubuntu, sans-serif;
    background: var(--bg-dark);
    color: var(--text-primary);
    overflow: hidden;
    height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ---- Header ---- */
#app-header {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 8px 20px;
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

#app-header h1 {
    font-size: 16px;
    font-weight: 600;
    color: var(--accent);
}

#app-header .subtitle {
    font-size: 12px;
    color: var(--text-secondary);
}

.header-actions {
    margin-left: auto;
    display: flex;
    gap: 8px;
    align-items: center;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--danger);
    display: inline-block;
}
.status-dot.connected { background: var(--success); }

/* ---- Main layout ---- */
#app-container {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* ---- Controls panel (RIGHT) ---- */
#controls-panel {
    width: 300px;
    min-width: 260px;
    background: var(--bg-panel);
    border-left: 1px solid var(--border);
    overflow-y: auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    flex-shrink: 0;
    order: 2;
}

.ctrl-section {
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 10px;
}

.ctrl-section h3 {
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--accent);
    margin-bottom: 8px;
}

.ctrl-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 6px;
}

.ctrl-row label {
    font-size: 12px;
    color: var(--text-secondary);
    flex-shrink: 0;
    margin-right: 8px;
}

.ctrl-row input[type="number"],
.ctrl-row select {
    width: 90px;
    padding: 3px 6px;
    font-size: 12px;
    background: var(--bg-input);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 4px;
    outline: none;
}

.ctrl-row input[type="number"]:focus,
.ctrl-row select:focus {
    border-color: var(--accent);
}

.ctrl-row input[type="range"] {
    flex: 1;
    margin: 0 6px;
    accent-color: var(--accent);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 6px 14px;
    font-size: 12px;
    font-weight: 600;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg-panel);
    color: var(--text-primary);
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}

.btn:hover {
    background: var(--accent-dim);
    border-color: var(--accent);
}

.btn-primary {
    background: var(--accent);
    color: #000;
    border-color: var(--accent);
}

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

.btn-full {
    width: 100%;
}

/* ---- Content area (LEFT + CENTER) ---- */
#content-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    order: 1;
}

/* ---- Top row: images ---- */
#image-row {
    display: flex;
    gap: 2px;
    padding: 8px;
    flex-shrink: 0;
    height: 50%;
    min-height: 200px;
}

.image-panel {
    flex: 1;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 6px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.image-panel .panel-title {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
    padding: 6px 10px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.image-panel canvas {
    flex: 1;
    width: 100%;
    height: 100%;
    object-fit: contain;
}

/* ---- Bottom row: PSD chart + table ---- */
#bottom-row {
    display: flex;
    gap: 2px;
    padding: 0 8px 8px;
    flex: 1;
    min-height: 180px;
}

#psd-panel {
    flex: 1.5;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 6px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

#psd-panel .panel-title {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
    padding: 6px 10px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

#psd-panel canvas {
    flex: 1;
    width: 100%;
    height: 100%;
}

/* ---- Grain table ---- */
#table-panel {
    flex: 1;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 6px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

#table-panel .panel-title {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
    padding: 6px 10px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

#grain-table-wrap {
    flex: 1;
    overflow: auto;
}

#grain-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 11px;
    font-family: var(--font-mono);
}

#grain-table th {
    position: sticky;
    top: 0;
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border);
    padding: 4px 6px;
    text-align: right;
    color: var(--accent);
    font-weight: 600;
}

#grain-table td {
    padding: 3px 6px;
    text-align: right;
    border-bottom: 1px solid var(--border);
    color: var(--text-primary);
}

#grain-table tr:hover td {
    background: var(--accent-dim);
}

/* ---- Metrics bar ---- */
#metrics-bar {
    display: flex;
    gap: 16px;
    padding: 4px 10px;
    font-size: 11px;
    color: var(--text-secondary);
    flex-shrink: 0;
    flex-wrap: wrap;
}

.metric-item {
    display: flex;
    gap: 4px;
}

.metric-item .label { color: var(--text-secondary); }
.metric-item .value { color: var(--accent); font-weight: 600; font-family: var(--font-mono); }

/* ---- Help modal ---- */
.modal-overlay {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.7);
    z-index: 1000;
    justify-content: center;
    align-items: center;
}

.modal-overlay.active {
    display: flex;
}

.modal-content {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 24px;
    max-width: 700px;
    max-height: 80vh;
    overflow-y: auto;
    color: var(--text-primary);
}

.modal-content h2 {
    color: var(--accent);
    margin-bottom: 16px;
    font-size: 18px;
}

.modal-content h3 {
    color: var(--accent);
    margin: 14px 0 6px;
    font-size: 14px;
}

.modal-content p {
    font-size: 13px;
    line-height: 1.6;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.modal-content code {
    background: var(--bg-dark);
    padding: 1px 4px;
    border-radius: 3px;
    font-family: var(--font-mono);
    font-size: 12px;
    color: var(--accent);
}

.modal-content .close-btn {
    float: right;
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 20px;
    cursor: pointer;
}

.modal-content .close-btn:hover {
    color: var(--text-primary);
}

/* ---- Tooltip ---- */
[data-tooltip] {
    position: relative;
}

[data-tooltip]:hover::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: calc(100% + 4px);
    left: 50%;
    transform: translateX(-50%);
    background: var(--bg-dark);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    font-size: 11px;
    padding: 4px 8px;
    border-radius: 4px;
    white-space: nowrap;
    z-index: 999;
    pointer-events: none;
}

/* ---- Scrollbar ---- */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg-dark); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-secondary); }

/* ---- Loading spinner ---- */
.spinner {
    display: none;
    width: 16px;
    height: 16px;
    border: 2px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

.spinner.active { display: inline-block; }

@keyframes spin {
    to { transform: rotate(360deg); }
}
