/* ===== Tokens ===== */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --line: #e4e7ec;
  --text: #101828;
  --text-2: #475467;
  --text-3: #8a94a6;
  --brand: #0f5c4a;
  --brand-2: #127a61;
  --brand-soft: #e3f3ed;
  --on-brand: #ffffff;
  --ok: #12805c;
  --ok-soft: #e1f5ec;
  --warn: #b54708;
  --warn-soft: #fef0e1;
  --danger: #c4320a;
  --danger-soft: #fdece7;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --shadow-lg: 0 12px 32px -8px rgba(16, 24, 40, .18);
  --r: 16px;
  --r-sm: 10px;
  --nav-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --surface: #161b22;
    --surface-2: #1d232c;
    --line: #262d38;
    --text: #e8ebf0;
    --text-2: #a8b1bf;
    --text-3: #6f7a8a;
    --brand: #3cc499;
    --brand-2: #57d6ad;
    --brand-soft: #12302a;
    --on-brand: #04140f;
    --ok: #3cc499;
    --ok-soft: #12302a;
    --warn: #f5a35c;
    --warn-soft: #33230f;
    --danger: #f97a5c;
    --danger-soft: #3a1a12;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow-lg: 0 16px 40px -8px rgba(0, 0, 0, .6);
    color-scheme: dark;
  }
}

/* ===== Base ===== */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; min-height: 100dvh; background: var(--bg); color: var(--text);
  font: 16px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
svg.i { width: 20px; height: 20px; flex: none; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--text-2); }
.small { font-size: .85rem; }
.hidden { display: none !important; }

/* ===== Layout ===== */
.shell { min-height: 100dvh; padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px); }
.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 12px) 16px 12px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(180%) blur(14px); -webkit-backdrop-filter: saturate(180%) blur(14px);
}
.topbar .marca { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: -.01em; }
.topbar .marca img { width: 30px; height: 30px; border-radius: 9px; }
.topbar .esp { flex: 1; }
.avatar {
  width: 36px; height: 36px; border-radius: 50%; border: 0; display: grid; place-items: center;
  background: var(--brand-soft); color: var(--brand); font-weight: 700; font-size: .85rem;
}
main { max-width: 720px; margin: 0 auto; padding: 4px 16px 24px; }
.titulo { font-size: 1.6rem; line-height: 1.2; font-weight: 750; letter-spacing: -.02em; margin: 8px 0 4px; }
.sub { color: var(--text-2); margin: 0 0 20px; }

/* Barra inferior (mobile) / topo (desktop) */
.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; display: flex; justify-content: space-around;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--line);
}
.nav a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--text-3); text-decoration: none; font-size: .72rem; font-weight: 600;
}
.nav a[aria-current="page"] { color: var(--brand); }
.nav a svg.i { width: 22px; height: 22px; }
@media (min-width: 900px) {
  .shell { padding-bottom: 24px; }
  .nav {
    position: sticky; top: 0; bottom: auto; height: auto; padding: 0; border: 0; background: transparent;
    justify-content: center; gap: 4px; max-width: 720px; margin: 0 auto 8px;
  }
  .nav a { flex: none; flex-direction: row; gap: 8px; padding: 8px 14px; border-radius: 999px; font-size: .9rem; }
  .nav a[aria-current="page"] { background: var(--brand-soft); }
}

/* ===== Componentes ===== */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); box-shadow: var(--shadow); }
.card.pad { padding: 18px; }
.stack > * + * { margin-top: 12px; }
.row { display: flex; align-items: center; gap: 12px; }
.row .grow { flex: 1; min-width: 0; }
.sec { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 28px 0 10px; }
.sec h2 { font-size: .8rem; text-transform: uppercase; letter-spacing: .08em; color: var(--text-3); margin: 0; font-weight: 700; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 44px; padding: 0 16px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--surface); font-weight: 600; white-space: nowrap;
  transition: background .15s, transform .1s;
}
.btn:active { transform: scale(.98); }
.btn.pri { background: var(--brand); border-color: var(--brand); color: var(--on-brand); }
.btn.pri:hover { background: var(--brand-2); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }
.btn.sm { min-height: 36px; padding: 0 12px; font-size: .9rem; border-radius: 10px; }
.btn[disabled] { opacity: .55; cursor: default; }
.btn .spin { width: 16px; height: 16px; border-radius: 50%; border: 2px solid currentColor; border-right-color: transparent; animation: girar .7s linear infinite; }
@keyframes girar { to { transform: rotate(360deg); } }

