:root {
    --bg-1: #fdf8f0;
    --bg-2: #e9dfcd;
    --card: rgba(255, 255, 255, 0.72);
    --card-border: rgba(255, 255, 255, 0.95);
    --ink: #2f2a22;
    --ink-2: #6b6255;
    --ink-3: #9b9284;
    --shadow: 0 12px 30px -10px rgba(60, 45, 20, 0.22);
    --ok: #2f9e55;
    --ok-bg: #dff5e4;
    --warn: #c98a0b;
    --warn-bg: #fdf0cf;
    --alert: #d1453b;
    --alert-bg: #fde0dc;
    --off: #8c8577;
    --off-bg: #eeeae2;
    --c-uvb: #7c5cff;
    --c-heat: #f08c1a;
    --c-che: #e5462b;
    --c-mist: #2a9fd6;
    --c-temperature: #e8622c;
    --c-humidity: #1f8fd1;
    --radius: 22px;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    min-height: 100%;
    font-family: 'Fredoka', ui-rounded, 'Segoe UI', system-ui, sans-serif;
    color: var(--ink);
    background: radial-gradient(1200px 800px at 20% 0%, #fffaf2 0%, transparent 60%),
                radial-gradient(1000px 700px at 100% 100%, #dfeedd 0%, transparent 55%),
                linear-gradient(160deg, var(--bg-1), var(--bg-2));
    background-attachment: fixed;
}

button { font: inherit; color: inherit; cursor: pointer; }
h1, h2, h3 { margin: 0; }
.muted { color: var(--ink-2); }
.small { font-size: 0.82rem; }

/* ---------- Top bar ---------- */
.topbar {
    display: flex; align-items: center; justify-content: space-between; gap: 1rem;
    flex-wrap: wrap;
    padding: 1rem 1.5rem 0.25rem;
    max-width: 1800px; margin: 0 auto;
}
.brand { display: flex; align-items: center; gap: 0.75rem; }
.brand-icon {
    font-size: 2rem; width: 3.2rem; height: 3.2rem; display: grid; place-items: center;
    background: linear-gradient(145deg, #b8f08a, #4fbf5a);
    border-radius: 18px; box-shadow: var(--shadow), inset 0 -4px 0 rgba(0,0,0,0.08);
}
.brand h1 { font-size: 1.55rem; font-weight: 700; letter-spacing: -0.01em; }
.subtitle { margin: 0; color: var(--ink-2); font-size: 0.9rem; }
.topbar-right { display: flex; align-items: center; gap: 0.9rem; }
.clock { font-weight: 600; font-size: 1.25rem; font-variant-numeric: tabular-nums; }

/* ---------- Pills / chips ---------- */
.pill {
    display: inline-flex; align-items: center; gap: 0.4rem;
    padding: 0.3rem 0.75rem; border-radius: 999px; font-size: 0.8rem; font-weight: 600;
    background: var(--off-bg); color: var(--off); white-space: nowrap;
}
.pill .dot { width: 0.5rem; height: 0.5rem; border-radius: 50%; background: currentColor; }
.pill-ok    { background: var(--ok-bg); color: var(--ok); }
.pill-ok .dot { animation: pulse 2s infinite; }
.pill-warn  { background: var(--warn-bg); color: var(--warn); }
.pill-alert { background: var(--alert-bg); color: var(--alert); }
.pill-live  { background: #fde0dc; color: #d1453b; }
.pill-live .dot { animation: pulse 1.4s infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.3; } }

.chip {
    border: 2px solid var(--card-border); background: var(--card);
    backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
    border-radius: 14px; padding: 0.45rem 0.85rem; font-weight: 600; font-size: 0.85rem;
    box-shadow: 0 6px 16px -8px rgba(60,45,20,0.25);
    transition: transform 0.18s cubic-bezier(0.34, 1.56, 0.64, 1), background 0.15s;
}
.chip:hover { transform: translateY(-2px) scale(1.03); background: #fff; }
.chip:active { transform: translateY(1px) scale(0.97); }
.chip.active { background: #2f2a22; color: #fff; border-color: #2f2a22; }
.chip-amber   { color: #8a5a00; }
.chip-emerald { color: #0f6b47; }
.chip-lime    { color: #3f6b0a; }
.chip-sky     { color: #0b5f8a; }

.icon-btn {
    border: none; background: rgba(255,255,255,0.8); border-radius: 10px;
    width: 2rem; height: 2rem; display: grid; place-items: center; font-size: 1rem;
    transition: transform 0.15s;
}
.icon-btn:hover { transform: scale(1.08); background: #fff; }

.select {
    border: none; background: rgba(255,255,255,0.85); border-radius: 10px;
    padding: 0.35rem 0.5rem; font: inherit; font-size: 0.82rem; font-weight: 500; color: var(--ink);
}

/* ---------- Layout ---------- */
.layout {
    max-width: 1800px; margin: 0 auto; padding: 1rem 1.5rem 1.5rem;
    display: grid; gap: 1.25rem;
    grid-template-columns: minmax(380px, 0.8fr) minmax(0, 1.2fr);
    grid-template-areas: "scene side" "history history";
}
.scene-card   { grid-area: scene; }
.side         { grid-area: side; display: flex; flex-direction: column; gap: 1.25rem; min-width: 0; }
.history-card { grid-area: history; }

@media (max-width: 1100px) {
    .layout { grid-template-columns: 1fr; grid-template-areas: "side" "scene" "history"; padding: 0.75rem; gap: 0.9rem; }
    .side { gap: 0.9rem; }
    .topbar { padding: 0.75rem 0.75rem 0; }
}


.card {
    background: var(--card); border: 2px solid var(--card-border); border-radius: var(--radius);
    backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
    box-shadow: var(--shadow); padding: 1rem 1.1rem; min-width: 0;
}
.card-head {
    display: flex; align-items: center; justify-content: space-between; gap: 0.75rem;
    flex-wrap: wrap; margin-bottom: 0.8rem;
}
.card-head h2 { font-size: 1.1rem; font-weight: 600; }

/* ---------- 3D scene ---------- */
.scene-card {
    position: relative; padding: 0; overflow: hidden; min-height: 640px;
    background: radial-gradient(circle at 50% 40%, #fffaf1 0%, #e9dfcd 100%);
}
.scene { position: absolute; inset: 0; cursor: grab; }
.scene:active { cursor: grabbing; }
.scene canvas { display: block; }
.scene-labels { position: absolute; inset: 0; pointer-events: none; overflow: hidden; z-index: 1; }
.scene-toolbar { position: absolute; top: 0.9rem; right: 0.9rem; display: flex; gap: 0.5rem; z-index: 2; }
.scene-presets {
    position: absolute; bottom: 0.9rem; left: 0.5rem; right: 0.5rem;
    display: flex; gap: 0.4rem; flex-wrap: wrap; justify-content: center; pointer-events: none; z-index: 2;
}
.scene-presets > * { pointer-events: auto; }
.scene-hint {
    position: absolute; top: 1rem; left: 1rem; font-size: 0.78rem; color: var(--ink-2);
    background: rgba(255,255,255,0.6); padding: 0.3rem 0.7rem; border-radius: 999px;
}

.scene-label {
    position: absolute; left: 0; top: 0; pointer-events: auto;
    display: flex; align-items: center; gap: 0.45rem;
    padding: 0.3rem 0.65rem 0.3rem 0.35rem; margin-top: -6px;
    border-radius: 999px; border: 2px solid #fff;
    background: rgba(255,255,255,0.9); box-shadow: 0 8px 18px -8px rgba(40,30,10,0.45);
    font-size: 0.78rem; line-height: 1.05; text-align: left; white-space: nowrap;
    transition: background 0.3s, box-shadow 0.3s;
}
.scene-label::after {
    content: ""; position: absolute; left: 50%; bottom: -7px; transform: translateX(-50%);
    border: 6px solid transparent; border-top-color: #fff; border-bottom: 0;
}
.scene-label:hover { box-shadow: 0 10px 22px -6px rgba(40,30,10,0.55); }
.sl-icon {
    width: 1.6rem; height: 1.6rem; border-radius: 50%; display: grid; place-items: center;
    background: var(--off-bg); font-size: 0.95rem;
}
.sl-text { display: flex; flex-direction: column; }
.sl-name { color: var(--ink-2); font-size: 0.68rem; font-weight: 500; }
.sl-value { font-weight: 700; font-size: 0.88rem; }
.scene-label[data-status="on"] .sl-icon   { background: #fff1c9; box-shadow: 0 0 0 3px rgba(255,190,60,0.35); }
.scene-label[data-status="ok"] .sl-icon   { background: var(--ok-bg); }
.scene-label[data-status="warn"] .sl-icon { background: var(--warn-bg); }
.scene-label[data-status="alert"] .sl-icon{ background: var(--alert-bg); animation: pulse 1.2s infinite; }
.scene-label[data-status="on"] .sl-value  { color: var(--ok); }
.scene-label[data-status="off"] .sl-value { color: var(--off); }
.scene-label[data-status="warn"] .sl-value{ color: var(--warn); }
.scene-label[data-status="alert"] .sl-value{ color: var(--alert); }
.scene-label[data-status="unknown"] .sl-value{ color: var(--ink-3); font-weight: 500; }

/* ---------- Camera ---------- */
.camera-card { padding-bottom: 1.1rem; }
.camera-controls { display: flex; align-items: center; gap: 0.4rem; }
.camera-wrap {
    position: relative; aspect-ratio: 16 / 9; border-radius: 16px; overflow: hidden;
    background: #1d1f1c; box-shadow: inset 0 0 0 1px rgba(0,0,0,0.1);
}
.camera-wrap video-stream, .camera-wrap video { display: block; width: 100%; height: 100%; }
.camera-wrap video { object-fit: contain; background: #1d1f1c; }
.camera-wrap .cam-msg {
    position: absolute; inset: 0; display: grid; place-items: center; color: #d8d2c4; font-size: 0.9rem;
}

/* ---------- Tiles ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(235px, 1fr)); gap: 0.9rem; }
@media (max-width: 560px) { .tiles { grid-template-columns: 1fr 1fr; gap: 0.6rem; } }

.tile {
    position: relative; text-align: left; overflow: hidden;
    background: var(--card); border: 2px solid var(--card-border); border-radius: 20px;
    padding: 0.85rem 0.95rem; box-shadow: var(--shadow);
    display: flex; flex-direction: column; gap: 0.35rem; min-height: 9.5rem;
    transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.2s;
}
.tile:hover { transform: translateY(-3px); box-shadow: 0 16px 34px -12px rgba(60,45,20,0.35); }
.tile::before {
    content: ""; position: absolute; inset: 0 0 auto 0; height: 5px; background: var(--accent, #ccc);
    opacity: 0.85;
}
.tile-head { display: flex; align-items: center; justify-content: space-between; gap: 0.4rem; }
.tile-name { display: flex; align-items: center; gap: 0.45rem; font-weight: 600; font-size: 0.95rem; min-width: 0; }
.tile-pill { flex-shrink: 0; }
.tile-icon {
    width: 2.1rem; height: 2.1rem; border-radius: 12px; display: grid; place-items: center; font-size: 1.15rem;
    background: color-mix(in srgb, var(--accent) 16%, white);
}
.tile-value { font-size: 2.1rem; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; }
.tile-value .unit { font-size: 1rem; font-weight: 500; color: var(--ink-2); margin-left: 0.15rem; }
.tile-sub { font-size: 0.76rem; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile-entity { font-size: 0.68rem; color: var(--ink-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: auto; }
.tile.is-on .tile-icon { box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 25%, transparent); }
.tile.is-on { background: linear-gradient(160deg, color-mix(in srgb, var(--accent) 12%, white), var(--card) 70%); }
.tile.unavailable .tile-value { color: var(--ink-3); font-size: 1.2rem; }

.state-badge {
    font-size: 1.5rem; font-weight: 700; letter-spacing: 0.02em;
}
.tile.is-on .state-badge { color: var(--accent); }
.tile.is-off .state-badge { color: var(--off); }

.range-bar { position: relative; height: 8px; border-radius: 99px; background: var(--alert-bg); margin: 0.2rem 0 0.1rem; }
.range-bar .warn-band, .range-bar .ideal-band { position: absolute; top: 0; bottom: 0; border-radius: 99px; }
.range-bar .warn-band { background: var(--warn-bg); }
.range-bar .ideal-band { background: #bfe8c9; }
.range-bar .marker {
    position: absolute; top: 50%; width: 14px; height: 14px; border-radius: 50%;
    transform: translate(-50%, -50%); background: #fff; border: 3px solid var(--ink);
    transition: left 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.sparkline { width: 100%; height: 34px; display: block; }

/* ---------- History ---------- */
.segmented { display: inline-flex; background: rgba(255,255,255,0.7); border-radius: 12px; padding: 3px; gap: 2px; }
.segmented button {
    border: none; background: transparent; padding: 0.35rem 0.8rem; border-radius: 9px; font-weight: 600; font-size: 0.85rem; color: var(--ink-2);
}
.segmented button.active { background: var(--ink); color: #fff; }

.chart-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(420px, 100%), 1fr)); gap: 1rem; }
.chart-box {
    background: rgba(255,255,255,0.65); border-radius: 18px; padding: 0.85rem 0.9rem 0.6rem;
    border: 2px solid rgba(255,255,255,0.9); transition: box-shadow 0.3s;
}
.chart-box.highlight { box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent, #7c5cff) 45%, transparent); }
.chart-title { display: flex; align-items: center; justify-content: space-between; gap: 0.6rem; flex-wrap: wrap; margin-bottom: 0.4rem; }
.chart-title h3 { font-size: 0.98rem; font-weight: 600; }
.chart-stats { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.stat { font-size: 0.74rem; background: rgba(255,255,255,0.9); border-radius: 8px; padding: 0.2rem 0.5rem; color: var(--ink-2); }
.stat b { color: var(--ink); font-weight: 600; }
.chart-canvas { position: relative; height: 240px; }
.timeline-box { margin-top: 1rem; }
.timeline-canvas { height: 220px; }
.chart-empty[hidden] { display: none; }
.chart-empty { position: absolute; inset: 0; display: grid; place-items: center; color: var(--ink-3); font-size: 0.9rem; }

.footer { text-align: center; padding: 0 1rem 1.5rem; }

@media (max-width: 1100px) {
    .scene-card { min-height: 0; height: min(78vh, 720px); }
}

/* ---------- Phone (keep last so it overrides base rules) ---------- */
@media (max-width: 600px) {
    .topbar { gap: 0.5rem; }
    .brand-icon { width: 2.6rem; height: 2.6rem; font-size: 1.6rem; border-radius: 14px; }
    .brand h1 { font-size: 1.25rem; }
    .subtitle { font-size: 0.78rem; }
    .topbar-right { width: 100%; justify-content: space-between; gap: 0.5rem; }
    .topbar-right .muted, .topbar-right .small { font-size: 0.72rem; }
    .clock { font-size: 1.05rem; }
    .layout { padding: 0.6rem; gap: 0.75rem; }
    .side { gap: 0.75rem; }
    .card { padding: 0.8rem 0.85rem; border-radius: 20px; }
    .scene-card { padding: 0; height: 70vh; min-height: 420px; }
    .scene-hint { display: none; }
    .scene-toolbar { top: 0.6rem; right: 0.6rem; }
    .scene-presets { bottom: 0.6rem; gap: 0.3rem; }
    .chip { padding: 0.35rem 0.6rem; font-size: 0.78rem; border-radius: 12px; }
    .scene-label { font-size: 0.7rem; padding: 0.2rem 0.5rem 0.2rem 0.25rem; gap: 0.3rem; }
    .sl-icon { width: 1.3rem; height: 1.3rem; font-size: 0.8rem; }
    .sl-name { font-size: 0.6rem; }
    .sl-value { font-size: 0.78rem; }

    .chart-grid { grid-template-columns: 1fr; gap: 0.75rem; }
    .chart-box { padding: 0.7rem 0.6rem 0.5rem; border-radius: 14px; }
    .chart-canvas { height: 200px; }
    .timeline-canvas { height: 200px; }
    .segmented button { padding: 0.3rem 0.65rem; }
    .tiles { gap: 0.55rem; }
    .tile { padding: 0.75rem 0.7rem; min-height: 8.5rem; }
    .tile-value { font-size: 1.75rem; }
    .state-badge { font-size: 1.3rem; }
    .tile-entity { display: none; }
    .tile-head { flex-direction: column; align-items: flex-start; gap: 0.35rem; }
    .tile-name { font-size: 0.88rem; gap: 0.35rem; }
    .tile-icon { width: 1.8rem; height: 1.8rem; font-size: 1rem; border-radius: 10px; }
    .tile-pill { font-size: 0.7rem; padding: 0.2rem 0.55rem; }
    .camera-controls { gap: 0.3rem; }
}
