/* =====================================================================
   AllowMeIn Access — single stylesheet.
   Built mobile-first; the "driving" view scales up to fill the screen.
   ===================================================================== */

:root {
    --bg:         #17191d;
    --bg-2:       #22252c;
    --bg-3:       #2b2f38;
    --line:       #383c46;
    --fg:         #f3f4f7;
    --mid:        #a5a9b3;
    --dim:        #707086;
    --ok:         #1b944b;
    --ok-bright:  #4ade80;
    --warn:       #f5a524;
    --bad:        #dc2626;
    --bad-bright: #f87171;
    --info:       #295797;
    --accent:     #3b82f6;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--fg);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    -webkit-font-smoothing: antialiased;
    min-height: 100vh;
}

/* Bottom-nav room + iPhone home-bar safe area.
   Calibrated to the actual nav height (icon ~22px + 4px gap + label ~13px
   + 10px top padding + 8px bottom padding = ~68px), plus a comfortable
   buffer so the last card never tucks under. */
body {
    padding-bottom: calc(96px + env(safe-area-inset-bottom));
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

img { max-width: 100%; }

/* ======================================================================
   TOP BAR
   Mobile: logo + proximity chip + menu button.  User info lives behind
   the menu button so it never overflows.
   Desktop: everything inline.
   ====================================================================== */

.topbar {
    display: grid;
    grid-template-columns: auto 1fr auto;
    gap: 10px;
    align-items: center;
    padding: 10px 14px;
    background: rgba(34, 37, 44, 0.92);
    border-bottom: 1px solid var(--line);
    position: sticky;
    top: 0;
    z-index: 50;
    min-height: 52px;
    backdrop-filter: blur(12px);
}
.topbar .logo { display: flex; align-items: center; }
.topbar .logo img { height: 26px; display: block; }

.topbar .badges {
    display: flex;
    gap: 6px;
    flex-wrap: nowrap;
    overflow: hidden;
    justify-self: start;
    align-items: center;
}

.topbar .menu-btn {
    border: 1px solid var(--line);
    background: var(--bg-3);
    color: var(--fg);
    width: 36px; height: 36px;
    border-radius: 10px;
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    transition: background 0.15s;
}
.topbar .menu-btn:hover { background: var(--line); }

.user-menu {
    position: fixed;
    top: 60px;
    right: 12px;
    background: var(--bg-2);
    border: 1px solid var(--line);
    border-radius: 12px;
    box-shadow: 0 12px 32px rgba(0,0,0,0.5);
    padding: 14px 16px;
    min-width: 200px;
    z-index: 60;
}
.user-menu .user-name {
    display: block;
    font-weight: 600;
    margin-bottom: 2px;
}
.user-menu .user-email {
    display: block;
    color: var(--mid);
    font-size: 12px;
    margin-bottom: 12px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--line);
}
.user-menu a {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 0;
    color: var(--fg);
    text-decoration: none;
}
.user-menu a:hover { color: var(--accent); text-decoration: none; }
.user-menu a.danger { color: var(--bad-bright); }
.user-menu a.danger:hover { color: var(--bad); }
.user-menu a .ico {
    width: 18px;
    text-align: center;
    margin: 0;
    color: var(--mid);
    font-family: ui-monospace, "SF Mono", monospace;
}
.user-menu[hidden] { display: none; }

@media (min-width: 720px) {
    .topbar { padding: 12px 18px; }
    .topbar .logo img { height: 30px; }
}

/* ======================================================================
   BADGES
   ====================================================================== */

