/* ===== Light theme by default ===== */
:root {
    --bg: #f3f6fa;
    --bg-accent: linear-gradient(180deg, #f3f6fa 0%, #ffffff 100%);
    --card: #ffffff;
    --card-border: rgba(0, 0, 0, 0.08);
    --metric-bg: #f7f9fc;
    --text: #1a2332;
    --dim: #6b7d92;
    --accent: #1976d2;
    --green: #2e9e5a;
    --red: #d63031;
    --orange: #e67e22;
    --blue: #2979ff;
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    --radius: 12px;
    --font-mono: "SF Mono", "Menlo", "Consolas", "Liberation Mono", monospace;
}

/* Dark theme opt-in */
body.dark {
    --bg: #0d1520;
    --bg-accent: linear-gradient(180deg, #0d1520 0%, #111d2e 100%);
    --card: rgba(16, 28, 44, 0.95);
    --card-border: rgba(120, 159, 184, 0.15);
    --metric-bg: rgba(20, 38, 58, 0.9);
    --text: #e8eef5;
    --dim: #8199af;
    --accent: #64b5f6;
    --green: #4caf50;
    --red: #ef5350;
    --orange: #ffb74d;
    --blue: #42a5f5;
    --shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

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

html {
    font-size: 15px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "PingFang SC", "Noto Sans SC", sans-serif;
    background: var(--bg-accent);
    background-color: var(--bg);
    color: var(--text);
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
    line-height: 1.5;
}

a {
    color: inherit;
    text-decoration: none;
}

button, input {
    font: inherit;
}

/* ===== Connecting overlay ===== */
.connecting-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.88);
    backdrop-filter: blur(4px);
    z-index: 200;
    border-radius: var(--radius);
    gap: 12px;
}

body.dark .connecting-overlay {
    background: rgba(13, 21, 32, 0.88);
}

.connecting-overlay.hidden {
    display: none;
}

.connecting-spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--card-border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

.connecting-text {
    font-size: 0.93rem;
    color: var(--dim);
    font-weight: 600;
}

/* ===== Container ===== */
.container {
    position: relative;
    width: min(100%, 540px);
    margin: 0 auto;
    padding: 60px 12px 40px;
}

.debug-container {
    width: min(100%, 1200px);
}

/* Mobile status bar takes ~2 rows → bump container top padding */
@media (max-width: 560px) {
    .container { padding-top: 82px; }
}

/* ===== Fixed status bar ===== */
.status-bar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 6px;
    padding: 6px 12px;
    background: var(--card);
    border-bottom: 1px solid var(--card-border);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
    min-height: 44px;
}

