:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --danger: #dc2626;
    --success: #16a34a;
    --border: #e2e8f0;
    --text: #1e293b;
    --muted: #64748b;
    --bg: #f8fafc;
    --card: #ffffff;
}

html.dark-mode {
    --primary: #3b82f6;
    --primary-dark: #60a5fa;
    --danger: #f87171;
    --success: #4ade80;
    --border: #2a3947;
    --text: #eef6f9;
    --muted: #93aab3;
    --bg: #0e161d;
    --card: #16212b;
}
html.dark-mode .badge-inactive { background: #1f2a33; color: var(--muted); }
html.dark-mode input, html.dark-mode select {
    background: var(--card); color: var(--text); border-color: var(--border);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
}

/* --- Auth page --- */
/* Colours pulled from the branch site's own palette (styles.css) so the
   login screen feels like part of the same product, not a generic form. */
.auth-body {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: linear-gradient(135deg, #3faec8 0%, #55bbd3 100%);
}
.auth-card {
    background: var(--card);
    padding: 2.5rem;
    border-radius: 12px;
    border-top: 4px solid #00d4d8;
    box-shadow: 0 8px 32px rgba(10,14,26,0.18);
    width: 100%;
    max-width: 380px;
}
.auth-card h1 {
    margin-top: 0;
    font-size: 1.5rem;
    color: #0a0e1a;
}
.auth-card input[type="text"]:focus,
.auth-card input[type="password"]:focus {
    outline: 2px solid #00d4d8;
    outline-offset: 1px;
    border-color: #00d4d8;
}
#login-btn {
    background: #00d4d8;
    color: #0a0e1a;
    font-weight: 800;
    letter-spacing: 1.5px;
    text-transform: uppercase;
    font-size: 0.85rem;
    margin-top: 2.75rem;
    width: 100%;
    padding: 0.85rem 1.1rem;
}
#login-btn:hover { background: #00a8ab; }

/* --- Forms --- */
label {
    display: block;
    font-size: 0.85rem;
    font-weight: 600;
    margin: 1rem 0 0.35rem;
}
input[type="text"], input[type="password"], input[type="date"], select {
    width: 100%;
    padding: 0.6rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.95rem;
}
input:focus, select:focus {
    outline: 2px solid var(--primary);
    outline-offset: 1px;
}
.checkbox-label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: normal;
    margin-top: 1rem;
}
.checkbox-label input { width: auto; }

button {
    cursor: pointer;
    font-size: 0.9rem;
    border: none;
    border-radius: 8px;
    padding: 0.6rem 1.1rem;
}
.btn-primary {
    background: var(--primary);
    color: #fff;
    margin-top: 1.5rem;
    width: 100%;
}
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary {
    background: var(--border);
    color: var(--text);
}
.btn-link {
    color: var(--primary);
    text-decoration: none;
}
.theme-toggle-btn {
    display: inline-flex; align-items: center; justify-content: center;
    width: 32px; height: 32px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--card);
    color: var(--text);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
.theme-toggle-btn svg { width: 16px; height: 16px; }
.theme-toggle-btn .icon-moon { display: none; }
html.dark-mode .theme-toggle-btn .icon-sun { display: none; }
html.dark-mode .theme-toggle-btn .icon-moon { display: block; }

.alert {
    padding: 0.7rem 1rem;
    border-radius: 8px;
    margin-bottom: 1rem;
    font-size: 0.9rem;
}
.alert-error { background: #fee2e2; color: #991b1b; }
.alert-success { background: #dcfce7; color: #166534; }

/* --- Layout --- */
.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--card);
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border);
}
.topbar nav a {
    margin-left: 1rem;
    color: var(--muted);
    text-decoration: none;
    font-size: 0.9rem;
}
.topbar nav a:hover { color: var(--primary); }
.container {
    max-width: 1000px;
    margin: 2rem auto;
    padding: 0 1.5rem;
}