.badge {
    display: inline-flex;
    align-items: center;
    padding: 3px 9px;
    border-radius: 999px;
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.06em;
    background: var(--bg-3);
    color: var(--mid);
    border: 1px solid var(--line);
    white-space: nowrap;
    line-height: 1.4;
    text-transform: uppercase;
}
.badge-lan   { background: #064e3b; color: #d1fae5; border-color: #065f46; }
.badge-near  { background: #1e40af; color: #dbeafe; border-color: #1d4ed8; }
.badge-far   { background: #92400e; color: #fef3c7; border-color: #b45309; }
.badge-off   { background: #7f1d1d; color: #fee2e2; border-color: #991b1b; }
.badge-nofix { background: #4b5563; color: #e5e7eb; border-color: #6b7280; }
.badge-admin { background: var(--accent); color: white; border-color: var(--accent); }
.badge-conn  {
    font-family: ui-monospace, "SF Mono", monospace;
    font-size: 10px;
    padding: 3px 6px;
    min-width: 36px;
    justify-content: center;
}
.badge-conn-up   { background: #064e3b; color: #d1fae5; border-color: #065f46; }
.badge-conn-down { background: #4b5563; color: #e5e7eb; border-color: #6b7280; }

/* ======================================================================
   BOTTOM NAV
   ====================================================================== */

.bottomnav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    background: rgba(34, 37, 44, 0.92);
    backdrop-filter: blur(12px);
    border-top: 1px solid var(--line);
    padding-bottom: env(safe-area-inset-bottom);
    z-index: 50;
}
.bottomnav a {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    text-align: center;
    padding: 10px 4px 8px;
    color: var(--dim);
    font-size: 11px;
    letter-spacing: 0.03em;
    text-decoration: none;
    border-top: 2px solid transparent;
    transition: color 0.15s, background 0.15s;
}
.bottomnav a:hover { color: var(--mid); text-decoration: none; }
.bottomnav a.on {
    color: var(--fg);
    border-top-color: var(--accent);
    background: linear-gradient(180deg, rgba(59,130,246,0.10), transparent 60%);
}
.bottomnav a .glyph {
    font-size: 22px;
    line-height: 1;
    font-family: ui-monospace, "SF Mono", monospace;
}
.bottomnav a.on .glyph { transform: scale(1.05); }
.bottomnav a .lbl { font-weight: 500; }

/* Generic icon glyph used inside .btn — keeps it from inheriting bold/sized text */
.btn .ico {
    margin-right: 6px;
    font-family: ui-monospace, "SF Mono", monospace;
    font-weight: normal;
}
.btn.icon-only .ico { margin-right: 0; }
.btn.icon-only { padding: 8px 10px; min-width: 36px; }

/* ======================================================================
   GENERIC LAYOUT
   ====================================================================== */

main { padding: 14px; max-width: 1100px; margin: 0 auto; }
h1, h2, h3 { font-weight: 600; }
h2 { margin: 0 0 4px; font-size: 22px; }
h3 { margin: 24px 0 8px; font-size: 16px; color: var(--mid); }

.crumbs { font-size: 13px; color: var(--mid); margin-bottom: 18px; }
.crumbs a { color: var(--accent); }

.banner {
    border-radius: 8px;
    padding: 12px 14px;
    margin-bottom: 12px;
    font-size: 14px;
}
.banner-info { background: #1e3a5f; color: #dbeafe; border: 1px solid #2563eb; }
.banner-warn { background: #78350f; color: #fef3c7; border: 1px solid #d97706; }
.banner-bad  { background: #7f1d1d; color: #fee2e2; border: 1px solid #dc2626; }
.banner-ok   { background: #064e3b; color: #d1fae5; border: 1px solid #10b981; }

/* ======================================================================
   FORM CONTROLS — note: NO global "input { width: 100% }"; that broke
   every checkbox. Only block-level controls get full width.
   ====================================================================== */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 10px 16px;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: var(--bg-3);
    color: var(--fg);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s;
    text-decoration: none;
}
.btn:hover { background: var(--bg-2); text-decoration: none; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: white; }
.btn-primary:hover { background: #2563eb; }
.btn-danger  { background: var(--bad); border-color: var(--bad); color: white; }
.btn-small { padding: 6px 10px; font-size: 12px; }
.btn-block { display: flex; width: 100%; }

input[type=text], input[type=email], input[type=tel], input[type=password],
input[type=number], input[type=search], input[type=url],
select, textarea {
    display: block;
    width: 100%;
    padding: 10px 12px;
    background: var(--bg-3);
    color: var(--fg);
    border: 1px solid var(--line);
    border-radius: 6px;
    font-size: 14px;
    font-family: inherit;
}
textarea { min-height: 80px; resize: vertical; }

/* Override Chrome's autofill yellow tint that breaks dark-theme contrast. */
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
input:-webkit-autofill:active {
    -webkit-box-shadow: 0 0 0 30px var(--bg-3) inset !important;
    -webkit-text-fill-color: var(--fg) !important;
    caret-color: var(--fg);
    transition: background-color 0s 600000s, color 0s 600000s;
}

input[type=checkbox], input[type=radio] {
    /* keep the native size; never let "width: 100%" leak in */
    width: 18px;
    height: 18px;
    margin: 0;
    accent-color: var(--accent);
    vertical-align: middle;
}

label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; color: var(--mid); font-size: 13px; margin-bottom: 4px; }

/* Inline checkbox/radio with label */
label.check {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    margin: 6px 0;
    font-size: 14px;
    cursor: pointer;
    color: var(--fg);
    user-select: none;
}
label.check input { flex: none; }

/* ======================================================================
   DOOR GRID
   ====================================================================== */

/* Door tiles — mirrors allowmein.ca's "feel": chunky 2:1 buttons,
   colour IS the state (green = locked/safe, red = unlocked/danger),
   no separate label, big door name centred, .changing animates while
   the command is in flight so the user gets immediate feedback even
   before the WS confirms the new state. */

.door-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 10px;
    margin-top: 12px;
}
.door {
    appearance: none;
    -webkit-appearance: none;
    font: inherit;
    color: white;
    cursor: pointer;
    display: grid;
    place-items: center;
    padding: 14px;
    border: none;
    border-radius: 14px;
    aspect-ratio: 2 / 1;
    text-align: center;
    overflow: hidden;
    position: relative;
    transition: transform 0.08s, filter 0.15s, box-shadow 0.15s;
    box-shadow: 0 2px 0 rgba(0,0,0,0.25);
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}
.door .name {
    font-size: clamp(16px, 3.5vw, 22px);
    font-weight: 700;
    line-height: 1.15;
    letter-spacing: 0.01em;
    padding: 0 6px;
}
.door .door-glyph {
    position: absolute;
    top: 8px;
    right: 10px;
    font-size: 13px;
    opacity: 0.7;
    line-height: 1;
    font-family: ui-monospace, "SF Mono", monospace;
}
.door .reason {
    position: absolute;
    bottom: 6px;
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    opacity: 0.9;
}

/* Colour = state. Allowmein convention:
   green = locked/safe   red = unlocked/danger. */
.door.state-off { background-color: #1b944b; }   /* locked → green */
.door.state-on  { background-color: #dc2626; }   /* unlocked → red */
.door.momentary { background-color: #295797; }   /* gates, pulses → blue */
.door.momentary.state-on { background-color: #dc2626; }

.door:hover  { filter: brightness(1.08); }
.door:active { transform: scale(0.97); }

.door.denied {
    background-color: #4b5563 !important;
    color: #d1d5db;
    cursor: not-allowed;
    opacity: 0.7;
}
.door.denied:hover { filter: none; }

/* In-flight feedback — pulses opacity until the WS push (or the HTTP
   open response) settles back. Replaces the previous .busy pulse. */
.door.changing { animation: door-changing 1s ease-in-out infinite; }
@keyframes door-changing {
    0%   { opacity: 1; }
    50%  { opacity: 0.45; }
    100% { opacity: 1; }
}

/* ======================================================================
   DRIVING VIEW — huge tappable buttons
   ====================================================================== */

.driving {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 14px;
    height: calc(100vh - 220px);
    padding: 0;
    max-width: 900px;
    margin: 12px auto 0;
}
.driving .door {
    min-height: 0;
    border-radius: 20px;
    padding: 24px;
    text-align: center;
    justify-content: center;
    align-items: center;
    gap: 12px;
}
.driving .door .name { font-size: clamp(1.6rem, 6vw, 2.6rem); }
.driving .door .state { font-size: 14px; }

/* ======================================================================
   TABLES — admin views
   Wrapped in .table-scroll so they don't break the viewport on mobile.
   ====================================================================== */

.table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 12px 0;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--bg-2);
}
table.data {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}
table.data th,
table.data td {
    padding: 10px 12px;
    text-align: left;
    border-bottom: 1px solid var(--line);
    white-space: nowrap;
    vertical-align: middle;
}
table.data th {
    color: var(--mid);
    font-weight: 500;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    background: var(--bg-2);
    position: sticky;
    top: 0;
}
table.data tr:last-child td { border-bottom: none; }
table.data tbody tr:hover td { background: var(--bg-3); }
table.data td.actions { text-align: right; }
table.data td.wrap { white-space: normal; }
table.data td.mono {
    font-family: ui-monospace, "SF Mono", monospace;
    font-size: 12px;
    color: var(--mid);
}

/* ======================================================================
   ADMIN — user editor (NOT a wide flat table)
   Each user is a card; edit affordances live inline.
   ====================================================================== */

.user-cards { display: grid; gap: 10px; margin-top: 12px; }
.user-card {
    background: var(--bg-2);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 14px 16px;
}
.user-card.pending { border-color: #d97706; }
.user-card-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 8px;
    flex-wrap: wrap;
}
.user-card-name { font-weight: 600; font-size: 15px; }
.user-card-email { color: var(--mid); font-size: 12px; }
.user-card-meta {
    margin-top: 4px;
    color: var(--mid);
    font-size: 12px;
}
.user-card-body {
    margin-top: 12px;
    display: grid;
    grid-template-columns: 1fr;
    gap: 12px;
}
@media (min-width: 720px) {
    .user-card-body { grid-template-columns: auto 1fr auto; align-items: start; }
}
.user-card-flags { display: flex; gap: 16px; flex-wrap: wrap; }
.door-pills {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}
.door-pill {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 8px;
    background: var(--bg-3);
    border: 1px solid var(--line);
    border-radius: 999px;
    font-size: 12px;
    cursor: pointer;
    user-select: none;
}
.door-pill input { width: 14px; height: 14px; }
.door-pill.on { background: #1e3a5f; border-color: #2563eb; }
.user-card-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    align-items: center;
}

/* ======================================================================
   ADMIN — door editor as cards (mobile-friendly)
   ====================================================================== */

.door-card {
    background: var(--bg-2);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 14px 16px;
    display: grid;
    gap: 10px;
}
.door-card.disabled { opacity: 0.55; }
.door-card-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    flex-wrap: wrap;
}
.door-card-name { font-weight: 600; font-size: 15px; }
.door-card-id {
    font-family: ui-monospace, "SF Mono", monospace;
    font-size: 11px;
    color: var(--dim);
}
.door-card-body {
    display: grid;
    grid-template-columns: 1fr;
    gap: 10px;
}
@media (min-width: 720px) {
    .door-card-body { grid-template-columns: 2fr 1fr auto auto; align-items: end; }
}
.door-card-actions { display: flex; justify-content: flex-end; align-items: end; }

/* ======================================================================
   TOASTS
   ====================================================================== */

#toasts {
    position: fixed;
    top: 64px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 100;
    display: flex;
    flex-direction: column;
    gap: 6px;
    pointer-events: none;
}
.toast {
    background: var(--bg-2);
    border: 1px solid var(--line);
    color: var(--fg);
    padding: 10px 16px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.4);
    font-size: 14px;
    max-width: 90vw;
    animation: toast-in 0.2s ease-out;
}
.toast.ok  { border-color: var(--ok); }
.toast.bad { border-color: var(--bad); }
@keyframes toast-in {
    from { opacity: 0; transform: translateY(-8px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ======================================================================
   LOGIN / REGISTER CARD
   ====================================================================== */

.card {
    max-width: 440px;
    margin: 40px auto;
    padding: 24px;
    background: var(--bg-2);
    border: 1px solid var(--line);
    border-radius: 12px;
}
.card h2 { margin-top: 0; }

/* ======================================================================
   CAMERAS
   ====================================================================== */

.cam-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 8px;
    margin-top: 12px;
}
.cam {
    position: relative;
    background: var(--bg-2);
    border: 1px solid var(--line);
    border-radius: 8px;
    overflow: hidden;
}
.cam img { width: 100%; display: block; aspect-ratio: 16/9; object-fit: cover; }
.cam .label {
    position: absolute;
    left: 6px;
    bottom: 6px;
    background: rgba(0,0,0,0.6);
    color: white;
    padding: 2px 6px;
    font-size: 12px;
    border-radius: 4px;
}

/* ======================================================================
   HELPERS
   ====================================================================== */

.hidden { display: none !important; }
.muted { color: var(--mid); }
.dim   { color: var(--dim); }
.right { text-align: right; }
.spread { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.spacer { flex: 1; }

/* The .row class was previously forcing flex:1 on every child, which
   crammed labels into thin columns. Switched to a sane gap-based flex
   that lets each child size to content. Use .row-equal for the old
   behaviour explicitly. */
.row {
    display: flex;
    gap: 12px;
    align-items: center;
    flex-wrap: wrap;
}
.row-equal > * { flex: 1; min-width: 0; }

.grid-2 {
    display: grid;
    grid-template-columns: 1fr;
    gap: 12px;
}
@media (min-width: 720px) {
    .grid-2 { grid-template-columns: 1fr 1fr; }
}

/* Bigger touch targets on small screens */
@media (max-width: 600px) {
    .btn  { padding: 12px 16px; font-size: 15px; }
}

/* Stats tiles for the admin dashboard */
.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 10px;
    margin-top: 12px;
}
.stat {
    background: var(--bg-2);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 14px;
}
.stat-num {
    font-size: 28px;
    font-weight: 600;
    line-height: 1;
    margin-bottom: 6px;
    display: flex;
    align-items: baseline;
    gap: 8px;
}
.stat-badge {
    font-size: 11px;
    background: var(--bad);
    color: white;
    padding: 2px 8px;
    border-radius: 999px;
    font-weight: 600;
    letter-spacing: 0.02em;
}
.stat-label { color: var(--mid); font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; font-weight: 600; }
a.stat-link {
    text-decoration: none;
    color: inherit;
    transition: border-color 0.15s, background 0.15s, transform 0.08s;
}
a.stat-link:hover {
    border-color: var(--accent);
    background: var(--bg-3);
    text-decoration: none;
}
a.stat-link:active { transform: scale(0.98); }

.ok-pill, .deny-pill, .err-pill {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.ok-pill   { background: #064e3b; color: #d1fae5; }
.deny-pill { background: #78350f; color: #fef3c7; }
.err-pill  { background: #7f1d1d; color: #fee2e2; }
