/* ==========================================================================
   Portão — folha de estilo
   Paleta quente sobre papel, tipografia serifada nos títulos, movimento
   discreto. Tudo em classes: a CSP não permite estilos inline.
   ========================================================================== */

:root {
  --bg: #faf9f5;
  --surface: #fffefb;
  --surface-2: #f2f0e8;
  --border: #e6e2d7;
  --border-strong: #d6d1c3;
  --text: #1f1e1d;
  --muted: #6b6862;
  --accent: #c95f38;
  --accent-hover: #b5502c;
  --accent-soft: #fbeee7;
  --ok: #4a7c59;
  --ok-soft: #eaf2ec;
  --bad: #b0473c;
  --bad-soft: #fbecea;
  --shadow: 0 1px 2px rgb(31 30 29 / 0.04), 0 8px 24px -12px rgb(31 30 29 / 0.18);
  --shadow-lift: 0 2px 4px rgb(31 30 29 / 0.06), 0 16px 32px -16px rgb(31 30 29 / 0.26);
  --radius: 20px;
  --radius-sm: 12px;
  --serif: ui-serif, "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --ease: cubic-bezier(0.22, 0.61, 0.36, 1);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1f1e1d;
    --surface: #262624;
    --surface-2: #302f2c;
    --border: #3b3a36;
    --border-strong: #4a4844;
    --text: #f5f4ee;
    --muted: #a5a199;
    --accent: #d97757;
    --accent-hover: #e28a6d;
    --accent-soft: #33261f;
    --ok: #7fb08c;
    --ok-soft: #232b25;
    --bad: #d98077;
    --bad-soft: #32211f;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.2), 0 8px 24px -12px rgb(0 0 0 / 0.5);
    --shadow-lift: 0 2px 4px rgb(0 0 0 / 0.25), 0 16px 32px -16px rgb(0 0 0 / 0.6);
    color-scheme: dark;
  }
}

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

