:root {
    color-scheme: light;
    --page: #e8eff4;
    --surface: #ffffff;
    --surface-soft: #f2f6f8;
    --text: #092338;
    --muted: #526a7c;
    --border: #bccdd8;
    --navy: #082b45;
    --navy-soft: #104766;
    --accent: #007f7b;
    --accent-soft: #dff2f0;
    --online: #087a55;
    --offline: #778695;
    --warning: #ad5b0c;
    --danger: #b52e32;
    --temperature: #d45740;
    --humidity: #1973a9;
    --pressure: #6d58a8;
    --signal: #168462;
    --shadow: 0 18px 45px rgba(20, 47, 68, 0.12);
    --shadow-small: 0 7px 24px rgba(20, 47, 68, 0.1);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
    box-sizing: border-box;
}

html {
    min-width: 320px;
    background: var(--page);
}

body {
    min-width: 320px;
    min-height: 100vh;
    margin: 0;
    background: var(--page);
    color: var(--text);
}

.page-decoration {
    position: fixed;
    z-index: 0;
    inset: 0 0 auto;
    height: 300px;
    pointer-events: none;
    background:
        linear-gradient(117deg, rgba(5, 105, 111, 0.95), rgba(11, 39, 64, 0.98)),
        var(--navy);
    clip-path: polygon(0 0, 100% 0, 100% 70%, 0 100%);
}

.page-decoration::after {
    position: absolute;
    inset: 0;
    background-image:
        linear-gradient(rgba(255, 255, 255, 0.035) 1px, transparent 1px),
        linear-gradient(90deg, rgba(255, 255, 255, 0.035) 1px, transparent 1px);
    background-size: 32px 32px;
    content: "";
    mask-image: linear-gradient(to right, transparent, black 25%, black 75%, transparent);
}

button,
input,
select {
    font: inherit;
}

button {
    display: inline-flex;
    min-height: 42px;
    align-items: center;
    justify-content: center;
    gap: 0.45rem;
    border: 0;
    border-radius: 10px;
    padding: 0.65rem 1rem;
    background: var(--navy);
    color: #fff;
    cursor: pointer;
    font-weight: 750;
    transition: transform 150ms ease, box-shadow 150ms ease, background 150ms ease;
}

button svg {
    width: 1rem;
    fill: none;
    stroke: currentColor;
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke-width: 1.7;
}

button:hover:not(:disabled) {
    transform: translateY(-1px);
    background: var(--navy-soft);
    box-shadow: 0 7px 18px rgba(11, 39, 64, 0.15);
}

button:disabled {
    cursor: wait;
    opacity: 0.56;
}

button.secondary {
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
}

button.secondary:hover:not(:disabled) {
    background: var(--surface-soft);
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
a:focus-visible {
    outline: 3px solid rgba(8, 124, 122, 0.25);
    outline-offset: 2px;
}

.shell {
    position: relative;
    z-index: 1;
    width: min(1200px, calc(100% - 2.5rem));
    margin: 0 auto;
    padding: 2.15rem 0 3rem;
    transition: filter 180ms ease, opacity 180ms ease;
}

.topbar,
.topbar-actions,
.section-heading,
.monitor-heading,
.panel-heading,
.filters,
.device-meta,
.chart-legend,
footer,
.brand-block {
    display: flex;
    align-items: center;
}

.topbar,
.section-heading,
.monitor-heading,
.panel-heading,
footer {
    justify-content: space-between;
    gap: 1.5rem;
}

.topbar {
    min-height: 112px;
    padding-bottom: 1.75rem;
    color: #fff;
}

.brand-block {
    gap: 1rem;
}

.brand-mark {
    display: grid;
    width: 3.4rem;
    height: 3.4rem;
    flex: 0 0 auto;
    place-items: center;
    border: 1px solid rgba(255, 255, 255, 0.28);
    border-radius: 15px;
    background: rgba(255, 255, 255, 0.96);
    box-shadow: inset 0 1px rgba(255, 255, 255, 0.12);
}

.brand-mark img {
    width: 2.85rem;
    height: 2.85rem;
    object-fit: contain;
}

.topbar-actions,
.filters,
.chart-legend {
    gap: 0.75rem;
    flex-wrap: wrap;
}

.topbar-actions button:not(.secondary) {
    background: #ffffff;
    color: var(--navy);
    box-shadow: 0 7px 20px rgba(4, 23, 38, 0.16);
}

.topbar-actions button:not(.secondary):hover:not(:disabled) {
    background: #edf5f7;
}

.topbar-actions .secondary {
    border-color: rgba(255, 255, 255, 0.28);
    background: rgba(255, 255, 255, 0.11);
    color: #fff;
}

.topbar-actions .secondary:hover:not(:disabled) {
    background: rgba(255, 255, 255, 0.18);
}

.eyebrow,
.section-kicker {
    margin: 0 0 0.35rem;
    color: var(--accent);
    font-size: 0.72rem;
    font-weight: 850;
    letter-spacing: 0.13em;
    text-transform: uppercase;
}

.eyebrow {
    color: #9ce4dd;
}

h1,
h2,
h3,
p {
    margin-top: 0;
}

h1 {
    margin-bottom: 0.25rem;
    font-size: clamp(2rem, 4vw, 3.05rem);
    letter-spacing: -0.045em;
    line-height: 1.05;
}

h2 {
    margin-bottom: 0;
    font-size: clamp(1.3rem, 2vw, 1.72rem);
    letter-spacing: -0.025em;
}

h3 {
    margin-bottom: 0.25rem;
}

.subtitle {
    max-width: 660px;
    margin-bottom: 0;
    color: rgba(255, 255, 255, 0.88);
}

.muted,
.updated-at,
footer {
    color: var(--muted);
}

.updated-at {
    color: rgba(255, 255, 255, 0.9);
    font-size: 0.8rem;
}

.message {
    display: none;
    margin: 0 0 1.1rem;
    padding: 0.85rem 1rem;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--surface);
    box-shadow: var(--shadow-small);
}

