/* ==========================================================================
   Compact Scientific Workspace, Light/Dark Themes, Header, Views & Accordions
   ========================================================================== */
:root {
    --paper: #eceef0; --panel: #fbfcfc; --plot-bg: #fafafa; --chrome: #d4dfea;
    --header-rule: #8fa8be; --selection: #d3e1ef; --warning-bg: #f4ebd7; --warning-ink: #63430f;
    --dark-bg: #fbfcfc; --medium-bg: #f1f3f4; --light-bg: #e2e5e8;
    --border-color: #c3c9ce; --primary-accent: #1f5e9c; --primary-accent-hover: #184b7e;
    --text-light: #1b1f23; --text-medium: #3f464d; --text-dark: #5a636b;
    color-scheme: light;
}
[data-theme="dark"] {
    --paper: #16181b; --panel: #202327; --plot-bg: #242424; --chrome: #253444; --header-rule: #536e88;
    --dark-bg: #202327; --medium-bg: #2b2f34; --light-bg: #343a40; --border-color: #535c65;
    --primary-accent: #8fb6e0; --primary-accent-hover: #b0ccea;
    --text-light: #e9ecef; --text-medium: #c9ced3; --text-dark: #a3aab1;
    --selection: #34475b; --warning-bg: #3e3525; --warning-ink: #f0d393;
    color-scheme: dark;
}

body.app-shell { background: var(--paper); color: var(--text-light); font-family: 'Inter', 'Segoe UI', system-ui, sans-serif; font-size: 13px; }
input, select, textarea, button { font-family: inherit; }

#app-container { padding: 0; gap: 0; box-sizing: border-box; }
#controls-panel { padding: 16px; background: var(--panel); border: 0; border-right: 1px solid var(--border-color); border-radius: 0; overflow-y: auto; }
#results-area { padding: 0; background: var(--plot-bg); border: 0; border-radius: 0; color: var(--text-light); }
#drag-handle { width: 5px; background: var(--border-color); }

.panels-swapped #controls-panel { order: 3; border-right: 0; border-left: 1px solid var(--border-color); border-radius: 0; }
.panels-swapped #drag-handle { order: 2; }
.panels-swapped #results-area { order: 1; border-radius: 0; }

.control-group { margin-bottom: 12px; padding-top: 12px; }
.control-label { font-size: 12px; margin-bottom: 4px; }
.control-input, .control-select, dialog input, dialog select { background: var(--medium-bg); color: var(--text-light); border-color: var(--border-color); border-radius: 4px; padding: 5px 8px; font-size: 13px; }
.section-title { margin: 0 0 8px; font-size: 14px; font-weight: 600; color: var(--text-light); }

