/* ===========================================================================
   Components.

   Every value here resolves to a token. If you find yourself typing a hex
   code or a pixel size in this file, it belongs in tokens.css instead --
   that is what keeps a redesign to one file.
   =========================================================================== */

*, *::before, *::after { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--wall);
    color: var(--ink);
    font-family: var(--font-ui);
    font-size: var(--step-0);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}


:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* --- Typography ---------------------------------------------------------- */

.eyebrow {
    font-family: var(--font-label);
    font-size: var(--step--1);
    font-weight: 600;
    letter-spacing: var(--tracking-label);
    text-transform: uppercase;
    color: var(--ink-soft);
}

h1, h2, h3 {
    font-family: var(--font-label);
    font-weight: 600;
    letter-spacing: var(--tracking-tight);
    margin: 0;
}
h1 { font-size: var(--step-3); }
h2 { font-size: var(--step-2); }
h3 { font-size: var(--step-1); }

.numeric {
    font-variant-numeric: tabular-nums;
    font-feature-settings: "tnum";
}

/* --- Room heading: an eyebrow with a rule running to the edge ------------- */

.room-head {
    display: flex;
    align-items: center;
    gap: var(--gap);
    margin: var(--gap-section) 0 var(--gap-wide);
}
.room-head::after {
    content: "";
    flex: 1;
    height: 1px;
    background: var(--wall-line);
}
.room-head__count {
    font-family: var(--font-label);
    font-size: var(--step--1);
    color: var(--ink-faint);
    font-variant-numeric: tabular-nums;
}

/* ===========================================================================
   THE WINDOW TILE  -- the signature element.

   A blind is drawn at its real position and tilt rather than described by a
   number. Twenty of these read as a floor plan: you see which side of the
   house is shaded without parsing a single percentage.

   Driven by two custom properties, both 0-100, matching the Shelly API:
       --pos   100 = fully open (blind raised), 0 = fully closed
       --tilt  100 = slats open (light through), 0 = slats closed
   =========================================================================== */

.window {
    --pitch: 9px;
    /* Slat depth within its pitch. Closed slats fill it and block the light;
       open slats shrink to a third and let it through. */
    --slat-h: calc(var(--pitch) * (1 - var(--tilt) * 0.0062));
    /* How far down the glass the blind hangs. */
    --cover: calc((100 - var(--pos)) * 1%);

    display: flex;
    flex-direction: column;
    background: var(--wall-raised);
    border-radius: var(--radius-tile);
    box-shadow: var(--lift);
    overflow: hidden;
}

.window__glass {
    position: relative;
    aspect-ratio: 4 / 5;
    background-image: linear-gradient(180deg,
        var(--day-high) 0%, var(--day-mid) 55%, var(--day-low) 100%);
    box-shadow: var(--recess);
    cursor: ns-resize;
    touch-action: none;
    overflow: hidden;
}

/* Light spilling onto the sill below the blind edge. */
.window__glass::after {
    content: "";
    position: absolute;
    left: 0; right: 0;
    top: var(--cover);
    height: 26%;
    background: linear-gradient(180deg, var(--day-spill), transparent);
    pointer-events: none;
    transition: top var(--travel);
}

.window__blind {
    position: absolute;
    inset: 0 0 auto 0;
    height: var(--cover);
    transition: height var(--travel);
    background-image:
        /* the lit upper edge of each slat */
        repeating-linear-gradient(180deg,
            #FFFFFF1F 0, transparent 1px, transparent var(--pitch)),
        /* the slats themselves */
        repeating-linear-gradient(180deg,
            var(--slat) 0,
            var(--slat) calc(var(--slat-h) - 1px),
            var(--slat-edge) calc(var(--slat-h) - 1px),
            var(--slat-edge) var(--slat-h),
            transparent var(--slat-h),
            transparent var(--pitch));
    box-shadow: 0 2px 5px var(--slat-shadow);
}

/* The headrail: a blind is never fully gone, the box stays at the top. */
.window__blind::before {
    content: "";
    position: absolute;
    inset: 0 0 auto 0;
    height: 5px;
    background: var(--slat-edge);
}

.window__label {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--gap-tight);
    padding: var(--gap-tight) var(--gap);
}
.window__name {
    font-family: var(--font-label);
    font-weight: 600;
    font-size: var(--step-0);
    letter-spacing: 0.02em;
}
.window__read {
    font-family: var(--font-label);
    font-size: var(--step--1);
    color: var(--ink-soft);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}
