/* ===================================================
   PTC Platform — Global Layout & Responsive Shell
   =================================================== */

:root,
html[data-theme="light"] {
    --bg: #f8fafc;
    --surface: #ffffff;
    --ink: #0f172a;
    --muted: #64748b;
    --line: #e2e8f0;
    --soft: #f1f5f9;
    --input-bg: #ffffff;
    --input-border: #cbd5e1;
    --brand: #0284c7;
    --brand-dark: #0369a1;
    --sidebar: #0f172a;
    --sidebar-text: #94a3b8;
    --danger: #ef4444;
    --radius: 10px;
    --nav-h: 56px;
    --sidebar-w: 240px;
    --body-text: #334155;
}

html[data-theme="dark"] {
    --bg: #0b1220;
    --surface: #111827;
    --ink: #f1f5f9;
    --muted: #94a3b8;
    --line: #334155;
    --soft: #1e293b;
    --input-bg: #0f172a;
    --input-border: #475569;
    --brand: #38bdf8;
    --brand-dark: #0ea5e9;
    --sidebar: #020617;
    --sidebar-text: #94a3b8;
    --danger: #f87171;
    --body-text: #cbd5e1;
}

*, *::before, *::after { box-sizing: border-box; }

html {
    max-width: 100%;
    overflow-x: hidden;
    -webkit-text-size-adjust: 100%;
}

body {
    margin: 0;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    color: var(--body-text);
    font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
    line-height: 1.5;
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
}

img { max-width: 100%; height: auto; display: block; }

a { color: inherit; }

/* ---------- Top bar ---------- */
.site-nav {
    position: sticky;
    top: 0;
    z-index: 1000;
    height: var(--nav-h);
    background: var(--sidebar);
    border-bottom: 1px solid #1e293b;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    gap: 12px;
}

.site-nav__left {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.site-nav__brand {
    color: #fff;
    font-size: 1.125rem;
    font-weight: 700;
    text-decoration: none;
    letter-spacing: -0.02em;
    display: inline-flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.site-nav__logo {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid #334155;
    background: #0f172a;
    flex-shrink: 0;
}

.site-nav__logo-fallback {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: #0284c7;
    color: #fff;
    font-size: 0.85rem;
    font-weight: 700;
}

.site-nav__links {
    display: flex;
    align-items: center;
    gap: 8px;
}

.site-nav__links a {
    color: #e2e8f0;
    text-decoration: none;
    font-weight: 600;
    font-size: 0.875rem;
    padding: 8px 12px;
    border-radius: 6px;
}

.site-nav__links a:hover {
    color: #fff;
    background: #1e293b;
}

.nav-toggle {
    display: none;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    border: 1px solid #334155;
    border-radius: 8px;
    background: transparent;
    color: #fff;
    cursor: pointer;
    padding: 0;
}

.nav-toggle:focus-visible {
    outline: 2px solid var(--brand);
    outline-offset: 2px;
}

.nav-toggle svg { width: 22px; height: 22px; }

/* ---------- App shell (sidebar + content) ---------- */
.dashboard-wrapper {
    display: flex;
    flex: 1 0 auto;
    width: 100%;
    min-width: 0;
    align-items: stretch;
    position: relative;
}

.main-content {
    flex: 1;
    min-width: 0;
    width: 100%;
    padding: 32px 40px;
    background: var(--surface);
}

.content-header h2 {
    margin: 0 0 4px;
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--ink);
}

.welcome {
    margin: 0 0 28px;
    font-size: 0.9375rem;
    color: var(--muted);
}

/* ---------- Sidebar ---------- */
.sidebar {
    width: var(--sidebar-w);
    flex-shrink: 0;
    background: var(--sidebar);
    padding: 16px 0;
    display: flex;
    flex-direction: column;
    align-self: stretch;
}

.sidebar-menu {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    height: 100%;
    gap: 4px;
}

.sidebar-menu li { padding: 0 12px; }

.sidebar-menu a {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 11px 14px;
    color: var(--sidebar-text);
    text-decoration: none;
    font-size: 0.875rem;
    font-weight: 500;
    border-radius: 8px;
    transition: background 0.15s ease, color 0.15s ease;
}

.sidebar-menu a:hover {
    color: #fff;
    background: #1e293b;
}

.sidebar-menu a.active {
    color: #fff;
    background: var(--brand);
}

.sidebar-menu a svg {
    flex-shrink: 0;
    width: 20px;
    height: 20px;
}

.sidebar-menu .logout-item { margin-top: auto; }

.sidebar-menu .logout-btn { color: #f87171; }

.sidebar-menu .logout-btn:hover {
    background: rgba(239, 68, 68, 0.12);
    color: var(--danger);
}

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.55);
    z-index: 900;
    border: 0;
    padding: 0;
    cursor: pointer;
}

/* ---------- Stats / cards shared ---------- */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 16px;
}

.stat-card {
    background: var(--bg);
    border: 1px solid var(--line);
    padding: 22px;
    border-radius: var(--radius);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.06);
}

.stat-label {
    display: block;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin-bottom: 8px;
}

.stat-card .balance {
    margin: 0;
    font-size: 1.75rem;
    font-weight: 700;
    color: #16a34a;
}

.stat-value {
    margin: 0;
    font-size: 1rem;
    font-weight: 600;
    color: var(--ink);
    word-break: break-word;
}

/* ---------- Forms / alerts ---------- */
.container {
    width: 100%;
    max-width: 1100px;
    margin: 0 auto;
    padding: 24px 16px;
}

.form-container {
    width: 100%;
    max-width: 440px;
    margin: 40px auto;
    padding: 28px 24px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 12px;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.04);
}

