/* Additions for OpenDPS control on top of the Qianli monitor stylesheet.
 * Everything reuses that file's custom properties; nothing is redefined.
 * Load after style.css. */

/* The original is a fixed-viewport dashboard (body: 100vh, overflow hidden).
 * Keep that: the layout is header + dashboard + chart, nothing below. */

/* ---- header ------------------------------------------------------------- */

/* style.css gives header a backdrop-filter, which creates a stacking context.
 * The device tooltip lives inside the header, so its z-index only orders it
 * *within* that context - and .dashboard is a later sibling, so the panels
 * painted straight over it (and blurred it through their own backdrop-filter).
 * Lifting the whole header above them is what actually puts the tooltip on
 * top. */
header {
    position: relative;
    z-index: 100;
}

/* style.css only lays .header-controls out at its breakpoints; the base case
 * relies on inline-block flow, which a flex child breaks. Make it explicit. */
.header-controls {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 8px;
}

.baud-group {
    display: flex;
    align-items: center;
    gap: 8px;
    height: 38px;
    /* matches .btn: 10px padding + line box + 1px border */
    padding: 0 12px;
    background: var(--btn-bg);
    border: 1px solid var(--btn-border);
    border-radius: 8px;
}

.baud-group label {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.baud-group select {
    padding: 0;
    background: transparent;
    border: 0;
    color: var(--text-main);
    font-family: 'Share Tech Mono', monospace;
    font-size: 0.9rem;
    outline: none;
    cursor: pointer;
}

.baud-group select option {
    background: var(--bg-input);
}

/* Output button reads as state, not just an action. */
#btn-out.is-on {
    background: var(--color-voltage-bg);
    border-color: var(--color-voltage-border);
    color: var(--color-voltage);
}

/* ---- device tooltip on the status indicator ---------------------------- */

.status-indicator {
    position: relative;
    cursor: default;
    outline: none;
}

/* Only offer the tooltip when there is a device behind it to describe. */
.status-indicator.has-device {
    cursor: help;
}

.device-tip {
    /* style.css defines .u-tooltip as display:none, shown only when the chart
     * plugin adds .visible. We borrow the look but drive it from :hover, so
     * the base display has to be restored - animating opacity alone leaves a
     * 0x0 box that never appears. */
    display: block;
    position: absolute;
    top: calc(100% + 10px);
    left: 0;
    z-index: 1500;
    min-width: 230px;
    text-align: left;
    /* Fully opaque: this floats over the big readouts, and .u-tooltip's 90%
     * background lets them show through and blur the numbers. */
    background: var(--bg-elevated);
    backdrop-filter: none;
    border: 1px solid var(--border-medium);
    box-shadow: 0 8px 24px var(--shadow-heavy);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.12s ease, visibility 0.12s;
}

/* Hover only reveals it while a device is attached; with none there is
 * nothing to report but dashes. */
.status-indicator.has-device:hover .device-tip,
.status-indicator.has-device:focus .device-tip,
.status-indicator.has-device:focus-within .device-tip {
    opacity: 1;
    visibility: visible;
}

.device-tip .u-tooltip-row {
    justify-content: space-between;
    gap: 16px;
}

.device-tip .u-tooltip-row span:last-child {
    color: var(--text-main);
}

/* ---- setpoint row inside the value panels ------------------------------ */

.setpoint {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 12px;
    min-height: 30px;
    font-family: 'Share Tech Mono', monospace;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.setpoint b {
    color: var(--text-main);
    font-weight: 400;
}

.voltage-panel .setpoint b {
    color: var(--color-voltage);
}

.current-panel .setpoint b {
    color: var(--color-current);
}

.power-panel .setpoint b {
    color: var(--color-power);
}

.setpoint-editor {
    display: flex;
    align-items: center;
    gap: 6px;
}

.setpoint-editor.hidden {
    display: none;
}

.setpoint-editor input {
    width: 7.5em;
    padding: 5px 8px;
    background: var(--bg-input);
    color: var(--text-main);
    border: 1px solid var(--border-medium);
    border-radius: 4px;
    font-family: 'Share Tech Mono', monospace;
    outline: none;
}

.setpoint-editor input:focus {
    border-color: var(--color-voltage-border);
}

/* ---- fault indication ---------------------------------------------------
 * A tripped OCP or an output collapsed at the current limit turns the two
 * panels that matter red; ordinary CC limiting only tints the current panel,
 * since running in CC is normal operation rather than a fault. */

.dashboard.fault .voltage-panel,
.dashboard.fault .current-panel {
    border-color: var(--color-danger);
    background:
        linear-gradient(var(--color-danger-bg), var(--color-danger-bg)),
        var(--bg-panel);
    animation: fault-pulse 1.3s ease-in-out infinite;
}

.dashboard.fault .voltage-panel .panel-value,
.dashboard.fault .current-panel .panel-value {
    color: var(--color-danger);
}

@keyframes fault-pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(255, 85, 85, 0); }
    50% { box-shadow: 0 0 22px 0 rgba(255, 85, 85, 0.35); }
}

/* Constant-current: informative, not alarming. */
.dashboard.limiting .current-panel {
    border-color: var(--color-current);
    box-shadow: 0 0 16px 0 var(--color-current-glow);
}

