/* ------------------------------------------------------------------ Base */
* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
html { -webkit-text-size-adjust: 100%; }
body {
  font: 500 16px/1.45 var(--font-ui);
  background: var(--bg);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  -webkit-font-smoothing: antialiased;
}
::selection { background: var(--red); color: var(--on-red); }
img { max-width: 100%; }
a { color: var(--red-text); font-weight: 700; text-underline-offset: 3px; }
h1, h2, h3, h4 { margin: 0 0 .6rem; line-height: 1.15; font-family: var(--font-display); font-weight: 600; letter-spacing: .005em; }
h1 { font-size: clamp(1.6rem, 2.4vw, 2.1rem); }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; }
main > h1, .page-title { display: inline-block; }
main > h1::after, .page-title::after, .card > h2::after, dialog > h2::after {
  content: ""; display: block; height: 4px; width: 40px; border-radius: 4px; background: var(--red); margin-top: .35rem;
}
.card > h2::after { width: 28px; height: 3px; }
p { margin: .4rem 0; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.money, .num { font-variant-numeric: tabular-nums; }
.money { white-space: nowrap; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.ico { flex: none; display: inline-block; vertical-align: middle; }

:focus-visible { outline: 3px solid var(--red); outline-offset: 2px; border-radius: 10px; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, summary:focus-visible { outline: 3px solid var(--red); outline-offset: 2px; }

/* ------------------------------------------------------------------ Controlos */
button, input, select, textarea { font: inherit; color: inherit; }
button {
  display: inline-flex; align-items: center; justify-content: center; gap: .45rem;
  border: 1.5px solid var(--line); background: var(--surface); color: var(--ink);
  border-radius: 14px; padding: .55rem 1rem; min-height: var(--tap);
  font-weight: 700; cursor: pointer; user-select: none;
  transition: transform 110ms var(--ease-out), background-color 140ms, border-color 140ms, box-shadow 140ms, color 140ms;
}
button:hover:not(:disabled) { border-color: var(--red); }
button:active:not(:disabled) { transform: scale(.96); }
button:disabled { opacity: .42; cursor: not-allowed; }
button.primary { background: var(--red); color: var(--on-red); border-color: var(--red); }
button.primary:hover:not(:disabled) { background: var(--red-deep); border-color: var(--red-deep); }
button.danger { color: var(--red-text); border-color: color-mix(in srgb, var(--red) 45%, var(--line)); background: var(--surface); }
button.danger:hover:not(:disabled) { background: var(--red-tint); }
button.ghost { background: transparent; border-color: transparent; }
button.ghost:hover:not(:disabled) { background: var(--surface-2); border-color: transparent; }
button.pill, .pill { border-radius: var(--radius-pill); }
button[aria-pressed="true"]:not(.pill):not(.chip) { background: var(--red); border-color: var(--red); color: var(--on-red); }
.icon-btn { width: var(--tap); padding: 0; border-radius: 50%; border-color: transparent; background: transparent; }
.icon-btn:hover:not(:disabled) { background: var(--surface-2); border-color: transparent; }

input, select, textarea {
  border: 1.5px solid var(--line); background: var(--surface); color: var(--ink);
  border-radius: 14px; padding: .6rem .85rem; min-height: var(--tap); width: 100%;
  transition: border-color 140ms, box-shadow 140ms;
}
input::placeholder, textarea::placeholder { color: color-mix(in srgb, var(--muted) 80%, transparent); }
input:focus, select:focus, textarea:focus { border-color: var(--red); box-shadow: 0 0 0 4px color-mix(in srgb, var(--red) 18%, transparent); outline: none; }
input:focus-visible, select:focus-visible, textarea:focus-visible { outline: none; }
select {
  appearance: none; padding-right: 2.4rem; cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' fill='none' stroke='%23C3111A' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right .7rem center; background-size: 18px;
}
input[type="checkbox"], input[type="radio"] {
  appearance: none; width: 26px; height: 26px; min-height: 0; padding: 0; flex: none; cursor: pointer;
  border: 2px solid var(--line-strong); border-radius: 9px; background: var(--surface); display: inline-grid; place-items: center;
  transition: background-color 120ms, border-color 120ms, transform 120ms var(--ease-pop);
}
input[type="radio"] { border-radius: 50%; }
input[type="checkbox"]::after { content: ""; width: 12px; height: 7px; border: 3px solid transparent; border-top: 0; border-right: 0; transform: rotate(-45deg) translate(1px, -1px); }
input[type="checkbox"]:checked, input[type="radio"]:checked { background: var(--red); border-color: var(--red); }
input[type="checkbox"]:checked::after { border-color: var(--white); }
input[type="checkbox"]:active { transform: scale(.88); }
input[type="date"], input[type="time"] { min-width: 10rem; }
input[type="color"] { padding: .25rem; height: var(--tap); }
label { display: grid; gap: .3rem; font-size: .78rem; font-weight: 800; color: var(--red-text); text-transform: uppercase; letter-spacing: .04em; }
label > input, label > select, label > textarea { text-transform: none; letter-spacing: 0; font-size: 1rem; font-weight: 600; color: var(--ink); }
label.row { display: flex; text-transform: none; letter-spacing: 0; font-size: .95rem; font-weight: 600; color: var(--ink); gap: .6rem; }
details > summary { cursor: pointer; font-weight: 800; min-height: 40px; display: flex; align-items: center; gap: .4rem; color: var(--red-text); list-style: none; }
details > summary::-webkit-details-marker { display: none; }
details:not(.why):not(.po) > summary::before {
  content: ""; width: 9px; height: 9px; border: solid currentColor; border-width: 0 2.5px 2.5px 0; transform: rotate(-45deg); transition: transform 140ms; margin: 0 .25rem 0 .1rem;
}
details[open]:not(.why):not(.po) > summary::before { transform: rotate(45deg); }

.grid-form { display: grid; gap: .85rem; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 24px; padding: 1.1rem 1.2rem; box-shadow: var(--shadow-1); min-width: 0; }
.card + .card { margin-top: 1rem; }
.cols > .card + .card, .kpis > .card + .card, .counter-list > .card + .card, .insights-grid > .card + .card { margin-top: 0; }
.cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 340px), 1fr)); gap: 1rem; align-items: start; }