.window__read b { color: var(--ink); font-weight: 600; }

/* --- Tile states --------------------------------------------------------- */

/* Travelling: a hairline creeps along the blind edge. Restrained on purpose --
   twenty tiles animating at once would be a light show, not a dashboard. */
.window.is-moving .window__blind {
    transition: height 1.2s linear;
}
.window.is-moving .window__glass::before {
    content: "";
    position: absolute;
    left: 0; right: 0;
    top: var(--cover);
    height: 2px;
    background: var(--accent);
    opacity: 0.9;
    animation: edge-pulse 1.1s ease-in-out infinite;
    z-index: 2;
}
@keyframes edge-pulse {
    50% { opacity: 0.25; }
}

/* Offline: the daylight is gone because we cannot see it, not because it is
   dark. Hatching says "no reading available" rather than "closed". */
.window.is-offline .window__glass {
    background-image: repeating-linear-gradient(135deg,
        var(--wall-recess) 0 6px, var(--wall-line) 6px 12px);
    cursor: not-allowed;
}
.window.is-offline .window__blind,
.window.is-offline .window__glass::after { display: none; }
.window.is-offline .window__name { color: var(--ink-faint); }

/* Uncalibrated: position is unknown, so the blind is drawn as an outline
   instead of a lie. */
.window.is-uncalibrated .window__blind {
    background-image: repeating-linear-gradient(180deg,
        var(--slat) 0, var(--slat) 1px, transparent 1px, transparent var(--pitch));
    opacity: 0.55;
}

.window__flag {
    display: flex;
    align-items: center;
    gap: var(--gap-tight);
    padding: var(--gap-tight) var(--gap);
    font-size: var(--step--1);
    font-family: var(--font-label);
    letter-spacing: 0.02em;
    background: var(--wall-recess);
    color: var(--ink-soft);
    border-top: 1px solid var(--wall-line);
}
.window__flag.is-alert { color: var(--alert); }
.window__flag.is-warn  { color: var(--warn); }
.window__flag::before {
    content: "";
    width: 6px; height: 6px;
    border-radius: var(--radius-pill);
    background: currentColor;
    flex: none;
}

/* --- Grid ---------------------------------------------------------------- */

.window-grid {
    display: grid;
    gap: var(--gap);
    grid-template-columns: repeat(auto-fill, minmax(148px, 1fr));
}

/* ===========================================================================
   Controls
   =========================================================================== */

.btn {
    font-family: var(--font-label);
    font-size: var(--step-0);
    font-weight: 600;
    letter-spacing: 0.03em;
    color: var(--ink);
    background: var(--wall-raised);
    border: 1px solid var(--wall-line);
    border-radius: var(--radius-control);
    padding: 9px 16px;
    cursor: pointer;
    box-shadow: var(--lift);
    transition: background var(--tap), transform var(--tap);
}
.btn:hover { background: var(--wall); }
.btn:active { transform: translateY(1px); box-shadow: none; }

.btn--primary {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-ink);
}
.btn--primary:hover { background: var(--accent-hover); }

.btn--quiet {
    background: transparent;
    box-shadow: none;
    border-color: transparent;
    color: var(--ink-soft);
}
.btn--quiet:hover { background: var(--wall-recess); color: var(--ink); }

