/* Doorbell — shared styles.
 *
 * Layouts are intrinsic (flex-wrap, auto-fit grids, min()) rather than driven by
 * media queries, so they stay correct when common.js applies CSS zoom to undo a
 * phone's "desktop site" mode. */

:root {
  --bg: #fbf6ee;
  --bg-2: #f4ebdc;
  --surface: #ffffff;
  --surface-2: #fdf9f2;
  --ink: #1f2a33;
  --muted: #5b6871;
  --border: #e6dccb;
  --brand: #1f3b4d;
  --brand-ink: #ffffff;
  --accent: #c9822a;
  --accent-soft: #fbe7c8;
  --accent-ink: #6f4511;
  --danger: #b3261e;
  --danger-soft: #fbe3e1;
  --ok: #2b7a4b;
  --ok-soft: #dff2e6;
  --focus: #2f7fc1;
  --shadow: 0 1px 2px rgb(31 42 51 / 6%), 0 6px 24px rgb(31 42 51 / 8%);
  --radius: 18px;
  --radius-sm: 12px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11171c;
    --bg-2: #161e24;
    --surface: #1b242b;
    --surface-2: #202a32;
    --ink: #edf1f3;
    --muted: #9eabb4;
    --border: #2d3a44;
    --brand: #a9d0e6;
    --brand-ink: #10202b;
    --accent: #e6a650;
    --accent-soft: #3a2c19;
    --accent-ink: #f6d39f;
    --danger: #ff8a80;
    --danger-soft: #3b1f1d;
    --ok: #7fd6a0;
    --ok-soft: #173326;
    --focus: #7cc0f5;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 6px 24px rgb(0 0 0 / 30%);
  }
}

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

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  background: var(--bg);
}

body {
  margin: 0;
  /* --zoom is set by common.js when it undoes "desktop site" mode; viewport units
     are scaled by CSS zoom, so divide it out again. */
  min-height: calc(100vh / var(--zoom, 1));
  min-height: calc(100dvh / var(--zoom, 1));
  display: flex;
  flex-direction: column;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 1rem;
  line-height: 1.5;
  color: var(--ink);
  background:
    radial-gradient(60rem 30rem at 100% -10%, var(--accent-soft), transparent 70%),
    var(--bg);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

h1, h2, h3 { line-height: 1.2; margin: 0 0 .5em; text-wrap: balance; }
h1 { font-size: 2rem; letter-spacing: -.01em; }
h2 { font-size: 1.3rem; }
h3 { font-size: 1.05rem; }
p { margin: 0 0 1em; }
a { color: var(--brand); }
@media (prefers-color-scheme: dark) { a { color: var(--brand); } }

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

.wrap {
  width: 100%;
  max-width: 46rem;
  margin: 0 auto;
  padding: 0 1rem;
}
.wrap-wide { max-width: 72rem; }

main { flex: 1 0 auto; padding-bottom: 2rem; }

/* ------------------------------------------------------------ header & footer */

.topbar {
  display: flex;
  align-items: center;
  gap: .6rem;
  padding: .9rem 0;
  flex-wrap: wrap;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: .55rem;
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--ink);
  text-decoration: none;
}
.brand img { width: 2rem; height: 2rem; border-radius: 9px; }
.topbar .spacer { flex: 1; }

.footer {
  border-top: 1px solid var(--border);
  padding: 1.1rem 0 1.4rem;
  color: var(--muted);
  font-size: .92rem;
  text-align: center;
}
.footer strong { color: var(--ink); font-variant-numeric: tabular-nums; }
.footer .dot { margin: 0 .5rem; opacity: .6; }

/* ------------------------------------------------------------ surfaces */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: clamp(1rem, 4%, 1.6rem);
}
.card + .card { margin-top: 1rem; }
.stack > * + * { margin-top: 1rem; }

