/* ============================================================================
   Q0RO IoT — page-level components.
   Everything here consumes the Q0RO tokens (tokens.css); no new palette.
   ========================================================================== */

/* The off-canvas drawer is fixed; clip the horizontal overflow it can create
   on narrow pages. */
html, body { overflow-x: clip; }

/* Overview scrolls in this module instead of filling the viewport. */
body[data-v="overview"] .col { height: auto; min-height: 100vh; }
body[data-v="overview"] .page {
  display: grid;
  align-content: start;
  min-height: 0;
  padding-bottom: 40px;
}

.page-view { display: none; }
.page-view.on { display: grid; gap: var(--space-4); }
.page-view .head { margin-bottom: 2px; }

/* ─────────────────────────────────────────────────────────────── headers */

.head-sub { display: block; margin-top: 4px; font-size: 12px; color: var(--text-muted); max-width: 72ch; }
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ─────────────────────────────────────────────────────────────── device cards */

.dv-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 12px; }
.dev-card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px;
  overflow: hidden;
  cursor: pointer;
  transition: border-color var(--duration-fast) var(--ease), transform var(--duration-fast) var(--ease),
    background var(--duration-fast) var(--ease);
}
.dev-card::before {
  content: "";
  position: absolute;
  left: 0; top: 14px; bottom: 14px;
  width: 2px;
  border-radius: 2px;
  background: var(--neutral-soft);
}
.dev-card.is-on::before { background: var(--success); box-shadow: 0 0 14px var(--success-line); }
.dev-card.is-off::before { background: var(--danger); opacity: 0.8; }
.dev-card.is-disabled { opacity: 0.6; }
@media (hover: hover) {
  .dev-card:hover { border-color: var(--accent-line); transform: translateY(-1px); background: var(--card-hover-bg); }
}
.dev-card:active { transform: scale(0.995); }
.dev-card:focus-visible { outline: 2px solid var(--focus-color); outline-offset: 2px; }