/* --- Table --- */
.data-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 1rem;
    background: var(--card);
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}
.data-table th, .data-table td {
    text-align: left;
    padding: 0.7rem 1rem;
    border-bottom: 1px solid var(--border);
    font-size: 0.88rem;
}
.data-table th {
    background: #f1f5f9;
    font-weight: 600;
    color: var(--muted);
}
.badge {
    display: inline-block;
    padding: 0.2rem 0.6rem;
    border-radius: 999px;
    font-size: 0.78rem;
    font-weight: 600;
}
.badge-active { background: #dcfce7; color: #166534; }
.badge-inactive { background: #f1f5f9; color: var(--muted); }
.badge-success { background: #dcfce7; color: #166534; }
.badge-fail { background: #fee2e2; color: #991b1b; }

/* --- Modal --- */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 50;
}
.modal-content {
    background: var(--card);
    padding: 2rem;
    border-radius: 12px;
    width: 100%;
    max-width: 420px;
}
.modal-actions {
    display: flex;
    gap: 0.6rem;
    justify-content: flex-end;
    margin-top: 1.5rem;
}
/* Prevent .btn-primary's own "width: 100%" from making Save dominate
   the row and squashing Cancel — give both buttons equal flex space. */
.modal-actions button {
    flex: 1;
    width: auto;
    margin-top: 0;
}
.sheet-handle { display: none; }

/* --- Filters --- */
.filters {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin: 1rem 0;
}
.filters input, .filters select { width: auto; }

.pagination {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-top: 1rem;
}

.action-link {
    color: var(--primary);
    cursor: pointer;
    text-decoration: none;
    font-size: 0.85rem;
    margin-right: 0.75rem;
}
.action-link.danger { color: var(--danger); }

/* --- Branch picker (dashboard/landing page) --- */
.branch-picker {
    margin-top: 2rem;
    padding: 1.25rem;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    max-width: 420px;
}
.branch-picker label { margin-top: 0; }
.branch-picker select { margin-bottom: 0.25rem; }
.branch-picker .btn-primary { width: auto; margin-top: 1rem; }

/* ════════════════════════════════════════════
   RESPONSIVE — admin pages (Users / Login Log)
════════════════════════════════════════════ */
@media (max-width: 720px) {
    .topbar {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.6rem;
        padding: 1rem;
    }
    .topbar nav {
        display: flex;
        flex-wrap: wrap;
        gap: 0.25rem 1rem;
    }
    .topbar nav a { margin-left: 0; }
    .topbar span {
        display: flex;
        align-items: center;
        gap: 0.4rem;
        font-size: 0.85rem;
    }
    .container {
        margin: 1rem auto;
        padding: 0 1rem;
    }
    #new-user-btn { width: 100%; }
    .filters {
        flex-direction: column;
        align-items: stretch;
    }
    .filters input, .filters select, .filters button { width: 100%; }
    .pagination {
        flex-wrap: wrap;
        justify-content: center;
    }
}

@media (max-width: 640px) {
    /* Turn the data table into a stack of cards: each row becomes a
       block, each cell becomes a labelled line (label from data-label). */
    .data-table thead { display: none; }
    .data-table, .data-table tbody, .data-table tr, .data-table td {
        display: block;
        width: 100%;
    }
    .data-table {
        border-radius: 10px;
        box-shadow: none;
        background: none;
    }
    .data-table tr {
        background: var(--card);
        border: 1px solid var(--border);
        border-radius: 10px;
        margin-bottom: 0.75rem;
        padding: 0.4rem 0;
        box-shadow: 0 1px 3px rgba(0,0,0,0.06);
    }
    .data-table td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 1rem;
        text-align: right;
        border-bottom: 1px solid var(--border);
        white-space: normal !important;
        max-width: none !important;
        overflow: visible !important;
        text-overflow: clip !important;
    }
    .data-table td:last-child { border-bottom: none; }
    .data-table td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--muted);
        text-align: left;
        flex-shrink: 0;
    }
    .data-table td[data-label="Actions"] {
        justify-content: flex-start;
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    .data-table td[data-label="Actions"]::before { display: none; }

    /* Long values (User Agent, Landing Page) don't fit next to their
       label on one line — stack the value underneath instead, using
       the same font/colour as every other cell so it still matches. */
    .data-table td[data-label="User Agent"],
    .data-table td[data-label="Landing Page"] {
        flex-direction: column;
        align-items: flex-start;
        text-align: left;
        gap: 0.2rem;
    }
    .data-table td[data-label="User Agent"] { word-break: break-word; }
}

@media (max-width: 480px) {
    .modal-content {
        max-width: calc(100vw - 2rem);
        padding: 1.5rem;
        max-height: 90vh;
        overflow-y: auto;
    }
    .modal-actions {
        flex-direction: column-reverse;
        gap: 0.5rem;
    }
    .modal-actions button { width: 100%; flex: 0 0 auto; }
    .auth-card { padding: 1.75rem 1.5rem; }
}

/* ════════════════════════════════════════════
   iPHONE — Apple HIG adaptations
   (safe areas, 44pt touch targets, native-feeling
   segmented nav, and a bottom sheet for the modal)
════════════════════════════════════════════ */
@media (max-width: 480px) {
    body { padding-bottom: env(safe-area-inset-bottom, 0); }

    .topbar {
        padding-top: calc(1rem + env(safe-area-inset-top, 0));
        padding-left: calc(1rem + env(safe-area-inset-left, 0));
        padding-right: calc(1rem + env(safe-area-inset-right, 0));
    }
    .topbar nav {
        display: flex;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        gap: 0.4rem;
        width: 100%;
        padding-bottom: 2px;
        scrollbar-width: none;
    }
    .topbar nav::-webkit-scrollbar { display: none; }
    .topbar nav strong {
        display: flex; align-items: center;
        flex-shrink: 0; font-size: 0.95rem;
    }
    /* Segmented-control feel for the section links, closer to how
       iOS groups top-level destinations than a row of plain links. */
    .topbar nav a {
        flex-shrink: 0;
        min-height: 44px;
        display: flex; align-items: center;
        margin-left: 0;
        padding: 0 0.9rem;
        background: var(--bg);
        border-radius: 10px;
        font-weight: 600;
        color: var(--text);
        -webkit-tap-highlight-color: transparent;
    }
    .topbar nav a:active { background: var(--border); }
    .topbar span {
        width: 100%;
        justify-content: space-between;
        padding-top: 0.4rem;
        border-top: 1px solid var(--border);
        margin-top: 0.4rem;
    }
    .topbar span a.btn-link {
        min-height: 44px;
        display: flex; align-items: center;
        font-weight: 600;
    }

    /* Buttons and action links get real touch targets */
    button, .btn-primary, .btn-secondary {
        min-height: 44px;
    }
    .action-link {
        display: inline-flex;
        align-items: center;
        min-height: 44px;
        padding: 0 0.4rem;
        margin-right: 0.25rem;
    }

    /* Modal becomes an iOS-style bottom sheet: anchored to the
       bottom edge, rounded top corners, drag handle, safe-area aware. */
    .modal {
        align-items: flex-end;
        background: rgba(15, 23, 42, 0.4);
    }
    .modal-content {
        max-width: 100%;
        width: 100%;
        border-radius: 20px 20px 0 0;
        padding: 0.5rem 1.25rem calc(1.25rem + env(safe-area-inset-bottom, 0));
        max-height: 85vh;
        animation: sheetUp 0.28s cubic-bezier(0.32, 0.72, 0, 1) both;
    }
    .sheet-handle {
        display: block;
        width: 36px; height: 5px;
        background: var(--border);
        border-radius: 999px;
        margin: 0.6rem auto 1rem;
    }
    @media (prefers-reduced-motion: reduce) {
        .modal-content { animation: none; }
    }
}

@keyframes sheetUp {
    from { transform: translateY(100%); }
    to   { transform: translateY(0); }
}