@media (prefers-reduced-motion: reduce) {
    .dashboard.fault .voltage-panel,
    .dashboard.fault .current-panel {
        animation: none;
        box-shadow: 0 0 22px 0 rgba(255, 85, 85, 0.35);
    }
}

/* Output off: the big numbers are the configured setpoints, not measurements. */
.dashboard.showing-setpoints .panel-value {
    opacity: 0.45;
}

.dashboard.showing-setpoints .setpoint-label {
    color: var(--text-main);
}

.badge {
    padding: 3px 9px;
    font-family: 'Share Tech Mono', monospace;
    font-size: 0.72rem;
    color: var(--text-muted);
    border: 1px solid var(--border-medium);
    border-radius: 6px;
}

.badge.cv {
    color: var(--color-voltage);
    border-color: var(--color-voltage-border);
}

.badge.cc {
    color: var(--color-current);
    border-color: var(--color-current-border);
}

/* ---- graph controls ----------------------------------------------------- */

.legend-key i {
    width: 14px;
    height: 2px;
    display: inline-block;
    border-radius: 1px;
}

.control-group.hint {
    margin-left: auto;
}

.control-group.hint label {
    font-size: 0.78rem;
    opacity: 0.7;
}

/* ---- calibration modal -------------------------------------------------- */

.modal {
    position: fixed;
    inset: 0;
    z-index: 2000;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 24px;
    overflow-y: auto;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
}

.modal.hidden {
    display: none;
}

.modal-card {
    width: min(780px, 100%);
    padding: 24px;
    background: var(--bg-elevated);
    border: 1px solid var(--border-subtle);
    border-radius: 16px;
    box-shadow: 0 8px 40px var(--shadow-heavy);
}

.modal-card h3 {
    margin: 18px 0 6px;
    font-size: 1rem;
}

.modal-card p {
    color: var(--text-muted);
    font-size: 0.85rem;
    line-height: 1.6;
}

.modal-card code {
    color: var(--color-voltage);
    font-family: 'Share Tech Mono', monospace;
}

.modal-card hr {
    border: 0;
    border-top: 1px solid var(--border-subtle);
    margin: 20px 0;
}

.modal-card .row {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    align-items: center;
    margin-top: 12px;
}

.modal-card input {
    background: var(--bg-input);
    color: var(--text-main);
    border: 1px solid var(--border-medium);
    border-radius: 4px;
    padding: 6px 10px;
    font-family: 'Share Tech Mono', monospace;
    outline: none;
}

.sub {
    margin-top: 12px;
    font-family: 'Share Tech Mono', monospace;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.sub b {
    color: var(--text-main);
    font-weight: 400;
}

/* Outcomes reported inside the calibration dialog, which covers the status
 * indicator the rest of the UI uses. */
.sub.err {
    color: var(--color-danger);
}

.sub.ok {
    color: var(--color-voltage);
}

.modal-card input.invalid {
    border-color: var(--color-danger-border);
    background: var(--color-danger-bg);
}

/* Guided calibration */
#guide-panel.hidden,
#guide-cancel.hidden {
    display: none;
}

/* `hidden` is not a blanket utility in this sheet.
 *
 * Two of its users - .marker-editor and .drag-overlay - fade out through
 * opacity and pointer-events, and a global `.hidden { display: none }` would
 * cut those transitions dead. So everything that hides outright opts in by
 * name. Miss one and the class silently does nothing, which is exactly how the
 * resistor field stayed on screen in electronic-load mode. */
.control-group.hidden,
#resistor-note.hidden,
#cc-note.hidden {
    display: none;
}

#guide-panel {
    margin-top: 12px;
    padding: 14px 16px;
    background: var(--color-voltage-bg);
    border: 1px solid var(--color-voltage-border);
    border-radius: 8px;
}

#guide-step {
    margin-top: 0;
    color: var(--text-main);
}

#guide-step b {
    color: var(--color-voltage);
}

#guide-progress {
    margin-top: 0;
}

.modal-card p.warn {
    padding: 10px 14px;
    background: var(--color-current-bg);
    border: 1px solid var(--color-current-border);
    border-radius: 8px;
}

.modal-card p.warn b {
    color: var(--color-current);
}

#load-budget {
    color: var(--color-current);
}

.sub.warn-range {
    color: var(--color-current);
}

.manual {
    margin-top: 18px;
    padding-top: 8px;
    border-top: 1px solid var(--border-subtle);
}

.manual summary {
    cursor: pointer;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.manual summary:hover {
    color: var(--text-main);
}

table.cal {
    width: 100%;
    border-collapse: collapse;
    font-family: 'Share Tech Mono', monospace;
    font-size: 0.85rem;
    margin-top: 10px;
}

table.cal th,
table.cal td {
    padding: 6px 8px;
    text-align: left;
    border-bottom: 1px solid var(--border-subtle);
}

table.cal th {
    color: var(--text-muted);
    font-weight: 400;
}

table.cal input {
    width: 100%;
}

@media (max-width: 900px) {
    .setpoint {
        justify-content: flex-start;
    }

    .control-group.hint {
        display: none;
    }
}
