/* ==========================================================================
   Facility Manager: glassmorphism design system
   Plain CSS3, no build step. Tokens first, then base, layout, components.
   ========================================================================== */

:root {
    /* Colour tokens */
    --bg-1: #dbeafe;
    --bg-2: #e0f2fe;
    --bg-3: #ede9fe;
    --ink: #0f172a;
    --ink-soft: #334155;
    --muted: #475569;
    --line: rgba(15, 23, 42, 0.12);
    --glass: rgba(255, 255, 255, 0.62);
    --glass-strong: rgba(255, 255, 255, 0.82);
    --glass-border: rgba(255, 255, 255, 0.75);
    --shadow: 0 8px 32px rgba(31, 41, 90, 0.12);
    --side-bg: rgba(15, 23, 42, 0.78);
    --side-ink: #e2e8f0;

    --primary: #1d4ed8;
    --primary-hover: #1e40af;
    --primary-soft: rgba(29, 78, 216, 0.12);
    --success: #15803d;
    --success-soft: rgba(21, 128, 61, 0.14);
    --warning: #b45309;
    --warning-soft: rgba(180, 83, 9, 0.15);
    --danger: #b91c1c;
    --danger-soft: rgba(185, 28, 28, 0.13);
    --info: #0369a1;
    --info-soft: rgba(3, 105, 161, 0.13);
    --neutral-soft: rgba(71, 85, 105, 0.14);

    /* Shape and spacing tokens */
    --radius: 16px;
    --radius-sm: 10px;
    --gap: 1rem;
    --side-w: 252px;

    /* Type */
    --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;

    /* Motion */
    --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
}

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

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

body {
    margin: 0;
    min-height: 100vh;
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.5;
    color: var(--ink);
    background:
        radial-gradient(900px 600px at 8% -10%, rgba(99, 102, 241, 0.28), transparent 60%),
        radial-gradient(800px 600px at 100% 0%, rgba(34, 211, 238, 0.28), transparent 60%),
        radial-gradient(900px 700px at 60% 110%, rgba(236, 72, 153, 0.18), transparent 60%),
        linear-gradient(135deg, var(--bg-1), var(--bg-2) 45%, var(--bg-3));
    background-attachment: fixed;
}

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

h1, h2, h3, h4 { line-height: 1.25; margin: 0 0 0.5rem; color: var(--ink); }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 0.75rem; }

:focus-visible { outline: 3px solid rgba(29, 78, 216, 0.55); outline-offset: 2px; border-radius: 6px; }

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.right { text-align: right; }
.center { text-align: center; }
.nowrap { white-space: nowrap; }
.mono { font-family: var(--mono); }
.mt { margin-top: var(--gap); }
.mb { margin-bottom: var(--gap); }
.hidden { display: none !important; }

/* --------------------------------------------------------------------------
   Layout: sidebar + content
   -------------------------------------------------------------------------- */

.app { display: grid; grid-template-columns: var(--side-w) minmax(0, 1fr); min-height: 100vh; }

.sidebar {
    position: sticky; top: 0; height: 100vh; overflow-y: auto;
    padding: 1.25rem 0.9rem;
    background: var(--side-bg);
    -webkit-backdrop-filter: blur(18px) saturate(140%);
    backdrop-filter: blur(18px) saturate(140%);
    color: var(--side-ink);
    border-right: 1px solid rgba(255, 255, 255, 0.12);
}