/* Up / Stop / Down: one control, three segments, sized for a thumb. */
.drive {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    border: 1px solid var(--wall-line);
    border-radius: var(--radius-control);
    overflow: hidden;
    background: var(--wall-raised);
    box-shadow: var(--lift);
}
.drive button {
    appearance: none;
    border: 0;
    background: transparent;
    padding: 11px 0;
    cursor: pointer;
    color: var(--ink);
    display: grid;
    place-items: center;
    transition: background var(--tap);
}
.drive button + button { border-left: 1px solid var(--wall-line); }
.drive button:hover { background: var(--wall); }
.drive button:active { background: var(--wall-recess); }
.drive svg { width: 17px; height: 17px; display: block; }

/* Sliders. The track shows daylight; the filled part shows the blind. */
.slider {
    display: grid;
    gap: var(--gap-tight);
}
.slider__head {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    font-family: var(--font-label);
    font-size: var(--step--1);
    letter-spacing: var(--tracking-label);
    text-transform: uppercase;
    color: var(--ink-soft);
}
.slider__value {
    font-variant-numeric: tabular-nums;
    letter-spacing: 0;
    color: var(--ink);
    font-weight: 600;
}
.slider input[type="range"] {
    appearance: none;
    width: 100%;
    height: 26px;
    background: transparent;
    cursor: pointer;
    margin: 0;
}
.slider input[type="range"]::-webkit-slider-runnable-track {
    height: 26px;
    border-radius: var(--radius-control);
    box-shadow: var(--recess);
    background:
        linear-gradient(90deg, var(--slat) 0 var(--fill), transparent var(--fill)),
        linear-gradient(180deg, var(--day-high), var(--day-low));
}
.slider input[type="range"]::-moz-range-track {
    height: 26px;
    border-radius: var(--radius-control);
    box-shadow: var(--recess);
    background:
        linear-gradient(90deg, var(--slat) 0 var(--fill), transparent var(--fill)),
        linear-gradient(180deg, var(--day-high), var(--day-low));
}
.slider input[type="range"]::-webkit-slider-thumb {
    appearance: none;
    width: 8px; height: 26px;
    background: var(--ink);
    border-radius: 1px;
    border: 0;
    box-shadow: 0 0 0 1px #FFFFFF44;
}
.slider input[type="range"]::-moz-range-thumb {
    width: 8px; height: 26px;
    background: var(--ink);
    border-radius: 1px;
    border: 0;
}

/* --- Chips (room / group filters) ---------------------------------------- */

.chips { display: flex; flex-wrap: wrap; gap: var(--gap-tight); }
.chip {
    font-family: var(--font-label);
    font-size: var(--step--1);
    letter-spacing: 0.04em;
    padding: 5px 12px;
    border-radius: var(--radius-pill);
    border: 1px solid var(--wall-line);
    background: transparent;
    color: var(--ink-soft);
    cursor: pointer;
    transition: all var(--tap);
}
.chip:hover { border-color: var(--ink-faint); color: var(--ink); }
.chip[aria-pressed="true"] {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-ink);
}

/* --- Virtual keys --------------------------------------------------------- */

.key {
    display: grid;
    grid-template-columns: auto 1fr;
    align-items: center;
    gap: var(--gap);
    width: 100%;
    text-align: left;
    padding: var(--gap) var(--gap-wide) var(--gap) var(--gap);
    background: var(--wall-raised);
    border: 1px solid var(--wall-line);
    border-left: 3px solid var(--accent);
    border-radius: var(--radius-control);
    cursor: pointer;
    color: inherit;
    font: inherit;
    box-shadow: var(--lift);
    transition: background var(--tap), transform var(--tap);
}
.key:hover { background: var(--wall); }
.key:active { transform: translateY(1px); box-shadow: none; }

/* The glyph is a miniature of the target state: the same slat language as the
   tiles, so a key visibly means "put the blinds like this". */
