:root {
    --sidebar-bg: #0b1a3a;
    --sidebar-active: #1c3d82;
    --sidebar-hover: #14295a;
    --sidebar-text: #cbd5e8;
    --sidebar-text-muted: #8896b8;
    --sidebar-text-active: #ffffff;

    --bg: #f5f7fb;
    --card: #ffffff;
    --border: #e5eaf2;
    --border-strong: #d5dbe8;

    --text: #0f172a;
    --text-muted: #64748b;
    --text-faint: #94a3b8;

    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --primary-fg: #ffffff;
    --primary-soft: #eff5ff;
    --primary-icon: #3b82f6;

    --success: #16a34a;
    --success-soft: #dcfce7;

    --danger: #dc2626;
    --danger-soft: #fee2e2;

    --warn: #d97706;
    --warn-soft: #fef3c7;

    --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
    --shadow-card: 0 1px 3px rgba(15, 23, 42, 0.06), 0 1px 2px rgba(15, 23, 42, 0.04);
    --shadow-modal: 0 10px 30px rgba(15, 23, 42, 0.12);

    --radius-lg: 14px;
    --radius: 10px;
    --radius-sm: 8px;
    --radius-pill: 999px;

    --sidebar-width: 240px;
    --topbar-height: 72px;

    --font: -apple-system, BlinkMacSystemFont, "Segoe UI Variable", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }

html, body { margin: 0; padding: 0; height: 100%; }

body {
    font-family: var(--font);
    font-size: 14px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    /* Safety net: no matter what a page renders, the body never scrolls sideways.
       Individual scroll containers (tables, code blocks) still scroll normally. */
    overflow-x: hidden;
}

/* Long words / URLs / emails should wrap instead of blowing out cells and cards. */
.card, .data-table td, .data-table th, .action-row, .attention {
    overflow-wrap: anywhere;
    word-break: break-word;
}

#app { min-height: 100vh; }

.app-loading {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    height: 100vh; gap: 1rem; color: var(--text-muted);
}
.app-loading-spinner {
    width: 42px; height: 42px; border: 3px solid var(--border);
    border-top-color: var(--primary); border-radius: 50%;
    animation: spin 800ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

#blazor-error-ui {
    background: #fff3cd; bottom: 0; box-shadow: 0 -1px 2px rgba(0, 0, 0, .1);
    display: none; left: 0; padding: .75rem 1.25rem; position: fixed; width: 100%; z-index: 1000;
}
#blazor-error-ui .dismiss { cursor: pointer; position: absolute; right: 1rem; top: .75rem; }

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

h1, h2, h3, h4 { color: var(--text); margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.0625rem; }
h4 { font-size: 0.9375rem; }

/* ===== Layout shell ===== */

.app-shell { display: flex; min-height: 100vh; }

.sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; width: var(--sidebar-width);
    background: var(--sidebar-bg); color: var(--sidebar-text);
    display: flex; flex-direction: column; z-index: 20;
}
.sidebar-brand {
    padding: 1.5rem 1.25rem; font-size: 1.125rem; font-weight: 600; color: #fff;
    letter-spacing: -0.01em;
}
.sidebar-nav { flex: 1; padding: 0.5rem 0.75rem; display: flex; flex-direction: column; gap: 2px; }
.sidebar-nav .nav-link {
    display: flex; align-items: center; gap: 0.75rem;
    padding: 0.625rem 0.75rem; border-radius: var(--radius-sm);
    color: var(--sidebar-text); text-decoration: none;
    font-weight: 500; transition: background 120ms ease, color 120ms ease;
}
.sidebar-nav .nav-link:hover { background: var(--sidebar-hover); color: #fff; }
.sidebar-nav .nav-link.active { background: var(--primary); color: #fff; }
.sidebar-nav .nav-link svg { width: 18px; height: 18px; flex-shrink: 0; }

.sidebar-user {
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    padding: 0.875rem 1rem;
    display: flex; align-items: center; gap: 0.75rem;
}
.sidebar-user .avatar {
    width: 36px; height: 36px; border-radius: 50%;
    background: var(--primary); color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-weight: 600; font-size: 0.8125rem; flex-shrink: 0;
}
.sidebar-user .user-meta { flex: 1; min-width: 0; }
.sidebar-user .user-name { color: #fff; font-weight: 500; font-size: 0.875rem; }
.sidebar-user .user-email {
    color: var(--sidebar-text-muted); font-size: 0.75rem;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.sidebar-user .chevron { color: var(--sidebar-text-muted); }

.main { flex: 1; margin-left: var(--sidebar-width); display: flex; flex-direction: column; min-width: 0; }

.topbar {
    height: var(--topbar-height);
    background: var(--card);
    border-bottom: 1px solid var(--border);
    display: flex; align-items: center; justify-content: space-between;
    padding: 0 2rem;
    position: sticky; top: 0; z-index: 10;
}
.topbar .page-title { font-size: 1.375rem; font-weight: 600; }
.topbar-actions { display: flex; align-items: center; gap: 0.75rem; }

.page-content { padding: 2rem; max-width: 1400px; width: 100%; }

/* ===== Buttons ===== */

.btn {
    display: inline-flex; align-items: center; gap: 0.5rem;
    padding: 0.5rem 1rem; border-radius: var(--radius-pill);
    border: 1px solid transparent; background: transparent;
    font-family: inherit; font-size: 0.875rem; font-weight: 500;
    color: var(--text); cursor: pointer;
    transition: background 120ms ease, border-color 120ms ease, color 120ms ease;
    text-decoration: none;
}
.btn:hover { text-decoration: none; }
.btn svg { width: 16px; height: 16px; }

.btn-primary {
    background: var(--primary); color: #fff; border-color: var(--primary);
    box-shadow: 0 1px 2px rgba(37, 99, 235, 0.25);
}
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); color: #fff; }
.btn-primary:disabled { background: var(--border-strong); border-color: var(--border-strong); cursor: not-allowed; box-shadow: none; }

.btn-secondary { background: var(--card); border-color: var(--border-strong); color: var(--text); }
.btn-secondary:hover { background: var(--bg); border-color: var(--text-faint); }

.btn-ghost { color: var(--text-muted); }
.btn-ghost:hover { background: var(--bg); color: var(--text); }

.btn-danger-ghost { color: var(--danger); }
.btn-danger-ghost:hover { background: var(--danger-soft); }

.btn-icon {
    width: 40px; height: 40px; padding: 0; border-radius: 50%;
    display: inline-flex; align-items: center; justify-content: center;
    background: transparent; border: 1px solid transparent; color: var(--text-muted);
}
.btn-icon:hover { background: var(--bg); color: var(--text); }
.btn-icon svg { width: 20px; height: 20px; }

.btn-sm { padding: 0.375rem 0.75rem; font-size: 0.8125rem; }
.btn-link { background: none; border: none; color: var(--primary); padding: 0.25rem 0.5rem; }
.btn-link:hover { text-decoration: underline; }

/* ===== Cards ===== */

.card {
    background: var(--card); border: 1px solid var(--border);
    border-radius: var(--radius-lg); box-shadow: var(--shadow-card);
}
.card-body { padding: 1.25rem 1.5rem; }
.card-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 1.25rem 1.5rem; border-bottom: 1px solid var(--border);
}
.card-header h3 { font-size: 1rem; }

/* ===== Stat cards ===== */

.stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; margin-bottom: 1.5rem; }
.stat-card { display: flex; align-items: center; gap: 1rem; padding: 1.25rem 1.5rem; }
.stat-icon {
    width: 52px; height: 52px; border-radius: 14px;
    background: var(--primary-soft); color: var(--primary);
    display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.stat-icon svg { width: 24px; height: 24px; }
.stat-value { font-size: 1.75rem; font-weight: 600; color: var(--text); line-height: 1.1; }
.stat-label { color: var(--text-muted); font-size: 0.875rem; margin-top: 2px; }

/* ===== Two-column dashboard grid ===== */

.dash-grid { display: grid; grid-template-columns: 2fr 1fr; gap: 1.25rem; align-items: start; }
.dash-side > * + * { margin-top: 1.25rem; }

/* ===== Data table ===== */

.data-table { width: 100%; border-collapse: collapse; }
.data-table th, .data-table td {
    padding: 0.875rem 1.5rem; text-align: left; font-weight: 400; color: var(--text);
}
.data-table thead th {
    color: var(--text-muted); font-size: 0.8125rem; font-weight: 500;
    border-bottom: 1px solid var(--border);
    background: transparent;
}
.data-table tbody tr { border-bottom: 1px solid var(--border); }
.data-table tbody tr:last-child { border-bottom: none; }
.data-table tbody tr:hover { background: rgba(37, 99, 235, 0.02); }
.data-table td .cell-with-icon { display: flex; align-items: center; gap: 0.625rem; color: var(--text-muted); }
.data-table td .cell-with-icon svg { width: 16px; height: 16px; color: var(--text-faint); }

.table-empty { padding: 2.5rem 1.5rem; text-align: center; color: var(--text-muted); }

/* ===== Type pills ===== */

.pill {
    display: inline-flex; align-items: center; padding: 0.25rem 0.625rem;
    border-radius: var(--radius-pill); font-size: 0.75rem; font-weight: 500;
    background: var(--bg); color: var(--text-muted); border: 1px solid transparent;
}
.pill-rent      { background: #dcfce7; color: #15803d; }
.pill-overdue   { background: #fee2e2; color: #b91c1c; }
.pill-inspection{ background: #dbeafe; color: #1d4ed8; }
.pill-maintenance{background: #fef3c7; color: #a16207; }
.pill-rule      { background: #ede9fe; color: #6d28d9; }
.pill-cleanliness{background: #ccfbf1; color: #0f766e; }
.pill-renewal   { background: #e0e7ff; color: #4338ca; }
.pill-moveout   { background: #ffe4e6; color: #be123c; }
.pill-general   { background: #e2e8f0; color: #475569; }

.pill-status-scheduled { background: #dbeafe; color: #1d4ed8; }
.pill-status-sent      { background: #dcfce7; color: #15803d; }
.pill-status-cancelled { background: #e2e8f0; color: #475569; }

/* ===== Alert/attention card ===== */

.attention {
    text-align: center; padding: 1.5rem 1.5rem 1.75rem;
}
.attention-badge {
    width: 56px; height: 56px; border-radius: 50%;
    display: inline-flex; align-items: center; justify-content: center;
    margin-bottom: 0.75rem;
}
.attention-badge.ok    { background: var(--success-soft); color: var(--success); }
.attention-badge.warn  { background: var(--warn-soft); color: var(--warn); }
.attention-badge.alert { background: var(--danger-soft); color: var(--danger); }
.attention-badge svg   { width: 28px; height: 28px; }
.attention h4 { font-size: 1rem; }
.attention p  { color: var(--text-muted); margin: 0.25rem 0 0; font-size: 0.8125rem; }

.attention-header {
    display: flex; align-items: center; gap: 0.5rem;
    padding: 1rem 1.5rem 0; color: var(--text); font-weight: 500;
}
.attention-header svg { width: 18px; height: 18px; color: var(--warn); }

/* ===== Quick action rows ===== */

.action-list { display: flex; flex-direction: column; }
.action-row {
    display: flex; align-items: center; gap: 0.875rem;
    padding: 0.875rem 1.5rem;
    border: none; background: transparent; text-align: left;
    color: var(--text); cursor: pointer; font-family: inherit; font-size: 0.875rem;
    border-top: 1px solid var(--border); width: 100%;
    text-decoration: none;
}
.action-row:first-child { border-top: none; }
.action-row:hover { background: var(--bg); }
.action-row .icon-puck {
    width: 36px; height: 36px; border-radius: 10px;
    background: var(--primary-soft); color: var(--primary);
    display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.action-row .icon-puck svg { width: 18px; height: 18px; }
.action-row .action-label { flex: 1; }
.action-row .action-title { font-weight: 500; }
.action-row .action-sub { color: var(--text-muted); font-size: 0.75rem; }
.action-row .chevron { color: var(--text-faint); }

/* ===== Forms ===== */

.form-row { display: grid; gap: 1rem; grid-template-columns: repeat(12, 1fr); }
.form-field { display: flex; flex-direction: column; gap: 0.375rem; }
.form-field.col-6  { grid-column: span 6; }
.form-field.col-4  { grid-column: span 4; }
.form-field.col-3  { grid-column: span 3; }
.form-field.col-12 { grid-column: span 12; }
.form-label { font-size: 0.8125rem; font-weight: 500; color: var(--text); }
.form-hint  { font-size: 0.75rem; color: var(--text-muted); }
.form-control, .form-select, textarea.form-control {
    width: 100%; padding: 0.5rem 0.75rem; border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm); font-family: inherit; font-size: 0.875rem;
    background: var(--card); color: var(--text); transition: border-color 120ms ease, box-shadow 120ms ease;
}
.form-control:focus, .form-select:focus, textarea.form-control:focus {
    outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}
textarea.form-control { min-height: 90px; resize: vertical; }
.checkbox { display: inline-flex; align-items: center; gap: 0.5rem; }

/* ===== Utilities ===== */

.stack > * + * { margin-top: 1rem; }
.row-flex { display: flex; align-items: center; gap: 0.75rem; }
.row-flex.between { justify-content: space-between; }
.gap-2 { gap: 0.5rem; }
.mt-1 { margin-top: 0.5rem; } .mt-2 { margin-top: 1rem; } .mt-3 { margin-top: 1.5rem; }
.mb-0 { margin-bottom: 0; } .mb-2 { margin-bottom: 1rem; }
.text-muted { color: var(--text-muted); }
.text-faint { color: var(--text-faint); }
.hidden { display: none; }

/* ===== Auth pages ===== */

.auth-page {
    min-height: 100vh; background: var(--bg);
    display: flex; align-items: center; justify-content: center;
    padding: 1.5rem;
}
.auth-shell { width: 100%; max-width: 400px; }
.auth-card { box-shadow: 0 10px 40px rgba(15, 23, 42, 0.08); }

/* Show-on-mobile helper (hidden on desktop by default) */
.mobile-only { display: none; }

/* Backdrop shown when off-canvas sidebar is open on mobile */
.sidebar-backdrop {
    display: none; position: fixed; inset: 0; z-index: 15;
    background: rgba(15, 23, 42, 0.4); backdrop-filter: blur(2px);
}

/* Wrap wide data tables so they scroll horizontally instead of blowing out the page */
.table-scroll { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* ===== Responsive tightening ===== */

@media (max-width: 1100px) {
    .stat-grid { grid-template-columns: repeat(2, 1fr); }
    .dash-grid { grid-template-columns: 1fr; }
}

@media (max-width: 720px) {
    .mobile-only { display: inline-flex; }

    /* Off-canvas sidebar: slides in from left, backdrop behind it */
    .sidebar {
        transform: translateX(-100%); transition: transform 200ms ease;
        width: 260px;
        box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
    }
    .sidebar.open { transform: translateX(0); }
    .sidebar.open ~ .sidebar-backdrop,
    .sidebar-backdrop.open { display: block; }

    .main { margin-left: 0; }

    /* Top bar: tighter and taller for touch */
    .topbar {
        padding: 0 0.875rem;
        height: 60px;
    }
    .topbar .page-title { font-size: 1.125rem; }

    /* Icon-only Create button on mobile — label lives in the sidebar action anyway */
    .topbar-actions .btn-primary { padding: 0.5rem 0.75rem; }
    .topbar-actions .btn-primary .btn-label { display: none; }

    .page-content { padding: 1rem; }

    /* Cards are edge-to-edge on very small screens */
    .card { border-radius: 12px; }
    .card-body, .card-header { padding: 1rem 1rem; }

    .stat-grid { grid-template-columns: 1fr; gap: 0.75rem; margin-bottom: 1rem; }
    .stat-card { padding: 1rem; }
    .stat-icon { width: 44px; height: 44px; border-radius: 12px; }
    .stat-icon svg { width: 20px; height: 20px; }
    .stat-value { font-size: 1.5rem; }

    /* Forms: single-column, larger inputs for touch (44px = Apple HIG target) */
    .form-field.col-6, .form-field.col-4, .form-field.col-3 { grid-column: span 12; }
    .form-control, .form-select, textarea.form-control { padding: 0.625rem 0.75rem; font-size: 1rem; }
    .btn { padding: 0.625rem 1rem; font-size: 0.9375rem; }
    .btn-sm { padding: 0.5rem 0.875rem; font-size: 0.875rem; }
    .btn-icon { width: 44px; height: 44px; }

    /* Cards scroll horizontally on mobile to contain wide content (data tables).
       Applied to every card so it works even where `:has()` isn't supported
       (older mobile Firefox / Samsung Internet). Border-radius stays intact
       because overflow lives on the card, not the page. */
    .card { overflow-x: auto; -webkit-overflow-scrolling: touch; }
    .data-table { min-width: 560px; }
    .data-table th, .data-table td { padding: 0.75rem 1rem; }

    /* Auth pages never need horizontal scroll — keep them clean. */
    .auth-card { overflow-x: visible; }

    /* Prevent the topbar from getting too cramped: the page-title truncates
       rather than pushing the action buttons off-screen. */
    .topbar .page-title {
        overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
        min-width: 0;
    }
    .topbar > .row-flex { min-width: 0; flex: 1; }
}