.dv-head { display: flex; align-items: flex-start; gap: 11px; }
.dv-head .ic { width: 38px; height: 38px; flex: none; border-radius: 12px; }
.dv-head .ic .ti { width: 20px; height: 20px; }
.dv-title { min-width: 0; flex: 1; }
.dv-title b { display: block; font-size: 14px; font-weight: var(--weight-semibold); color: var(--text-primary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dv-title small { display: flex; align-items: center; gap: 6px; margin-top: 2px; color: var(--text-muted); font-size: 10.5px; }
.dv-status { display: inline-flex; align-items: center; gap: 5px; font-size: 10px; color: var(--text-muted); white-space: nowrap; }
.dv-status .dot { width: 6px; height: 6px; }
.dv-status.ok { color: var(--success); }
.dv-status.bad { color: var(--danger); }

.dv-state { display: flex; align-items: center; gap: 7px; flex-wrap: wrap; min-height: 26px; }
.dv-meta { display: grid; gap: 4px; font-size: 10.5px; color: var(--text-muted); }
.dv-meta div { display: flex; gap: 7px; align-items: baseline; min-width: 0; }
.dv-meta span { width: 52px; flex: none; font-size: 9px; letter-spacing: 0.08em; text-transform: uppercase; opacity: 0.75; }
.dv-meta b { font-family: var(--font-mono); font-size: 10.5px; font-weight: var(--weight-regular); color: var(--text-secondary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dv-foot { display: flex; align-items: center; gap: 6px; margin-top: auto; padding-top: 8px; border-top: 1px solid var(--divider); }
.dv-foot .sp { flex: 1; }
.dv-foot button { min-height: 28px; height: 28px; padding: 0 9px; font-size: 10.5px; border-radius: 9px; }
.dv-foot button .ti { width: 13px; height: 13px; }
.dv-foot .ago { font-size: 9.5px; color: var(--text-muted); }

/* state chips */
.state-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 24px;
  padding: 0 9px;
  border-radius: var(--radius-pill);
  border: 1px solid var(--border);
  background: var(--neutral-soft);
  color: var(--text-secondary);
  font-size: 10.5px;
  font-weight: var(--weight-semibold);
  letter-spacing: 0.02em;
  white-space: nowrap;
}
.state-chip .ti { width: 12px; height: 12px; }
.state-chip.on, .state-chip.open, .state-chip.unlocked {
  color: var(--success); border-color: var(--success-line); background: var(--success-soft);
}
.state-chip.off, .state-chip.closed, .state-chip.locked {
  color: var(--text-secondary); border-color: var(--border-strong); background: var(--control-bg);
}
.state-chip.unknown { color: var(--warning); border-color: var(--warning-line); background: var(--warning-soft); }
.state-chip.bad { color: var(--danger); border-color: var(--danger-line); background: var(--danger-soft); }
.state-chip.is-live { box-shadow: 0 0 0 3px rgba(78, 217, 162, 0.06); }

/* ─────────────────────────────────────────────────────────────── door cards */

.door-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(330px, 1fr)); gap: 12px; }
.door-card { display: flex; flex-direction: column; gap: 12px; padding: 16px; }
.door-head { display: flex; align-items: flex-start; gap: 11px; }
.door-head .ic { width: 38px; height: 38px; flex: none; border-radius: 12px; }
.door-head .ic .ti { width: 20px; height: 20px; }
.door-head b { display: block; font-size: 14px; font-weight: var(--weight-semibold); }
.door-head small { display: block; margin-top: 2px; font-size: 10.5px; color: var(--text-muted); }
.door-head .sp { flex: 1; }

.door-states { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 7px; }
.door-state {
  padding: 9px 10px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--sunken-bg);
  min-width: 0;
}
.door-state span { display: flex; align-items: center; gap: 4px; font-size: 8.5px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--text-muted); }
.door-state span .ti { width: 11px; height: 11px; }
.door-state b { display: block; margin-top: 4px; font-size: 12px; font-weight: var(--weight-semibold); color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.door-state b.ok { color: var(--success); }
.door-state b.warn { color: var(--warning); }
.door-state b.bad { color: var(--danger); }
.door-state em { display: block; margin-top: 2px; font-style: normal; font-size: 8.5px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.door-note { display: flex; gap: 7px; padding: 8px 10px; border-radius: var(--radius-sm); background: var(--warning-soft); border: 1px solid var(--warning-line); color: var(--text-secondary); font-size: 10.5px; line-height: 1.45; }
.door-note .ti { width: 14px; height: 14px; color: var(--warning); flex: none; margin-top: 1px; }

.door-actions { display: flex; align-items: center; gap: 7px; padding-top: 2px; }
.door-actions .sp { flex: 1; }
.door-actions button { min-height: 32px; height: 32px; font-size: 11.5px; }
.door-actions .unlock { min-width: 116px; }
.door-actions .unlock .ti { width: 15px; height: 15px; }
.door-actions .ago { font-size: 9.5px; color: var(--text-muted); }

/* ─────────────────────────────────────────────────────────────── activity */

.activity-list { display: flex; flex-direction: column; }
.act {
  display: grid;
  grid-template-columns: 26px minmax(0, 1fr) auto;
  gap: 10px;
  align-items: start;
  padding: 10px 2px;
  border-bottom: 1px solid var(--divider);
}
.act:last-child { border-bottom: 0; }
.act .act-ic { width: 26px; height: 26px; display: grid; place-items: center; border-radius: 8px; background: var(--control-bg); color: var(--text-secondary); }
.act .act-ic .ti { width: 14px; height: 14px; }
.act .act-main { min-width: 0; }
.act .act-main b { display: block; font-size: 11.5px; font-weight: var(--weight-semibold); color: var(--text-primary); overflow-wrap: anywhere; }
.act .act-main small { display: block; margin-top: 2px; font-size: 10px; color: var(--text-muted); overflow-wrap: anywhere; }
.act .act-time { font-size: 9.5px; color: var(--text-muted); white-space: nowrap; font-family: var(--font-mono); }
.act .badge { margin-left: 6px; }

/* ─────────────────────────────────────────────────────────────── settings */

.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 12px; align-items: start; }
#mqttCard { grid-column: span 2; }
#mqttCard .form-grid { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
#networkCard .kv { grid-template-columns: 96px minmax(0, 1fr); }
#networkCard .kv dd { font-size: 10px; overflow-wrap: anywhere; }
@media (max-width: 1100px) {
  #mqttCard { grid-column: auto; }
}
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 10px 12px; }
.form-grid label { margin-top: 0; }
.form-grid .full { grid-column: 1 / -1; }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 12px; }
.hint { font-size: 10.5px; color: var(--text-muted); line-height: 1.5; }
.mono-box {
  padding: 9px 11px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--sunken-bg);
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--text-secondary);
  overflow-wrap: anywhere;
}
.kv { display: grid; grid-template-columns: minmax(110px, auto) minmax(0, 1fr); gap: 6px 14px; font-size: 11.5px; }
.kv dt { color: var(--text-muted); }
.kv dd { margin: 0; color: var(--text-secondary); min-width: 0; overflow-wrap: anywhere; }
.kv dd.mono { font-family: var(--font-mono); font-size: 10.5px; }