.status-bar-left {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.status-title {
    font-size: 0.88rem;
    font-weight: 700;
    color: var(--text);
}

.status-bar-metrics {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.sbm {
    font-family: var(--font-mono);
    font-size: 0.88rem;
    font-weight: 700;
    color: var(--text);
    white-space: nowrap;
}

.sbm.abnormal {
    color: var(--red);
    font-weight: 800;
}

.sbm small {
    font-size: 0.68em;
    color: var(--dim);
    font-weight: 500;
    margin-left: 1px;
}

.status-bar-actions {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

.nav-pill {
    padding: 4px 10px;
    border-radius: 999px;
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--dim);
    border: 1px solid var(--card-border);
    transition: all 0.15s ease;
}

.nav-pill.active {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}

/* ===== Section label ===== */
.section-label {
    font-size: 0.72rem;
    font-weight: 700;
    color: var(--dim);
    text-transform: uppercase;
    letter-spacing: 0.14em;
    margin: 16px 0 8px 4px;
}

/* ===== Device matrix ===== */
/* === Dashboard gauges === */
.dashboard {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    margin-bottom: 16px;
}
.gauge {
    background: var(--card);
    border-radius: 14px;
    padding: 14px 10px;
    text-align: center;
    box-shadow: var(--shadow);
    transition: background 0.2s;
}
.gauge-label {
    font-size: 0.75rem;
    color: var(--dim);
    font-weight: 600;
    margin-bottom: 4px;
}
.gauge-value {
    font-size: 1.8rem;
    font-weight: 800;
    font-family: "SF Mono", "Menlo", "Consolas", monospace;
    color: var(--text);
    line-height: 1.1;
}
.gauge-value.abnormal {
    color: #e53935;
}
.gauge-unit {
    font-size: 0.7rem;
    color: var(--dim);
    margin-top: 2px;
}
@media (max-width: 480px) {
    .dashboard { grid-template-columns: repeat(2, 1fr); }
    .gauge-value { font-size: 1.4rem; }
}

.device-matrix {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 14px;
}

.dev-chip {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 12px;
    background: var(--card);
    border: 1px solid var(--card-border);
    border-radius: 10px;
    box-shadow: var(--shadow);
    flex: 1 1 90px;
    min-width: 90px;
    transition: opacity 0.2s;
}

.dev-chip.offline {
    opacity: 0.5;
    background: var(--metric-bg);
}

.dev-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
    background: var(--dim);
}

.dev-dot.on {
    background: var(--green);
    box-shadow: 0 0 6px rgba(46, 158, 90, 0.4);
}

.dev-dot.off {
    background: var(--red);
    box-shadow: 0 0 6px rgba(214, 48, 49, 0.3);
}

.dev-name {
    font-size: 0.72rem;
    color: var(--dim);
    white-space: nowrap;
}

.dev-val {
    font-family: var(--font-mono);
    font-size: 0.78rem;
    font-weight: 700;
    color: var(--text);
    margin-left: auto;
    white-space: nowrap;
}

.dev-chip.offline .dev-val {
    color: var(--red);
}

/* ===== Cards ===== */
.card {
    background: var(--card);
    border: 1px solid var(--card-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 14px;
    margin-bottom: 10px;
}

.card.no-margin {
    margin-bottom: 0;
    border-top: none;
    border-radius: 0 0 var(--radius) var(--radius);
}

.card-title {
    font-size: 0.74rem;
    font-weight: 700;
    color: var(--dim);
    text-transform: uppercase;
    letter-spacing: 0.14em;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.card-subtitle {
    font-size: 0.66rem;
    font-weight: 500;
    color: var(--dim);
    text-transform: none;
    letter-spacing: 0.02em;
    opacity: 0.8;
}

.miner-all-off-btn {
    margin-left: auto;
}

/* ===== Data-source badge (generator power card) ===== */
.source-line {
    font-size: 0.72rem;
    color: var(--dim);
    margin: -4px 0 10px;
    display: flex;
    align-items: center;
    gap: 6px;
}
.source-badge {
    display: inline-flex;
    align-items: center;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 0.72rem;
    font-weight: 600;
    border: 1px solid transparent;
}
.source-badge.ok {
    background: rgba(52, 168, 83, 0.12);
    color: var(--green);
    border-color: rgba(52, 168, 83, 0.35);
}
.source-badge.warn {
    background: rgba(255, 179, 0, 0.15);
    color: var(--orange);
    border-color: rgba(255, 179, 0, 0.4);
}
.source-badge.err {
    background: rgba(229, 57, 53, 0.12);
    color: var(--red);
    border-color: rgba(229, 57, 53, 0.35);
}
.source-badge.off {
    background: var(--metric-bg);
    color: var(--dim);
    border-color: var(--card-border);
}

/* ===== Collapsible details ===== */
.detail-section {
    margin-bottom: 10px;
}

.detail-summary {
    display: flex;
    align-items: center;
    padding: 10px 14px;
    background: var(--card);
    border: 1px solid var(--card-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    cursor: pointer;
    font-size: 0.82rem;
    font-weight: 700;
    color: var(--dim);
    letter-spacing: 0.06em;
    user-select: none;
    list-style: none;
    gap: 8px;
}

.detail-summary::-webkit-details-marker {
    display: none;
}

.detail-summary::before {
    content: "\25B6";
    font-size: 0.6rem;
    color: var(--dim);
    transition: transform 0.2s;
}

details[open] > .detail-summary {
    border-radius: var(--radius) var(--radius) 0 0;
    margin-bottom: 0;
}

details[open] > .detail-summary::before {
    transform: rotate(90deg);
}

/* ===== Layout helpers ===== */
.row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 8px;
}

.col1 { width: 100%; }
.col2 { width: calc(50% - 4px); }
.col3 { width: calc(33.333% - 5.34px); }

.m {
    background: var(--metric-bg);
    border: 1px solid var(--card-border);
    border-radius: 10px;
    padding: 10px 12px;
    min-height: 64px;
}

.m.wide {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.m .lb {
    font-size: 0.72rem;
    color: var(--dim);
    margin-bottom: 2px;
}

.m .v {
    font-size: 1rem;
    font-weight: 700;
    color: var(--text);
    font-family: var(--font-mono);
}

.m .v.hi { color: var(--accent); font-size: 1.1rem; }
.m .v.ok { color: var(--green); }
.m .v.err { color: var(--red); }
.m .v.warn { color: var(--orange); }
.m .v.off { color: var(--dim); }

.u {
    font-size: 0.62em;
    color: var(--dim);
    font-weight: 500;
    margin-left: 2px;
}

/* ===== Breaker row ===== */
.breaker-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.breaker-label {
    flex: 1;
}

.breaker-label .lb {
    font-size: 0.78rem;
    color: var(--dim);
    margin-bottom: 2px;
}

.breaker-label .v {
    font-size: 1.1rem;
    font-weight: 800;
    font-family: var(--font-mono);
}

.breaker-btns {
    display: flex;
    gap: 8px;
}

.breaker-btns .b {
    min-width: 64px;
    min-height: 44px;
}

/* ===== Buttons & inputs ===== */
.btns, .inp, .controls-grid {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    margin-top: 10px;
}

.controls-grid {
    margin-top: 0;
    margin-bottom: 10px;
}

.inp.wrap { align-items: flex-start; }

.inp label, .helper-text {
    font-size: 0.78rem;
    color: var(--dim);
}

.helper-line {
    margin-top: 8px;
    font-size: 0.76rem;
    color: var(--dim);
    line-height: 1.5;
}

.b {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    border: none;
    border-radius: 10px;
    padding: 11px 14px;
    font-size: 0.9rem;
    font-weight: 700;
    color: #fff;
    cursor: pointer;
    min-height: 44px;
    min-width: 44px;
    transition: transform 0.1s ease, opacity 0.1s ease;
}

.b:active { transform: scale(0.97); }
.b:disabled { opacity: 0.45; cursor: not-allowed; }
.b.green { background: var(--green); }
.b.red { background: var(--red); }
.b.blue { background: var(--blue); }
.b.orange { background: var(--orange); color: #1d1200; }
.b.gray { background: rgba(107, 125, 146, 0.18); color: var(--text); }

.b.sm {
    padding: 8px 12px;
    font-size: 0.8rem;
    border-radius: 8px;
    min-height: 36px;
}

.b.xs {
    padding: 5px 10px;
    font-size: 0.76rem;
    border-radius: 6px;
    min-height: 28px;
    min-width: 36px;
}

/* ===== Badge ===== */
.badge {
    display: inline-flex;
    align-items: center;
    padding: 2px 7px;
    border-radius: 999px;
    background: rgba(25, 118, 210, 0.1);
    border: 1px solid rgba(25, 118, 210, 0.2);
    color: var(--accent);
    font-size: 0.68rem;
    font-weight: 600;
}

.mono {
    font-family: var(--font-mono);
}

/* ===== Theme toggle ===== */
.theme-btn,
.icon-btn {
    width: 34px;
    height: 34px;
    border-radius: 999px;
    border: 1px solid var(--card-border);
    background: transparent;
    color: var(--text);
    cursor: pointer;
    font-size: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    line-height: 1;
}
.icon-btn:hover { background: var(--metric-bg); }
.icon-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.icon-btn.spinning { animation: spin 0.8s linear infinite; }
.icon-btn.small { width: 24px; height: 24px; font-size: 0.95rem; }

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

/* ===== Update banner (PWA 新版本提示) ===== */
.update-banner {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    bottom: max(18px, env(safe-area-inset-bottom, 0px));
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    background: var(--card);
    border: 1px solid var(--card-border);
    border-radius: 999px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
    z-index: 280;
    max-width: calc(100vw - 24px);
}
.update-banner[hidden] { display: none; }
.update-banner-text {
    font-size: 0.88rem;
    font-weight: 700;
    color: var(--text);
}
.update-banner .b.sm { min-height: 32px; padding: 6px 14px; }

/* ===== Inputs ===== */
input[type="number"],
input[type="text"],
input[type="password"],
input[type="file"] {
    width: 100%;
    padding: 10px 12px;
    border-radius: 10px;
    border: 1px solid var(--card-border);
    background: var(--metric-bg);
    color: var(--text);
    outline: none;
    font-size: 0.93rem;
    min-height: 44px;
}

.inp input[type="number"] {
    width: 88px;
}

input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(25, 118, 210, 0.15);
}

/* ===== Three-phase tables - FIXED layout ===== */
.ph-tbl, .data-table {
    width: 100%;
    border-collapse: collapse;
}

.fixed-tbl {
    table-layout: fixed;
}

/* 4-column tables (phase + V + I + P) */
.fixed-tbl.cols-4 .col-phase { width: 10%; }
.fixed-tbl.cols-4 .col-v { width: 30%; }
.fixed-tbl.cols-4 .col-i { width: 30%; }
.fixed-tbl.cols-4 .col-p { width: 30%; }

/* 6-column tables (phase + V + I + P + THD_V + THD_I) */
.fixed-tbl.cols-6 .col-phase { width: 8%; }
.fixed-tbl.cols-6 .col-v { width: 18%; }
.fixed-tbl.cols-6 .col-i { width: 18%; }
.fixed-tbl.cols-6 .col-p { width: 18%; }
.fixed-tbl.cols-6 .col-thd { width: 19%; }

/* 3-column tables (phase + V + P) */
.fixed-tbl.cols-3 .col-phase { width: 12%; }
.fixed-tbl.cols-3 .col-v { width: 44%; }
.fixed-tbl.cols-3 .col-p { width: 44%; }

.ph-tbl th, .data-table th {
    text-align: center;
    font-size: 0.72rem;
    color: var(--dim);
    padding: 8px 4px;
    font-weight: 600;
    border-bottom: 2px solid var(--card-border);
}

.ph-tbl td, .data-table td {
    text-align: center;
    padding: 8px 4px;
    font-family: var(--font-mono);
    font-weight: 700;
    font-size: 0.88rem;
    border-top: 1px solid var(--card-border);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Phase colors - high contrast for light theme */
.ph-tbl .pa td { color: #b8860b; }
.ph-tbl .pb td { color: #1565c0; }
.ph-tbl .pc td { color: #7b1fa2; }
.ph-tbl .total td { color: var(--text); font-weight: 800; }

body.dark .ph-tbl .pa td { color: #ffd54f; }
body.dark .ph-tbl .pb td { color: #64b5f6; }
body.dark .ph-tbl .pc td { color: #ce93d8; }

.table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* ===== Connection dot ===== */
.conn-dot {
    display: inline-block;
    width: 9px;
    height: 9px;
    border-radius: 50%;
}

.conn-dot.on {
    background: var(--green);
    box-shadow: 0 0 8px rgba(46, 158, 90, 0.5);
}

.conn-dot.off {
    background: var(--red);
    box-shadow: 0 0 8px rgba(214, 48, 49, 0.4);
}

/* ===== Miner compact grid ===== */
.miner-compact {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.miner-chip {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 10px;
    background: var(--metric-bg);
    border: 1px solid var(--card-border);
    border-radius: 8px;
    flex: 1 1 80px;
    min-width: 80px;
}

.miner-chip .mc-label {
    font-size: 0.72rem;
    color: var(--dim);
    font-weight: 600;
}

.miner-chip .mc-status {
    font-size: 0.78rem;
    font-weight: 700;
    font-family: var(--font-mono);
}

/* ===== Log area ===== */
.log-area {
    max-height: 200px;
    overflow: auto;
    background: var(--metric-bg);
    border: 1px solid var(--card-border);
    border-radius: 10px;
    padding: 10px;
    font-family: var(--font-mono);
    font-size: 0.74rem;
    line-height: 1.7;
}

.log-area.tall { max-height: 420px; }
.log-info { color: var(--dim); }
.log-ok { color: var(--green); }
.log-err { color: var(--red); }

/* ===== Stack layout ===== */
.stack {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.miner-item, .miner-net-item { min-height: auto; }

.mini-actions {
    display: flex;
    gap: 6px;
}

/* ===== Load bank (cab) card — summary + two blocks ===== */
.cab-summary {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
    margin-bottom: 10px;
}

.cab-summary-item {
    background: var(--metric-bg);
    border: 1px solid var(--card-border);
    border-radius: 10px;
    padding: 8px 10px;
    text-align: center;
}

.cab-summary-label {
    font-size: 0.66rem;
    color: var(--dim);
    letter-spacing: 0.08em;
    text-transform: uppercase;
    margin-bottom: 2px;
}

.cab-summary-value {
    font-family: var(--font-mono);
    font-size: 1.1rem;
    font-weight: 800;
    color: var(--text);
    line-height: 1.1;
}
.cab-summary-value .u {
    font-size: 0.58em;
    color: var(--dim);
    font-weight: 500;
    margin-left: 2px;
}

.cab-block {
    margin-top: 10px;
}

.cab-block-head {
    font-size: 0.66rem;
    color: var(--dim);
    letter-spacing: 0.14em;
    text-transform: uppercase;
    margin-bottom: 6px;
    padding-left: 2px;
}

.cab-tbl th,
.cab-tbl td {
    text-align: right;
    padding: 6px 8px;
}
.cab-tbl .col-phase,
.cab-tbl td:first-child {
    text-align: left;
    width: 36px;
}

/* Diff cell coloring */
.diff-ok    { color: var(--green); }
.diff-warn  { color: var(--orange); }
.diff-pending { opacity: 0.55; font-style: italic; }

/* Mobile tweaks for cab summary + tables */
@media (max-width: 560px) {
    .cab-summary { gap: 6px; }
    .cab-summary-item { padding: 6px 8px; }
    .cab-summary-value { font-size: 0.98rem; }
    .cab-tbl th, .cab-tbl td { padding: 5px 4px; font-size: 0.84rem; }
    .cab-tbl th { font-size: 0.68rem; }
}

@media (max-width: 380px) {
    .cab-summary { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    .cab-summary-value { font-size: 0.88rem; }
}

/* ===== Miner contactor tiles (v2) — 固定 3 列，desktop + mobile 一致 ===== */
.miner-compact-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
}

.miner-tile {
    background: var(--metric-bg);
    border: 1px solid var(--card-border);
    border-left: 3px solid var(--card-border);
    border-radius: 10px;
    padding: 9px 10px 8px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
    transition: border-color 0.15s, background 0.15s;
}

.miner-tile.empty {
    color: var(--dim);
    justify-content: center;
    min-height: 90px;
}

.miner-tile[data-state="running"] {
    border-left-color: var(--green);
    background:
        linear-gradient(180deg, rgba(52, 168, 83, 0.06), transparent 40%),
        var(--metric-bg);
}
.miner-tile[data-state="powered"] {
    border-left-color: #ffb300;
    background:
        linear-gradient(180deg, rgba(255, 179, 0, 0.06), transparent 40%),
        var(--metric-bg);
}
.miner-tile[data-state="tripped"] {
    border-left-color: var(--red);
    background:
        linear-gradient(180deg, rgba(229, 57, 53, 0.08), transparent 40%),
        var(--metric-bg);
}

.miner-tile-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.miner-tile-label {
    font-size: 0.78rem;
    color: var(--dim);
    font-family: var(--font-mono);
    font-weight: 700;
    letter-spacing: 0.04em;
}

.miner-tile-led {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--dim);
    opacity: 0.55;
}
.miner-tile[data-state="running"] .miner-tile-led {
    background: var(--green);
    box-shadow: 0 0 8px rgba(52, 168, 83, 0.6);
    opacity: 1;
    animation: miner-pulse-g 2.2s ease-in-out infinite;
}
.miner-tile[data-state="powered"] .miner-tile-led {
    background: #ffb300;
    box-shadow: 0 0 6px rgba(255, 179, 0, 0.55);
    opacity: 1;
}
.miner-tile[data-state="tripped"] .miner-tile-led {
    background: var(--red);
    box-shadow: 0 0 8px rgba(229, 57, 53, 0.7);
    opacity: 1;
    animation: miner-pulse-r 1.2s ease-in-out infinite;
}

@keyframes miner-pulse-g { 0%,100% { opacity: 1 } 50% { opacity: 0.55 } }
@keyframes miner-pulse-r { 0%,100% { opacity: 1 } 50% { opacity: 0.35 } }

.miner-tile-status {
    font-size: 1.02rem;
    font-weight: 800;
    line-height: 1.1;
    text-align: center;
    padding: 4px 0 6px;
    letter-spacing: 0.08em;
    color: var(--dim);
}
.miner-tile[data-state="running"] .miner-tile-status { color: var(--green); }
.miner-tile[data-state="powered"] .miner-tile-status { color: #c98100; }
.miner-tile[data-state="tripped"] .miner-tile-status { color: var(--red); }

.miner-tile-actions {
    display: flex;
    gap: 4px;
}
.miner-tile-actions .tile-btn {
    flex: 1 1 0;
    min-width: 0;
    padding: 5px 0;
    min-height: 28px;
    font-size: 0.78rem;
    font-weight: 700;
    border-radius: 6px;
    border: 1px solid var(--card-border);
    background: var(--card);
    color: var(--text);
    cursor: pointer;
    transition: background 0.12s, transform 0.1s;
}
.miner-tile-actions .tile-btn.on  { color: var(--green); border-color: rgba(52, 168, 83, 0.35); }
.miner-tile-actions .tile-btn.off { color: var(--red);   border-color: rgba(229, 57, 53, 0.35); }
.miner-tile-actions .tile-btn.on:hover  { background: rgba(52, 168, 83, 0.1); }
.miner-tile-actions .tile-btn.off:hover { background: rgba(229, 57, 53, 0.1); }
.miner-tile-actions .tile-btn:active { transform: scale(0.96); }

.miner-all-off-btn { padding: 3px 10px; font-size: 0.72rem; min-height: 26px; }

/* ===== Water gate card (v2) ===== */
.gate-card .gate-limits {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin-bottom: 12px;
}
.gate-limit {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    background: var(--metric-bg);
    border: 1px solid var(--card-border);
    border-left: 3px solid var(--card-border);
    border-radius: 10px;
    transition: border-color 0.15s;
}
.gate-limit[data-state="hit"] {
    border-left-color: #ffb300;
    background: linear-gradient(180deg, rgba(255, 179, 0, 0.06), transparent 60%), var(--metric-bg);
}
.gate-limit[data-state="safe"] {
    border-left-color: var(--green);
}
.limit-led {
    width: 12px; height: 12px; border-radius: 50%;
    background: var(--dim); opacity: 0.5;
    flex-shrink: 0;
}
.gate-limit[data-state="hit"] .limit-led {
    background: #ffb300;
    box-shadow: 0 0 8px rgba(255, 179, 0, 0.55);
    opacity: 1;
}
.gate-limit[data-state="safe"] .limit-led {
    background: var(--green);
    box-shadow: 0 0 6px rgba(52, 168, 83, 0.4);
    opacity: 1;
}
.limit-body { min-width: 0; }
.limit-label {
    font-size: 0.68rem;
    color: var(--dim);
    letter-spacing: 0.08em;
    text-transform: uppercase;
}
.limit-status {
    font-size: 0.96rem;
    font-weight: 700;
    color: var(--text);
}

.gate-group { margin-bottom: 10px; }
.gate-group-label {
    font-size: 0.66rem;
    color: var(--dim);
    letter-spacing: 0.14em;
    text-transform: uppercase;
    margin-bottom: 6px;
    padding-left: 2px;
}

.gate-pulse-row,
.gate-cont-row {
    display: flex;
    gap: 8px;
}
.gate-pulse-row .b,
.gate-cont-row .b { flex: 1 1 0; min-width: 0; }

.gate-pulse-input {
    display: flex;
    align-items: center;
    gap: 4px;
    background: var(--metric-bg);
    border: 1px solid var(--card-border);
    border-radius: 8px;
    padding: 4px 10px;
    flex: 0 0 110px;
}
.gate-pulse-input input {
    width: 56px;
    background: transparent;
    border: none;
    color: var(--text);
    font-family: var(--font-mono);
    font-size: 0.92rem;
    font-weight: 700;
    outline: none;
    padding: 0;
}
.gate-pulse-input span { color: var(--dim); font-size: 0.72rem; }

.emergency-stop {
    width: 100%;
    margin-top: 12px;
    padding: 14px 0;
    background: linear-gradient(180deg, #e53935, #b71c1c);
    color: #fff;
    border: none;
    border-radius: 10px;
    font-size: 1.02rem;
    font-weight: 800;
    letter-spacing: 0.22em;
    box-shadow: 0 2px 10px rgba(229, 57, 53, 0.35), inset 0 -2px 0 rgba(0, 0, 0, 0.18);
    cursor: pointer;
    transition: transform 0.1s, box-shadow 0.15s;
    min-height: 52px;
}
.emergency-stop:hover { box-shadow: 0 4px 14px rgba(229, 57, 53, 0.5), inset 0 -2px 0 rgba(0, 0, 0, 0.2); }
.emergency-stop:active { transform: scale(0.985); }

.item-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 8px;
}

/* ===== Feedback toast ===== */
#feedback {
    position: fixed;
    left: 50%;
    bottom: 18px;
    transform: translateX(-50%);
    padding: 10px 20px;
    border-radius: 999px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
    display: none;
    z-index: 300;
    max-width: calc(100vw - 24px);
    text-align: center;
    font-weight: 600;
    font-size: 0.88rem;
}

#feedback.show { display: block; }
#feedback.ok { background: var(--green); color: #fff; border: 1px solid var(--green); }
#feedback.err { background: var(--red); color: #fff; border: 1px solid var(--red); }

/* ===== Alert bars ===== */
.sensor-alert, #alert-bar {
    position: fixed;
    left: 0;
    right: 0;
    padding: 10px 14px;
    font-size: 0.84rem;
    font-weight: 700;
    text-align: center;
    display: none;
    z-index: 260;
}

.sensor-alert {
    top: 52px;
    background: #e65100;
    color: #fff;
    z-index: 90;
    white-space: normal;
    word-break: break-word;
    line-height: 1.4;
}

.sensor-alert.show { display: block; }

#alert-bar {
    top: 92px;
    background: #c62828;
    color: #fff;
    z-index: 90;
    white-space: normal;
    word-break: break-word;
    line-height: 1.4;
}

/* On mobile the status bar wraps to 2 rows — push the alert bar below */
@media (max-width: 560px) {
    .sensor-alert { top: 76px; }
    #alert-bar { top: 120px; }
}

#alert-bar.show { display: block; }

/* ===== Login overlay ===== */
.login-overlay {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: rgba(243, 246, 250, 0.85);
    backdrop-filter: blur(14px);
    z-index: 500;
}

body.dark .login-overlay {
    background: rgba(13, 21, 32, 0.85);
}

.login-box {
    width: min(100%, 400px);
    background: var(--card);
    border: 1px solid var(--card-border);
    border-radius: 16px;
    padding: 28px 24px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.login-box h1 {
    font-size: 1.4rem;
    color: var(--text);
    margin-bottom: 4px;
}

.login-box .b { width: 100%; }

.login-error {
    min-height: 20px;
    color: var(--red);
    font-size: 0.84rem;
}

/* ===== Debug page ===== */
.debug-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
    margin-bottom: 12px;
}

/* Sticky toolbar with pause / refresh / window selector / export */
.debug-toolbar {
    position: sticky;
    top: 0;
    z-index: 40;
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    align-items: center;
    justify-content: space-between;
    padding: 10px 14px;
    background: var(--card);
    border: 1px solid var(--card-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    margin-bottom: 12px;
}

.debug-toolbar-group {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
}

.inline-field {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 0.78rem;
    color: var(--dim);
}

.inline-field select,
.inline-field input {
    background: var(--metric-bg);
    border: 1px solid var(--card-border);
    border-radius: 6px;
    padding: 4px 8px;
    color: var(--text);
    font-size: 0.82rem;
}

.err-text { color: var(--red); }

/* Card-title trailing actions (copy / status badges) */
.card-title-actions {
    margin-left: auto;
    display: flex;
    gap: 6px;
    align-items: center;
}

/* Filter bar above a paginated list */
.filter-bar {
    display: flex;
    gap: 8px;
    margin-bottom: 8px;
    flex-wrap: wrap;
}

.filter-input,
.filter-select {
    background: var(--metric-bg);
    border: 1px solid var(--card-border);
    border-radius: 8px;
    padding: 6px 10px;
    color: var(--text);
    font-size: 0.82rem;
}

.filter-input {
    flex: 1 1 180px;
    min-width: 0;
}

.filter-select {
    flex: 0 0 auto;
    min-width: 140px;
}

/* Pagination bar */
.pagination-bar {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px dashed var(--card-border);
    font-size: 0.8rem;
    color: var(--dim);
}

.pagination-bar button[data-page-act] {
    min-width: 36px;
    padding: 4px 8px;
    min-height: 30px;
    font-size: 0.82rem;
    border-radius: 6px;
}

.pagination-bar button[disabled] {
    opacity: 0.35;
}

/* Sample table actions column + row hover */
.data-table td.col-actions { width: 1%; white-space: nowrap; text-align: right; }
.data-table td.empty-row { text-align: center; color: var(--dim); padding: 18px 0; }
.data-table tbody tr:hover { background: var(--metric-bg); }

/* Modal for row details */
.modal[hidden] { display: none; }
.modal {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}
.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(10, 14, 20, 0.55);
}
.modal-panel {
    position: relative;
    background: var(--card);
    border: 1px solid var(--card-border);
    border-radius: 14px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
    width: min(900px, 100%);
    max-height: min(80vh, 700px);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.modal-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 14px;
    border-bottom: 1px solid var(--card-border);
    font-weight: 700;
    gap: 10px;
}
.modal-actions { display: flex; gap: 6px; }
.modal-panel .code-block {
    border: none;
    border-radius: 0;
    min-height: 0;
    max-height: none;
    flex: 1;
    margin: 0;
}

.code-block {
    min-height: 320px;
    max-height: 520px;
    overflow: auto;
    background: var(--metric-bg);
    border: 1px solid var(--card-border);
    border-radius: 12px;
    padding: 12px;
    color: var(--text);
    font-family: var(--font-mono);
    font-size: 0.74rem;
    line-height: 1.6;
    white-space: pre-wrap;
    word-break: break-word;
}

.log-entry + .log-entry {
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid var(--card-border);
}

.log-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
    margin-bottom: 6px;
    color: var(--dim);
    font-size: 0.72rem;
}

.log-text {
    white-space: pre-wrap;
    word-break: break-word;
    color: var(--text);
}

.field {
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex: 1 1 220px;
}

.field.compact { flex: 1 1 260px; }
.field span { font-size: 0.78rem; color: var(--dim); }

/* ===== Debug page topbar ===== */
.topbar {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 8px;
}

.title-row {
    display: flex;
    align-items: center;
    gap: 10px;
}

.title-row h1 {
    font-size: 1.3rem;
    font-weight: 800;
}

.topbar .sub {
    font-size: 0.82rem;
    color: var(--dim);
    margin-top: 2px;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.nav-strip {
    display: flex;
    gap: 6px;
    margin-bottom: 14px;
}

.nav-link {
    padding: 6px 14px;
    border-radius: 8px;
    font-size: 0.84rem;
    font-weight: 600;
    color: var(--dim);
    border: 1px solid var(--card-border);
    transition: all 0.15s ease;
}

.nav-link.active {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}

/* ===== Responsive ===== */
@media (max-width: 820px) {
    .debug-grid { grid-template-columns: 1fr; }
}

@media (max-width: 560px) {
    .container, .debug-container { width: 100%; }

    .status-bar {
        flex-wrap: wrap;
        padding: 4px 8px;
        gap: 4px;
    }

    .status-bar-left {
        flex: 1 1 auto;
        min-width: 0;
    }
    .status-title { font-size: 0.8rem; }

    .status-bar-metrics {
        order: 3;
        width: 100%;
        justify-content: space-between;
        padding: 2px 0;
        border-top: 1px solid var(--card-border);
    }
    .sbm { font-size: 0.8rem; }

    .status-bar-actions { gap: 4px; flex-wrap: nowrap; }
    .nav-pill { padding: 3px 8px; font-size: 0.72rem; }
    .btn-logout { padding: 4px 8px; min-height: 30px; font-size: 0.8rem; }
    .logout-label { display: none; }

    /* Keep 2/3-col grids on mobile — don't collapse to single column.
       Only shrink paddings/fonts so cells stay readable at ~430px width. */
    .row { gap: 6px; }
    .col2 { width: calc(50% - 3px); }
    .col3 { width: calc(33.333% - 4px); }
    .m { padding: 7px 8px; min-height: 52px; }
    .m .lb { font-size: 0.66rem; line-height: 1.25; }
    .m .v { font-size: 0.92rem; }
    .m .v .u { font-size: 0.6rem; margin-left: 1px; }
    .m .v.hi { font-size: 1rem; }

    .btns .b, .controls-grid .b { width: 100%; }

    .breaker-row { flex-direction: column; align-items: stretch; }
    .breaker-btns { width: 100%; }
    .breaker-btns .b { flex: 1; }

    /* Dev matrix on mobile: 2-up with consistent width */
    .dev-chip { flex: 1 1 calc(50% - 6px); }

    .topbar { flex-direction: column; gap: 8px; }
    .header-actions { width: 100%; justify-content: flex-end; }

    /* Gate card: pulse input moves to its own line */
    .gate-pulse-row { flex-wrap: wrap; }
    .gate-pulse-input { flex: 1 1 100%; }
    .gate-pulse-row .b { flex: 1 1 calc(50% - 4px); }

    /* Three-phase tables: tighter cells */
    .ph-tbl th, .data-table th { font-size: 0.68rem; padding: 6px 4px; }
    .ph-tbl td, .data-table td { font-size: 0.82rem; padding: 6px 4px; }
}

@media (max-width: 380px) {
    /* Only on truly narrow widths drop 3-col to 2-col; col2 stays 2-col */
    .col3 { width: calc(50% - 3px); }
    .dev-chip { flex: 1 1 100%; }
    .miner-compact-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
