/* ESP32 IR Blaster - web UI styles.
   No frameworks, no web fonts, no external requests: everything is served
   from the device so the UI works on an isolated network or through the
   captive portal with no internet at all. */

:root {
  --bg:      #12151a;
  --panel:   #191d25;
  --panel2:  #1f242e;
  --line:    #2b3240;
  --text:    #e6e9ef;
  --muted:   #939cad;
  --accent:  #3b82f6;
  --accent2: #1d4ed8;
  --ok:      #22c55e;
  --warn:    #f59e0b;
  --danger:  #ef4444;
  --radius:  12px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg:     #f4f6fa;
    --panel:  #ffffff;
    --panel2: #eef1f6;
    --line:   #d9dfe9;
    --text:   #131720;
    --muted:  #5b6577;
  }
}

* { box-sizing: border-box; }

/* Must outrank any rule that sets `display` on an element we toggle with the
   `hidden` attribute -- the modal overlay is display:flex, which would
   otherwise beat the user-agent's [hidden] rule and leave the scrim covering
   the whole page. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
        Helvetica, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

h2 { font-size: 16px; margin: 0 0 10px; }
hr { border: 0; border-top: 1px solid var(--line); margin: 18px 0; }
code { background: var(--panel2); padding: 1px 5px; border-radius: 4px; font-size: 92%; }
.muted { color: var(--muted); font-weight: 400; }
.note  { color: var(--muted); font-size: 13px; margin: 10px 0 0; }
.empty { color: var(--muted); text-align: center; padding: 40px 16px; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; flex-wrap: wrap;
  padding: 12px 16px calc(12px + env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--line);
  /* Scrolls away: only the tab bar pins, so nothing depends on guessing this
     bar's height, which changes as the status pills wrap. */
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 8px; font-size: 16px; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted);
       transition: background .3s; flex: none; }
.dot.live { background: var(--ok); }
.dot.dead { background: var(--danger); }

.pills { display: flex; gap: 6px; flex-wrap: wrap; }
.pill {
  font-size: 12px; padding: 3px 9px; border-radius: 999px;
  background: var(--panel2); border: 1px solid var(--line); color: var(--muted);
  white-space: nowrap;
}
.pill.ok   { color: var(--ok);     border-color: color-mix(in srgb, var(--ok) 40%, var(--line)); }
.pill.warn { color: var(--warn);   border-color: color-mix(in srgb, var(--warn) 40%, var(--line)); }
.pill.bad  { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); }

/* ---------- tabs ---------- */
.tabs {
  display: flex; gap: 2px; padding: 0 8px; overflow-x: auto;
  background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 19; scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  appearance: none; background: none; border: 0; color: var(--muted);
  padding: 12px 14px; font-size: 14px; cursor: pointer; white-space: nowrap;
  border-bottom: 2px solid transparent;
}
.tab.active { color: var(--text); border-bottom-color: var(--accent); }

main { padding: 16px; max-width: 980px; margin: 0 auto;
       padding-bottom: calc(32px + env(safe-area-inset-bottom)); }
.panel { display: none; }
.panel.active { display: block; }

/* ---------- cards ---------- */
.card {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 16px; margin-bottom: 16px;
}
.card.danger { border-color: color-mix(in srgb, var(--danger) 35%, var(--line)); }

/* ---------- forms ---------- */
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
input, select, textarea {
  width: 100%; margin-top: 5px; padding: 9px 11px; font-size: 15px;
  color: var(--text); background: var(--panel2);
  border: 1px solid var(--line); border-radius: 9px; font-family: inherit;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input[type=file] { padding: 7px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 0 14px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-top: 4px; }

.switch { display: flex; align-items: center; gap: 10px; cursor: pointer;
          color: var(--text); font-size: 14px; }
.switch input { display: none; }
.switch span {
  width: 40px; height: 23px; border-radius: 999px; background: var(--line);
  position: relative; transition: background .18s; flex: none; margin: 0;
}
.switch span::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 17px; height: 17px; border-radius: 50%; background: #fff;
  transition: transform .18s;
}
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(17px); }

/* ---------- buttons ---------- */
.btn {
  appearance: none; border: 1px solid var(--line); background: var(--panel2);
  color: var(--text); padding: 9px 15px; border-radius: 9px; font-size: 14px;
  cursor: pointer; font-family: inherit; transition: filter .15s;
}
.btn:hover:not(:disabled) { filter: brightness(1.18); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent2); color: #fff; }
.btn.danger  { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.danger-ghost { color: var(--danger); }
.btn.file { position: relative; overflow: hidden; display: inline-block; }
.btn.file input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; }