.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--divider); }
.switch-row:last-child { border-bottom: 0; }
.switch-row > div { min-width: 0; }
.switch-row b { display: block; font-size: 11.5px; font-weight: var(--weight-medium); }
.switch-row small { display: block; margin-top: 1px; font-size: 10px; color: var(--text-muted); }
.switch { position: relative; width: 40px; height: 23px; flex: none; border-radius: 999px; background: var(--control-bg); border: 1px solid var(--control-border); padding: 0; min-height: 0; }
.switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 17px; height: 17px; border-radius: 50%; background: var(--text-muted); transition: transform var(--duration-fast) var(--ease), background var(--duration-fast); }
.switch.on { background: var(--accent-soft); border-color: var(--accent-line); }
.switch.on::after { transform: translateX(17px); background: var(--primary); }

/* ─────────────────────────────────────────────────────────────── empty state */

.empty { display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 34px 18px; text-align: center; }
.empty .ic.big { width: 46px; height: 46px; border-radius: 15px; margin-bottom: 2px; }
.empty b { font-size: 14px; font-weight: var(--weight-semibold); }
.empty small { max-width: 44ch; font-size: 11.5px; color: var(--text-muted); line-height: 1.55; }
.empty .row { margin-top: 6px; }

/* ─────────────────────────────────────────────────────────────── drawer */

.device-detail { display: grid; gap: 14px; }
.device-detail .states { display: flex; flex-wrap: wrap; gap: 6px; }
.device-detail h3 { margin: 6px 0 0; font-size: 10px; letter-spacing: 0.12em; text-transform: uppercase; color: var(--text-muted); font-weight: var(--weight-semibold); }
.detail-actions { display: flex; flex-wrap: wrap; gap: 7px; }
.detail-actions button { min-height: 32px; height: 32px; padding: 0 11px; font-size: 11.5px; }
.detail-actions .danger { color: var(--danger); border-color: var(--danger-line); }
@media (hover: hover) { .detail-actions .danger:hover { background: var(--danger-soft); } }

/* ─────────────────────────────────────────────────────────────── dialogs */

dialog { max-height: min(88dvh, 820px); overflow-y: auto; }
dialog h2 { margin-bottom: 2px; }
dialog .sub { display: block; margin: 3px 0 14px; color: var(--text-muted); font-size: 11.5px; line-height: 1.5; }
dialog .form-actions { position: sticky; bottom: -22px; margin: 16px -22px -20px; padding: 12px 22px; background: var(--panel-bg-strong, var(--surface)); border-top: 1px solid var(--divider); }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.probe-result { margin-top: 10px; }
.probe-result .kv { font-size: 10.5px; }

/* ─────────────────────────────────────────────────────────────── misc */

.page-toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.page-toolbar .sp { flex: 1; }
.page-toolbar .mu { font-size: 11px; }
.seg.pill-seg { border-radius: var(--radius-pill); }
.seg.pill-seg button { min-height: 30px; padding: 0 12px; font-size: 11px; border-radius: var(--radius-pill); }

.ws-badge {
  position: fixed;
  right: 14px;
  bottom: calc(14px + var(--safe-bottom));
  z-index: var(--z-topbar, 25);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 27px;
  padding: 0 10px;
  border-radius: var(--radius-pill);
  border: 1px solid var(--border);
  background: var(--nav-bg);
  color: var(--text-muted);
  font-size: 9.5px;
  letter-spacing: 0.04em;
  box-shadow: var(--shadow-md);
  -webkit-backdrop-filter: blur(var(--glass-blur));
  backdrop-filter: blur(var(--glass-blur));
  pointer-events: none;
}
.ws-badge .dot { width: 5px; height: 5px; }
.ws-badge.live { color: var(--success); border-color: var(--success-line); }
.ws-badge.dead { color: var(--warning); border-color: var(--warning-line); }

/* auth overlay visibility */
body:not([data-auth]) #auth { display: none; }
body[data-auth] .side, body[data-auth] .col { visibility: hidden; }
body[data-auth] .ws-badge { display: none; }
#auth .auth-legal a { color: var(--text-muted); text-decoration: underline; text-underline-offset: 2px; }
.sso-btn:disabled { opacity: 0.45; cursor: not-allowed; }

