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

:root {
    --sidebar-w: 220px;
    --sidebar-bg: #1a1f2e;
    --sidebar-active-bg: #252b3d;
    --sidebar-text: #8b92a5;
    --sidebar-active: #ffffff;
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --primary-light: #eff6ff;
    --primary-ghost: #e0e7ff;
    --bg: #f8fafc;
    --surface: #ffffff;
    --surface-alt: #f1f5f9;
    --border: #e2e8f0;
    --border-light: #f1f5f9;
    --text: #1e293b;
    --text-muted: #64748b;
    --text-subtle: #94a3b8;
    --success: #22c55e;
    --success-bg: #f0fdf4;
    --danger: #ef4444;
    --danger-bg: #fef2f2;
    --warning: #f59e0b;
    --warning-bg: #fffbeb;
    --info: #3b82f6;
    --info-bg: #eff6ff;
    --radius: 8px;
    --radius-sm: 6px;
    --radius-pill: 999px;
    --radius-full: 999px;
    --shadow-sm: 0 1px 2px rgba(0,0,0,.05);
    --shadow: 0 1px 3px rgba(0,0,0,.08);
    --shadow-md: 0 4px 6px rgba(0,0,0,.07);
    --shadow-lg: 0 10px 25px rgba(0,0,0,.1);
}

html { font-size: 16px; }
body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    min-height: 100vh;
    display: flex;
    -webkit-font-smoothing: antialiased;
}

/* ---- Sidebar ---- */
.sidebar {
    width: var(--sidebar-w);
    background: var(--sidebar-bg);
    position: fixed; top:0; left:0; bottom:0;
    display: flex; flex-direction: column;
    z-index: 200;
    transition: transform .2s ease;
    overflow-y: auto;
}

.sidebar-brand {
    padding: 0 1.25rem;
    height: 64px; display: flex; align-items: center;
    font-size: 1.1rem; font-weight: 700; color: #fff;
    border-bottom: 1px solid rgba(255,255,255,.06);
    text-decoration: none; letter-spacing: -0.02em;
}

.sidebar-nav { flex:1; padding: .5rem .625rem; }

.sidebar-nav a {
    display: flex; align-items: center; gap: .6rem;
    padding: .5rem .75rem; margin-bottom: 1px;
    color: var(--sidebar-text); text-decoration: none;
    font-size: .875rem; font-weight: 500; border-radius: var(--radius-sm);
    transition: all .12s;
}
.sidebar-nav a:hover { background: var(--sidebar-active-bg); color: var(--sidebar-active); }
.sidebar-nav a.active { background: rgba(37,99,235,.15); color: #fff; border-left: 3px solid var(--primary); margin-left: -3px; }

.sidebar-section {
    color: var(--sidebar-text); font-size: .675rem; text-transform: uppercase;
    letter-spacing: .8px; padding: 1rem .75rem .35rem; font-weight: 600; opacity: .5;
}

.sidebar-footer {
    padding: .625rem .75rem; border-top: 1px solid rgba(255,255,255,.06);
}
.sidebar-footer a { color: var(--sidebar-text); text-decoration: none; font-size: .8rem; padding: .4rem .5rem; display: block; border-radius: var(--radius-sm); }
.sidebar-footer a:hover { background: var(--sidebar-active-bg); color: var(--sidebar-active); }

/* ---- Main Content ---- */
.main { margin-left: var(--sidebar-w); flex: 1; min-width: 0; }

.topbar {
    background: var(--surface); border-bottom: 1px solid var(--border);
    height: 64px; display: flex; align-items: center; padding: 0 1.5rem; gap: 1rem;
    position: sticky; top: 0; z-index: 100;
    box-shadow: var(--shadow-sm);
}
.topbar-spacer { flex: 1; }

.hamburger {
    display: none; background: none; border: none; cursor: pointer;
    padding: .375rem; color: var(--text); border-radius: var(--radius-sm);
}
.hamburger:hover { background: var(--bg); }
.hamburger svg { width: 22px; height: 22px; }

.content { padding: 1.5rem 2rem; max-width: 1400px; }

/* ---- Cards ---- */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    padding: 1.25rem;
    margin-bottom: 1rem;
}
.card-header {
    display: flex; justify-content: space-between; align-items: center;
    margin-bottom: .75rem; padding-bottom: .75rem;
    border-bottom: 1px solid var(--border-light);
}
.card-header h2 { font-size: 1.125rem; font-weight: 600; letter-spacing: -0.01em; }

/* ---- Typography ---- */
h1 { font-size: 1.5rem; font-weight: 700; color: var(--text); margin-bottom: 1rem; letter-spacing: -0.02em; }
h2 { font-size: 1.125rem; font-weight: 600; }
h3 { font-size: 1rem; font-weight: 600; }
.text-muted { color: var(--text-muted); }
.text-sm { font-size: .75rem; }