/* ---------- remotes ---------- */
.toolbar { margin-bottom: 14px; }
.toolbar input[type=search] { margin: 0 0 10px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  font-size: 13px; padding: 5px 12px; border-radius: 999px; cursor: pointer;
  background: var(--panel); border: 1px solid var(--line); color: var(--muted);
}
.chip.active { background: var(--accent); border-color: var(--accent2); color: #fff; }

.group-title { font-size: 13px; color: var(--muted); margin: 18px 0 8px;
               text-transform: uppercase; letter-spacing: .05em; }
.cmd-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 10px; }
.cmd {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px; display: flex; gap: 8px;
  align-items: center; justify-content: space-between;
}
.cmd-main { flex: 1; min-width: 0; text-align: left; background: none;
            border: 0; color: var(--text); cursor: pointer; padding: 2px;
            font-family: inherit; font-size: 15px; }
.cmd-main:hover { color: var(--accent); }
.cmd-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cmd-meta { font-size: 12px; color: var(--muted); margin-top: 2px; }
.cmd.sending { outline: 2px solid var(--ok); }
.icon-btn {
  background: none; border: 0; color: var(--muted); cursor: pointer;
  font-size: 18px; padding: 4px 8px; border-radius: 7px; line-height: 1;
}
.icon-btn:hover { background: var(--panel2); color: var(--text); }

/* ---------- learn ---------- */
.learn-state { background: var(--panel2); border-radius: var(--radius);
               padding: 18px; margin: 14px 0; text-align: center; }
.learn-big { font-size: 21px; font-weight: 600; margin-bottom: 4px; }
.learn-big.waiting { color: var(--warn); }
.learn-big.ok      { color: var(--ok); }
.learn-big.bad     { color: var(--danger); }

.bar { height: 6px; background: var(--line); border-radius: 999px;
       overflow: hidden; margin-top: 12px; }
.bar > div { height: 100%; width: 0; background: var(--accent);
             transition: width .25s linear; }