.brand { display: flex; align-items: center; gap: 0.6rem; padding: 0.25rem 0.5rem 1.1rem; color: #fff; font-weight: 700; font-size: 1.05rem; }
.brand-mark { width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; background: linear-gradient(135deg, #6366f1, #22d3ee); color: #fff; font-weight: 800; }

.nav-group { margin-top: 1rem; }
.nav-title { padding: 0 0.6rem 0.3rem; font-size: 0.72rem; letter-spacing: 0.08em; text-transform: uppercase; color: rgba(226, 232, 240, 0.6); }
.nav a {
    display: flex; align-items: center; gap: 0.6rem;
    padding: 0.5rem 0.65rem; margin-bottom: 2px;
    color: var(--side-ink); border-radius: var(--radius-sm);
    transition: background 0.15s var(--ease);
}
.nav a:hover { background: rgba(255, 255, 255, 0.1); text-decoration: none; }
.nav a.active { background: rgba(255, 255, 255, 0.18); color: #fff; font-weight: 600; }

.main { min-width: 0; display: flex; flex-direction: column; }

.topbar {
    display: flex; align-items: center; justify-content: space-between; gap: 1rem;
    padding: 0.75rem 1.5rem;
    background: var(--glass);
    -webkit-backdrop-filter: blur(14px);
    backdrop-filter: blur(14px);
    border-bottom: 1px solid var(--glass-border);
    position: sticky; top: 0; z-index: 20;
}
.topbar .who { display: flex; align-items: center; gap: 0.75rem; font-size: 0.9rem; }
.menu-btn { display: none; }

.content { padding: 1.5rem; max-width: 1280px; width: 100%; margin: 0 auto; }

.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.1rem; }
.page-head p { margin: 0; color: var(--muted); }
.actions { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; }

/* --------------------------------------------------------------------------
   Glass surfaces
   -------------------------------------------------------------------------- */

.card, .glass {
    background: var(--glass);
    -webkit-backdrop-filter: blur(16px) saturate(150%);
    backdrop-filter: blur(16px) saturate(150%);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}
.card { padding: 1.15rem 1.25rem; margin-bottom: var(--gap); }
.card > h2:first-child, .card > h3:first-child { margin-bottom: 0.75rem; }

@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
    .card, .glass, .topbar { background: var(--glass-strong); }
    .sidebar { background: #0f172a; }
}

.grid { display: grid; gap: var(--gap); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid > .card { margin-bottom: 0; }

.stat { padding: 1rem 1.1rem; }
.stat .label { font-size: 0.8rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.stat .value { font-size: 1.55rem; font-weight: 700; margin-top: 0.15rem; word-break: break-word; }
.stat .hint { font-size: 0.82rem; color: var(--muted); margin-top: 0.15rem; }
.stat.tone-danger .value { color: var(--danger); }
.stat.tone-success .value { color: var(--success); }
.stat.tone-warning .value { color: var(--warning); }

dl.facts { display: grid; grid-template-columns: max-content 1fr; gap: 0.35rem 1.25rem; margin: 0; }
dl.facts dt { color: var(--muted); }
dl.facts dd { margin: 0; word-break: break-word; }

/* --------------------------------------------------------------------------
   Buttons
   -------------------------------------------------------------------------- */

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem;
    padding: 0.5rem 0.95rem; min-height: 38px;
    font: inherit; font-weight: 600; font-size: 0.92rem; line-height: 1.2;
    color: var(--ink); background: var(--glass-strong);
    border: 1px solid var(--line); border-radius: var(--radius-sm);
    cursor: pointer; text-decoration: none;
    transition: transform 0.12s var(--ease), background 0.15s var(--ease), box-shadow 0.15s var(--ease);
}
.btn:hover { text-decoration: none; background: #fff; box-shadow: 0 4px 14px rgba(15, 23, 42, 0.1); }
.btn:active { transform: translateY(1px); }
.btn[disabled], .btn.is-busy { opacity: 0.6; cursor: not-allowed; }
.btn-primary { color: #fff; background: var(--primary); border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); color: #fff; }
.btn-danger { color: #fff; background: var(--danger); border-color: var(--danger); }
.btn-danger:hover { background: #991b1b; color: #fff; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--primary); }
.btn-sm { min-height: 32px; padding: 0.3rem 0.65rem; font-size: 0.85rem; }

/* --------------------------------------------------------------------------
   Forms
   -------------------------------------------------------------------------- */

.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem 1.25rem; }
.form-grid .full { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: 0.3rem; min-width: 0; }
.field label, .field .label { font-weight: 600; font-size: 0.88rem; }
.field .req { color: var(--danger); }
.field .help { font-size: 0.8rem; color: var(--muted); }
.field .error { font-size: 0.83rem; color: var(--danger); font-weight: 600; }

input[type="text"], input[type="email"], input[type="password"], input[type="number"], input[type="date"],
input[type="search"], input[type="tel"], select, textarea {
    width: 100%; min-height: 40px;
    padding: 0.5rem 0.7rem;
    font: inherit; color: var(--ink);
    background: rgba(255, 255, 255, 0.85);
    border: 1px solid rgba(15, 23, 42, 0.22);
    border-radius: var(--radius-sm);
    transition: border-color 0.15s var(--ease), box-shadow 0.15s var(--ease);
}
textarea { min-height: 84px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(29, 78, 216, 0.2); }
input[readonly], select[disabled], input[disabled] { background: rgba(226, 232, 240, 0.7); color: var(--muted); }
.field.has-error input, .field.has-error select, .field.has-error textarea { border-color: var(--danger); }

.check { display: flex; align-items: center; gap: 0.5rem; font-weight: 500; }
.check input { width: 18px; height: 18px; accent-color: var(--primary); }

.form-actions { display: flex; gap: 0.6rem; flex-wrap: wrap; margin-top: 1.25rem; }

/* Filters bar */
.filters { display: flex; flex-wrap: wrap; gap: 0.6rem; align-items: flex-end; margin-bottom: 1rem; }
.filters .field { min-width: 150px; flex: 1 1 150px; }
.filters .field.grow { flex: 2 1 220px; }

/* --------------------------------------------------------------------------
   Tables
   -------------------------------------------------------------------------- */

.table-wrap { overflow-x: auto; border-radius: var(--radius-sm); }
table.table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
.table th, .table td { padding: 0.65rem 0.75rem; text-align: left; vertical-align: top; border-bottom: 1px solid var(--line); }
.table th { font-size: 0.76rem; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); background: rgba(255, 255, 255, 0.45); white-space: nowrap; }
.table tbody tr:hover { background: rgba(255, 255, 255, 0.5); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table tfoot td { font-weight: 700; background: rgba(255, 255, 255, 0.45); }
.table tr.row-muted td { color: var(--muted); }

/* --------------------------------------------------------------------------
   Badges, alerts, toasts
   -------------------------------------------------------------------------- */

.badge { display: inline-block; padding: 0.15rem 0.6rem; font-size: 0.78rem; font-weight: 700; border-radius: 999px; text-transform: capitalize; white-space: nowrap; }
.badge-success { color: var(--success); background: var(--success-soft); }
.badge-warning { color: var(--warning); background: var(--warning-soft); }
.badge-danger { color: var(--danger); background: var(--danger-soft); }
.badge-info { color: var(--info); background: var(--info-soft); }
.badge-neutral { color: var(--ink-soft); background: var(--neutral-soft); }

.alert { padding: 0.75rem 1rem; border-radius: var(--radius-sm); margin-bottom: 0.75rem; border: 1px solid transparent; font-weight: 500; }
.alert-success { background: #dcfce7; border-color: #86efac; color: #14532d; }
.alert-error { background: #fee2e2; border-color: #fca5a5; color: #7f1d1d; }
.alert-warning { background: #fef3c7; border-color: #fcd34d; color: #78350f; }
.alert-info { background: #e0f2fe; border-color: #7dd3fc; color: #0c4a6e; }
.alert ul { margin: 0.25rem 0 0; padding-left: 1.2rem; }

#toasts { position: fixed; top: 70px; right: 1rem; z-index: 60; display: grid; gap: 0.5rem; width: min(420px, calc(100vw - 2rem)); }
#toasts .alert { margin: 0; display: flex; gap: 0.75rem; justify-content: space-between; align-items: flex-start; box-shadow: var(--shadow); animation: toast-in 0.25s var(--ease); }
#toasts .alert button { background: none; border: 0; font-size: 1.2rem; line-height: 1; cursor: pointer; color: inherit; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: none; } }

.empty { padding: 2rem 1rem; text-align: center; color: var(--muted); }
.empty strong { display: block; color: var(--ink); margin-bottom: 0.25rem; }

/* Pagination */
.pager { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-top: 1rem; font-size: 0.9rem; color: var(--muted); }

/* --------------------------------------------------------------------------
   Dialog (confirm with optional reason)
   -------------------------------------------------------------------------- */

dialog { border: 1px solid var(--glass-border); border-radius: var(--radius); padding: 1.4rem; width: min(480px, calc(100vw - 2rem)); background: rgba(255, 255, 255, 0.95); color: var(--ink); box-shadow: 0 24px 64px rgba(15, 23, 42, 0.35); }
dialog::backdrop { background: rgba(15, 23, 42, 0.45); -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px); }
dialog h3 { margin-bottom: 0.4rem; }

/* Invoice line editor + payment allocation */
.line-row { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 1fr) minmax(0, 1.2fr) minmax(0, 1.2fr) auto; gap: 0.6rem; align-items: end; margin-bottom: 0.6rem; }
.line-row .line-total { padding-bottom: 0.55rem; font-weight: 700; text-align: right; font-variant-numeric: tabular-nums; }
.alloc-row { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr) minmax(0, 1fr) minmax(0, 1.2fr); gap: 0.6rem; align-items: center; padding: 0.4rem 0; border-bottom: 1px solid var(--line); }
.alloc-head { font-size: 0.76rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); font-weight: 700; }

.perm-group { margin-bottom: 1.1rem; }
.perm-group h3 { margin-bottom: 0.4rem; }
.perm-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 0.4rem 1rem; }

pre.json { margin: 0; padding: 0.6rem; max-height: 220px; overflow: auto; font-size: 0.8rem; background: rgba(15, 23, 42, 0.06); border-radius: var(--radius-sm); white-space: pre-wrap; word-break: break-word; }

/* --------------------------------------------------------------------------
   Guest (sign-in) screen
   -------------------------------------------------------------------------- */

.guest { min-height: 100vh; display: grid; place-items: center; padding: 1.25rem; }
.guest-card { width: min(430px, 100%); padding: 2rem 1.75rem; }
.guest-card .brand { color: var(--ink); justify-content: center; padding-bottom: 1.25rem; font-size: 1.25rem; }

/* --------------------------------------------------------------------------
   Responsive
   -------------------------------------------------------------------------- */

@media (max-width: 1100px) {
    .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 900px) {
    .app { grid-template-columns: minmax(0, 1fr); }
    .sidebar {
        position: fixed; left: 0; top: 0; bottom: 0; width: min(290px, 86vw); z-index: 50;
        transform: translateX(-100%); transition: transform 0.25s var(--ease);
    }
    body.nav-open .sidebar { transform: none; }
    body.nav-open::after { content: ""; position: fixed; inset: 0; background: rgba(15, 23, 42, 0.4); z-index: 40; }
    .menu-btn { display: inline-flex; }
    .content { padding: 1rem; }
    .topbar { padding: 0.6rem 1rem; }
}

@media (max-width: 640px) {
    .grid-2, .grid-3, .grid-4, .form-grid { grid-template-columns: minmax(0, 1fr); }
    .line-row, .alloc-row { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
    .line-row .field:first-child, .alloc-row > :first-child { grid-column: 1 / -1; }
    dl.facts { grid-template-columns: minmax(0, 1fr); gap: 0; }
    dl.facts dt { margin-top: 0.5rem; }
    h1 { font-size: 1.35rem; }
    .topbar .who .name { display: none; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation: none !important; transition: none !important; }
}

@media print {
    body { background: #fff; }
    .sidebar, .topbar, .actions, .filters, .form-actions, #toasts, .pager { display: none !important; }
    .app { display: block; }
    .card { box-shadow: none; background: #fff; border: 1px solid #ccc; -webkit-backdrop-filter: none; backdrop-filter: none; }
}

/* Company logo */
.brand-logo { display: block; padding: 0; }
.sidebar .brand-logo { background: #fff; border-radius: 12px; padding: 0.7rem 0.75rem; margin: 0 0.25rem 1.1rem; }
.sidebar .brand-logo img { display: block; width: 100%; height: auto; max-width: 170px; margin: 0 auto; }
.guest-card .brand-logo { padding-bottom: 1rem; }
.guest-card .brand-logo img { display: block; width: 240px; max-width: 80%; height: auto; margin: 0 auto; }

/* ---- Release 2.0: notices, requests, two-factor ---- */
.bell { position: relative; }
.bell-count { display: inline-block; min-width: 1.25rem; padding: 0 .35rem; margin-left: .3rem; border-radius: 999px; background: var(--danger); color: #fff; font-size: .72rem; font-weight: 700; line-height: 1.25rem; text-align: center; }
.codes { margin: .75rem 0; padding: .9rem 1rem; border-radius: var(--radius-sm); background: var(--glass-strong); border: 1px dashed var(--line); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .95rem; line-height: 1.8; white-space: pre-wrap; user-select: all; }
.notice { padding: .8rem 1rem; border-bottom: 1px solid var(--line); }
.notice:last-child { border-bottom: 0; }
.notice.unread { background: var(--primary-soft); border-left: 3px solid var(--primary); }
.notice-head { display: flex; justify-content: space-between; gap: .75rem; align-items: baseline; font-weight: 600; }
.notice-foot { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; font-size: .85rem; color: var(--muted); margin-top: .25rem; }
.comment { padding: .7rem .9rem; margin: .6rem 0; border-radius: var(--radius-sm); background: var(--glass-strong); border: 1px solid var(--line); }
.comment.internal { background: var(--warning-soft); border-color: transparent; }
.comment .meta { font-size: .8rem; color: var(--muted); margin-bottom: .25rem; }