.message.visible {
    display: block;
}

.message.error {
    border-color: rgba(181, 46, 50, 0.3);
    color: var(--danger);
}

.message.success {
    border-color: rgba(8, 122, 85, 0.28);
    color: var(--online);
}

.monitor-panel,
.panel,
.stat {
    border: 1px solid var(--border);
    background: var(--surface);
    box-shadow: var(--shadow);
}

.monitor-panel {
    overflow: hidden;
    border-radius: 22px;
    padding: 1.55rem;
}

.monitor-heading {
    align-items: flex-start;
}

.monitor-heading .muted {
    margin: 0.35rem 0 0;
}

.status-badge,
.config-status,
.alert-state {
    display: inline-flex;
    align-items: center;
    gap: 0.42rem;
    border-radius: 999px;
    padding: 0.42rem 0.72rem;
    font-size: 0.76rem;
    font-weight: 800;
    white-space: nowrap;
}

.status-badge {
    background: #edf1f4;
    color: var(--offline);
}

.status-badge.online {
    background: #e1f3ec;
    color: var(--online);
}

.dot {
    width: 0.52rem;
    height: 0.52rem;
    border-radius: 50%;
    background: currentColor;
    box-shadow: 0 0 0 4px color-mix(in srgb, currentColor 12%, transparent);
}

.status-badge.online .dot {
    animation: pulse 2.4s ease-out infinite;
}

@keyframes pulse {
    50% { box-shadow: 0 0 0 7px transparent; }
}

.reading-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 0.85rem;
    margin: 1.5rem 0;
}

