/* ============ 3. Control panel ============ */
.panel {
  position: absolute; z-index: 2;
  left: 50%; bottom: calc(16px + var(--safe-bottom));
  width: min(560px, calc(100% - 24px - 2 * var(--safe-side)));
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px - var(--safe-top) - var(--safe-bottom));
  overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin;
  scrollbar-color: var(--line) transparent;
  display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; padding: 20px 24px;
  background: var(--panel);
  border: 1px solid var(--edge); border-radius: 24px;
  box-shadow: 0 18px 48px -12px rgba(0, 0, 0, .34);
  transform: translateX(-50%);
  -webkit-user-select: none; user-select: none;
}
/* The script loads after the page can first paint. Until it has applied the saved settings,
   the pale default panel must not show over a dark screen. */
body:not(.booted) .panel { visibility: hidden; }

.btn {
  min-height: 44px; padding: 0 14px; border-radius: 12px;
  background: var(--field); font-weight: 600; white-space: nowrap;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.btn:hover { background: var(--field-hover); }
.btn:active { transform: translateY(1px); }
.btn.solid, .btn[aria-pressed="true"] { background: var(--solid); color: var(--solid-ink); }
.btn.solid:hover, .btn[aria-pressed="true"]:hover { filter: brightness(1.18); }
.icon { flex: none; width: 18px; height: 18px; }

/* Visually hidden, still reachable by keyboard and screen readers */
.vh {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip-path: inset(50%); white-space: nowrap;
}

/* ---- Header: logo, help, full screen, hide ---- */
.panel > header { display: flex; align-items: center; gap: 10px; }
header .logo, .reopen svg { flex: none; width: 24px; height: 24px; }
.app-name { font-size: 17px; font-weight: 650; letter-spacing: -.01em; }
.shade { fill: var(--lamp); }
.header-actions { display: flex; gap: 8px; margin-left: auto; }
#helpBtn { width: 44px; padding: 0; }

/* ---- Help, messages and the wake lock warning ---- */
.help {
  display: grid; gap: 8px; padding: 12px 14px;
  border: 1px solid var(--edge); border-radius: 12px;
}
.help p { font-size: 13px; }
#note:empty { display: none; }
#note, #wakeText { font-size: 13px; }
#wakeText:not(.vh) { display: flex; gap: 8px; }
/* The warning in the panel and the floating notice share one dot */
#wakeText::before, .wake-alert::before {
  content: ""; flex: none; width: 8px; height: 8px; border-radius: 50%;
  background: var(--warn);
}
#wakeText::before { margin-top: 5px; }

/* ---- Brightness slider ---- */
.row-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; font-weight: 600; }
output, #timerText { font-variant-numeric: tabular-nums; }
input[type="range"] {
  -webkit-appearance: none; appearance: none;
  display: block; width: 100%; height: 44px; background: transparent; cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 10px; border-radius: 10px; box-shadow: inset 0 0 0 1px var(--track-edge);
  background: linear-gradient(to right, var(--ink) var(--pct, 100%), var(--track) var(--pct, 100%));
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 28px; height: 28px; margin-top: -9px;
  border-radius: 50%; background: var(--solid-ink); border: 3px solid var(--ink);
}
input[type="range"]::-moz-range-track {
  height: 10px; border-radius: 10px; background: var(--track); box-shadow: inset 0 0 0 1px var(--track-edge);
}
input[type="range"]::-moz-range-progress { height: 10px; border-radius: 10px; background: var(--ink); }
input[type="range"]::-moz-range-thumb {
  width: 22px; height: 22px; border-radius: 50%;
  background: var(--solid-ink); border: 3px solid var(--ink);
}

/* ---- Swatches: one row, with the selected name shown above ---- */
.color-name { color: var(--muted); font-weight: 500; text-align: right; }
.swatches {
  /* --per-row is set from the palette size when the swatches are built */
  display: grid; grid-template-columns: repeat(var(--per-row, 6), minmax(0, 1fr));
  gap: 10px 4px; margin-top: 12px;
}
.swatch { position: relative; justify-self: center; cursor: pointer; }
.dot {
  display: block; width: 44px; height: 44px; border-radius: 50%;
  background: var(--c);
  box-shadow: inset 0 0 0 1px var(--line);
  forced-color-adjust: none;
}
.swatch:hover .dot { transform: scale(1.08); }
.swatch input:checked ~ .dot { outline: 2.5px solid var(--ink); outline-offset: 2.5px; }
.swatch input:focus-visible ~ .dot { outline: 3px dashed var(--ink); outline-offset: 3px; }
/* Night mode: swatches become shades of red (--night), brighter for lighter colors.
   Controls need an outline here because their fill matches the panel. */