/* Every modal button shares one look, in both themes. */
.btn { font: inherit; font-size: 13px; padding: 5px 14px; border-radius: 4px; border: 1px solid var(--border-color); cursor: pointer; }
.btn-primary { border-color: var(--primary-accent); color: #fff; }
.btn-secondary:hover:not(:disabled) { background: #e1eaf3; color: #17426b; }
[data-theme="dark"] .btn-primary { color: #172431; }
[data-theme="dark"] .btn-secondary:hover:not(:disabled) { background: var(--selection); color: var(--text-light); }
.btn:disabled { background: var(--medium-bg); color: var(--text-dark); opacity: .6; }
.btn:focus-visible, button:focus-visible, summary:focus-visible { outline: 2px solid var(--primary-accent); outline-offset: 2px; }

/* Underlined text tabs */
.tab-buttons { gap: 0; margin-bottom: 12px; border-bottom: 1px solid var(--border-color); }
.tab-btn { padding: 9px 6px; font-size: 13px; gap: 6px; background: none; }
.tab-btn.active { background: none; color: var(--primary-accent); border-bottom-color: var(--primary-accent); }
.tab-btn:hover:not(:disabled) { background: var(--selection); }

.peak-table th { background: var(--light-bg); color: var(--text-medium); }
.ka2-warning-banner { background: var(--warning-bg); color: var(--warning-ink); border: 1px solid var(--border-color); }
.ka2-warning-banner b, .peak-table tbody tr.ka2-suspect input { color: var(--warning-ink); }
.sol-badge.sg { color: #37734c; }
.sol-badge.sg.sg-tied { color: #8b5d16; }
.sol-badge.alt { color: #7d5796; }

/* ---- Header (brand, buttons, menus, Report, primary action, dark switch) ---- */
.workspace-header { display: flex; align-items: center; min-height: 40px; gap: 12px; padding: 0 16px; background: var(--chrome); border-bottom: 1px solid var(--header-rule); color: var(--text-medium); flex-shrink: 0; box-shadow: 0 1px 2px #0002; }
.workspace-brand { color: var(--text-light); font-size: 16px; font-weight: 650; margin-right: 4px; }
.workspace-header button, .workspace-header summary { font: inherit; padding: 5px 10px; cursor: pointer; border: 1px solid var(--border-color); border-radius: 4px; }
.workspace-header .hbtn, .menubar summary { font: inherit; font-size: 13px; padding: 4px 12px; min-height: 28px; box-sizing: border-box; display: inline-flex; align-items: center; color: var(--text-medium); background: none; border: 1px solid transparent; border-radius: 4px; cursor: pointer; text-decoration: none; }
.workspace-header .hbtn:hover:not(:disabled), .menubar summary:hover { color: var(--text-light); border-color: var(--border-color); }
.workspace-header .hbtn:disabled { opacity: .45; cursor: default; }
.workspace-menu { position: relative; margin: 0; }
.workspace-menu summary { list-style: none; }
.workspace-menu.is-disabled > summary { pointer-events: none; opacity: .45; }
.menubar { display: flex; margin-left: auto; }
.menubar summary::after { content: ""; margin-left: 6px; border: 4px solid transparent; border-top-color: currentColor; border-bottom: 0; }
.menubar details[open] > summary { color: var(--primary-accent); border-color: var(--primary-accent); background: var(--selection); }
.menu-items { position: absolute; top: calc(100% + 4px); left: 0; z-index: 4000; min-width: 220px; padding: 4px; background: var(--panel); border: 1px solid var(--border-color); border-radius: 6px; box-shadow: 0 6px 20px #0003; }
.menu-items button { display: block; width: 100%; text-align: left; margin: 0; border: 0 !important; border-radius: 4px; background: none; padding: 8px 12px; color: var(--text-light); font: inherit; cursor: pointer; }
.menu-items button:hover:not(:disabled), .menu-items button:focus { background: var(--selection) !important; color: var(--text-light) !important; outline: none; }
.menu-items button:disabled { color: var(--text-dark); cursor: default; }
.menu-items hr { border: 0; border-top: 1px solid var(--border-color); margin: 4px 8px; }
.workspace-header .fit-btn { font: inherit; font-weight: 600; padding: 5px 16px; min-height: 32px; border-radius: 4px; cursor: pointer; background: var(--primary-accent); color: #fff; border: 1px solid var(--primary-accent); box-shadow: 0 1px 2px #0002; }
.workspace-header .fit-btn:hover:not(:disabled) { background: var(--primary-accent-hover); border-color: var(--primary-accent-hover); }
.workspace-header .fit-btn:disabled { opacity: .5; cursor: default; }
[data-theme="dark"] .workspace-header .fit-btn { color: #172431; }
.theme-switch { display: flex; align-items: center; gap: 8px; margin: 0; color: var(--text-medium); cursor: pointer; }
.theme-switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.theme-switch .track { position: relative; width: 30px; height: 16px; border-radius: 8px; background: var(--border-color); transition: background .15s; }
.theme-switch .track i { position: absolute; top: 2px; left: 2px; width: 12px; height: 12px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px #0004; transition: left .15s; }
.theme-switch input:checked + .track { background: var(--primary-accent); }
.theme-switch input:checked + .track i { left: 16px; }
.theme-switch input:focus-visible + .track { outline: 2px solid var(--primary-accent); outline-offset: 2px; }

/* ---- Index / Stop, and the indexing lock ---- */
.workspace-header .fit-btn.is-stop { background: #c0392b; border-color: #c0392b; color: #fff; }
.workspace-header .fit-btn.is-stop:hover:not(:disabled) { background: #a5301f; border-color: #a5301f; }
[data-theme="dark"] .workspace-header .fit-btn.is-stop { background: #d9604f; border-color: #d9604f; color: #1b1f23; }
.menubar .workspace-menu.is-disabled > summary { opacity: .5; }
body.is-indexing .menubar, body.is-indexing #hb-report,
body.is-indexing #btn-about, body.is-indexing #swap-panels, body.is-indexing .theme-switch { pointer-events: none; opacity: .55; }
body.is-indexing #xrd-chart { cursor: progress; }

/* ---- Right panel: underlined tabs, tool strip, plot, views ---- */
#workspace-views { display: flex; gap: 0; border-bottom: 1px solid var(--border-color); padding: 0 8px; margin: 0; flex-shrink: 0; background: var(--panel); }
#workspace-views button { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: 9px 14px; font: inherit; font-size: 13px; color: var(--text-dark); cursor: pointer; }
#workspace-views button:hover { background: var(--selection); color: var(--text-light); }
#workspace-views button[aria-selected="true"] { color: var(--primary-accent); border-bottom-color: var(--primary-accent); background: none; }
.workspace-view { flex: 1; min-height: 0; overflow: auto; padding: 16px 20px; }
.workspace-view button { background: var(--medium-bg); color: var(--text-light); border: 1px solid var(--border-color); border-radius: 4px; padding: 6px 12px; cursor: pointer; font: inherit; }
.workspace-view h1 { font-size: 17px; margin: 0 0 8px; }
.workspace-view h2 { font-size: 14px; margin: 18px 0 6px; }
[hidden] { display: none !important; }
#workspace-log { padding-left: 22px; }
#workspace-log li { margin-bottom: 10px; white-space: pre-wrap; overflow-wrap: anywhere; }
#workspace-log li[data-type="error"] { color: var(--error-red); }
#workspace-history article { display: flex; align-items: center; justify-content: space-between; gap: 12px; border-bottom: 1px solid var(--border-color); padding: 10px; }
#workspace-history article p { margin: 0; min-width: 0; overflow-wrap: anywhere; }
#workspace-history article button { flex: none; }
.about { max-width: 780px; line-height: 1.55; color: var(--text-light); }
.about a { color: var(--primary-accent); }
.about li { margin-bottom: 4px; }
.hint { color: var(--text-dark); font-size: 12px; }

.plot-pane { display: flex; flex-direction: column; flex: 1 1 auto; min-height: 0; width: 100%; position: relative; }
.plot-host { flex: 1 1 auto; min-height: 0; position: relative; padding: 4px 8px 8px; }
#chart-toolbar.ptools { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; padding: 8px 12px; background: var(--paper) !important; border-bottom: 0 !important; color: var(--text-medium) !important; font-size: 12px !important; }
.ptools .chk { display: inline-flex; align-items: center; gap: 4px; cursor: pointer; }
.ptools .axis-select { font: inherit; padding: 2px 4px; background: var(--medium-bg); color: var(--text-light); border: 1px solid var(--border-color); border-radius: 4px; }
.ptools .tool-sep { width: 1px; height: 18px; background: var(--border-color); }
.ptools > button { font: inherit; padding: 3px 10px; min-height: 26px; background: var(--medium-bg); color: var(--text-light); border: 1px solid var(--border-color); border-radius: 4px; cursor: pointer; }
.ptools > button:hover { background: var(--selection); border-color: var(--primary-accent); }
.seg { display: inline-flex; border: 1px solid var(--border-color); border-radius: 4px; overflow: hidden; background: var(--medium-bg); }
.seg button { font: inherit; border: 0; border-right: 1px solid var(--border-color); border-radius: 0; padding: 3px 12px; min-height: 24px; background: transparent; color: var(--text-light); cursor: pointer; }
.seg button:last-child { border-right: 0; }
.seg button:hover { background: var(--selection); }
.seg button[aria-pressed="true"] { background: var(--primary-accent); color: #fff; font-weight: 600; }
[data-theme="dark"] .seg button[aria-pressed="true"] { color: #172431; }
.info-tip { position: relative; margin-left: auto; }
.info-btn { width: 20px; height: 20px; padding: 0 !important; min-height: 0 !important; border-radius: 50% !important; font-weight: 700; font-style: italic; display: inline-flex; align-items: center; justify-content: center; border: 1px solid var(--border-color); background: var(--medium-bg); color: var(--text-medium); cursor: help; }
.info-pop { position: absolute; right: 0; top: calc(100% + 6px); z-index: 50; display: none; grid-template-columns: auto 1fr; gap: 4px 12px; width: max-content; max-width: min(380px, 80vw); padding: 10px 12px; background: var(--panel); color: var(--text-light); border: 1px solid var(--border-color); border-radius: 6px; box-shadow: 0 6px 20px #0003; font-size: 12px; }
.info-tip:hover .info-pop, .info-tip:focus-within .info-pop { display: grid; }
.placeholder { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; color: var(--text-dark); }
.placeholder-icon { width: 3.5rem; height: 3.5rem; margin-bottom: .75rem; }
.placeholder h2 { margin: 0 0 4px; font-size: 16px; font-weight: 600; color: var(--text-medium); }

/* ---- Left panel: metadata and solution accordions ---- */
.meta-dl, .sol-dl { display: grid; grid-template-columns: max-content 1fr; gap: 3px 12px; margin: 6px 0 0; font-size: 12px; }
.meta-dl dt, .sol-dl dt { color: var(--text-dark); }
.meta-dl dd, .sol-dl dd { margin: 0; color: var(--text-light); overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }
.sol-none { color: var(--text-dark) !important; font-style: italic; }
.sol-toolbar { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.sol-toolbar .control-label { margin: 0; white-space: nowrap; }
.sol-list { display: flex; flex-direction: column; gap: 4px; }
.sol-acc, .meta-acc { border: 1px solid var(--border-color); border-radius: 6px; background: var(--panel); }
.sol-acc[open], .meta-acc[open] { border-color: var(--primary-accent); }
.sol-acc > summary, .meta-acc > summary { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 10px; padding: 7px 10px; cursor: pointer; list-style: none; font-size: 12px; border-radius: 6px; }
.sol-acc > summary::-webkit-details-marker, .meta-acc > summary::-webkit-details-marker { display: none; }
.sol-acc > summary::before, .meta-acc > summary::before { content: ""; align-self: center; border-left: 5px solid var(--text-dark); border-top: 4px solid transparent; border-bottom: 4px solid transparent; transition: transform .12s; }
.sol-acc[open] > summary::before, .meta-acc[open] > summary::before { transform: rotate(90deg); }
.sol-acc > summary:hover, .meta-acc > summary:hover { background: var(--selection); }
.meta-acc > summary { display: block; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meta-acc > summary::before { display: inline-block; margin-right: 8px; }
.meta-acc.is-empty > summary { color: var(--text-dark); font-style: italic; font-weight: 400; pointer-events: none; }
.meta-acc.is-empty > summary::before { display: none; }
.sol-acc[open] > summary, .meta-acc[open] > summary { background: var(--selection); border-bottom: 1px solid var(--border-color); border-radius: 6px 6px 0 0; }
.sol-sys { font-weight: 600; text-transform: capitalize; }
.sol-cell, .sol-v, .sol-m { color: var(--text-medium); font-variant-numeric: tabular-nums; }
.sol-m { margin-left: auto; font-weight: 600; color: var(--text-light); }
.sol-body { padding: 4px 12px 12px; }
.sol-empty { padding: 8px 2px; }
.sol-badge { font-size: 10px; padding: 0 5px; border: 1px solid var(--border-color); border-radius: 8px; color: var(--text-medium); }
.sg-item { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 6px; margin-bottom: 4px; }
.sg-item label { flex: 1 1 auto; cursor: pointer; }
.sg-remove { border: 0; background: none; color: var(--text-dark); cursor: pointer; font-size: 15px; line-height: 1; padding: 0 4px; border-radius: 3px; }
.sg-remove:hover { background: var(--selection); color: var(--text-light); }
.sg-src { flex-basis: 100%; color: var(--text-dark); font-size: 11px; margin-left: 20px; }

/* ---- Status bar ---- */
#workspace-status { display: flex; justify-content: space-between; gap: 0; padding: 6px 0; background: var(--panel); border-top: 1px solid var(--border-color); font-size: 12px; flex-shrink: 0; flex-wrap: nowrap; white-space: nowrap; overflow: hidden; }
#workspace-status > * { padding: 0 12px; box-sizing: border-box; }
#workspace-state { flex: 0 0 var(--split-x, 340px); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#snap-readout { flex: 1 1 auto; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-variant-numeric: tabular-nums; color: var(--text-medium); }
#workspace-solution { margin-left: 0; flex-shrink: 0; font-variant-numeric: tabular-nums; }
#workspace-status #app-footer { position: static; font-size: 11px; z-index: auto; flex-shrink: 0; }
.panels-swapped #snap-readout { order: -1; flex: 0 0 calc(100% - var(--split-x, 340px)); }
.panels-swapped #workspace-state { flex: 1 1 auto; }
#status-box { bottom: 44px; background: var(--medium-bg); color: var(--text-light); border: 1px solid var(--border-color); }

@media (max-width: 760px) {
    body.app-shell { overflow: auto; height: auto; min-height: 100dvh; }
    #app-container { flex-direction: column; }
    #controls-panel { width: auto !important; min-width: 0; max-width: none; overflow-y: auto; }
    #results-area { min-height: 480px; }
    #drag-handle { display: none; }
    .workspace-header { flex-wrap: wrap; padding: 6px 8px; gap: 10px; }
    .menubar { margin-left: 0; }
    .panels-swapped #controls-panel { order: 0; }
    #workspace-status { position: static; }
    #workspace-state, .panels-swapped #workspace-state { flex: 1 1 auto; }
    .panels-swapped #snap-readout { order: 0; flex: 1 1 auto; }
}

/* ==========================================================================
   Contrast, run progress, colour key, run lock, focus and small screens
   ========================================================================== */
:root {
    --input-border: #6f7983;   /* at least 3:1 against the panel and the header */
    --error-ink: #b42318; --ok-ink: #2c6a43; --badge-mc: #1f5e9c;
    /* The plot colours: chart.js reads these, and the tool strip shows them. */
    --plot-data: #626b73; --plot-exp: #c0271f; --plot-sol: #1d5fd1; --plot-sim: #15716a; --plot-bkg: #885d18;
}
[data-theme="dark"] {
    --input-border: #7f8a94; --error-ink: #ff9a8f; --ok-ink: #7fd0a0; --badge-mc: #8fb6e0;
    --plot-data: #c9ced3; --plot-exp: #ff8a80; --plot-sol: #7fb0ff; --plot-sim: #80c9bc; --plot-bkg: #d9b56b;
}
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

/* A field has to be visible as a field: its border, not its 1.1:1 fill, marks it. */
.control-input, .control-select, dialog input, dialog select, .ptools .axis-select { border-color: var(--input-border); }
input[type="range"].custom-slider { border: 1px solid var(--input-border); box-sizing: border-box; }
input[type="range"].custom-slider:focus-visible { outline: 2px solid var(--primary-accent); outline-offset: 2px; }
.theme-switch .track { background: var(--input-border); }
#workspace-log li[data-type="error"] { color: var(--error-ink); }
.sol-badge.mc { color: var(--badge-mc); }
[data-theme="dark"] .sol-badge.sg { color: #7fd0a0; }
[data-theme="dark"] .sol-badge.sg.sg-tied { color: #e6b566; }
[data-theme="dark"] .sol-badge.alt { color: #c9a5e0; }
.delete-peak-btn:hover { color: var(--error-ink); }

/* Run progress: a bar under the header, and the whole status bar for its line. */
#progress-bar-container { height: 4px; background: var(--border-color); flex-shrink: 0; }
#progress-bar { height: 100%; width: 0; background: var(--primary-accent); transition: width .3s linear; }
body.is-indexing #workspace-state { flex: 1 1 auto; color: var(--text-light); font-weight: 600; }
body.is-indexing #snap-readout { display: none; }
#workspace-solution { min-width: 0; flex: 0 1 auto; overflow: hidden; text-overflow: ellipsis; }
#workspace-status #gpu-chip { flex-shrink: 0; align-self: center; margin-right: 8px; padding: 1px 8px; border-radius: 9px; background: var(--warning-bg); color: var(--warning-ink); border: 1px solid currentColor; font-size: 11px; cursor: help; }
.gpu-note { margin: 10px 0 0; padding: 6px 8px; background: var(--warning-bg); color: var(--warning-ink); border-radius: 4px; }

/* During a run only the Solutions tab is shown, and it is the one thing worth
   watching: it stays at full strength and scrollable. Its controls are
   disabled one by one (setUIState), so nothing else has to be greyed out. */
.tab-btn:disabled { opacity: .55; cursor: default; }
.tab-btn:disabled.active { opacity: 1; }

/* Messages: over the empty end of the tab bar, not over the tick marks at the
   foot of the plot. One neutral surface with a coloured edge, so the text has
   the same contrast as the rest of the page in both themes. */
#status-box { top: 52px; bottom: auto; right: 12px; text-align: left; cursor: pointer; padding: 10px 14px 10px 12px; border-left-width: 4px; box-shadow: 0 4px 14px #0003; }
#status-box.success, #status-box.error, #status-box.info { background: var(--panel); color: var(--text-light); }
#status-box.success { border-left-color: var(--ok-ink); }
#status-box.info { border-left-color: var(--primary-accent); }
#status-box.error { border-left-color: var(--error-ink); padding-right: 30px; }
#status-box .status-action { margin-left: 10px; font: inherit; font-weight: 600; padding: 2px 10px; border-radius: 4px; cursor: pointer; background: var(--medium-bg); color: var(--primary-accent); border: 1px solid var(--input-border); }
#status-box .status-action:hover { background: var(--selection); }
#status-box.error::after { content: "\00d7"; position: absolute; top: 5px; right: 10px; font-size: 16px; line-height: 1; color: var(--text-dark); }

/* Colour key of the plot, on the toggles that show and hide each layer. */
.swatch { display: inline-block; flex: none; width: 3px; height: 12px; border-radius: 1px; background: var(--c); }
.swatch.swatch-line { width: 14px; height: 2px; }

/* 2-theta limits can be typed as well as dragged. */
.slider-group.has-entry { grid-template-columns: minmax(56px, auto) 1fr 78px; }
.slider-group.has-entry .tth-entry { grid-column: 3; padding: 4px 6px; text-align: right; font-variant-numeric: tabular-nums; }

/* The bar beside each crystal system once told GPU systems from CPU ones; every
   system runs on the GPU now, so it marked all six and meant nothing. */
p.control-label { margin-top: 0; }
@media (min-width: 761px) { #controls-panel { max-width: min(700px, calc(100vw - 382px)); } }   /* a dragged panel cannot squeeze the plot out of a narrower window */
.checkbox-label.system-gpu { padding-left: 0; }
.checkbox-label.system-gpu::before { content: none; }

.sol-ang { color: var(--text-dark); }
.sol-dl .sol-fom { color: var(--text-medium); }

@media (pointer: coarse) {
    .workspace-header .hbtn, .menubar summary, .workspace-header .fit-btn, .seg button, .ptools > button, .ptools .axis-select, #workspace-views button { min-height: 40px; }
    .info-btn { width: 32px; height: 32px; }
}