.muted { color: var(--muted); }
.small { font-size: .9rem; }
.center { text-align: center; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: .3rem;
  padding: .1rem .55rem;
  border-radius: 999px;
  font-size: .8rem;
  font-weight: 600;
  background: var(--bg-2);
  color: var(--muted);
  white-space: nowrap;
}
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.accent { background: var(--accent-soft); color: var(--accent-ink); }

.conn {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  font-size: .85rem;
  color: var(--muted);
}
.conn::before {
  content: "";
  width: .55rem;
  height: .55rem;
  border-radius: 50%;
  background: var(--muted);
}
.conn.online::before { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-soft); }
.conn.offline::before { background: var(--danger); }

/* ------------------------------------------------------------ buttons & forms */

.btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .45rem;
  min-height: 2.75rem;
  padding: .55rem 1.05rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  line-height: 1.2;
  text-decoration: none;
  cursor: pointer;
  transition: background-color .15s, border-color .15s, transform .1s;
  touch-action: manipulation;
}
.btn:hover { border-color: var(--muted); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn svg { width: 1.15em; height: 1.15em; flex: none; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn-primary:hover { border-color: var(--brand); filter: brightness(1.08); }
.btn-accent { background: var(--accent); border-color: var(--accent); color: #2a1a05; }
.btn-danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); }
.btn-danger:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn-danger-solid { background: var(--danger); border-color: var(--danger); color: #fff; }
@media (prefers-color-scheme: dark) { .btn-danger-solid { color: #2b0c0a; } }
.btn-small { min-height: 2.25rem; padding: .35rem .8rem; font-size: .9rem; }
.btn.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-ink); }

.btn-row { display: flex; flex-wrap: wrap; gap: .5rem; }

label { font-weight: 600; display: block; margin-bottom: .35rem; }
input[type="text"],
input[type="password"],
input[type="url"] {
  width: 100%;
  min-height: 2.9rem;
  padding: .6rem .9rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--ink);
  font: inherit;
  font-size: max(1rem, 16px); /* ≥16px: keeps iOS from zooming into the field */
}
input:focus { border-color: var(--focus); outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 30%, transparent); }

.field-row { display: flex; flex-wrap: wrap; gap: .6rem; }
.field-row > input { flex: 1 1 14rem; }
.field-row > .btn { flex: 0 0 auto; }

.error { color: var(--danger); font-weight: 600; }

/* ------------------------------------------------------------ welcome */

.hero { padding: clamp(1rem, 5%, 2.5rem) 0 1.2rem; }
.hero .lead { font-size: 1.12rem; color: var(--muted); max-width: 36rem; }

.steps {
  list-style: none;
  padding: 0;
  margin: 1.5rem 0 0;
  display: grid;
  gap: .8rem;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 13rem), 1fr));
  counter-reset: step;
}
.steps li {
  counter-increment: step;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
}
.steps li::before {
  content: counter(step);
  display: inline-grid;
  place-items: center;
  width: 1.9rem;
  height: 1.9rem;
  margin-bottom: .5rem;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent-ink);
  font-weight: 700;
}
.steps h3 { margin-bottom: .25rem; }
.steps p { margin: 0; color: var(--muted); font-size: .95rem; }

.links-grid {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 17rem), 1fr));
}
.link-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: .7rem;
  min-width: 0;
}
.link-card h3 { display: flex; align-items: center; gap: .5rem; margin: 0; }
.link-card h3 svg { width: 1.4rem; height: 1.4rem; color: var(--accent); }
.link-card .qr {
  display: block;
  width: min(100%, 13rem);
  aspect-ratio: 1;
  margin: 0 auto;
  border-radius: var(--radius-sm);
  background: #fff;
  padding: .3rem;
  border: 1px solid var(--border);
  image-rendering: pixelated;
}
.link-card .url {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .85rem;
  min-height: 2.5rem;
  background: var(--surface);
}
.link-card .btn-row .btn { flex: 1 1 auto; }

.device-list { list-style: none; padding: 0; margin: 0; display: grid; gap: .5rem; }
.device-list li {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .5rem .8rem;
  padding: .7rem .9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}
