:root {
    --bg: #0f172a;
    --bg-soft: #1e3a5f;
    --card: #1e293b;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --text: #f1f5f9;
    --muted: #94a3b8;
    --success: #22c55e;
    --danger: #ef4444;
    --border: #334155;
    --surface: #0f172a;
    --credential-value: #fde047;
    --card-shadow: 0 16px 40px rgba(0, 0, 0, 0.3);
    --touch-min: 48px;
    --radius: 14px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

html {
    -webkit-text-size-adjust: 100%;
    scroll-behavior: smooth;
}

body {
    margin: 0;
    font-family: "Sarabun", "Segoe UI", system-ui, -apple-system, sans-serif;
    background: linear-gradient(160deg, var(--bg) 0%, var(--bg-soft) 100%);
    background-attachment: fixed;
    color: var(--text);
    min-height: 100vh;
    min-height: 100dvh;
    line-height: 1.5;
}

/* ── Guest pages ── */
.guest-page {
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    padding:
        calc(16px + var(--safe-top))
        calc(16px + var(--safe-right))
        calc(24px + var(--safe-bottom))
        calc(16px + var(--safe-left));
}

.guest-container {
    width: 100%;
    max-width: 440px;
    margin: 0 auto;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 24px 20px;
    box-shadow: var(--card-shadow);
}

.card h1 {
    margin: 0 0 6px;
    font-size: 1.35rem;
    font-weight: 700;
    line-height: 1.3;
}

.subtitle {
    color: var(--muted);
    margin: 0 0 20px;
    font-size: 0.95rem;
}

.portal-logo {
    display: block;
    max-width: 200px;
    max-height: 96px;
    width: auto;
    height: auto;
    margin: 0 auto 16px;
    object-fit: contain;
}

.logo-preview {
    margin-bottom: 16px;
    padding: 16px;
    border: 1px dashed var(--border);
    border-radius: 12px;
    background: #0f172a;
    text-align: center;
}

.logo-preview img {
    max-width: 200px;
    max-height: 96px;
    object-fit: contain;
}

.form-group {
    margin-bottom: 16px;
}

label {
    display: block;
    margin-bottom: 8px;
    font-weight: 600;
    font-size: 0.95rem;
}

.field-hint {
    display: block;
    margin-top: 6px;
    font-size: 0.8rem;
    color: var(--muted);
}

.field-error {
    display: block;
    margin-top: 6px;
    font-size: 0.85rem;
    color: #fecaca;
}

input.input-invalid,
input.input-invalid:focus {
    border-color: var(--danger);
    box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.25);
}

.btn:disabled {
    opacity: 0.7;
    cursor: not-allowed;
}

.btn:disabled:active {
    transform: none;
}

input, select, textarea {
    width: 100%;
    min-height: var(--touch-min);
    padding: 14px 16px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--surface);
    color: var(--text);
    font-size: 16px;
    font-family: inherit;
    -webkit-appearance: none;
    appearance: none;
}

input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.3);
}

input[type="tel"],
input[inputmode="numeric"] {
    letter-spacing: 0.05em;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: var(--touch-min);
    padding: 14px 24px;
    border: none;
    border-radius: 12px;
    background: var(--accent);
    color: white;
    font-size: 1.05rem;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    text-decoration: none;
    transition: background 0.2s, transform 0.1s;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.btn:active { transform: scale(0.98); }
.btn:hover { background: var(--accent-hover); }

.btn-block { width: 100%; }

.btn-outline {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
    min-height: 40px;
    padding: 8px 14px;
    font-size: 0.85rem;
}

.btn-outline:hover { background: rgba(255, 255, 255, 0.05); }

.alert {
    padding: 14px 16px;
    border-radius: 12px;
    margin-bottom: 16px;
    font-size: 0.95rem;
    line-height: 1.45;
}

.alert-error {
    background: rgba(239, 68, 68, 0.15);
    border: 1px solid var(--danger);
    color: #fecaca;
}

.alert-success {
    background: rgba(34, 197, 94, 0.15);
    border: 1px solid var(--success);
    color: #bbf7d0;
}

.alert-info {
    background: rgba(59, 130, 246, 0.15);
    border: 1px solid var(--accent);
    color: #bfdbfe;
}

.credential-box {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 16px;
    margin: 12px 0;
}

.credential-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 8px;
}