body {
  margin: 0;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: max(20px, env(safe-area-inset-top)) 18px max(20px, env(safe-area-inset-bottom));
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 var(--sans);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

.app { width: 100%; max-width: 400px; }

/* --- Vistas ------------------------------------------------------------- */

.view { display: none; }
.view.is-active { display: block; animation: view-in 0.42s var(--ease) both; }

@keyframes view-in {
  from { opacity: 0; transform: translateY(10px) scale(0.985); }
  to   { opacity: 1; transform: none; }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 30px 26px;
}
.card--center { display: grid; place-items: center; min-height: 190px; }

/* --- Cabeçalhos --------------------------------------------------------- */

.hero { text-align: center; margin-bottom: 26px; }
.hero--tight { margin-bottom: 22px; }

.mark {
  width: 58px;
  height: 58px;
  margin: 0 auto 16px;
  display: grid;
  place-items: center;
  border-radius: 16px;
  background: var(--accent-soft);
  color: var(--accent);
}
.mark svg { width: 34px; height: 34px; stroke: currentColor; }
.mark__bars { animation: mark-slide 5.5s var(--ease) infinite; transform-origin: center; }

@keyframes mark-slide {
  0%, 58%, 100% { transform: translateX(0); }
  22%, 40%      { transform: translateX(-7px); opacity: 0.85; }
}

.title {
  margin: 0 0 6px;
  font-family: var(--serif);
  font-size: 27px;
  font-weight: 500;
  letter-spacing: -0.015em;
}
.title--sm { font-size: 21px; margin: 0; }

.lede { margin: 0; color: var(--muted); font-size: 14.5px; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 22px;
}

/* --- Indicador do dispositivo ------------------------------------------- */

.pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 5px 11px 5px 9px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 12.5px;
  font-weight: 500;
  color: var(--muted);
  transition: background 0.3s var(--ease), color 0.3s var(--ease);
}
.pill__dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  flex: none;
}
.pill.is-online { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.pill.is-online .pill__dot { background: var(--ok); animation: beat 2.4s ease-in-out infinite; }
.pill.is-offline { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.pill.is-offline .pill__dot { background: var(--bad); }

@keyframes beat {
  0%, 100% { box-shadow: 0 0 0 0 rgb(74 124 89 / 0.5); }
  60%      { box-shadow: 0 0 0 6px rgb(74 124 89 / 0); }
}

/* --- Palco do portão ---------------------------------------------------- */

.stage {
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 20px 18px 18px;
  margin-bottom: 22px;
  text-align: center;
}

.gate { width: 100%; max-width: 240px; height: auto; display: block; margin: 0 auto; }
.gate__rail, .gate__bar, .gate__post, .gate__ground {
  stroke: var(--muted);
  stroke-linecap: round;
  transition: stroke 0.4s var(--ease);
}
.gate__rail  { stroke-width: 3.5; }
.gate__bar   { stroke-width: 2.5; opacity: 0.75; }
.gate__post  { stroke-width: 4.5; stroke: var(--border-strong); }
.gate__ground{ stroke-width: 2.5; stroke: var(--border-strong); opacity: 0.7; }

/* A folha segue a posição real, não um ciclo: --pos vem do servidor (0..1) e
   --travel é o tempo que falta para o fim do curso. */
.gate__slide {
  transform: translateX(calc(var(--pos, 0) * -186px));
  transition: transform var(--travel, 320ms) linear;
}

.stage.is-abrindo  .gate__rail,
.stage.is-abrindo  .gate__bar,
.stage.is-fechando .gate__rail,
.stage.is-fechando .gate__bar { stroke: var(--accent); }

.stage.is-parado .gate__rail,
.stage.is-parado .gate__bar { stroke: var(--text); opacity: 0.6; }

.stage.is-aberto .gate__post { stroke: var(--accent); opacity: 0.6; }
.stage.is-fechado .gate__rail,
.stage.is-fechado .gate__bar { stroke: var(--muted); }

.stage.is-offline { opacity: 0.55; }

.state {
  margin: 14px 0 0;
  font-family: var(--serif);
  font-size: 25px;
  font-weight: 500;
  letter-spacing: -0.015em;
}
.state--enter { animation: state-in 0.35s var(--ease) both; }
.stage.is-abrindo .state, .stage.is-fechando .state { color: var(--accent); }

@keyframes state-in {
  from { opacity: 0; transform: translateY(5px); }
  to   { opacity: 1; transform: none; }
}

.state__hint {
  margin: 3px 0 0;
  font-size: 12.5px;
  color: var(--muted);
  min-height: 1.2em;
}

/* --- Botões ------------------------------------------------------------- */

.btn {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  width: 100%;
  padding: 14px 18px;
  border: 1px solid transparent;
  border-radius: 14px;
  font: 500 15.5px/1.2 var(--sans);
  color: var(--text);
  background: none;
  cursor: pointer;
  transition: background 0.2s var(--ease), color 0.2s var(--ease),
              transform 0.12s var(--ease), box-shadow 0.2s var(--ease), opacity 0.2s;
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn:not(:disabled):active { transform: scale(0.982); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn__icon { display: grid; place-items: center; width: 19px; height: 19px; flex: none; }
.btn__icon svg { width: 100%; height: 100%; stroke: currentColor; }

.btn--primary {
  background: var(--accent);
  color: #fff;
  box-shadow: var(--shadow);
}
.btn--primary:not(:disabled):hover { background: var(--accent-hover); box-shadow: var(--shadow-lift); }

.btn--action {
  padding: 19px;
  font-size: 17px;
  font-weight: 600;
  background: var(--accent);
  color: #fff;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.btn--action:not(:disabled):hover { background: var(--accent-hover); box-shadow: var(--shadow-lift); }

/* Anel que pulsa enquanto o telemóvel espera pela biometria. */
.btn__ring {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  opacity: 0;
  background: radial-gradient(circle at center, rgb(255 255 255 / 0.35), transparent 62%);
}
.btn--action.is-scanning .btn__ring { animation: scan 1.25s ease-in-out infinite; }
.btn--action.is-scanning { pointer-events: none; }

@keyframes scan {
  0%   { opacity: 0; transform: scale(0.75); }
  45%  { opacity: 1; }
  100% { opacity: 0; transform: scale(1.5); }
}

.btn--action.is-done { animation: done 0.55s var(--ease); }
@keyframes done {
  30% { transform: scale(1.035); }
  60% { transform: scale(0.995); }
}

.btn--ghost {
  margin-top: 12px;
  padding: 11px;
  font-size: 14px;
  color: var(--muted);
  border-color: var(--border);
}
.btn--ghost:not(:disabled):hover { background: var(--surface-2); color: var(--text); }
.btn--tight { margin-top: 10px; }

.btn--link {
  width: auto;
  padding: 6px 2px;
  font-size: 13.5px;
  color: var(--muted);
  text-decoration: underline;
  text-underline-offset: 3px;
}
.btn--link:hover { color: var(--text); }

.footnote {
  margin: 12px 0 0;
  text-align: center;
  font-size: 12.5px;
  color: var(--muted);
}

/* --- Campos ------------------------------------------------------------- */

.field { display: block; margin-bottom: 16px; }
.field__label {
  display: block;
  margin-bottom: 7px;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
}
.field__input {
  width: 100%;
  padding: 13px 14px;
  font: 15.5px var(--sans);
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: border-color 0.2s var(--ease), box-shadow 0.2s var(--ease);
}
.field__input::placeholder { color: var(--muted); opacity: 0.6; }
.field__input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.field__input--code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 17px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  text-align: center;
}

/* --- Avisos ------------------------------------------------------------- */

.alert {
  margin: 14px 0 0;
  padding: 11px 13px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  animation: view-in 0.3s var(--ease) both;
}
.alert--error { background: var(--bad-soft); color: var(--bad); }
.is-hidden { display: none !important; }

/* --- Secções recolhíveis ------------------------------------------------ */

.panel { border-top: 1px solid var(--border); }
.panel:first-of-type { margin-top: 24px; }

.panel__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 2px;
  cursor: pointer;
  list-style: none;
  user-select: none;
}
.panel__head::-webkit-details-marker { display: none; }
.panel__title { font-size: 14px; font-weight: 500; color: var(--muted); }
.panel__chevron { width: 17px; height: 17px; color: var(--muted); transition: transform 0.28s var(--ease); }
.panel__chevron svg { width: 100%; height: 100%; stroke: currentColor; }
.panel[open] .panel__chevron { transform: rotate(180deg); }
.panel[open] .log, .panel[open] .devices { animation: view-in 0.32s var(--ease) both; }

/* --- Listas ------------------------------------------------------------- */

.log, .devices {
  margin: 0 0 14px;
  padding: 0;
  list-style: none;
  font-size: 13.5px;
  max-height: 240px;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.log li {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 9px 2px;
  border-bottom: 1px solid var(--border);
  color: var(--muted);
}
.log li:last-child { border-bottom: none; }
.log time {
  flex: none;
  width: 88px;
  font-variant-numeric: tabular-nums;
  font-size: 12.5px;
  opacity: 0.8;
}
.log .log__what { color: var(--text); }
.log .log__what--bad { color: var(--bad); }
.log .log__what--ok { color: var(--ok); }
.log .log__empty { color: var(--muted); font-style: italic; }

.devices li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 2px;
  border-bottom: 1px solid var(--border);
}
.devices li:last-child { border-bottom: none; }
.devices .devices__name { color: var(--text); font-size: 14px; }
.devices .devices__meta { display: block; font-size: 12px; color: var(--muted); }
.devices .devices__remove {
  flex: none;
  width: auto;
  padding: 5px 9px;
  font-size: 12.5px;
  color: var(--bad);
  border-radius: 8px;
}
.devices .devices__remove:hover { background: var(--bad-soft); }

/* --- Rodapé ------------------------------------------------------------- */

.footbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 6px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}
.footbar__who { font-size: 13.5px; color: var(--muted); }

/* --- Modal do código ---------------------------------------------------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 22px;
  background: rgb(15 14 13 / 0.55);
  backdrop-filter: blur(3px);
  animation: view-in 0.24s var(--ease) both;
}
.modal[hidden] { display: none; }

.modal__box {
  width: 100%;
  max-width: 340px;
  padding: 28px 24px 22px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lift);
  text-align: center;
  animation: modal-pop 0.3s var(--ease) both;
}

@keyframes modal-pop {
  from { opacity: 0; transform: translateY(12px) scale(0.96); }
  to   { opacity: 1; transform: none; }
}

.modal__mark {
  width: 46px;
  height: 46px;
  margin: 0 auto 14px;
  display: grid;
  place-items: center;
  border-radius: 13px;
  background: var(--accent-soft);
  color: var(--accent);
}
.modal__mark svg { width: 26px; height: 26px; stroke: currentColor; }

.modal__title {
  margin: 0 0 6px;
  font-family: var(--serif);
  font-size: 20px;
  font-weight: 500;
}
.modal__lede { margin: 0 0 20px; font-size: 13.5px; color: var(--muted); }
.modal__lede b { color: var(--text); font-weight: 600; }

.code {
  margin: 0 0 18px;
  padding: 16px 12px;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 25px;
  font-weight: 600;
  letter-spacing: 0.12em;
  color: var(--accent);
  user-select: all;
  animation: state-in 0.35s var(--ease) both;
}

.modal__expiry {
  margin: 14px 0 0;
  font-size: 12.5px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.modal__expiry--dead { color: var(--bad); }

/* --- Toast -------------------------------------------------------------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: max(22px, env(safe-area-inset-bottom));
  z-index: 20;
  max-width: min(360px, calc(100vw - 36px));
  padding: 12px 17px;
  border-radius: 13px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  text-align: center;
  box-shadow: var(--shadow-lift);
  opacity: 0;
  transform: translate(-50%, 14px);
  pointer-events: none;
  transition: opacity 0.28s var(--ease), transform 0.28s var(--ease);
}
.toast.is-visible { opacity: 1; transform: translate(-50%, 0); }
.toast.is-ok  { background: var(--ok); color: #fff; }
.toast.is-bad { background: var(--bad); color: #fff; }

/* --- Spinner ------------------------------------------------------------ */

.spinner {
  width: 26px;
  height: 26px;
  border: 2.5px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* --- Movimento reduzido ------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