.device-list .name { font-weight: 600; flex: 1 1 10rem; min-width: 0; overflow-wrap: anywhere; }

/* ------------------------------------------------------------ doorbell & ringer pages */

.stage {
  flex: 1 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 1rem 1rem 2rem;
  text-align: center;
}
.stage h1 { margin: 0; overflow-wrap: anywhere; }
.stage .kicker {
  text-transform: uppercase;
  letter-spacing: .12em;
  font-size: .78rem;
  font-weight: 700;
  color: var(--accent-ink);
}
@media (prefers-color-scheme: dark) { .stage .kicker { color: var(--accent); } }

.bell-button {
  position: relative;
  appearance: none;
  border: 0;
  padding: 0;
  background: none;
  cursor: pointer;
  width: min(72%, 17rem, calc(45dvh / var(--zoom, 1)));
  aspect-ratio: 200 / 280;
  border-radius: 22%/16%;
  transition: filter .35s, transform .12s;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  filter: drop-shadow(0 14px 22px rgb(80 50 10 / 28%));
}
.bell-button svg { width: 100%; height: 100%; display: block; }
.bell-button .push { transform-origin: 100px 140px; transition: transform .12s; }
.bell-button:not(:disabled):active .push,
.bell-button.pressed .push { transform: scale(.93); }
.bell-button:not(:disabled):hover .glow { opacity: 1; }
.bell-button .glow { opacity: .55; transition: opacity .2s; }
.bell-button:focus-visible { outline-offset: 6px; }
.bell-button:disabled { cursor: default; }
/* The countdown takes the place of the bell glyph. */
.bell-button.cooling .glyph { opacity: 0; }
.bell-button .glyph { transition: opacity .2s; }
.bell-button > svg { transition: filter .35s; }
.bell-button.cooling > svg { filter: brightness(.45) saturate(.6); }
.bell-button.inactive > svg { filter: grayscale(1) brightness(.75) opacity(.6); }

.countdown {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
}
.countdown svg { width: 62%; height: auto; overflow: visible; }
.countdown .track { fill: none; stroke: rgb(255 255 255 / 25%); stroke-width: 7; }
.countdown .progress {
  fill: none;
  stroke: #fff;
  stroke-width: 7;
  stroke-linecap: round;
  transform: rotate(-90deg);
  transform-origin: 50% 50%;
  transition: stroke-dashoffset 1s linear;
}
.countdown .num {
  fill: #fff;
  font-size: 30px;
  font-weight: 800;
  font-family: system-ui, sans-serif;
  font-variant-numeric: tabular-nums;
}
.countdown .unit { fill: rgb(255 255 255 / 80%); font-size: 10px; font-weight: 600; letter-spacing: .1em; }

.status-line {
  min-height: 3.2rem;
  font-size: 1.1rem;
  font-weight: 600;
  max-width: 26rem;
}
.status-line .sub { display: block; font-weight: 400; font-size: .95rem; color: var(--muted); }

.notice {
  width: 100%;
  max-width: 30rem;
  padding: 1rem 1.2rem;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  text-align: center;
}
.notice.warn { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-ink); }
.notice.danger { background: var(--danger-soft); border-color: var(--danger); color: var(--danger); }