:root[data-night="true"] .dot { background: var(--night); }
:root[data-night="true"] .btn { box-shadow: inset 0 0 0 1px var(--line); }
:root[data-night="true"] .btn:hover { box-shadow: inset 0 0 0 2px var(--ink); }

/* ---- Options row: Night mode on the left, sleep timer on the right ---- */
.options {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  padding-top: 16px; border-top: 1px solid var(--edge);
}
.timer { display: flex; align-items: center; gap: 10px; margin-left: auto; font-weight: 600; }
.select { position: relative; }
.select select {
  -webkit-appearance: none; appearance: none;
  min-width: 118px; padding: 0 38px 0 14px; text-align: left;
  font-size: 16px;   /* iOS tends to zoom in on smaller form fields */
}
.select .icon { position: absolute; right: 12px; top: 13px; pointer-events: none; }
.select option { background: var(--solid-ink); color: var(--solid); }
.timer-status { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
#timerText { font-size: 13px; }
.clock { font-size: 20px; font-weight: 600; }
.nowrap { white-space: nowrap; }

/* Keyboard hints, only where there is likely to be a keyboard. Each one wraps as a whole. */
.keys { display: none; flex-wrap: wrap; gap: 6px 14px; }
@media (hover: hover) and (pointer: fine) { .keys { display: flex; } }
kbd {
  font-family: inherit; font-weight: 600; line-height: 1; padding: 1px 6px;
  border: 1px solid var(--line); border-radius: 5px; background: var(--field);
}

/* ---- The shortcut switch in Help. Drawn here, because a native checkbox would not follow Night mode ---- */
.switch {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  min-height: 44px; font-weight: 600; text-align: left;
}
.switch-track {
  position: relative; flex: none; width: 44px; height: 26px;
  border: 2px solid var(--line); border-radius: 13px; background: var(--field);
}
.switch-track::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 16px; height: 16px; border-radius: 50%; background: var(--line);
}
.switch[aria-checked="true"] .switch-track { border-color: var(--ink); background: var(--ink); }
.switch[aria-checked="true"] .switch-track::after { left: 21px; background: var(--panel); }

/* ============ 4. Floating buttons over the light ============ */
.reopen, .wake-alert {
  position: absolute; z-index: 2;
  background: var(--panel); border: 1px solid var(--line);
}
/* Corner button: shown while the panel is hidden */
.reopen {
  border-color: var(--edge);
  right: calc(16px + env(safe-area-inset-right, 0px));
  bottom: calc(16px + var(--safe-bottom));
  width: 48px; height: 48px; border-radius: 50%;
  display: grid; place-items: center;
  visibility: hidden; opacity: 0;
}

/* ============ 5. Hidden controls ============
   There is one hidden state. The panel can't be seen or clicked, but it stays reachable by
   keyboard and screen readers, and moving focus into it shows it again. */
body.panel-hidden .panel { opacity: 0; pointer-events: none; }
/* Hiding it yourself also slides it down a little. Auto-hide only fades. */
@media (prefers-reduced-motion: no-preference) {
  body.panel-hidden:not(.hide-slowly) .panel { transform: translate(-50%, 12px); }
}
body.panel-hidden .reopen { visibility: visible; opacity: .72; }
body.panel-hidden .reopen:hover,
body.panel-hidden .reopen:focus-visible { opacity: 1; }
/* Left alone while hidden, the corner button and the cursor go too */
body.quiet { cursor: none; }
body.quiet .reopen:not(:focus-visible) { opacity: 0; pointer-events: none; }

/* Wake lock notice: shown when keeping the screen awake failed and the panel is out of sight */
.wake-alert {
  top: calc(16px + var(--safe-top)); left: 50%;
  transform: translateX(-50%);
  min-height: 44px; padding: 0 16px; border-radius: 22px;
  display: none; align-items: center; gap: 8px;
  font-size: 13px; white-space: nowrap;
}
body.wake-failed.panel-hidden .wake-alert { display: inline-flex; }

/* ============ 6. Motion (enabled after first paint so the controls don't animate on load) ============
   The light has no CSS transitions. The script animates it: see "Light transitions". */
/* Theme colors never fade, so they all change at once and text stays readable */
.ready .panel { transition: opacity .22s ease, transform .22s cubic-bezier(.22, 1, .36, 1); }
.ready.panel-hidden .panel { transition: opacity .16s ease, transform .16s ease; }
.ready .reopen { transition: opacity .22s ease, visibility 0s; }
.ready:not(.panel-hidden) .reopen { transition: opacity .16s ease, visibility 0s .16s; }
/* What goes by itself fades slowly, so it doesn't catch the eye */
.ready.panel-hidden.hide-slowly .panel, .ready.quiet .reopen { transition: opacity .8s ease; }
.ready .dot { transition: transform .15s ease; }