.reading {
    position: relative;
    min-width: 0;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 1rem 1rem 1.05rem;
    background: linear-gradient(145deg, #fff, var(--surface-soft));
}

.reading::after {
    position: absolute;
    right: -1.7rem;
    bottom: -2.3rem;
    width: 6.5rem;
    height: 6.5rem;
    border-radius: 50%;
    background: currentColor;
    content: "";
    opacity: 0.045;
}

.reading-icon {
    display: grid;
    width: 2.15rem;
    height: 2.15rem;
    margin-bottom: 0.9rem;
    place-items: center;
    border-radius: 10px;
    background: #edf3f5;
    color: var(--muted);
}

.reading-icon svg {
    width: 1.15rem;
    fill: none;
    stroke: currentColor;
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke-width: 1.7;
}

.reading-label {
    display: block;
    margin-bottom: 0.36rem;
    color: var(--muted);
    font-size: 0.75rem;
    font-weight: 700;
}

.reading > div {
    display: flex;
    align-items: baseline;
}

.reading strong {
    overflow: hidden;
    font-size: clamp(1.8rem, 4vw, 2.55rem);
    line-height: 1;
    letter-spacing: -0.06em;
    text-overflow: ellipsis;
}

.reading > div > span {
    margin-left: 0.36rem;
    color: var(--muted);
    font-size: 0.76rem;
}

.temperature-reading,
.temperature-reading .reading-icon { color: var(--temperature); }
.humidity-reading,
.humidity-reading .reading-icon { color: var(--humidity); }
.pressure-reading,
.pressure-reading .reading-icon { color: var(--pressure); }
.signal-reading,
.signal-reading .reading-icon { color: var(--signal); }

.reading-label,
.reading > div > span {
    color: var(--muted);
}

.device-meta {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 0;
    border-top: 1px solid var(--border);
    padding-top: 1.05rem;
}

.device-meta > span {
    display: grid;
    gap: 0.2rem;
    padding: 0 1rem;
    border-right: 1px solid var(--border);
}

.device-meta > span:first-child { padding-left: 0; }
.device-meta > span:last-child { border-right: 0; }

.device-meta small {
    color: var(--muted);
    font-size: 0.68rem;
}

.device-meta strong {
    overflow: hidden;
    color: var(--text);
    font-size: 0.78rem;
    text-overflow: ellipsis;
}

.active-alert-panel {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    margin-top: 1rem;
    border: 1px solid rgba(181, 46, 50, 0.3);
    border-radius: 14px;
    padding: 1rem 1.15rem;
    background: #fff1f1;
    color: var(--danger);
}

.active-alert-panel p { color: inherit; }
.active-alert-panel span { font-size: 0.8rem; }
.hidden { display: none !important; }

.analysis,
.lower-grid {
    margin-top: 2.4rem;
}

.section-heading,
.panel-heading {
    margin-bottom: 1rem;
}

.filters {
    align-items: flex-end;
}

.filters label,
.config-form label,
.auth-card label {
    display: grid;
    gap: 0.36rem;
    color: var(--muted);
    font-size: 0.75rem;
    font-weight: 700;
}

input,
select {
    min-height: 42px;
    border: 1px solid #aebfcb;
    border-radius: 10px;
    padding: 0 0.75rem;
    background: var(--surface);
    color: var(--text);
}

input:hover,
select:hover {
    border-color: #718b9d;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 0.75rem;
    margin-bottom: 1rem;
}

.stat {
    min-width: 0;
    border-radius: 14px;
    padding: 1rem;
    box-shadow: var(--shadow-small);
}

.stat-label,
.stat-context {
    display: block;
    color: var(--muted);
    font-size: 0.7rem;
}

.stat-value {
    display: block;
    overflow: hidden;
    margin: 0.4rem 0 0.22rem;
    color: var(--navy);
    font-size: 1.28rem;
    letter-spacing: -0.02em;
    text-overflow: ellipsis;
}

.panel {
    border-radius: 18px;
    padding: 1.3rem;
}

.panel-heading { align-items: flex-start; }
.panel-heading p { margin-bottom: 0; }

.chart-panel {
    box-shadow: var(--shadow-small);
}

.chart-legend {
    color: var(--muted);
    font-size: 0.75rem;
}

.chart-legend span {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
}

.line {
    display: inline-block;
    width: 1.35rem;
    height: 3px;
    border-radius: 3px;
}

.temperature-line { background: var(--temperature); }
.humidity-line { background: var(--humidity); }

.chart {
    position: relative;
    min-height: 330px;
    border-radius: 12px;
    background:
        linear-gradient(180deg, rgba(222, 232, 239, 0.75), transparent 58%);
}

.chart svg {
    display: block;
    width: 100%;
    height: 330px;
    overflow: visible;
}

.chart-grid-line {
    stroke: var(--border);
    stroke-width: 1;
}

.chart-axis-label {
    fill: var(--muted);
    font-size: 11px;
}

.chart-series {
    fill: none;
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke-width: 2.5;
    vector-effect: non-scaling-stroke;
}

.temperature-series { stroke: var(--temperature); }
.humidity-series { stroke: var(--humidity); }
.temperature-point { fill: var(--temperature); }
.humidity-point { fill: var(--humidity); }

.empty-state {
    margin: 0;
    color: var(--muted);
    font-size: 0.84rem;
}

.chart-empty {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
}

.lower-grid {
    display: grid;
    grid-template-columns: minmax(0, 1.2fr) minmax(320px, 0.8fr);
    gap: 1rem;
}

.lower-grid .panel {
    box-shadow: var(--shadow-small);
}

.config-status {
    background: #edf1f4;
    color: var(--muted);
}

.config-status.applied {
    background: #e1f3ec;
    color: var(--online);
}

.config-status.pending {
    background: #fff0dd;
    color: var(--warning);
}

.config-form {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.9rem;
}

.full-field { grid-column: 1 / -1; }

.toggle-field {
    display: flex !important;
    min-height: 44px;
    align-items: center;
    gap: 0.65rem !important;
    border-radius: 10px;
    padding: 0.55rem 0.75rem;
    background: var(--accent-soft);
    color: var(--text) !important;
}

.toggle-field input {
    width: 1.05rem;
    height: 1.05rem;
    min-height: auto;
    accent-color: var(--accent);
}

.form-actions {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
}

.form-actions .muted { font-size: 0.73rem; }

.alerts-list {
    display: grid;
    gap: 0.65rem;
}

.alert-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
    border: 1px solid var(--border);
    border-radius: 11px;
    padding: 0.78rem;
    background: var(--surface-soft);
}