.campo { display: block; }
.campo > span { display: block; font-size: .85rem; font-weight: 600; color: var(--text-2); margin-bottom: 6px; }
.campo input, .campo select {
  width: 100%; min-height: 48px; padding: 0 14px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); outline: none; transition: border-color .15s, box-shadow .15s;
}
.campo input:focus, .campo select:focus { border-color: var(--brand); box-shadow: 0 0 0 4px color-mix(in srgb, var(--brand) 18%, transparent); }
.campo small { display: block; margin-top: 6px; color: var(--text-3); font-size: .8rem; }
.grade2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 420px) { .grade2.quebra { grid-template-columns: 1fr; } }

.alerta { padding: 12px 14px; border-radius: 12px; font-size: .9rem; background: var(--danger-soft); color: var(--danger); }
.alerta.ok { background: var(--ok-soft); color: var(--ok); }
.alerta.info { background: var(--surface-2); color: var(--text-2); }

.chip { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px; font-size: .72rem; font-weight: 700; background: var(--surface-2); color: var(--text-2); }
.chip.brand { background: var(--brand-soft); color: var(--brand); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }

/* Switch */
.sw { position: relative; display: inline-block; width: 46px; height: 28px; flex: none; }
.sw input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; z-index: 1; }
.sw span { position: absolute; inset: 0; border-radius: 999px; background: var(--line); transition: background .2s; }
.sw span::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .2s; }
.sw input:checked + span { background: var(--brand); }
.sw input:checked + span::after { transform: translateX(18px); }
.sw input:focus-visible + span { outline: 2px solid var(--brand); outline-offset: 2px; }

