/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * Copyright © 2026 Marcel Rymenhout and IniqDash contributors */
/* ==========================================================================
   Fan Widget V1 — widget-specific CSS only. Surfaces, colours, radii, shadows and type come from
   the shared tokens (assets/css/tokens.css).

   Identity: a rotor in a recessed housing that turns while the fan runs (speed sets the pace),
   a speed arc around the housing (state without animation), a stepped speed bar (one segment per
   fan step) and preset chips.

   Arrangements (container "fw" = the widget's own box):
     compact   < 260px   rotor + power on top, name/state, speed bar, one "Preset ▸" button
     standard  260–479   large rotor on the left; name/state + power; speed bar; preset chips
     wide     >= 480px   rotor | name/state | speed bar over chips | power — only while the box
                         is a strip (< 240px tall, or ≥ 2 : 1)
     tall     ≥ 300px tall (≥ 260 wide) / ≥ 360px tall (narrower), not a strip: the rotor moves to
                         the free middle and grows with the box (both axes); name, controls around it
   ========================================================================== */

.fw {
  position: relative;
  container: fw / size;
  min-width: 0;
  min-height: 92px;
  overflow: hidden;
  border-radius: var(--radius-device);
  color: var(--text-primary);
  font-family: var(--font);
  background: linear-gradient(165deg, var(--surface-primary-top), var(--surface-primary) 55%, var(--surface-secondary));
  box-shadow: var(--shadow-primary), inset 0 0 0 1px var(--border-subtle);
}
.fw__in {
  --fw-rotor: 52px;
  height: 100%;
  padding: 14px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-rows: auto auto minmax(0, 1fr) auto auto;
  grid-template-areas: "rotor power" "id id" ". ." "speed speed" "presets presets";
  row-gap: 6px;
}

/* ---- rotor ---- */
.fw__rotor {
  grid-area: rotor;
  position: relative;
  align-self: start;
  width: var(--fw-rotor);
  height: var(--fw-rotor);
  border-radius: 50%;
  cursor: pointer;
  background: radial-gradient(circle at 50% 40%, var(--surface-secondary), var(--page-bg));
  box-shadow: var(--shadow-icon), inset 0 0 0 1px var(--border-subtle);
}
.fw__rotor::before { content: ""; position: absolute; inset: 6%; border-radius: 50%; box-shadow: inset 0 0 0 1px var(--border-primary), inset 0 3px 8px rgba(0, 0, 0, .45); }   /* the recessed housing the blades turn in */
.fw__edge { fill: none; stroke: var(--text-primary); stroke-opacity: .22; stroke-width: 1.1; stroke-linecap: round; }
.fw[data-state="on"] .fw__edge { stroke-opacity: .4; }
.fw__level { position: absolute; inset: -4px; width: auto; height: auto; opacity: .9; }   /* speed: a shared ProgressRing */
.fw__level .iqv-ring__track { stroke: var(--border-subtle); }
.fw__rotor-svg { position: absolute; inset: 12%; width: 76%; height: 76%; }
.fw__blades path { fill: var(--surface-tertiary); stroke: var(--border-primary); stroke-width: .8; transition: fill var(--dur-medium) var(--ease); }
.fw__hub { fill: var(--surface-primary-top); stroke: var(--border-primary); }
.fw__hub-cap { fill: var(--text-muted); transition: fill var(--dur-medium) var(--ease); }
.fw[data-state="on"] .fw__blades path { fill: var(--text-secondary); }
.fw[data-state="on"] .fw__hub-cap { fill: var(--accent); }
/* the rotor turns through a shared visual layer (IniqVisuals: rotate while the CONFIRMED state is on; reduced motion: still) */
.fw[data-state="unavailable"] .fw__rotor, .fw[data-state="unknown"] .fw__rotor { opacity: var(--unavailable-opacity); cursor: default; }

