:root {
    color-scheme: dark;
    --bg: #091014;
    --panel: #101a20;
    --panel-2: #142229;
    --line: #243740;
    --text: #edf7f8;
    --muted: #90a7ad;
    --cyan: #2ee6d6;
    --green: #70e08f;
    --red: #ff647c;
    --yellow: #f6c65b;
    --blue: #6aa7ff;
    --shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
}

* {
    box-sizing: border-box;
}

html,
body {
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
}

body {
    margin: 0;
    min-height: 100vh;
    background:
        radial-gradient(circle at 15% 0%, rgba(46, 230, 214, 0.12), transparent 32rem),
        linear-gradient(135deg, #071014 0%, #0d171c 42%, #101417 100%);
    color: var(--text);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

html {
    scroll-behavior: smooth;
}

button,
input,
select {
    font: inherit;
    min-width: 0;
}

.shell {
    width: min(1480px, 100%);
    max-width: 100%;
    margin: 0 auto;
    padding: 28px;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
    margin-bottom: 24px;
    min-width: 0;
}

.eyebrow {
    margin: 0 0 6px;
    color: var(--cyan);
    font-size: 0.78rem;
    font-weight: 700;
    letter-spacing: 0;
    text-transform: uppercase;
}

h1,
h2 {
    margin: 0;
    letter-spacing: 0;
}

h1 {
    font-size: clamp(2rem, 4vw, 4.2rem);
    line-height: 1;
}

h2 {
    font-size: 1rem;
}

.live-status {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    margin-top: 10px;
    color: var(--muted);
    font-size: 0.82rem;
    font-weight: 700;
}

.live-dot {
    width: 8px;
    height: 8px;
    border-radius: 999px;
    background: var(--green);
    box-shadow: 0 0 0 5px rgba(112, 224, 143, 0.12);
}

.live-dot.loading {
    background: var(--yellow);
    box-shadow: 0 0 0 5px rgba(246, 198, 91, 0.12);
}

.live-dot.error {
    background: var(--red);
    box-shadow: 0 0 0 5px rgba(255, 100, 124, 0.12);
}

.top-actions {
    display: flex;
    align-items: end;
    gap: 10px;
    min-width: 0;
}

.device-search,
.config-form label {
    display: grid;
    gap: 7px;
    color: var(--muted);
    font-size: 0.82rem;
    font-weight: 700;
}

input,
select {
    width: 100%;
    min-height: 42px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #091217;
    color: var(--text);
    padding: 10px 12px;
    outline: none;
}

input:focus,
select:focus {
    border-color: var(--cyan);
    box-shadow: 0 0 0 3px rgba(46, 230, 214, 0.12);
}

.button {
    min-height: 42px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #17262d;
    color: var(--text);
    padding: 10px 14px;
    font-weight: 800;
    cursor: pointer;
    white-space: nowrap;
}

.button:hover {
    border-color: #3b5965;
}

.button.primary {
    border-color: rgba(46, 230, 214, 0.55);
    background: linear-gradient(135deg, #11bdae, #238bff);
    color: #041014;
}

.button.full {
    width: 100%;
}

.metrics-grid,
.charts-grid,
.work-grid {
    display: grid;
    gap: 16px;
}

.metrics-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    margin-bottom: 16px;
}

.metric,
.panel {
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 8px;
    background: linear-gradient(180deg, rgba(20, 34, 41, 0.95), rgba(13, 22, 27, 0.96));
    box-shadow: var(--shadow);
    min-width: 0;
}

.metric {
    min-height: 132px;
    padding: 18px;
}

.metric span,
.muted {
    color: var(--muted);
}

.metric span {
    display: block;
    font-size: 0.82rem;
    font-weight: 800;
    text-transform: uppercase;
}

.metric strong {
    display: block;
    margin-top: 10px;
    font-size: 2.6rem;
    line-height: 1;
}

.metric small {
    display: block;
    margin-top: 10px;
    color: var(--muted);
}

.charts-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    margin-bottom: 16px;
}

.chart-panel {
    min-height: 330px;
    overflow: hidden;
}

.chart-panel.wide {
    grid-column: 1 / -1;
    min-height: 280px;
}

.panel {
    padding: 18px;
}

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

.panel-subtitle {
    margin: 5px 0 0;
    color: var(--muted);
    font-size: 0.82rem;
    font-weight: 700;
}

.reading-tools {
    display: grid;
    grid-template-columns: minmax(150px, 0.8fr) minmax(170px, 1fr) auto;
    gap: 10px;
    align-items: end;
    margin-bottom: 12px;
}

.reading-tools label {
    display: grid;
    gap: 6px;
    color: var(--muted);
    font-size: 0.78rem;
    font-weight: 800;
}

.reading-summary {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 8px;
    margin-bottom: 12px;
}

.reading-summary div {
    min-height: 70px;
    border: 1px solid rgba(255, 255, 255, 0.07);
    border-radius: 8px;
    background: rgba(9, 18, 23, 0.64);
    padding: 10px;
}

.reading-summary span {
    display: block;
    color: var(--muted);
    font-size: 0.7rem;
    font-weight: 800;
    text-transform: uppercase;
}

.reading-summary strong {
    display: block;
    margin-top: 7px;
    font-size: 1.25rem;
}

.hidden {
    display: none !important;
}

.button.subtle {
    margin-top: 12px;
    background: rgba(46, 230, 214, 0.08);
    border-color: rgba(46, 230, 214, 0.26);
    color: var(--cyan);
}

canvas {
    width: 100% !important;
    max-width: 100% !important;
    height: 250px !important;
    display: block;
}

.chart-panel.wide canvas {
    height: 200px !important;
}

.work-grid {
    grid-template-columns: minmax(0, 1.45fr) minmax(320px, 0.55fr);
    align-items: start;
    margin-bottom: 16px;
}

.table-wrap {
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
    min-width: 720px;
}

th,
td {
    border-bottom: 1px solid rgba(255, 255, 255, 0.07);
    padding: 12px 10px;
    text-align: left;
    white-space: nowrap;
}

th {
    color: var(--muted);
    font-size: 0.78rem;
    text-transform: uppercase;
}

td {
    color: #dcebee;
}

.pill {
    display: inline-flex;
    align-items: center;
    min-height: 28px;
    border: 1px solid rgba(46, 230, 214, 0.3);
    border-radius: 999px;
    padding: 4px 10px;
    color: var(--cyan);
    background: rgba(46, 230, 214, 0.08);
    font-size: 0.78rem;
    font-weight: 800;
}

.pill.danger {
    border-color: rgba(255, 100, 124, 0.35);
    color: var(--red);
    background: rgba(255, 100, 124, 0.08);
}

.config-form {
    display: grid;
    gap: 13px;
}

.form-duo {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
}

.copy-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    min-width: 0;
}