.key__glyph {
    --pitch: 5px;
    --slat-h: calc(var(--pitch) * (1 - var(--tilt) * 0.0062));
    width: 38px; height: 44px;
    border-radius: 2px;
    background-image: linear-gradient(180deg, var(--day-high), var(--day-low));
    box-shadow: var(--recess);
    position: relative;
    overflow: hidden;
    flex: none;
}
.key__glyph::before {
    content: "";
    position: absolute;
    inset: 0 0 auto 0;
    height: calc((100 - var(--pos)) * 1%);
    background-image: repeating-linear-gradient(180deg,
        var(--slat) 0, var(--slat) calc(var(--slat-h) - 0.5px),
        var(--slat-edge) calc(var(--slat-h) - 0.5px), var(--slat-edge) var(--slat-h),
        transparent var(--slat-h), transparent var(--pitch));
}
.key__name { font-family: var(--font-label); font-weight: 600; }
.key__meta { font-size: var(--step--1); color: var(--ink-soft); }

.key-grid {
    display: grid;
    gap: var(--gap-tight);
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
}

/* --- Scenes --------------------------------------------------------------- */

.scene {
    display: grid;
    gap: var(--gap-tight);
    padding: var(--gap-wide);
    background: var(--wall-raised);
    border: 1px solid var(--wall-line);
    border-radius: var(--radius-control);
    cursor: pointer;
    text-align: left;
    color: inherit;
    font: inherit;
    box-shadow: var(--lift);
    transition: background var(--tap), transform var(--tap);
}
.scene:hover { background: var(--wall); }
.scene:active { transform: translateY(1px); box-shadow: none; }
.scene__name { font-family: var(--font-label); font-weight: 600; font-size: var(--step-1); }
.scene__steps { font-size: var(--step--1); color: var(--ink-soft); }

/* A scene is a sequence, so the steps are numbered -- the order genuinely
   carries information here, it is not decoration. */
.scene__seq {
    display: flex;
    gap: var(--gap-hair);
    margin-top: var(--gap-tight);
}
.scene__seq span {
    flex: 1;
    height: 3px;
    background: var(--accent);
    opacity: 0.28;
    border-radius: var(--radius-pill);
}
.scene__seq span:first-child { opacity: 0.85; }

/* --- Forms ----------------------------------------------------------------- */

.field { display: grid; gap: var(--gap-tight); margin-bottom: var(--gap-wide); }
.field label {
    font-family: var(--font-label);
    font-size: var(--step--1);
    letter-spacing: var(--tracking-label);
    text-transform: uppercase;
    color: var(--ink-soft);
}
.field input, .field select {
    font: inherit;
    color: var(--ink);
    background: var(--wall-recess);
    border: 1px solid var(--wall-line);
    border-radius: var(--radius-control);
    padding: 9px 11px;
    box-shadow: var(--recess);
}
.field input:focus, .field select:focus {
    border-color: var(--accent);
    outline: none;
    box-shadow: var(--recess), 0 0 0 2px var(--accent-faint);
}
.field__hint { font-size: var(--step--1); color: var(--ink-faint); }
.field__hint.is-alert { color: var(--alert); }

/* --- Navigation ------------------------------------------------------------ */

.nav-rail {
    display: flex;
    gap: var(--gap-tight);
    /* Grid and flex children default to min-width:auto, so a rail of four
       German labels refuses to shrink and silently widens whatever contains
       it. Scrolling is the honest fallback on a narrow phone. */
    min-width: 0;
    overflow-x: auto;
    scrollbar-width: none;
    padding: var(--gap-tight);
    background: var(--wall-recess);
    border-radius: var(--radius-control);
    border: 1px solid var(--wall-line);
}
.nav-rail::-webkit-scrollbar { display: none; }
.nav-rail a {
    flex: 1 0 auto;
    white-space: nowrap;
    text-align: center;
    padding: 9px 14px;
    border-radius: var(--radius-control);
    text-decoration: none;
    color: var(--ink-soft);
    font-family: var(--font-label);
    font-weight: 600;
    letter-spacing: 0.03em;
    transition: all var(--tap);
}
.nav-rail a:hover { color: var(--ink); background: var(--wall); }
.nav-rail a[aria-current="page"] {
    background: var(--wall-raised);
    color: var(--ink);
    box-shadow: var(--lift);
}

