/* ---------- Colors (light mode) ---------- */
  :root {
    --bg: #E6E9EE;
    --tile: #FFFFFF;
    --tile-border: #D3D8DF;
    --text: #1B2230;
    --text-muted: #6B7484;
    --on: #F5B83D;          /* amber: "light is on" */
    --on-text: #2A1D00;
    --on-glow: rgba(245, 184, 61, .45);
    --error: #D64545;
    --bar-h: 48px;          /* top bar height (without the notch area) */
    --log-yellow: #A87A00;  /* log level colors (darker in light mode so the text is readable) */
    --log-orange: #D2620A;
    --log-red: #D64545;
    --log-green: #1F8F55;
    --log-badge-text: #FFFFFF;
    color-scheme: light;
  }
  /* ---------- Colors (dark mode): system setting, unless "light" is forced ---------- */
  @media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
      --bg: #121519;
      --tile: #20252D;
      --tile-border: #2C333D;
      --text: #E8ECF2;
      --text-muted: #8A93A3;
      --on: #FFC24D;
      --on-glow: rgba(255, 194, 77, .35);
      --error: #FF7B7B;
      --log-yellow: #FFC24D;
      --log-orange: #FF9F43;
      --log-red: #FF7B7B;
      --log-green: #4CD787;
      --log-badge-text: #121519;
      color-scheme: dark;
    }
  }
  /* ---------- Colors (dark mode): forced from the settings menu ---------- */
  :root[data-theme="dark"] {
    --bg: #121519;
    --tile: #20252D;
    --tile-border: #2C333D;
    --text: #E8ECF2;
    --text-muted: #8A93A3;
    --on: #FFC24D;
    --on-glow: rgba(255, 194, 77, .35);
    --error: #FF7B7B;
    --log-yellow: #FFC24D;
    --log-orange: #FF9F43;
    --log-red: #FF7B7B;
    --log-green: #4CD787;
    --log-badge-text: #121519;
    color-scheme: dark;
  }

  * { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

  html, body {
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
    overflow: hidden;              /* no scrolling */
    overscroll-behavior: none;
    -webkit-user-select: none;
    user-select: none;
  }

  body {
    /* Pinned to the edges of the screen: fills it exactly in Safari and in the home screen app
       (100dvh comes out too short in the home screen app on iPhone) */
    position: fixed;
    inset: 0;
    /* The top bar handles the notch area itself.
       At the bottom, a bit less than the full home-indicator area, so the buttons reach further down */
    padding: 0 env(safe-area-inset-right, 0px) max(0px, calc(env(safe-area-inset-bottom, 0px) - 12px)) env(safe-area-inset-left, 0px);
  }

  button { font: inherit; color: inherit; cursor: pointer; }

  /* ---------- Main content ---------- */
  .app { height: 100%; }

  /* ---------- Top bar ---------- */
  .topbar {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: calc(var(--bar-h) + env(safe-area-inset-top, 0px));
    padding: env(safe-area-inset-top, 0px) 4px 0 16px;
    gap: 4px;
    background: var(--tile);
    border-bottom: 1px solid var(--tile-border);
  }
  .topbar h1 {
    flex: 1;
    min-width: 0;
    font-size: 1.15rem;
    font-weight: 700;
    letter-spacing: -.01em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .settings-toggle {
    flex: none;
    width: 44px; height: 44px;
    display: flex; align-items: center; justify-content: center;
    background: none;
    border: 0;
    border-radius: 12px;
  }
  /* Menu icon (three horizontal lines) drawn with a CSS mask, so it takes the text color in light and dark mode.
     The SVG is split into short lines ("\" at the end of a line continues the string). */
  .settings-toggle::before {
    content: "";
    width: 24px; height: 24px;
    background: currentColor;
    --menu: url("data:image/svg+xml,\
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' \
stroke-width='2' stroke-linecap='round'><path d='M4 6h16M4 12h16M4 18h16'/></svg>");
    -webkit-mask: var(--menu) center / contain no-repeat;
    mask: var(--menu) center / contain no-repeat;
  }
  .settings-toggle:active { background: var(--bg); }

  /* ---------- Fullscreen button (left of the title, touch devices only) ---------- */
  .fullscreen-toggle {
    display: none;
    flex: none;
    width: 40px; height: 44px;
    margin-left: -10px;
    align-items: center; justify-content: center;
    background: none;
    border: 0;
    border-radius: 12px;
  }
  @media (pointer: coarse) {
    .fullscreen-toggle { display: flex; }
  }
  /* Already without browser bars (opened from the home screen): no button needed */
  @media (display-mode: standalone), (display-mode: fullscreen) {
    .fullscreen-toggle { display: none; }
  }
  .fullscreen-toggle:active { background: var(--bg); }
  .fullscreen-toggle::before {
    content: "";
    width: 22px; height: 22px;
    background: currentColor;
    --expand: url("data:image/svg+xml,\
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' \
stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M8 3H5a2 2 0 0 \
0-2 2v3m18 0V5a2 2 0 0 0-2-2h-3m0 18h3a2 2 0 0 0 2-2v-3M3 16v3a2 2 0 0 0 2 2h3'/></svg>");
    --shrink: url("data:image/svg+xml,\
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' \
stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M8 3v3a2 2 0 0 \
1-2 2H3m18 0h-3a2 2 0 0 1-2-2V3m0 18v-3a2 2 0 0 1 2-2h3M3 16h3a2 2 0 0 1 2 2v3'/></svg>");
    -webkit-mask: var(--expand) center / contain no-repeat;
    mask: var(--expand) center / contain no-repeat;
  }
  .fullscreen-toggle.is-fullscreen::before {
    -webkit-mask-image: var(--shrink);
    mask-image: var(--shrink);
  }

  /* Instructions for iPhone, where Safari has no fullscreen for web pages */
  .fullscreen-help {
    display: none;
    position: absolute;
    top: calc(100% + 6px);
    left: 8px;
    z-index: 20;
    width: min(270px, calc(100vw - 16px));
    padding: 14px;
    font-size: .9rem;
    line-height: 1.4;
    background: var(--tile);
    border: 1px solid var(--tile-border);
    border-radius: 18px;
    box-shadow: 0 12px 32px rgba(0, 0, 0, .25);
  }
  .fullscreen-help.open { display: block; }
  .fullscreen-help p + p { margin-top: 6px; color: var(--text-muted); }

  /* ---------- Settings menu (drops down from the menu icon) ---------- */
  .settings {
    display: none;
    position: absolute;
    top: calc(100% + 6px);
    right: 8px;
    z-index: 20;
    width: min(270px, calc(100vw - 16px));
    padding: 14px;
    background: var(--tile);
    border: 1px solid var(--tile-border);
    border-radius: 18px;
    box-shadow: 0 12px 32px rgba(0, 0, 0, .25);
  }
  .settings.open { display: block; }

  .settings-label { font-size: .8rem; font-weight: 600; color: var(--text-muted); margin: 0 0 6px 4px; }

  .segmented {
    display: flex;
    gap: 4px;
    padding: 4px;
    margin-bottom: 14px;
    background: var(--bg);
    border-radius: 12px;
  }
  .segmented button {
    flex: 1;
    height: 40px;
    font-size: .9rem;
    font-weight: 600;
    background: none;
    border: 0;
    border-radius: 9px;
  }
  .segmented button.on { background: var(--on); color: var(--on-text); }

  .logout-button {
    width: 100%;
    height: 46px;
    font-size: 1rem;
    font-weight: 700;
    color: var(--error);
    background: none;
    border: 1px solid var(--tile-border);
    border-radius: 12px;
  }
  .logout-button:active { background: var(--bg); }

  /* Logs: day dropdown + "Ver" button */
  .logs-picker {
    display: flex;
    gap: 6px;
    margin-bottom: 14px;
  }
  .logs-picker select {
    flex: 1;
    min-width: 0;
    height: 44px;
    padding: 0 10px;
    font: inherit;
    font-size: 16px;               /* 16px or more stops iOS from zooming in */
    font-weight: 600;
    color: var(--text);
    background: var(--bg);
    border: 0;
    border-radius: 12px;
  }
  .logs-picker button {
    flex: none;
    min-width: 64px;
    padding: 0 12px;
    height: 44px;
    font-weight: 700;
    color: var(--on-text);
    background: var(--on);
    border: 0;
    border-radius: 12px;
  }
  .logs-picker :disabled { opacity: .5; }

  /* ---------- Rooms menu (left) + buttons (right) ---------- */
  .layout {
    display: flex;
    gap: 8px;
    padding: 8px;
    height: calc(100% - var(--bar-h) - env(safe-area-inset-top, 0px));
  }

  .rooms {
    flex: none;
    width: 20%;
    display: flex;
    flex-direction: column;
    gap: 3px;
  }
  .rooms button {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    padding: 0 2px;
    font-size: .64rem;
    font-weight: 600;
    line-height: 1.05;
    text-align: center;
    color: var(--text-muted);
    background: none;
    border: 0;
    border-radius: 12px;
    transition: background .18s, transform .08s;
  }
  .rooms button span { font-size: 1.05rem; line-height: 1; filter: grayscale(1); opacity: .6; }
  .rooms button:active { transform: scale(.94); }

  /* Selected room: set by pageFlip() */
  .rooms button.active {
    color: var(--text);
    background: var(--tile);
    box-shadow: inset 3px 0 0 var(--on);
  }
  .rooms button.active span { filter: none; opacity: 1; }

  /* ---------- Buttons area: one .room div per room, only the active one is shown ---------- */
  .panel {
    flex: 1;
    min-width: 0;
  }

  /* Grid: 4 columns × 5 rows = up to 20 buttons per room */
  .room {
    display: none;
    height: 100%;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(5, 1fr);
    gap: 6px;
  }
  .room.active { display: grid; }

  /* Room without buttons yet (the div must be written with nothing inside, not even spaces) */
  .room:empty::before {
    content: var(--room-empty-text, "Sin dispositivos");   /* text set by applyLanguage() */
    grid-area: 1 / 1 / -1 / -1;
    place-self: center;
    font-size: .9rem;
    font-weight: 600;
    color: var(--text-muted);
  }

  /* The checkbox inside each label stores the state; the label is the visible button */
  .panel input { position: absolute; opacity: 0; pointer-events: none; }

  .panel label {
    position: relative;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    min-width: 0;
    padding: 9px 5px 8px;
    background: var(--tile);
    border: 1px solid var(--tile-border);
    border-radius: 14px;
    cursor: pointer;
    transition: background .18s, box-shadow .18s, transform .08s;
    overflow: hidden;
  }
  .panel label:active { transform: scale(.95); }
  /* No iOS long-press menu (needed for momentary-fb buttons that are held down) */
  .panel label { -webkit-touch-callout: none; touch-action: manipulation; }

  .icon { font-size: 1.4rem; line-height: 1; filter: grayscale(1); opacity: .55; transition: filter .18s, opacity .18s; }
  .name { font-size: .68rem; font-weight: 600; line-height: 1.15; -webkit-hyphens: auto; hyphens: auto; }   /* long words split with a hyphen (needs lang="es") */

  /* Status indicator (dot) */
  .panel label::after {
    content: "";
    position: absolute;
    top: 8px; right: 7px;
    width: 7px; height: 7px;
    border-radius: 50%;
    background: var(--tile-border);
    transition: background .18s;
  }

  /* ---------- ON state ---------- */
  .panel label:has(input:checked) {
    background: var(--on);
    border-color: var(--on);
    color: var(--on-text);
    box-shadow: 0 0 16px var(--on-glow);
  }
  .panel label:has(input:checked) .icon { filter: none; opacity: 1; }
  .panel label:has(input:checked)::after { background: var(--on-text); }

  .panel label:has(input:focus-visible),
  button:focus-visible { outline: 3px solid var(--text); outline-offset: 2px; }

  /* ---------- Sliders (data-lvl): level 0-100, drag up/down anywhere on the tile ---------- */
  .slider {
    --level: 0;                      /* set by JavaScript */
    grid-row: span 3;                /* 1 column × 3 rows by default; e.g. style="grid-row: span 2" to change it */
    display: grid;
    grid-template: "icon value" auto ". ." 1fr "name name" auto / auto 1fr;
    min-width: 0;
    padding: 9px 6px 8px;
    background: linear-gradient(to top, var(--on) calc(var(--level) * 1%), var(--tile) 0);
    border: 1px solid var(--tile-border);
    border-radius: 14px;
    overflow: hidden;
    cursor: ns-resize;
    touch-action: none;              /* the finger moves the slider, not the page */
    -webkit-touch-callout: none;
    transition: border-color .18s, box-shadow .18s;
  }
  .slider .icon { grid-area: icon; }
  .slider .name { grid-area: name; }
  .slider .value {
    grid-area: value;
    justify-self: end;
    font-size: .72rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: var(--text-muted);
  }
  .slider.on { border-color: var(--on); }
  .slider.on .icon { filter: none; opacity: 1; }
  .slider.low-filled .name { color: var(--on-text); }     /* text on top of the amber fill */
  .slider.high-filled .value { color: var(--on-text); }
  .slider.dragging { box-shadow: 0 0 16px var(--on-glow); }
  .slider:focus-visible { outline: 3px solid var(--text); outline-offset: 2px; }

  /* Horizontal slider: class="slider horizontal". Full width of the room, one row high, fills from the left */
  .slider.horizontal {
    grid-column: 1 / -1;
    grid-row: span 1;
    grid-template: "icon name value" 1fr / auto 1fr auto;
    align-items: center;
    column-gap: 10px;
    padding: 0 14px;
    background: linear-gradient(to right, var(--on) calc(var(--level) * 1%), var(--tile) 0);
    cursor: ew-resize;
  }
  .slider.horizontal .name { font-size: .8rem; }
  .slider.horizontal .value { font-size: .85rem; }

  /* ---------- Address codes (data-adr), changed by fbAdr() ---------- */
  /* ^TXT: a second text for the "on" state (state 2), only shown while the button is on */
  .txt-on { display: none; }
  .has-on-text:is(:has(> input:checked), .on) > :not(.txt-on):is(.name, .adr-txt) { display: none; }
  .has-on-text:is(:has(> input:checked), .on) > .txt-on { display: block; }
  .rooms button .adr-txt { font-size: inherit; line-height: inherit; filter: none; opacity: 1; -webkit-hyphens: auto; hyphens: auto; }   /* not styled like the room icon; long words split with a hyphen */

  /* ^BOP: opacity per state (state 2 falls back to state 1) */
  [data-adr] { opacity: var(--adr-op1, 1); }
  [data-adr]:is(:has(> input:checked), .on) { opacity: var(--adr-op2, var(--adr-op1, 1)); }

  /* ^ENA: disabled */
  [data-adr].disabled { filter: grayscale(1) opacity(.4); }

  /* ---------- Landscape lock ---------- */
  .rotate-notice {
    display: none;
    position: fixed; inset: 0;
    background: var(--bg);
    align-items: center; justify-content: center;
    flex-direction: column; gap: 12px;
    text-align: center; font-size: 1.1rem; font-weight: 600;
    z-index: 30;
  }
  .rotate-notice div { font-size: 3rem; }
  @media (orientation: landscape) and (max-height: 600px) {
    .rotate-notice { display: flex; }
    .app, .login, .logout-screen, .logs-screen { visibility: hidden; }
  }

  @media (prefers-reduced-motion: reduce) {
    * { transition: none !important; }
    .spinner { animation-duration: 2s; }   /* slower, but it must keep turning to show progress */
  }

  /* ---------- Login ---------- */
  .login { height: 100%; }

  .login-card {
    height: 100%;
    max-width: 420px;
    margin: 0 auto;
    padding: env(safe-area-inset-top, 0px) 24px 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .login-logo {
    width: 76px; height: 76px;
    margin: 0 auto 18px;
    display: flex; align-items: center; justify-content: center;
    font-size: 2.4rem;
    background: var(--on);
    border-radius: 22px;
    box-shadow: 0 0 24px var(--on-glow);
  }

  /* The form stacks its fields like the card, so the button stretches to the full width */
  .login-card form {
    display: flex;
    flex-direction: column;
  }

  .login-title { text-align: center; font-size: 1.9rem; font-weight: 700; letter-spacing: -.02em; }
  .login-subtitle { text-align: center; color: var(--text-muted); font-size: 1rem; margin: 6px 0 30px; }

  .login-label { font-size: .95rem; font-weight: 600; margin: 0 0 8px 4px; }

  .login-input {
    width: 100%;
    height: 58px;
    margin-bottom: 18px;
    padding: 0 16px;
    font: inherit;
    font-size: 1.1rem;           /* 16px or more stops iOS from zooming in */
    color: var(--text);
    background: var(--tile);
    border: 2px solid var(--tile-border);
    border-radius: 16px;
    outline: none;
    -webkit-user-select: text;   /* body disables selection; inputs need it */
    user-select: text;
    transition: border-color .18s;
  }
  .login-input:focus { border-color: var(--on); }

  .login-button {
    height: 62px;
    margin-top: 10px;
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--on-text);
    background: var(--on);
    border: none;
    border-radius: 16px;
    box-shadow: 0 0 18px var(--on-glow);
    transition: transform .08s;
  }
  .login-button:active { transform: scale(.97); }

  /* ---------- Login expired (forces a page reload) ---------- */
  .login-logo.expired {
    background: var(--tile);
    border: 2px solid var(--tile-border);
    box-shadow: none;
  }
  .expired-title { color: var(--error); }
  .expired-text { margin-bottom: 26px; line-height: 1.4; }

  /* Feedback messages stacked on the same spot */
  .login-feedback {
    display: grid;
    min-height: 3em;
    margin-top: 18px;
    text-align: center;
    font-size: 1rem;
    font-weight: 600;
    color: var(--error);
  }
  .login-feedback > div { grid-area: 1 / 1; transition: opacity .2s; }

  /* ---------- Logs screen (idDiv_logs) ---------- */
  .logs-screen { height: 100%; }
  .logs-screen .topbar { padding-left: 4px; }
  /* The longest date ("Miércoles, 30 de Septiembre de 2026") must fit between the two buttons:
     the font gets a bit smaller on narrow phones (about 18 font sizes = width of the longest date) */
  .logs-screen .topbar h1 { font-size: min(1.1rem, calc((100vw - 104px) / 18)); }

  /* Icon buttons in the logs top bar (same size as the menu button) */
  .bar-button {
    flex: none;
    width: 44px; height: 44px;
    display: flex; align-items: center; justify-content: center;
    background: none;
    border: 0;
    border-radius: 12px;
  }
  .bar-button:active { background: var(--bg); }
  .bar-button::before {
    content: "";
    width: 24px; height: 24px;
    background: currentColor;
    -webkit-mask: var(--icon) center / contain no-repeat;
    mask: var(--icon) center / contain no-repeat;
  }
  .logs-back {
    --icon: url("data:image/svg+xml,\
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' \
stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'><path d='M15 5l-7 7 7 7'/></svg>");
  }
  .logs-reload {
    --icon: url("data:image/svg+xml,\
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' \
stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M20 12a8 8 0 1 1-2.34-5.66'/>\
<path d='M20 4v5h-5'/></svg>");
  }

  .logs-status {
    padding: 6px 6px 8px;
    font-size: .85rem;
    font-weight: 600;
    color: var(--text-muted);
  }
  .logs-status[hidden] { display: none; }

  /* The list scrolls by itself; the top bar stays in place */
  .logs-lines {
    height: calc(100% - var(--bar-h) - env(safe-area-inset-top, 0px));
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    padding: 6px 8px 12px;
    -webkit-user-select: text;     /* log lines can be selected and copied */
    user-select: text;
  }

  /* One row per log line: time | level | text */
  .log-line {
    display: grid;
    grid-template-columns: auto auto 1fr;
    align-items: baseline;
    column-gap: 8px;
    padding: 7px 6px;
    font-size: .8rem;
    line-height: 1.3;
    border-bottom: 1px solid var(--tile-border);
  }
  .log-line:has(> .log-text:only-child) { grid-template-columns: 1fr; }
  .log-time {
    font-variant-numeric: tabular-nums;
    color: var(--text-muted);
  }
  .log-level {
    min-width: 64px;               /* same width for all levels, so the texts line up */
    padding: 1px 5px;
    font-size: .66rem;
    font-weight: 700;
    text-align: center;
    border-radius: 6px;
    background: var(--tile-border);
    color: var(--text-muted);
  }
  .log-text { min-width: 0; overflow-wrap: anywhere; }

  /* Level colors (the table _LOG_LEVEL_COLORS in ui.js says which level uses which color).
     Neutral: normal text. Colored levels: badge and text in that color. */
  .level-neutral .log-level { background: var(--tile); color: var(--text); border: 1px solid var(--tile-border); }
  .level-muted   .log-text  { color: var(--text-muted); }
  .level-yellow { --log-color: var(--log-yellow); }
  .level-orange { --log-color: var(--log-orange); }
  .level-red    { --log-color: var(--log-red); }
  .level-green  { --log-color: var(--log-green); }
  :is(.level-yellow, .level-orange, .level-red, .level-green) .log-level { background: var(--log-color); color: var(--log-badge-text); }
  :is(.level-yellow, .level-orange, .level-red, .level-green) .log-text  { color: var(--log-color); font-weight: 600; }

  /* ---------- Logout in progress ---------- */
  .logout-screen { height: 100%; }

  .logout-card {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 20px;
    font-size: 1.05rem;
    font-weight: 600;
    color: var(--text-muted);
  }

  .spinner {
    width: 56px; height: 56px;
    border: 5px solid var(--tile-border);
    border-top-color: var(--on);
    border-radius: 50%;
    animation: spin .8s linear infinite;
  }
  @keyframes spin { to { transform: rotate(360deg); } }