/* Ringer */
.bell-wrap {
  position: relative;
  width: min(62%, 15rem, calc(45dvh / var(--zoom, 1)));
  aspect-ratio: 1;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: radial-gradient(circle at 50% 40%, var(--surface), var(--bg-2));
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.bell-wrap svg { width: 78%; height: 78%; overflow: visible; }
.bell-wrap .bell { transform-origin: 100px 30px; }
.bell-wrap .clapper { transform-origin: 100px 60px; }
.bell-wrap .waves { opacity: 0; }
.bell-wrap .muted-mark { display: none; }
.is-muted .bell-wrap .muted-mark { display: inline; }

.ringing .bell-wrap { animation: halo 1.1s ease-out infinite; }
.ringing .bell-wrap .bell { animation: swing .9s ease-in-out infinite; }
.ringing .bell-wrap .clapper { animation: swing-clapper .9s ease-in-out infinite; }
.ringing .bell-wrap .waves { animation: waves .9s ease-out infinite; }
.ringing body, body.ringing { animation: flash 1.1s ease-in-out infinite; }

@keyframes swing {
  0%, 100% { transform: rotate(0); }
  20% { transform: rotate(16deg); }
  50% { transform: rotate(-14deg); }
  80% { transform: rotate(8deg); }
}
@keyframes swing-clapper {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-9px); }
  50% { transform: translateX(9px); }
  80% { transform: translateX(-4px); }
}
@keyframes waves {
  0% { opacity: 0; transform: scale(.9); }
  30% { opacity: 1; }
  100% { opacity: 0; transform: scale(1.15); }
}
@keyframes halo {
  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--accent) 60%, transparent), var(--shadow); }
  100% { box-shadow: 0 0 0 2.2rem transparent, var(--shadow); }
}
@keyframes flash {
  0%, 100% { background-color: var(--bg); }
  50% { background-color: var(--accent-soft); }
}
@media (prefers-reduced-motion: reduce) {
  .ringing .bell-wrap .bell,
  .ringing .bell-wrap .clapper { animation: none; }
  .ringing .bell-wrap .waves { animation: none; opacity: 1; }
}

.ring-alert {
  font-size: 1.6rem;
  font-weight: 800;
  color: var(--accent-ink);
}
@media (prefers-color-scheme: dark) { .ring-alert { color: var(--accent); } }

.facts { display: flex; flex-wrap: wrap; gap: .4rem; justify-content: center; }

.controls {
  width: 100%;
  max-width: 30rem;
  display: grid;
  gap: .55rem;
  /* Always two per row: mute + visual alert, then links + settings. */
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
.controls .btn { width: 100%; }

.sound-banner {
  width: 100%;
  max-width: 30rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: .6rem 1rem;
  padding: .8rem 1rem;
  border-radius: var(--radius);
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

/* Ringer settings dialog: one section per thing the settings touch. */
.settings section + section { margin-top: 1.1rem; padding-top: 1.1rem; border-top: 1px solid var(--border); }
.settings h3 {
  margin-bottom: .6rem;
  font-size: .8rem;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: var(--muted);
}
.settings p { margin: -.2rem 0 .6rem; }
.settings .field-row { margin-bottom: .6rem; }
.settings .btn-row .btn { flex: 1 1 auto; }

select {
  flex: 1 1 12rem;
  min-height: 2.9rem;
  padding: .6rem .9rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--ink);
  font: inherit;
  font-size: max(1rem, 16px);
}
select:focus { border-color: var(--focus); outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 30%, transparent); }

/* ------------------------------------------------------------ dialogs */

dialog {
  width: min(100% - 2rem, 38rem);
  max-height: min(100% - 2rem, 44rem);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 20px 60px rgb(0 0 0 / 35%);
}
dialog::backdrop { background: rgb(15 22 28 / 55%); }
dialog[open] { display: flex; flex-direction: column; }
.dialog-head {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .9rem 1rem .9rem 1.2rem;
  border-bottom: 1px solid var(--border);
}
.dialog-head h2 { margin: 0; flex: 1; font-size: 1.15rem; overflow-wrap: anywhere; }
.dialog-body { padding: 1rem 1.2rem 1.2rem; overflow: auto; }
.dialog-foot { display: flex; flex-wrap: wrap; gap: .5rem; justify-content: flex-end; padding: 0 1.2rem 1.2rem; }
.icon-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 50%;
  display: grid;
  place-items: center;
  cursor: pointer;
  font-size: 1.4rem;
  line-height: 1;
}
.icon-btn:hover { background: var(--bg-2); color: var(--ink); }