/* --- Empty and error states ------------------------------------------------ */

.notice {
    padding: var(--gap-wide);
    border-radius: var(--radius-control);
    border: 1px solid var(--wall-line);
    background: var(--wall-raised);
}
.notice--alert { border-left: 3px solid var(--alert); }
.notice--empty { border-style: dashed; background: transparent; }
.notice h3 { margin-bottom: var(--gap-tight); }
.notice p { margin: 0 0 var(--gap); color: var(--ink-soft); }
.notice p:last-child { margin-bottom: 0; }


/* ===========================================================================
   Selection and pending state
   =========================================================================== */

/* Selecting works across whatever is on screen, including a filtered view, so
   an ad-hoc set of blinds can be driven together without first inventing a
   room or a group for them. */
.window { position: relative; }

.window__pick {
    position: absolute;
    top: var(--gap-tight);
    right: var(--gap-tight);
    width: 24px; height: 24px;
    border-radius: var(--radius-pill);
    border: 1.5px solid #FFFFFFB3;
    background: #00000038;
    backdrop-filter: blur(3px);
    display: grid;
    place-items: center;
    cursor: pointer;
    padding: 0;
    opacity: 0;
    transition: opacity var(--tap), background var(--tap);
    z-index: 3;
}
.window:hover .window__pick,
.window__pick:focus-visible,
.window.is-picked .window__pick { opacity: 1; }
.window__pick svg { width: 13px; height: 13px; color: #fff; opacity: 0; }
.window.is-picked .window__pick { background: var(--accent); border-color: var(--accent); }
.window.is-picked .window__pick svg { opacity: 1; }

.window.is-picked { outline: 2px solid var(--accent); outline-offset: 2px; }

/* A queued command: the reading is what you will get, not what the device
   currently reports, so it is marked rather than shown as fact. */
.window.is-pending .window__read { color: var(--accent); }
.window.is-pending .window__read::after {
    content: "";
    display: inline-block;
    width: 5px; height: 5px;
    margin-left: 5px;
    border-radius: var(--radius-pill);
    background: var(--accent);
    animation: pending 900ms ease-in-out infinite;
}
@keyframes pending { 50% { opacity: 0.2; } }

/* Live values while dragging, centred on the glass. */
.window__live {
    position: absolute;
    inset: auto 0 0 0;
    display: none;
    justify-content: center;
    gap: var(--gap);
    padding: 6px 0;
    background: #00000059;
    backdrop-filter: blur(4px);
    color: #fff;
    font-family: var(--font-label);
    font-size: var(--step--1);
    font-variant-numeric: tabular-nums;
    letter-spacing: 0.04em;
    pointer-events: none;
    z-index: 2;
}
.window.is-dragging .window__live { display: flex; }
.window.is-dragging .window__blind { transition: none; }
.window.is-dragging .window__glass::after { transition: none; }

/* --- Selection bar --------------------------------------------------------- */

.pickbar {
    position: sticky;
    bottom: var(--gap);
    z-index: 20;
    display: grid;
    gap: var(--gap);
    grid-template-columns: auto 1fr auto;
    align-items: center;
    padding: var(--gap) var(--gap-wide);
    margin-top: var(--gap-wide);
    background: var(--wall-raised);
    border: 1px solid var(--wall-line);
    border-radius: var(--radius-tile);
    box-shadow: var(--lift);
}
.pickbar__count {
    font-family: var(--font-label);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}
.pickbar__sliders { display: grid; gap: var(--gap-tight); min-width: 0; }
.pickbar__actions { display: flex; gap: var(--gap-tight); }
@media (max-width: 640px) {
    .pickbar { grid-template-columns: 1fr; }
    .pickbar__actions { justify-content: stretch; }
    .pickbar__actions .btn { flex: 1; }
}