.alert-item div {
    display: grid;
    gap: 0.2rem;
}

.alert-item strong { font-size: 0.8rem; }
.alert-item div span { color: var(--muted); font-size: 0.68rem; }

.alert-state {
    padding: 0.3rem 0.55rem;
    background: #e7edf1;
    color: var(--muted);
}

.alert-item.active { border-color: rgba(181, 46, 50, 0.3); }
.alert-item.active .alert-state { background: #fff0f0; color: var(--danger); }

footer {
    margin-top: 2rem;
    padding-top: 1.2rem;
    border-top: 1px solid #cfdbe3;
    font-size: 0.76rem;
}

footer a {
    color: var(--navy);
    font-weight: 700;
    text-decoration: none;
}

footer a:hover { text-decoration: underline; }

.auth-panel {
    position: fixed;
    z-index: 20;
    inset: 0;
    display: grid;
    place-items: center;
    overflow: auto;
    padding: 1rem;
    background: rgba(6, 24, 39, 0.58);
    backdrop-filter: blur(8px);
}

.auth-card {
    display: grid;
    width: min(420px, 100%);
    border: 1px solid rgba(255, 255, 255, 0.5);
    border-radius: 20px;
    padding: 1.7rem;
    background: var(--surface);
    box-shadow: 0 30px 80px rgba(5, 22, 37, 0.28);
}

.auth-icon {
    display: grid;
    width: 2.8rem;
    height: 2.8rem;
    margin-bottom: 1.15rem;
    place-items: center;
    border-radius: 13px;
    background: var(--accent-soft);
    color: var(--accent);
}

.auth-icon svg {
    width: 1.35rem;
    fill: none;
    stroke: currentColor;
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke-width: 1.7;
}

.auth-card h2 {
    margin-bottom: 0.55rem;
    font-size: 1.7rem;
}

.auth-card > p:not(.section-kicker):not(.auth-error) {
    margin-bottom: 1.2rem;
    color: var(--muted);
    line-height: 1.55;
}

.auth-card input {
    margin-top: 0.35rem;
}

.auth-card button {
    width: 100%;
    margin-top: 0.8rem;
}

.auth-card small {
    margin-top: 0.85rem;
    color: var(--muted);
    text-align: center;
}

.auth-error {
    min-height: 1.25rem;
    margin: 0.55rem 0 0;
    color: var(--danger);
    font-size: 0.76rem;
}

body:not(.auth-locked) .auth-panel {
    display: none;
}

body.auth-locked {
    overflow: hidden;
}

body.auth-locked .shell {
    filter: blur(2px);
    opacity: 0.72;
    pointer-events: none;
    user-select: none;
}

@media (max-width: 1000px) {
    .topbar {
        align-items: flex-start;
    }

    .topbar-actions {
        justify-content: flex-end;
    }

    .reading-grid { grid-template-columns: 1fr 1fr; }
    .stats-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    .lower-grid { grid-template-columns: 1fr; }
}

@media (max-width: 720px) {
    .page-decoration { height: 400px; }
    .shell { width: min(100% - 1.25rem, 1200px); padding-top: 1.25rem; }

    .topbar,
    .section-heading,
    .panel-heading {
        align-items: flex-start;
        flex-direction: column;
    }

    .topbar-actions,
    .filters,
    .filters label,
    .filters select,
    .filters button {
        width: 100%;
    }

    .topbar-actions { align-items: stretch; }
    .updated-at { width: 100%; }
    .topbar-actions button { flex: 1; }

    .device-meta { grid-template-columns: 1fr 1fr; row-gap: 1rem; }
    .device-meta > span:nth-child(2) { border-right: 0; }
    .device-meta > span:nth-child(3) { padding-left: 0; }
    .stats-grid { grid-template-columns: 1fr 1fr; }
    .config-form { grid-template-columns: 1fr; }
    .config-form label,
    .full-field { grid-column: 1; }
}

@media (max-width: 480px) {
    .brand-mark { display: none; }
    h1 { font-size: 2rem; }
    .subtitle { font-size: 0.88rem; }
    .monitor-panel,
    .panel { padding: 1rem; }
    .monitor-heading,
    .active-alert-panel,
    footer { align-items: flex-start; flex-direction: column; }
    .reading-grid,
    .stats-grid { grid-template-columns: 1fr; }
    .device-meta { grid-template-columns: 1fr; }
    .device-meta > span { padding: 0; border-right: 0; }
    .chart-legend { display: none; }
    .auth-card { padding: 1.3rem; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        scroll-behavior: auto !important;
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