.badge {
  display: inline-flex; align-items: center; gap: .25rem; padding: .14rem .6rem; border-radius: var(--radius-pill);
  font-size: .74rem; font-weight: 800; letter-spacing: .02em; background: var(--surface-2); color: var(--muted); white-space: nowrap; vertical-align: middle;
}
.badge.ok { background: var(--ok-tint); color: var(--ok); }
.badge.warn { background: var(--warn-tint); color: var(--warn); }
.badge.bad { background: var(--red-tint); color: var(--red-text); }
.badge.promo { background: var(--red); color: var(--on-red); }
.badge.abc-A { background: var(--red); color: var(--on-red); }
.badge.abc-B { background: var(--red-tint); color: var(--red-text); }
.badge.abc-C { background: var(--surface-2); color: var(--muted); }


/* ------------------------------------------------------------------ Diálogos */
dialog {
  border: 0; border-radius: 30px; padding: 1.3rem 1.4rem 0; width: min(600px, calc(100% - 1.5rem)); max-height: min(92dvh, 900px);
  background: var(--surface); color: var(--ink); box-shadow: var(--shadow-pop); overflow: auto; overscroll-behavior: contain;
}
dialog[open] { animation: dlg-in 180ms var(--ease-pop); }
dialog::backdrop { background: rgb(18 18 18 / .55); backdrop-filter: blur(3px); }
dialog > h2 { font-size: 1.4rem; padding-right: 2rem; }
dialog .actions {
  position: sticky; bottom: 0; display: flex; gap: .5rem; justify-content: flex-end; flex-wrap: wrap; margin: 1rem -1.4rem 0; padding: .8rem 1.4rem 1.1rem;
  background: linear-gradient(to top, var(--surface) 80%, transparent);
}
dialog .actions button { min-height: 52px; border-radius: 18px; padding: .5rem 1.2rem; }
dialog .actions button.primary { min-width: 9rem; font-size: 1.05rem; }
@keyframes dlg-in { from { opacity: 0; transform: translateY(14px) scale(.97); } }
@media (max-width: 600px) {
  dialog { width: 100%; max-width: 100%; margin: auto 0 0; border-radius: 28px 28px 0 0; max-height: 94dvh; }
  dialog[open] { animation: sheet-in 200ms var(--ease-out); }
  .sheet, .pay-sheet { min-width: 0; }
  dialog .actions button { flex: 1 1 auto; }
}
@keyframes sheet-in { from { transform: translateY(40px); opacity: .4; } }

.toast {
  position: fixed; bottom: 1.2rem; left: 50%; transform: translateX(-50%); z-index: 100; max-width: calc(100% - 2rem);
  background: var(--ink); color: var(--surface); padding: .8rem 1.3rem; border-radius: var(--radius-pill); box-shadow: var(--shadow-2);
  font-weight: 700; animation: toast-in 180ms var(--ease-pop);
}
.toast.error { background: var(--red-deep); color: var(--white); }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 12px); } }
@media (max-width: 860px) { .toast { bottom: 5.5rem; } }