.credential-header label {
    margin: 0;
    font-size: 0.85rem;
    color: var(--muted);
    font-weight: 500;
}

.credential-box .value {
    font-family: "SF Mono", "Consolas", monospace;
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--credential-value);
    word-break: break-all;
    line-height: 1.4;
}

.credential-box .value-lg {
    font-size: 1.5rem;
    letter-spacing: 0.08em;
}

.privacy-note {
    font-size: 0.78rem;
    color: var(--muted);
    margin-top: 16px;
    line-height: 1.55;
}

.privacy-note a { color: var(--accent); }

.form-footer {
    margin-top: 8px;
    position: sticky;
    bottom: calc(8px + var(--safe-bottom));
}

/* Loading spinner */
.spinner-wrap {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    padding: 24px 0;
}

.spinner {
    width: 48px;
    height: 48px;
    border: 4px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.copy-toast {
    position: fixed;
    bottom: calc(24px + var(--safe-bottom));
    left: 50%;
    transform: translateX(-50%) translateY(80px);
    background: var(--success);
    color: #fff;
    padding: 12px 20px;
    border-radius: 999px;
    font-size: 0.9rem;
    font-weight: 600;
    opacity: 0;
    transition: transform 0.3s, opacity 0.3s;
    z-index: 9999;
    pointer-events: none;
}

.copy-toast.show {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
}

/* ── Admin ── */
.admin-layout {
    display: flex;
    min-height: 100vh;
    min-height: 100dvh;
}

.sidebar {
    width: 260px;
    background: #0b1220;
    border-right: 1px solid var(--border);
    padding: 16px 0;
    flex-shrink: 0;
}

.sidebar .brand {
    padding: 8px 20px 16px;
    font-size: 1rem;
    font-weight: 700;
    color: var(--accent);
    border-bottom: 1px solid var(--border);
    margin-bottom: 8px;
}

.sidebar nav a {
    display: block;
    padding: 14px 20px;
    color: var(--muted);
    text-decoration: none;
    font-size: 0.95rem;
    min-height: var(--touch-min);
    display: flex;
    align-items: center;
    transition: all 0.2s;
    -webkit-tap-highlight-color: transparent;
}

.sidebar nav a:hover,
.sidebar nav a.active {
    background: rgba(59, 130, 246, 0.1);
    color: var(--text);
    border-left: 3px solid var(--accent);
}

.admin-content {
    flex: 1;
    padding: 20px 16px;
    overflow-x: hidden;
    min-width: 0;
}

.admin-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 12px;
    margin-bottom: 20px;
    flex-wrap: wrap;
}

.admin-header h1 {
    margin: 0;
    font-size: 1.35rem;
}

.admin-user {
    color: var(--muted);
    font-size: 0.85rem;
    padding: 6px 12px;
    background: var(--card);
    border-radius: 8px;
    border: 1px solid var(--border);
}

.mobile-nav-toggle {
    display: none;
    position: fixed;
    bottom: calc(16px + var(--safe-bottom));
    right: calc(16px + var(--safe-right));
    z-index: 1001;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    border: none;
    font-size: 1.4rem;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
    margin-bottom: 20px;
}

.stat-card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 16px;
}

.stat-card .number {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--accent);
}

.stat-card .label {
    color: var(--muted);
    font-size: 0.8rem;
    margin-top: 4px;
}

.table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: 12px;
    border: 1px solid var(--border);
}

table {
    width: 100%;
    border-collapse: collapse;
    background: var(--card);
    min-width: 640px;
}