/* Drawer is off-canvas: display:none (not just visibility) so it cannot widen
   the page or appear in full-page captures while closed. */
body:not(.dopen) #drawer { display: none; }
body.dopen #drawer { display: block; visibility: visible; pointer-events: auto; }

/* ---------------------------------------------------------------- integrations */
.int-strip { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; }
.int-item {
  display: flex; flex-direction: column; gap: 3px; padding: 9px 11px;
  border: 1px solid var(--line); border-radius: var(--radius-sm);
  background: var(--control-bg); min-width: 0;
}
.int-item span { font-size: 9px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--text-muted); }
.int-item b { font-size: 11.5px; font-weight: var(--weight-semibold); color: var(--text-primary); overflow-wrap: anywhere; }
.int-item small { font-size: 10px; color: var(--text-muted); }
.int-item.ok b { color: var(--success, #4ed9a2); }
.int-item.bad b { color: var(--danger); }

.stack-list { display: flex; flex-direction: column; gap: 6px; }
.stack-row {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px;
  border: 1px solid var(--line); border-radius: var(--radius-sm);
  background: var(--control-bg); min-width: 0;
}
.stack-row .main { min-width: 0; flex: 1; }
.stack-row b { display: block; font-size: 11.5px; font-weight: var(--weight-medium); }
.stack-row small { display: block; margin-top: 2px; font-size: 10px; color: var(--text-muted); overflow-wrap: anywhere; }
.stack-row .row { margin: 0; gap: 4px; flex: none; }

.diag {
  margin: 10px 0 0; padding: 10px 12px; max-height: 300px; overflow: auto;
  border: 1px solid var(--line); border-radius: var(--radius-sm);
  background: rgba(0, 0, 0, 0.28); color: var(--text-secondary);
  font-family: var(--font-mono); font-size: 9.5px; line-height: 1.55;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.diag-step { display: flex; gap: 8px; align-items: flex-start; padding: 6px 0; border-bottom: 1px solid var(--divider); }
.diag-step:last-child { border-bottom: 0; }
.diag-step .dot { margin-top: 5px; }
.diag-step b { font-size: 10.5px; }
.diag-step small { display: block; font-size: 10px; color: var(--text-muted); overflow-wrap: anywhere; }

.check-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 5px; margin-top: 6px; }
.check-list label { display: flex; align-items: center; gap: 7px; margin: 0; padding: 7px 8px; border: 1px solid var(--line); border-radius: var(--radius-sm); font-size: 10.5px; }

.channel-pill {
  display: inline-flex; align-items: center; gap: 4px; padding: 2px 7px;
  border: 1px solid var(--line); border-radius: 999px; font-size: 9px;
  letter-spacing: 0.05em; text-transform: uppercase; color: var(--text-muted);
}
.channel-pill.direct { color: var(--accent); border-color: var(--accent-line, var(--line)); }
.channel-pill.ha { color: #b39dff; }
.channel-pill.auto { color: var(--warning); }

/* mobile */
@media (max-width: 720px) {
  /* app.css contains two mobile navigation generations: the bottom bar built
     from .side and the later floating .tabbar island. Q0RO IoT uses the
     bottom bar, so keep .side and never show the unused island. */
  .side { display: flex !important; }
  .tabbar { display: none !important; }
  body { padding-bottom: calc(66px + var(--safe-bottom)); }
  .nav a, .sfoot a { flex-direction: column; gap: 3px; }
  .nav a .t, .sfoot a .t { display: block; font-size: 9px; line-height: 1; }
  body[data-v="overview"] .col { min-height: calc(100dvh - 62px); }
  .dv-grid, .door-grid, .settings-grid { grid-template-columns: 1fr; }
  .dv-head .ic, .door-head .ic { width: 34px; height: 34px; }
  .door-states { gap: 5px; }
  .door-state { padding: 7px 8px; }
  .door-state b { font-size: 11px; }
  .head { align-items: flex-start; }
  .head-actions { width: 100%; }
  .head-actions button { flex: 1; }
  .ws-badge { display: none; }
  .act { grid-template-columns: 22px minmax(0, 1fr) auto; gap: 8px; }
  .act .act-ic { width: 22px; height: 22px; }
}

@media (max-width: 420px) {
  .door-states { grid-template-columns: 1fr 1fr 1fr; }
  .door-state span { font-size: 7.5px; letter-spacing: 0.05em; }
}