/* Estados vazios, mascote e celebração */
.mascot { border-radius: 50%; box-shadow: 0 0 0 4px var(--halo); background: var(--white); flex: none; }
.mascot.float { animation: float 3s ease-in-out infinite; }
.mascot.wave { animation: wave 900ms ease-in-out 2; transform-origin: 50% 85%; }
@keyframes float { 50% { transform: translateY(-5px) rotate(-2deg); } }
@keyframes wave { 0%, 100% { transform: rotate(0); } 20% { transform: rotate(-12deg); } 45% { transform: rotate(10deg); } 70% { transform: rotate(-6deg); } }
.empty-state { display: grid; justify-items: center; text-align: center; gap: .35rem; padding: 1.6rem 1rem; color: var(--muted); }
.empty-state strong { font: 600 1.2rem var(--font-display); color: var(--ink); margin-top: .4rem; }
.empty-state p { margin: 0; max-width: 32ch; font-weight: 600; }
.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 120; }
.confetti i { position: fixed; width: 10px; height: 14px; border-radius: 3px; box-shadow: 0 0 0 1px rgb(0 0 0 / .06); }
.celebrate {
  position: fixed; z-index: 110; left: 50%; top: 18%; transform: translateX(-50%); display: flex; align-items: center; gap: .8rem;
  background: var(--surface); color: var(--ink); padding: .7rem 1.4rem .7rem .7rem; border-radius: var(--radius-pill); box-shadow: var(--shadow-pop);
  border: 3px solid var(--red); pointer-events: none; animation: celebrate-in 220ms var(--ease-pop);
}
.celebrate strong { display: block; font: 600 1.3rem var(--font-display); }
.celebrate span { color: var(--muted); font-weight: 700; }
.celebrate.bye { opacity: 0; transform: translate(-50%, -10px); transition: opacity 260ms, transform 260ms; }
@keyframes celebrate-in { from { opacity: 0; transform: translate(-50%, 16px) scale(.9); } }

/* ------------------------------------------------------------------ Movimento reduzido */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; animation-iteration-count: 1 !important; transition-duration: 1ms !important; scroll-behavior: auto !important; }
  .kds .ticket-card.late, .mascot.float, .attract .start, .floor .table .badge.ok { animation: none !important; }
}

/* ------------------------------------------------------------------ Confirmação de ações sensíveis */
.confirm-body { display: grid; gap: .5rem; }
.confirm-message { font-size: 1.05rem; font-weight: 700; margin: 0; }
.confirm-consequence { margin: 0; padding: .55rem .8rem; border-radius: var(--radius-sm); background: var(--warn-tint); color: var(--warn); font-weight: 700; }

/* ------------------------------------------------------------------ Gaveta inferior (telemóvel/PDA — T5) */
.drawer-handle, .drawer-backdrop { display: none; }
@media (max-width: 820px) {
  .drawer {
    position: fixed; z-index: 40; left: 0; right: 0; bottom: 0; max-height: 86dvh; overflow: auto; overscroll-behavior: contain;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0; margin: 0; padding-bottom: calc(1rem + env(safe-area-inset-bottom));
    transform: translateY(105%); visibility: hidden; transition: transform 220ms var(--ease-out), visibility 0s linear 220ms;
    box-shadow: var(--shadow-pop);
  }
  .drawer.open { transform: none; visibility: visible; transition: transform 220ms var(--ease-out); }
  .drawer-handle {
    display: flex; flex-direction: column; align-items: center; gap: .25rem; width: 100%; min-height: 44px; padding: .35rem 0 .2rem;
    border: 0; border-radius: 0; background: transparent; position: sticky; top: 0; z-index: 2; touch-action: none;
  }
  .drawer-grip { width: 44px; height: 5px; border-radius: var(--radius-pill); background: var(--line-strong); }
  .drawer-close-text { font-size: .78rem; color: var(--muted); }
  .drawer-backdrop { display: block; position: fixed; inset: 0; z-index: 39; background: rgb(18 18 18 / .45); opacity: 0; pointer-events: none; transition: opacity 200ms; }
  .drawer-backdrop.open { opacity: 1; pointer-events: auto; }
}

/* ------------------------------------------------------------------ Gestos (deslizar, arrastar) */
.swipe-row { position: relative; overflow: hidden; }
.swipe-row > .swipe-under {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: flex-end; padding: 0 1rem; gap: .4rem;
  background: var(--gesture-reveal); color: var(--on-red); font-weight: 800; border-radius: inherit;
}
.swipe-row > .swipe-inner { position: relative; background: var(--surface); transition: transform 160ms var(--ease-out); touch-action: pan-y; }
.swipe-row > .swipe-inner.swiping { transition: none; }
.drag-ghost {
  position: fixed; z-index: 130; transform: translate(-50%, -120%); pointer-events: none; padding: .45rem .9rem;
  border-radius: var(--radius-pill); background: var(--drag-ghost); color: var(--surface); font-weight: 800; box-shadow: var(--shadow-pop);
}
.dragging { opacity: .55; }
.drop-target { outline: 4px dashed var(--red); outline-offset: 3px; }
[data-gesture~="drag"] { touch-action: none; }
.gesture-hint { font-size: .78rem; color: var(--muted); font-weight: 700; }

/* ------------------------------------------------------------------ Espera prevista (quiosque, QR, levantamento) */
.wait-chip {
  display: inline-flex; align-items: center; gap: .4rem; padding: .3rem .8rem; border-radius: var(--radius-pill);
  background: var(--surface-2); color: var(--ink); font-weight: 800; font-variant-numeric: tabular-nums;
}
.wait-chip small { color: var(--muted); font-weight: 700; }