/* ---- identity + state ---- */
.fw__id { grid-area: id; min-width: 0; }
.fw__name { margin: 0; font-size: 14px; font-weight: var(--iq-fw-title); line-height: 1.22; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; overflow: hidden; overflow-wrap: break-word; }
.fw[data-has-sub="true"] .fw__name { -webkit-line-clamp: 1; }
.fw__sub { margin: 1px 0 0; font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fw__sub[hidden] { display: none; }
.fw__state { margin: 3px 0 0; font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fw__state-t { font-weight: var(--iq-fw-status); letter-spacing: .08em; text-transform: uppercase; font-size: 11.5px; color: var(--text-secondary); }
.fw[data-state="on"] .fw__state-t { color: var(--accent); }
.fw[data-state="unavailable"] .fw__state-t, .fw[data-state="unknown"] .fw__state-t { text-transform: none; letter-spacing: 0; color: var(--text-muted); }
.fw[data-state="unavailable"] .fw__name, .fw[data-state="unknown"] .fw__name { color: var(--text-muted); }

/* ---- power ---- */
.fw__power {
  grid-area: power;
  align-self: start;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 40px;
  padding: 0 14px 0 11px;
  border: 0;
  border-radius: var(--radius-pill);
  cursor: pointer;
  font: 600 12.5px var(--font);
  color: var(--text-secondary);
  background: var(--page-bg);
  box-shadow: var(--shadow-icon), inset 0 0 0 1px var(--border-subtle);
}
.fw__power-i { width: 16px; height: 16px; display: grid; }
.fw__power-i svg { width: 100%; height: 100%; }
.fw__power[aria-pressed="true"] { color: var(--accent); background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent-line); }
.fw__power:disabled { cursor: not-allowed; color: var(--text-muted); opacity: .6; }
.fw__power:focus-visible, .fw__speed:focus-visible, .fw__chip:focus-visible, .fw__cycle:focus-visible { outline: var(--focus-ring); outline-offset: 2px; }

/* ---- speed: stepped bar (each segment one fan step) ---- */
.fw__speed {
  grid-area: speed;
  position: relative;
  display: flex;
  align-items: flex-end;
  gap: 3px;
  height: 40px;
  padding: 7px 46px 7px 10px;
  border-radius: 12px;
  cursor: pointer;
  background: var(--page-bg);
  box-shadow: var(--shadow-icon), inset 0 0 0 1px var(--border-subtle);
  touch-action: pan-y;
}
.fw__seg { flex: 1 1 0; height: 100%; display: flex; align-items: flex-end; }
.fw__seg i {                                  /* bars rise with each step: reads as "speed", not as a dimmer */
  display: block;
  width: 100%;
  height: calc(34% + var(--i) * 12%);
  max-height: 100%;
  border-radius: 3px;
  background: var(--surface-tertiary);
  transition: background var(--dur-fast) var(--ease);
}
.fw__seg.is-on i { background: linear-gradient(180deg, var(--accent), color-mix(in srgb, var(--accent) 55%, var(--surface-tertiary))); }
.fw__pct { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); font-size: 12px; font-weight: var(--iq-fw-display); font-variant-numeric: tabular-nums; color: var(--text-secondary); }
.fw[data-state="on"] .fw__pct { color: var(--text-primary); }
.fw__speed[aria-disabled="true"] { cursor: not-allowed; opacity: .55; }

/* ---- presets ---- */
.fw__presets { grid-area: presets; display: flex; gap: 6px; min-width: 0; }
.fw__chip { display: none; }
.fw__cycle {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 8px;
  width: 100%;
  height: 40px;
  padding: 0 12px;
  border: 0;
  border-radius: 12px;
  cursor: pointer;
  font: 500 12px var(--font);
  color: var(--text-muted);
  background: var(--page-bg);
  box-shadow: var(--shadow-icon), inset 0 0 0 1px var(--border-subtle);
}
.fw__cycle-v { margin-left: auto; font-weight: 600; color: var(--text-primary); }   /* opens the preset Flyout */
.fw__cycle:disabled, .fw__chip:disabled { cursor: not-allowed; opacity: .55; }

/* ---------------------------------------------------------------- standard: 260–479px */
@container fw (min-width: 260px) {
  .fw__in {
    --fw-rotor: clamp(56px, min(26cqw, calc(100cqh - 32px)), 116px);
    padding: 16px;
    grid-template-columns: var(--fw-rotor) minmax(0, 1fr) auto;
    grid-template-rows: auto minmax(0, 1fr) auto auto;
    grid-template-areas: "rotor id power" "rotor . ." "rotor speed speed" "presets presets presets";
    column-gap: 14px;
  }
  .fw__rotor { align-self: center; }
  .fw__name, .fw[data-has-sub="true"] .fw__name { font-size: 15px; -webkit-line-clamp: 2; }
  .fw__state-x { display: none; }              /* speed and preset are on their own controls here */
  .fw[data-caps="power speed preset"] .fw__rotor { align-self: start; }
  .fw__chip {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 1 1 auto;
    height: 38px;
    min-width: 44px;
    padding: 0 10px;
    border: 0;
    border-radius: var(--radius-pill);
    cursor: pointer;
    font: var(--iq-fw-control) 12px var(--font);
    color: var(--text-secondary);
    background: var(--page-bg);
    box-shadow: inset 0 0 0 1px var(--border-subtle);
  }
  .fw__chip { position: relative; }
  .fw__chip::after { content: ""; position: absolute; inset: -1px 0; }   /* 40px effective target */
  .fw__chip[aria-checked="true"] { color: var(--text-primary); background: var(--surface-tertiary); box-shadow: inset 0 0 0 1px var(--accent-line); font-weight: var(--iq-fw-control); }
  .fw[data-state="on"] .fw__chip[aria-checked="true"] { color: var(--accent); }
  .fw__cycle { display: none; }
}