/* ---------- schedules ---------- */
.dows { display: flex; gap: 6px; flex-wrap: wrap; margin: 4px 0 14px; }
.dow {
  width: 42px; text-align: center; font-size: 13px; padding: 6px 0;
  border-radius: 8px; cursor: pointer; user-select: none;
  background: var(--panel2); border: 1px solid var(--line); color: var(--muted);
}
.dow.on { background: var(--accent); border-color: var(--accent2); color: #fff; }

.sch {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px 14px; margin-bottom: 10px;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
}
.sch .time { font-size: 20px; font-variant-numeric: tabular-nums; }
.sch .grow { flex: 1; min-width: 140px; }
.sch.off { opacity: .55; }

/* ---------- system ---------- */
.kv { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 8px 20px; }
.kv div { display: flex; justify-content: space-between; gap: 10px;
          border-bottom: 1px dashed var(--line); padding: 5px 0; font-size: 14px; }
.kv span:first-child { color: var(--muted); }
.kv span:last-child { text-align: right; word-break: break-word; }

.mono {
  font: 12px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  background: var(--panel2); border: 1px solid var(--line); border-radius: 9px;
  padding: 10px; margin: 10px 0 0; white-space: pre-wrap; word-break: break-word;
  max-height: 320px; overflow: auto;
}
.log { max-height: 380px; }

.sticky-save {
  position: sticky; bottom: 0; display: flex; gap: 12px; align-items: center;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px 16px;
  margin-bottom: calc(8px + env(safe-area-inset-bottom));
  box-shadow: 0 -6px 20px rgba(0, 0, 0, .18);
}

/* ---------- modal & toasts ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .55);
  display: flex; align-items: center; justify-content: center;
  padding: 16px; z-index: 50;
}
.modal {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 18px; width: 100%; max-width: 420px;
  max-height: 90vh; overflow: auto;
}
.modal h3 { margin: 0 0 14px; font-size: 16px; }

.toasts { position: fixed; left: 50%; transform: translateX(-50%);
          bottom: calc(18px + env(safe-area-inset-bottom));
          display: flex; flex-direction: column; gap: 8px; z-index: 60;
          width: min(420px, calc(100vw - 32px)); }
.toast {
  background: var(--panel2); border: 1px solid var(--line); color: var(--text);
  border-radius: 10px; padding: 11px 14px; font-size: 14px;
  box-shadow: 0 8px 26px rgba(0, 0, 0, .35);
  animation: rise .18s ease-out;
}
.toast.ok  { border-color: var(--ok); }
.toast.bad { border-color: var(--danger); }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } }

@media (max-width: 520px) {
  .topbar { padding-bottom: 10px; }
  .cmd-grid { grid-template-columns: 1fr; }
}

/* Collapsible "advanced" block inside a card. */
details.advanced { margin: 12px 0 0; }
details.advanced > summary {
  cursor: pointer; color: var(--accent); font-size: 13px; padding: 4px 0;
  user-select: none;
}

/* A password field plus its "clear this secret" checkbox, behaving as one
   grid cell so the checkbox sits under its own input rather than becoming a
   sibling cell somewhere else in the row. */
.field { margin-bottom: 12px; }
.field > label:first-child {
  display: block; font-size: 13px; color: var(--muted); margin: 0;
}
.clearbox {
  display: flex; align-items: center; gap: 7px; margin: 7px 0 0;
  font-size: 12px; color: var(--muted); cursor: pointer;
}
.clearbox input[type=checkbox] { width: auto; margin: 0; flex: none; }

/* WiFi scan results: an explicit tap-to-pick list. A <datalist> cannot do this
   job, because it filters against whatever is already in the input. */
.scan-results {
  display: grid; gap: 4px; margin-top: 10px;
  max-height: 260px; overflow-y: auto;
}
.scan-row {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 8px 11px; border-radius: 9px; cursor: pointer;
  background: var(--panel2); border: 1px solid var(--line);
  color: var(--text); font: inherit; font-size: 14px; text-align: left;
}
.scan-row:hover { filter: brightness(1.15); }
.scan-row.sel { border-color: var(--accent); background: var(--panel); }
.scan-bars { font-size: 13px; color: var(--accent); letter-spacing: -1px; flex: none; }
.scan-name { flex: 1; min-width: 0; overflow: hidden;
             text-overflow: ellipsis; white-space: nowrap; }
.scan-meta { font-size: 12px; color: var(--muted); flex: none; }

/* Temperature slider in the built-in library. */
input[type=range] { accent-color: var(--accent); padding: 0; }

/* ---------- library sub-tabs ---------- */

.subtabs {
  display: flex; gap: 6px; margin-bottom: 16px;
  background: var(--panel2); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 4px;
}
.subtab {
  appearance: none; flex: 1; background: none; border: 0; cursor: pointer;
  color: var(--muted); font-size: 14px; font-weight: 500;
  padding: 9px 12px; border-radius: calc(var(--radius) - 5px);
  transition: background .15s, color .15s;
}
.subtab:hover { color: var(--text); }
.subtab.active { background: var(--panel); color: var(--text); }

.lib-pane { display: none; }
.lib-pane.active { display: block; }

/* ---------- TV remote ---------- */

.tv-remote { margin-top: 16px; }

/* The body is the remote itself: a rounded slab with the buttons laid out
   the way a hand expects to find them. */
.tv-remote-body {
  max-width: 260px; margin: 0 auto; padding: 22px 20px 26px;
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 34px;
  display: flex; flex-direction: column; align-items: center; gap: 20px;
}

.tvb {
  appearance: none; cursor: pointer; position: relative;
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 50%;
  width: 52px; height: 52px;
  display: grid; place-items: center;
  font-size: 19px; line-height: 1;
  transition: transform .09s, background .15s, border-color .15s, box-shadow .15s;
}
.tvb:hover:not(:disabled) { border-color: var(--accent); }

/* Pressing should feel like pressing: the button sinks, and a ring pulses
   outward so a press is visible even when the TV is out of shot. */
.tvb:active:not(:disabled) { transform: scale(.92); }
.tvb.sending { border-color: var(--accent); background: var(--accent); color: #fff; }
.tvb.sending::after {
  content: ''; position: absolute; inset: -4px; border-radius: 50%;
  border: 2px solid var(--accent); animation: tvPulse .5s ease-out;
}
@keyframes tvPulse {
  from { opacity: .8; transform: scale(1); }
  to   { opacity: 0;  transform: scale(1.7); }
}
.tvb.failed { border-color: var(--danger); color: var(--danger); }

/* A button with no captured code is shown, not hidden -- a missing button
   should look missing rather than silently absent. */
.tvb:disabled {
  cursor: not-allowed; opacity: .38;
  border-style: dashed; background: transparent;
}

.tvb-power { width: 58px; height: 58px; font-size: 22px; }
.tvb-power:not(:disabled) { color: var(--danger); }
.tvb-power:not(:disabled):hover { background: var(--danger); color: #fff;
                                  border-color: var(--danger); }

.tv-rockers { display: flex; gap: 26px; justify-content: center; }
.tv-rocker {
  display: flex; flex-direction: column; align-items: center; gap: 7px;
  padding: 9px 7px; border-radius: 30px;
  background: var(--bg); border: 1px solid var(--line);
}
.tv-rocker-label {
  font-size: 10px; letter-spacing: 1.4px; color: var(--muted); font-weight: 600;
}

.tv-extras { display: flex; flex-wrap: wrap; gap: 9px; justify-content: center; }
/* Extras carry words rather than glyphs, so they are pills, not circles. */
.tv-extras .tvb {
  width: auto; height: auto; border-radius: 999px;
  padding: 8px 15px; font-size: 12px; font-weight: 600;
}

.tv-remote-caption { text-align: center; margin-top: 12px; font-size: 13px; }

@media (prefers-reduced-motion: reduce) {
  .tvb, .tvb.sending::after { transition: none; animation: none; }
}

/* ---------- schedule fire log ---------- */

.fire {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 0; border-bottom: 1px solid var(--line);
}
.fire:last-child { border-bottom: 0; }
.fire .grow { flex: 1; min-width: 0; }
.fire .pill { flex: 0 0 auto; min-width: 74px; text-align: center; }