.alerts-list {
    display: grid;
    gap: 10px;
}

.alert-item {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 14px;
    align-items: center;
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-left: 4px solid var(--red);
    border-radius: 8px;
    background: rgba(9, 18, 23, 0.78);
    padding: 12px;
}

.alert-item.resolved {
    border-left-color: var(--green);
    opacity: 0.72;
}

.alert-title {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    margin-bottom: 5px;
}

.alert-title strong {
    color: var(--text);
}

.alert-meta {
    color: var(--muted);
    font-size: 0.84rem;
}

.state-ok {
    color: var(--green);
}

.state-warn {
    color: var(--yellow);
}

.state-danger {
    color: var(--red);
}

@media (max-width: 1050px) {
    .metrics-grid,
    .charts-grid,
    .work-grid {
        grid-template-columns: 1fr 1fr;
    }

    .work-grid > .panel:first-child,
    .chart-panel.wide {
        grid-column: 1 / -1;
    }
}

@media (max-width: 760px) {
    body {
        background:
            radial-gradient(circle at 50% -10%, rgba(46, 230, 214, 0.16), transparent 18rem),
            linear-gradient(180deg, #071014 0%, #0d171c 48%, #091014 100%);
    }

    .shell {
        padding: 10px;
    }

    .topbar,
    .top-actions {
        align-items: stretch;
        flex-direction: column;
    }

    .topbar {
        position: sticky;
        top: 0;
        z-index: 10;
        margin: -10px -10px 12px;
        padding: 10px;
        border-bottom: 1px solid rgba(255, 255, 255, 0.08);
        background: rgba(7, 16, 20, 0.92);
        backdrop-filter: blur(14px);
    }

    .eyebrow {
        margin-bottom: 3px;
        font-size: 0.66rem;
    }

    h1 {
        font-size: 1.35rem;
        line-height: 1.05;
    }

    .live-status {
        margin-top: 7px;
        gap: 7px;
        font-size: 0.7rem;
    }

    .live-dot {
        width: 7px;
        height: 7px;
        box-shadow: 0 0 0 4px rgba(112, 224, 143, 0.12);
    }

    h2 {
        font-size: 0.9rem;
    }

    input,
    select,
    .button {
        min-height: 40px;
        border-radius: 7px;
        font-size: 0.9rem;
    }

    .top-actions {
        display: grid;
        grid-template-columns: minmax(0, 1fr) 82px;
        align-items: end;
        gap: 8px;
        width: 100%;
    }

    .device-search {
        gap: 5px;
        font-size: 0.72rem;
    }

    .metrics-grid,
    .charts-grid,
    .work-grid,
    .form-duo {
        grid-template-columns: 1fr;
    }

    .metrics-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 8px;
        margin-bottom: 12px;
    }

    .metric {
        min-height: 92px;
        padding: 11px;
    }

    .metric span {
        font-size: 0.65rem;
    }

    .metric small {
        margin-top: 6px;
        font-size: 0.69rem;
        line-height: 1.25;
    }

    .chart-panel.wide,
    .work-grid > .panel:first-child {
        grid-column: auto;
    }

    .metric strong {
        margin-top: 7px;
        font-size: 1.65rem;
    }

    .panel {
        padding: 12px;
    }

    .panel-head {
        align-items: flex-start;
        margin-bottom: 12px;
    }

    .panel-subtitle {
        font-size: 0.72rem;
    }

    .reading-tools {
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
        gap: 8px;
    }

    .reading-tools label {
        font-size: 0.68rem;
    }

    .reading-tools .button {
        grid-column: 1 / -1;
    }

    .reading-summary {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 7px;
    }

    .reading-summary div {
        min-height: 60px;
        padding: 9px;
    }

    .reading-summary span {
        font-size: 0.62rem;
    }

    .reading-summary strong {
        margin-top: 5px;
        font-size: 1.05rem;
    }

    .chart-panel,
    .chart-panel.wide {
        min-height: 222px;
    }

    canvas,
    .chart-panel.wide canvas {
        height: 165px !important;
    }

    .table-wrap {
        overflow: visible;
    }

    table,
    thead,
    tbody,
    tr,
    td {
        display: block;
        width: 100%;
    }

    table {
        min-width: 0;
        border-collapse: separate;
        border-spacing: 0 10px;
    }

    thead {
        display: none;
    }

    tbody tr {
        border: 1px solid rgba(255, 255, 255, 0.08);
        border-radius: 8px;
        background: rgba(9, 18, 23, 0.78);
        padding: 9px 10px;
    }

    td {
        display: grid;
        grid-template-columns: 76px minmax(0, 1fr);
        gap: 10px;
        align-items: center;
        border-bottom: 0;
        padding: 6px 0;
        white-space: normal;
        font-size: 0.86rem;
        overflow-wrap: anywhere;
    }

    td::before {
        content: attr(data-label);
        color: var(--muted);
        font-size: 0.65rem;
        font-weight: 800;
        text-transform: uppercase;
    }

    .copy-row {
        grid-template-columns: 1fr;
    }

    #deviceApiKey {
        font-size: 0.76rem;
    }

    .alert-item {
        grid-template-columns: 1fr;
    }

    .alert-meta {
        font-size: 0.78rem;
        line-height: 1.45;
    }
}

@media (max-width: 420px) {
    .shell {
        padding: 8px;
    }

    .topbar {
        margin: -8px -8px 10px;
        padding: 10px 8px;
    }

    .metrics-grid {
        gap: 7px;
    }

    .metric {
        min-height: 88px;
        padding: 10px;
    }

    .metric strong {
        font-size: 1.48rem;
    }

    .panel {
        padding: 10px;
    }

    td {
        grid-template-columns: 70px minmax(0, 1fr);
    }
}