/* ---------------------------------------------------------------- wide: ≥ 480px */
@container fw (min-width: 480px) and ((max-height: 239px) or (min-aspect-ratio: 2 / 1)) {
  .fw__in {
    --fw-rotor: clamp(56px, calc(100cqh - 28px), 116px);
    padding: 14px 18px;
    grid-template-columns: var(--fw-rotor) minmax(0, 1fr) minmax(0, 1.25fr) auto;
    grid-template-rows: minmax(0, 1fr) auto auto minmax(0, 1fr);
    grid-template-areas: "rotor id . power" "rotor id speed power" "rotor id presets power" "rotor id . power";
    column-gap: 18px;
    row-gap: 6px;
  }
  .fw__id { align-self: center; }
  .fw__power { align-self: center; }
  .fw[data-caps="power"] .fw__in { grid-template-columns: var(--fw-rotor) minmax(0, 1fr) auto; grid-template-areas: "rotor id power" "rotor id power" "rotor id power" "rotor id power"; }
  .fw__name { font-size: 16px; }
}

/* ---------------------------------------------------------------- tall: room above and below
   The rotor is the identity: in a large or tall box it takes the free middle and grows with it,
   instead of staying a small disc in a corner of an empty card. */
@container fw (min-width: 260px) and (min-height: 300px) and (max-aspect-ratio: 2 / 1) {
  .fw__in, .fw[data-caps] .fw__in {
    --fw-rotor: clamp(64px, min(60cqw, calc(100cqh - 230px)), 220px);
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto minmax(0, 1fr) auto auto;
    grid-template-areas: "id power" "rotor rotor" "speed speed" "presets presets";
  }
  .fw__rotor, .fw[data-caps] .fw__rotor { align-self: center; justify-self: center; }
}
@container fw (max-width: 259px) and (min-height: 360px) {
  .fw__in, .fw[data-caps] .fw__in {
    --fw-rotor: clamp(52px, min(70cqw, calc(100cqh - 250px)), 160px);
    grid-template-rows: auto minmax(0, 1fr) auto auto auto;
    grid-template-areas: ". power" "rotor rotor" "id id" "speed speed" "presets presets";
  }
  .fw__rotor { align-self: center; justify-self: center; }
}

/* ---------------------------------------------------------------- micro (pass 2): rotor + power as an icon, name + state; nothing overlaps */
@container fw (max-width: 159px) {
  .fw__in { --fw-rotor: 44px; padding: 10px; column-gap: 8px; }
  .fw__power { width: 44px; height: 44px; padding: 0; justify-content: center; }
  .fw__power-t { display: none; }
  .fw__sub { display: none; }
  .fw__name { font-size: 13px; -webkit-line-clamp: 1; }
}
@container fw (max-height: 119px) { .fw__sub { display: none; } }
@container fw (max-width: 159px) { .fw__state-x { display: none; } }
@container fw (max-height: 104px) and (max-width: 159px) { .fw__in { --fw-rotor: 36px; padding: 8px 10px; row-gap: 2px; } .fw__power { width: 36px; height: 36px; } }

/* ================================================================ pass 2 — a physical fan: grille over the blades, pedestal under the housing (from the size where it can be read) */
.fw__grille { position: absolute; inset: 6%; width: 88%; height: 88%; pointer-events: none; }
.fw__wire { fill: none; stroke: var(--text-primary); stroke-opacity: .3; stroke-width: .9; }
.fw[data-state="on"] .fw__wire { stroke-opacity: .42; }
.fw__stand { display: none; position: absolute; left: 10%; top: 96%; width: 80%; height: 34%; overflow: visible; }
.fw__neck { fill: var(--border-primary); }
.fw__base { fill: var(--surface-interactive); }
.fw__base-top { fill: var(--border-primary); }
@container fw (min-width: 200px) and (min-height: 190px) {
  .fw__stand { display: block; }
  .fw__rotor { margin-bottom: calc(var(--fw-rotor) * .3); }
}
@container fw (min-width: 260px) and (max-height: 330px) { .fw__in { --fw-rotor: clamp(56px, min(24cqw, calc((100cqh - 32px) / 1.32)), 112px); } }
@container fw (min-width: 260px) and (min-height: 300px) and (max-aspect-ratio: 2 / 1) { .fw__in, .fw[data-caps] .fw__in { --fw-rotor: clamp(64px, min(52cqw, calc((100cqh - 230px) / 1.3)), 200px); } }