/* ---- Tables ---- */
table { width: 100%; border-collapse: collapse; font-size: .875rem; }
th, td { padding: .625rem .75rem; text-align: left; border-bottom: 1px solid var(--border-light); }
th { font-weight: 600; background: var(--surface-alt); font-size: .7rem; text-transform: uppercase; letter-spacing: .6px; color: var(--text-muted); }
tr:last-child td { border-bottom: none; }
tr:hover td { background: #f8fafc; }

/* ---- Buttons ---- */
.btn {
    display: inline-flex; align-items: center; gap: .35rem;
    padding: .5rem .875rem; font-size: .85rem; font-weight: 500;
    border: 1px solid var(--border); border-radius: var(--radius-sm);
    background: var(--surface); color: var(--text);
    text-decoration: none; cursor: pointer; transition: all .12s;
    white-space: nowrap;
}
.btn:hover { background: var(--bg); border-color: var(--text-subtle); }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); font-weight: 600; }
.btn-primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-danger:hover { background: #dc2626; }
.btn-success { background: var(--success); color: #fff; border-color: var(--success); font-weight: 600; }
.btn-success:hover { background: #16a34a; }
.btn-sm { padding: .25rem .55rem; font-size: .775rem; }
.btn-group { display: flex; gap: .4rem; flex-wrap: wrap; }

/* ---- Forms ---- */
.form-group { margin-bottom: .9rem; }
.form-group label { display: block; font-weight: 500; margin-bottom: .3rem; font-size: .85rem; color: var(--text); }
.form-control {
    width: 100%; padding: .5rem .7rem; font-size: .875rem;
    border: 1px solid var(--border); border-radius: var(--radius-sm);
    background: var(--surface); color: var(--text); font-family: inherit;
    transition: border-color .15s, box-shadow .15s;
}
.form-control:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.1); }
textarea.form-control { min-height: 80px; resize: vertical; }
select.form-control { appearance: auto; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
.form-check { display: flex; align-items: center; gap: .4rem; margin-bottom: .35rem; font-size: .85rem; }
.form-check input[type="checkbox"] { width: 1rem; height: 1rem; accent-color: var(--primary); }

/* ---- Badges ---- */
.badge {
    display: inline-flex; align-items: center;
    padding: .15rem .55rem; font-size: 0.68rem; font-weight: 600;
    border-radius: var(--radius-pill); text-transform: uppercase;
    letter-spacing: .4px; line-height: 1.4;
}
.badge-draft { background: #f1f5f9; color: #475569; }
.badge-sent { background: #dbeafe; color: #1d4ed8; }
.badge-paid, .badge-success { background: #dcfce7; color: #166534; }
.badge-overdue, .badge-danger { background: #fef2f2; color: #991b1b; }
.badge-warning { background: #fffbeb; color: #92400e; }
.badge-info { background: #e0f2fe; color: #0369a1; }
.badge-primary { background: #dbeafe; color: #1e40af; }

/* ---- Flash Messages ---- */
.flash { padding: .6rem 1rem; border-radius: var(--radius-sm); margin-bottom: .5rem; font-size: .85rem; font-weight: 500; }
.flash-success { background: var(--success-bg); color: #166534; border: 1px solid #bbf7d0; }
.flash-danger { background: var(--danger-bg); color: #991b1b; border: 1px solid #fecaca; }
.flash-warning { background: var(--warning-bg); color: #92400e; border: 1px solid #fde68a; }
.flash-info { background: var(--info-bg); color: #1e40af; border: 1px solid #bfdbfe; }

/* ---- Misc ---- */
.inline-form { display: inline; }
.mt-1 { margin-top: .5rem; } .mt-2 { margin-top: 1rem; } .mt-3 { margin-top: 1.5rem; }
.mb-1 { margin-bottom: .5rem; } .mb-2 { margin-bottom: 1rem; }

.grid-2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: .75rem; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: .75rem; }

.well {
    background: var(--bg); border: 1px solid var(--border-light);
    border-radius: var(--radius-sm); padding: 1rem; margin-bottom: .75rem;
}
.well h3 { font-size: .95rem; margin-bottom: .4rem; }

.login-container { max-width: 420px; margin: 4rem auto; padding: 0 1rem; }

/* ---- Stat Cards ---- */
.stat-card {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); box-shadow: var(--shadow-sm);
    padding: 1.25rem; text-align: center; text-decoration: none; color: var(--text);
    transition: transform .15s, box-shadow .15s;
}
.stat-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-md); }
.stat-num { font-size: 2rem; font-weight: 700; letter-spacing: -0.03em; }
.stat-label { font-size: .78rem; color: var(--text-muted); margin-top: .25rem; font-weight: 500; }

/* ---- Site image in list ---- */
.site-image {
    width: 100%; max-height: 180px; object-fit: cover;
    border-radius: var(--radius-sm); border: 1px solid var(--border); margin-bottom: .5rem;
}

/* ---- Chat ---- */
.chat-layout { display: flex; gap: 0; height: calc(100vh - 64px); margin: -1.5rem -2rem; }
.chat-sidebar {
    width: 280px; min-width: 220px; background: var(--surface);
    border-right: 1px solid var(--border); display: flex; flex-direction: column;
}
.chat-sidebar-header { padding: 1rem 1.25rem; border-bottom: 1px solid var(--border); }
.chat-sidebar-list { flex: 1; overflow-y: auto; padding: .5rem; }
.chat-room-item {
    display: flex; align-items: center; gap: .75rem; padding: .65rem .75rem;
    border-radius: var(--radius-sm); cursor: pointer; text-decoration: none; color: var(--text);
    transition: background .1s;
}
.chat-room-item:hover { background: var(--bg); }
.chat-room-item .avatar {
    width: 40px; height: 40px; border-radius: 50%; background: var(--primary);
    color: #fff; display: flex; align-items: center; justify-content: center;
    font-weight: 700; font-size: .9rem; flex-shrink: 0;
}
.chat-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-main-header { padding: .75rem 1.25rem; border-bottom: 1px solid var(--border); background: var(--surface); }
.chat-messages { flex: 1; overflow-y: auto; padding: 1rem; background: var(--bg); display: flex; flex-direction: column; gap: .35rem; }
.chat-message { max-width: 72%; display: flex; flex-direction: column; }
.chat-message.mine { align-self: flex-end; }
.chat-message .bubble {
    padding: .55rem .9rem; border-radius: 12px; font-size: .875rem; line-height: 1.45;
    word-wrap: break-word;
}
.chat-message.mine .bubble { background: var(--primary); color: #fff; border-bottom-right-radius: 4px; }
.chat-message:not(.mine) .bubble { background: var(--surface); border: 1px solid var(--border-light); border-bottom-left-radius: 4px; }
.chat-message .meta { font-size: .7rem; color: var(--text-muted); padding: 0 .3rem; margin-top: 2px; }
.chat-message.mine .meta { text-align: right; }
.chat-input-bar { padding: .75rem 1rem; border-top: 1px solid var(--border); background: var(--surface); display: flex; gap: .5rem; }

/* ---- Modal ---- */
.modal-overlay {
    display: none; position: fixed; inset: 0; background: rgba(0,0,0,.45);
    z-index: 1000; align-items: flex-start; justify-content: center; padding-top: 2rem;
}
.modal-overlay.active { display: flex; }
.modal-box {
    background: var(--surface); border-radius: var(--radius);
    box-shadow: var(--shadow-lg); width: 92%; max-width: 700px;
    max-height: 90vh; overflow-y: auto; padding: 1.5rem;
}

/* Calendar responsive views */
.calendar-desktop { display: block; }
.calendar-mobile { display: none; }

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

@media (max-width: 1024px) {
    .chat-sidebar { width: 240px; }
    .content { padding: 1.25rem; }
}

@media (max-width: 768px) {
    .sidebar { transform: translateX(-100%); }
    .sidebar.open { transform: translateX(0); box-shadow: var(--shadow-lg); }
    .main { margin-left: 0 !important; }
    .hamburger { display: block; }
    .form-row { grid-template-columns: 1fr; }
    .grid-3, .grid-2 { grid-template-columns: 1fr; }
    .content { padding: .75rem; }
    .topbar { padding: 0 .75rem; height: 56px; }
    .card { padding: .85rem; }
    .card-header { flex-wrap: wrap; gap: .5rem; }
    .card-header h2 { font-size: 1rem; }

    /* Tables: horizontal scroll with sticky left column */
    .table-responsive { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
    table { font-size: .8rem; }
    th, td { padding: .45rem .5rem; white-space: nowrap; }
    th { font-size: .65rem; }

    /* Chat: stack vertically */
    .chat-layout { flex-direction: column; margin: -.75rem; }
    .chat-sidebar { width: 100%; max-height: 180px; border-right: none; border-bottom: 1px solid var(--border); flex-shrink: 0; }
    .chat-layout .chat-main { height: calc(100vh - 290px); min-height: 400px; }
    .chat-messages { padding: .5rem; }
    .chat-message { max-width: 85%; }
    .chat-input-bar { padding: .5rem; gap: .3rem; }

    /* Calendar: convert month grid to compact list */
    .calendar-desktop { display: none; }
    .calendar-mobile { display: block; }

    /* Site modal: full width */
    .modal-box { width: 98%; max-width: 98%; border-radius: var(--radius-sm); padding: 1rem; max-height: 95vh; }

    /* Shift grid mobile cards already handled by shift-grid-desktop/mobile classes */

    /* Forms */
    .well { padding: .65rem; }

    /* Invoices: better mobile table */
    .inv-check { width: 18px; height: 18px; }

    /* Entity sites table */
    .btn-group { gap: .3rem; }
    .btn { padding: .4rem .65rem; font-size: .8rem; }
    .btn-sm { padding: .25rem .45rem; font-size: .7rem; }

    /* Sidebar brand */
    .sidebar-brand { height: 56px; }

    /* Topbar */
    .topbar form select { font-size: .7rem; max-width: 120px; }

    /* Chat widget on mobile */
    #chat-widget-container { bottom: 12px; right: 12px; }
    .chat-fab { width: 46px; height: 46px; font-size: 1.1rem; }
    .chat-panel { width: calc(100vw - 24px); right: 0; }
}

@media (max-width: 480px) {
    .content { padding: .5rem; }
    .btn-group { flex-direction: column; align-items: stretch; }
    .btn { justify-content: center; text-align: center; padding: .5rem .75rem; min-height: 44px; }
    .btn-sm { min-height: auto; }

    /* Dashboard stats */
    .stat-card { padding: .8rem; }
    .stat-num { font-size: 1.5rem; }

    /* Cards */
    .card { padding: .75rem; margin-bottom: .5rem; }
    h1 { font-size: 1.25rem; }
    .card-header h2 { font-size: .95rem; }

    /* Table on mobile */
    table { font-size: .75rem; }
    th, td { padding: .35rem .4rem; }

    /* Modal */
    .modal-box { padding: .75rem; }

    /* Calendar month names */
    .card table[style*="table-layout:fixed"] th,
    .card table[style*="table-layout:fixed"] td {
        font-size: .55rem; padding: .1rem;
        min-width: 0;
    }

    /* Chat widget smaller */
    .chat-fab { width: 42px; height: 42px; font-size: 1rem; }
    .chat-panel { max-height: 70vh; }

    /* Forms */
    .form-control { font-size: 16px; padding: .55rem .6rem; }
    .form-group label { font-size: .8rem; }

    /* Sidebar open overlay */
    .sidebar.open { width: 260px; }

    /* Officer dual list */
    .chat-room-item { padding: .5rem .6rem; }
    .avatar { width: 34px; height: 34px; font-size: .75rem; }

    /* Make touch targets better */
    a, button, select, input[type="checkbox"], input[type="radio"] {
        min-height: 44px;
    }
    .btn-sm, .badge, a.btn { min-height: auto; }
    select.form-control { min-height: 44px; }
}


/* ---- Chat Widget (Floating Bottom-Right) ---- */
#chat-widget-container { position: fixed; bottom: 20px; right: 20px; z-index: 500; }
.chat-fab {
    width: 52px; height: 52px; border-radius: 50%; border: none;
    background: var(--primary); color: #fff; font-size: 1.3rem;
    cursor: pointer; box-shadow: var(--shadow-lg); position: relative;
    transition: transform .15s;
}
.chat-fab:hover { transform: scale(1.08); }
.chat-fab-badge {
    position: absolute; top: -4px; right: -4px;
    background: var(--danger); color: #fff;
    font-size: .65rem; font-weight: 700; border-radius: 50%;
    width: 20px; height: 20px; display: flex; align-items: center; justify-content: center;
    border: 2px solid #fff;
}
.chat-panel {
    display: none; position: absolute; bottom: 64px; right: 0;
    width: 340px; max-height: 480px;
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); box-shadow: var(--shadow-lg);
    flex-direction: column; overflow: hidden;
}
.chat-panel.open { display: flex; }
.chat-panel-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: .65rem 1rem; border-bottom: 1px solid var(--border);
}
.chat-panel-header h3 { margin: 0; font-size: .95rem; }
.chat-panel-messages {
    flex: 1; overflow-y: auto; padding: .75rem; min-height: 200px;
    display: flex; flex-direction: column; gap: .3rem;
}
.chat-panel-input {
    display: flex; gap: .4rem; padding: .5rem .75rem;
    border-top: 1px solid var(--border);
}
.chat-panel-input input { flex: 1; font-size: .8rem; padding: .4rem .5rem; }
.chat-badge {
    display: inline-flex; align-items: center; justify-content: center;
    background: var(--danger); color: #fff;
    font-size: .6rem; font-weight: 700; border-radius: 50%;
    min-width: 18px; height: 18px; padding: 0 4px;
    margin-left: auto;
}
@media (max-width: 480px) {
    .chat-panel { width: calc(100vw - 40px); right: 0; }
}