th, td {
    padding: 12px 14px;
    text-align: left;
    border-bottom: 1px solid var(--border);
    font-size: 0.9rem;
}

th {
    background: #0b1220;
    color: var(--muted);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.badge {
    display: inline-block;
    padding: 4px 10px;
    border-radius: 20px;
    font-size: 0.72rem;
    font-weight: 600;
    white-space: nowrap;
}

.badge-success { background: rgba(34, 197, 94, 0.2); color: #4ade80; }
.badge-danger { background: rgba(239, 68, 68, 0.2); color: #f87171; }
.badge-muted { background: rgba(148, 163, 184, 0.15); color: var(--muted); }

.audit-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
    margin-bottom: 16px;
}

.audit-meta {
    margin: 0;
    color: var(--muted);
    font-size: 0.9rem;
}

.audit-filters {
    margin-bottom: 20px;
    max-width: none;
}

.filter-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px 16px;
}

.filter-span-2 { grid-column: 1 / -1; }

.filter-actions {
    display: flex;
    gap: 10px;
    margin-top: 16px;
    flex-wrap: wrap;
}

.pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    margin-top: 20px;
    flex-wrap: wrap;
}

.pagination-info {
    color: var(--muted);
    font-size: 0.9rem;
}

.btn-sm {
    min-height: 40px;
    padding: 8px 14px;
    font-size: 0.85rem;
}

.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #dc2626; }

.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    min-height: 100dvh;
    padding:
        calc(16px + var(--safe-top))
        calc(16px + var(--safe-right))
        calc(16px + var(--safe-bottom))
        calc(16px + var(--safe-left));
}

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

.status-dot {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    margin-right: 6px;
}

.status-active { background: var(--success); }
.status-inactive { background: var(--danger); }

pre.output {
    background: #0f172a;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 14px;
    overflow-x: auto;
    font-size: 0.8rem;
    white-space: pre-wrap;
    word-break: break-all;
}

/* Mobile guest cards for admin table alternative */
.guest-cards { display: none; }

.guest-card-item {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 16px;
    margin-bottom: 12px;
}

.guest-card-item .row {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    padding: 6px 0;
    border-bottom: 1px solid rgba(255,255,255,0.05);
    font-size: 0.9rem;
}

.guest-card-item .row:last-child { border-bottom: none; }

.guest-card-item .label {
    color: var(--muted);
    font-size: 0.8rem;
    flex-shrink: 0;
}

/* ── Tablet+ ── */
@media (min-width: 480px) {
    .guest-page { padding: 24px; }
    .card { padding: 28px 24px; }
    .card h1 { font-size: 1.5rem; }
    .stats-grid { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
}

@media (min-width: 768px) {
    .guest-container { max-width: 480px; }
    .admin-content { padding: 28px 32px; }
    .admin-header h1 { font-size: 1.6rem; }
    .stat-card .number { font-size: 2rem; }
    .filter-grid { grid-template-columns: repeat(3, 1fr); }
    .filter-span-2 { grid-column: span 2; }
}

/* ── Mobile admin nav ── */
@media (max-width: 767px) {
    .admin-layout { flex-direction: column; }

    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        width: min(280px, 85vw);
        z-index: 1000;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        overflow-y: auto;
        padding-bottom: calc(16px + var(--safe-bottom));
    }

    .sidebar.open { transform: translateX(0); }

    .sidebar-overlay {
        display: none;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 999;
    }

    .sidebar-overlay.open { display: block; }

    .mobile-nav-toggle { display: flex; align-items: center; justify-content: center; }

    .admin-content {
        padding-top: calc(12px + var(--safe-top));
        padding-bottom: calc(80px + var(--safe-bottom));
    }

    .table-wrap { display: none; }
    .guest-cards { display: block; }
}

@media (min-width: 768px) {
    .sidebar-overlay { display: none !important; }
}