/* Segmentado (troca de espaço) */
.seg { display: flex; gap: 4px; padding: 4px; border-radius: 14px; background: var(--surface-2); overflow-x: auto; scrollbar-width: none; }
.seg button { flex: 1; min-height: 40px; padding: 0 14px; border: 0; border-radius: 10px; background: transparent; font-weight: 650; color: var(--text-2); white-space: nowrap; }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* Lista */
.lista { list-style: none; margin: 0; padding: 0; }
.lista > li { display: flex; align-items: center; gap: 12px; padding: 14px 16px; }
.lista > li + li { border-top: 1px solid var(--line); }
.lista a.li, .lista button.li { all: unset; display: flex; align-items: center; gap: 12px; width: 100%; cursor: pointer; }
.ico { width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; background: var(--surface-2); color: var(--text-2); flex: none; }
.ico.brand { background: var(--brand-soft); color: var(--brand); }
.ico.ok { background: var(--ok-soft); color: var(--ok); }
.ico.err { background: var(--danger-soft); color: var(--danger); }
.li-t { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.li-s { font-size: .85rem; color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ===== Portas (botões de acionar) ===== */
.portas { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
@media (min-width: 640px) { .portas { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.porta {
  position: relative; overflow: hidden; display: flex; flex-direction: column; align-items: flex-start; gap: 18px;
  min-height: 148px; padding: 16px; border-radius: 20px; border: 1px solid var(--line); background: var(--surface);
  box-shadow: var(--shadow); text-align: left; user-select: none; -webkit-user-select: none; touch-action: manipulation;
  transition: transform .12s, border-color .2s;
}
.porta .ico { width: 48px; height: 48px; border-radius: 14px; background: var(--brand-soft); color: var(--brand); position: relative; z-index: 1; }
.porta .ico svg.i { width: 24px; height: 24px; }
.porta .txt { position: relative; z-index: 1; margin-top: auto; width: 100%; }
.porta .nome { display: block; font-weight: 700; font-size: 1.05rem; line-height: 1.25; }
.porta .dica { display: block; font-size: .8rem; color: var(--text-3); margin-top: 2px; }
.porta .fill { position: absolute; inset: 0; background: var(--brand); transform-origin: left; transform: scaleX(0); opacity: .14; }
.porta.segurando { transform: scale(.97); border-color: var(--brand); }
.porta.segurando .fill { transform: scaleX(1); transition: transform var(--hold, 800ms) linear; }
.porta.enviando .ico { background: var(--surface-2); color: var(--text-2); }
.porta.sucesso { border-color: var(--ok); }
.porta.sucesso .ico { background: var(--ok); color: #fff; }
.porta.sucesso .fill { transform: scaleX(1); background: var(--ok); opacity: .12; }
.porta.falha { border-color: var(--danger); }
.porta.falha .ico { background: var(--danger); color: #fff; }

.vazio { text-align: center; padding: 48px 24px; color: var(--text-2); }
.vazio .ico { margin: 0 auto 12px; width: 56px; height: 56px; border-radius: 18px; }

/* ===== Login ===== */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.login .caixa { width: 100%; max-width: 380px; }
.login .logo { width: 64px; height: 64px; border-radius: 18px; display: block; margin-bottom: 20px; box-shadow: var(--shadow-lg); }
.login h1 { font-size: 1.75rem; letter-spacing: -.02em; margin: 0 0 4px; }

/* ===== Histórico ===== */
.filtros { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
.filtros select { min-height: 38px; padding: 0 30px 0 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); font-size: .9rem; font-weight: 600; flex: none; }
.dia { font-size: .8rem; font-weight: 700; color: var(--text-3); margin: 20px 4px 8px; }
.dia::first-letter { text-transform: uppercase; }
.hora { font-size: .85rem; color: var(--text-3); font-variant-numeric: tabular-nums; flex: none; }

/* ===== Admin: dispositivos ===== */
.disp { padding: 16px; }
.disp + .disp { border-top: 1px solid var(--line); }
.disp .cab { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.saida { display: grid; grid-template-columns: auto 1fr; gap: 10px 12px; align-items: center; padding: 12px; border-radius: 12px; background: var(--surface-2); }
.saida + .saida { margin-top: 8px; }
.saida input[type="text"] { min-height: 42px; }
.saida .meta { grid-column: 2; font-size: .8rem; color: var(--text-3); margin-top: -6px; }
.perm-esp { padding: 16px; }
.perm-esp + .perm-esp { border-top: 1px solid var(--line); }
.check { display: flex; align-items: center; gap: 12px; padding: 10px 0; }
.check input { width: 22px; height: 22px; accent-color: var(--brand); flex: none; }

/* ===== Toast ===== */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px); z-index: 50;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
  max-width: calc(100% - 32px); width: max-content; padding: 12px 16px; border-radius: 14px;
  background: var(--text); color: var(--bg); font-weight: 600; font-size: .92rem; box-shadow: var(--shadow-lg);
}
#toast.on { opacity: 1; transform: translate(-50%, 0); }
#toast.err { background: var(--danger); color: #fff; }
@media (min-width: 900px) { #toast { bottom: 24px; } }

/* Menu do usuário */
.menu { position: absolute; right: 16px; top: calc(env(safe-area-inset-top) + 56px); min-width: 220px; padding: 6px; z-index: 30; box-shadow: var(--shadow-lg); }
.menu .quem { padding: 10px 12px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.menu button { display: flex; align-items: center; gap: 10px; width: 100%; padding: 10px 12px; border: 0; background: transparent; border-radius: 10px; text-align: left; }
.menu button:hover { background: var(--surface-2); }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }

/* Espaçamento e diálogo de confirmação */
.mt { margin-top: 16px; }
.dialogo { position: fixed; inset: 0; z-index: 60; display: grid; place-items: end center; padding: 16px; padding-bottom: calc(16px + env(safe-area-inset-bottom)); background: rgba(10, 14, 20, .45); }
.dialogo > .card { width: 100%; max-width: 420px; }
.dialogo p { margin: 0; font-weight: 600; }
.dialogo .perigo { background: var(--danger); border-color: var(--danger); color: #fff; }
@media (min-width: 640px) { .dialogo { place-items: center; } }
details summary { cursor: pointer; padding: 4px 0; }
.lista .avatar { flex: none; }

/* Correções de layout: título/subtítulo em linhas próprias e textos longos sem estourar a largura */
.li-t, .li-s { display: block; }
.grow { flex: 1; min-width: 0; }
.lista > li > .sw, .lista > li > .hora, .lista .chip { flex: none; }
main, .card { min-width: 0; max-width: 100%; }
.lista > li .li-s { white-space: normal; }
.lista a.li .li-s { white-space: nowrap; }

a.btn { text-decoration: none; color: inherit; }
a.btn.pri { color: var(--on-brand); }