.form-container h2 {
    margin: 0 0 20px;
    text-align: center;
    color: var(--ink);
    font-size: 1.375rem;
}

.form-group { margin-bottom: 16px; }

.form-group label {
    display: block;
    margin-bottom: 6px;
    font-size: 0.8125rem;
    font-weight: 600;
    color: var(--muted);
}

.form-group input,
.form-group select,
.form-group textarea {
    width: 100%;
    padding: 11px 14px;
    border: 1px solid var(--input-border);
    border-radius: 6px;
    font-size: 1rem;
    outline: none;
    background: var(--input-bg);
    color: var(--ink);
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
    border-color: #38bdf8;
    box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.15);
}

.btn-submit,
.btn {
    display: inline-block;
    width: 100%;
    padding: 12px 16px;
    border: none;
    border-radius: 6px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    text-align: center;
    text-decoration: none;
}

.btn-submit {
    background: #2563eb;
    color: #fff;
}

.btn-submit:hover { background: #1d4ed8; }

.btn-primary { background: #2563eb; color: #fff; }
.btn-secondary { background: #64748b; color: #fff; }

.alert {
    padding: 12px 14px;
    border-radius: 6px;
    margin-bottom: 16px;
    font-size: 0.875rem;
}

.alert-success { background: #dcfce7; color: #15803d; }
.alert-danger { background: #fee2e2; color: #991b1b; }
.alert-info { background: #e0f2fe; color: #0369a1; border: 1px solid #bae6fd; }

/* ---------- Footer ---------- */
.site-footer {
    background: var(--sidebar);
    color: #fff;
    padding: 16px 20px;
    text-align: center;
    font-size: 0.8125rem;
    margin-top: auto;
    width: 100%;
}

.site-footer p { margin: 0; }

/* ---------- Toast (XSS-safe via textContent in JS) ---------- */
#toast-container {
    position: fixed;
    top: 20px;
    right: 16px;
    z-index: 99999;
    display: flex;
    flex-direction: column;
    gap: 10px;
    max-width: 380px;
    width: calc(100% - 32px);
    pointer-events: none;
}

.toast-card {
    pointer-events: auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 16px;
    border-radius: 8px;
    color: #fff;
    font-size: 0.875rem;
    font-weight: 500;
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.2);
    animation: toastIn 0.3s ease forwards;
}

.toast-error { background: #ef4444; border-left: 4px solid #991b1b; }
.toast-success { background: #10b981; border-left: 4px solid #065f46; }
.toast-warning { background: #f59e0b; border-left: 4px solid #92400e; }

.toast-close {
    background: transparent;
    border: none;
    color: #fff;
    font-size: 1.25rem;
    cursor: pointer;
    opacity: 0.85;
    line-height: 1;
    padding: 0;
}

.toast-fade-out { opacity: 0; transform: translateY(-8px); transition: all 0.25s ease; }

@keyframes toastIn {
    from { transform: translateX(40px); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}

/* ===================================================
   Mobile / tablet — off-canvas sidebar (full-width content)
   =================================================== */
@media (max-width: 900px) {
    .nav-toggle { display: inline-flex; }

    .site-nav { padding: 0 12px; }

    .site-nav__links a { padding: 8px 10px; font-size: 0.8125rem; }

    /* Block layout so closed sidebar never steals width */
    .dashboard-wrapper {
        display: block !important;
        width: 100% !important;
        max-width: 100% !important;
        overflow-x: hidden;
    }

    .sidebar-overlay {
        display: none;
        position: fixed !important;
        inset: 0;
        z-index: 900;
        width: auto !important;
        height: auto !important;
        margin: 0 !important;
        border: 0;
        background: rgba(15, 23, 42, 0.55);
        cursor: pointer;
    }

    .sidebar {
        position: fixed !important;
        top: var(--nav-h);
        bottom: 0;
        left: -280px;
        width: min(280px, 86vw) !important;
        max-width: 280px;
        z-index: 950;
        padding: 12px 0 24px;
        margin: 0 !important;
        transform: none;
        transition: left 0.25s ease;
        box-shadow: none;
        overflow-y: auto;
        align-self: auto;
    }

    body.nav-open .sidebar {
        left: 0;
        box-shadow: 8px 0 24px rgba(0, 0, 0, 0.25);
    }

    body.nav-open .sidebar-overlay {
        display: block !important;
    }

    body.nav-open {
        overflow: hidden;
    }

    .sidebar-menu {
        height: auto;
        min-height: 100%;
    }

    .sidebar-menu .logout-item { margin-top: 24px; }

    .main-content {
        display: block !important;
        width: 100% !important;
        max-width: 100% !important;
        margin: 0 !important;
        padding: 16px 14px !important;
        box-sizing: border-box;
    }

    .content-header h2 { font-size: 1.25rem; }

    .welcome { font-size: 0.8125rem; margin-bottom: 20px; }

    .stats-grid { grid-template-columns: 1fr; gap: 12px; }

    .form-container {
        margin: 24px 16px;
        padding: 22px 18px;
        width: auto;
    }

    .home-container { padding: 40px 16px !important; }
    .home-container h1 { font-size: 1.75rem !important; }
    .home-container p { font-size: 1rem !important; }
    .cta-buttons { display: flex; flex-direction: column; gap: 10px; align-items: stretch; }
    .cta-buttons .btn { margin: 0 !important; width: 100%; }
}

@media (max-width: 480px) {
    .site-nav__brand { font-size: 1rem; }

    .main-content {
        padding: 14px 12px !important;
    }
}