.log { list-style: none; margin: 0; padding: 0; }
.log li {
  display: flex;
  flex-wrap: wrap;
  gap: .1rem .8rem;
  padding: .55rem 0;
  border-bottom: 1px solid var(--border);
}
.log li:last-child { border-bottom: 0; }
.log time {
  flex: 0 0 auto;
  min-width: 9.5rem;
  color: var(--muted);
  font-size: .88rem;
  font-variant-numeric: tabular-nums;
}
.log .msg { flex: 1 1 14rem; min-width: 0; overflow-wrap: anywhere; }
.log li.ring .msg { font-weight: 700; color: var(--accent-ink); }
@media (prefers-color-scheme: dark) { .log li.ring .msg { color: var(--accent); } }
.log li.end .msg,
.log li.remove .msg { color: var(--danger); }

/* ------------------------------------------------------------ admin */

.stat-row { display: flex; flex-wrap: wrap; gap: .6rem; }
.stat {
  flex: 1 1 8rem;
  padding: .8rem 1rem;
  border-radius: var(--radius-sm);
  background: var(--surface);
  border: 1px solid var(--border);
}
.stat b { display: block; font-size: 1.6rem; font-variant-numeric: tabular-nums; }
.stat span { color: var(--muted); font-size: .88rem; }

.section-head { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; margin: 1.6rem 0 .8rem; }
.section-head h2 { margin: 0; flex: 1 1 auto; }

.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 999px; padding: 3px; background: var(--surface); }
.segmented button {
  appearance: none;
  border: 0;
  background: none;
  color: var(--muted);
  font: inherit;
  font-weight: 600;
  font-size: .9rem;
  padding: .35rem .8rem;
  min-height: 2.1rem;
  border-radius: 999px;
  cursor: pointer;
}
.segmented button[aria-pressed="true"] { background: var(--brand); color: var(--brand-ink); }

.items { display: grid; gap: .7rem; }
.bulk { margin-bottom: .8rem; }
.feature { grid-template-columns: minmax(0, 1fr) auto; align-items: center; }
.feature p { margin: .15rem 0 0; color: var(--muted); font-size: .9rem; }
.item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .9rem 1rem;
  display: grid;
  gap: .6rem;
  min-width: 0;
}
.item-head { display: flex; flex-wrap: wrap; align-items: center; gap: .4rem .6rem; }
.item-head h3 { margin: 0; flex: 1 1 10rem; min-width: 0; overflow-wrap: anywhere; }
.item .meta { display: flex; flex-wrap: wrap; gap: .2rem 1rem; color: var(--muted); font-size: .88rem; }
.item.terminated { background: var(--surface-2); }
.item .btn-row .btn { flex: 0 1 auto; }
.empty { padding: 1.4rem; text-align: center; color: var(--muted); border: 1px dashed var(--border); border-radius: var(--radius); }

.login { max-width: 24rem; margin: 3rem auto 0; }

/* ------------------------------------------------------------ toast */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(1.2rem + env(safe-area-inset-bottom));
  transform: translate(-50%, 1rem);
  max-width: calc(100% - 2rem);
  padding: .65rem 1.1rem;
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  font-weight: 600;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 10;
  text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ------------------------------------------------------------ print (QR codes) */

.print-sheet { display: none; }

@media print {
  @page { margin: 14mm; }
  body { background: #fff !important; color: #000; display: block; min-height: 0; padding: 0; }
  body > *:not(.print-sheet) { display: none !important; }
  .print-sheet { display: block; }
  .print-card {
    break-inside: avoid;
    page-break-inside: avoid;
    text-align: center;
    border: 2px solid #000;
    border-radius: 12px;
    padding: 10mm;
    margin: 0 auto 10mm;
    max-width: 120mm;
  }
  .print-card img { width: 80mm; height: 80mm; }
  .print-card h2 { font-size: 20pt; margin: 0 0 4mm; }
  .print-card p { font-size: 12pt; margin: 3mm 0 0; }
  .print-card .url { font-family: monospace; font-size: 7.5pt; word-break: break-all; color: #333; }
}
