:root {
    --bg: #f5f7fb;
    --surface: #ffffff;
    --surface-2: #eef2f7;
    --text: #172033;
    --muted: #667085;
    --border: #d9e1ec;
    --primary: #2563eb;
    --primary-strong: #1d4ed8;
    --success: #16a34a;
    --danger: #dc2626;
    --warning: #d97706;
    --info: #0891b2;
    --sidebar: #111827;
    --sidebar-text: #d6deec;
    --shadow: 0 16px 40px rgba(23, 32, 51, .08);
}

[data-theme="dark"] {
    --bg: #111827;
    --surface: #172033;
    --surface-2: #202b3d;
    --text: #edf2f7;
    --muted: #aab5c5;
    --border: #314052;
    --sidebar: #0b1220;
    --sidebar-text: #e5e7eb;
    --shadow: 0 16px 40px rgba(0, 0, 0, .24);
}

* {
    box-sizing: border-box;
}

body {
    min-height: 100vh;
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    letter-spacing: 0;
}

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

.auth-shell {
    display: grid;
    place-items: center;
    padding: 24px;
    background: linear-gradient(135deg, #edf2ff 0%, #f8fafc 48%, #fff7ed 100%);
}

.auth-panel {
    width: min(100%, 420px);
}

.login-card,
.panel,
.stat-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: var(--shadow);
}

.login-card {
    padding: 28px;
}

.demo-access {
    display: grid;
    gap: 4px;
    margin-top: 18px;
    padding: 12px;
    border-radius: 8px;
    background: var(--surface-2);
    color: var(--muted);
    font-size: .9rem;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--sidebar-text);
    font-weight: 800;
    min-height: 40px;
}

.brand-login {
    color: var(--text);
    margin-bottom: 24px;
}

.brand-mark {
    display: inline-grid;
    place-items: center;
    width: 38px;
    height: 38px;
    border-radius: 8px;
    background: var(--primary);
    color: #fff;
}

.app-shell {
    display: grid;
    grid-template-columns: 260px minmax(0, 1fr);
    min-height: 100vh;
}

.sidebar {
    position: sticky;
    top: 0;
    height: 100vh;
    padding: 18px;
    background: var(--sidebar);
    color: var(--sidebar-text);
}

.sidebar .nav {
    margin-top: 22px;
}

.sidebar .nav-link {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 42px;
    color: var(--sidebar-text);
    border-radius: 8px;
    font-weight: 600;
}

.sidebar .nav-link i {
    width: 22px;
    text-align: center;
}

.sidebar .nav-link:hover,
.sidebar .nav-link.active {
    background: rgba(255, 255, 255, .1);
    color: #fff;
}

.main-area {
    min-width: 0;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    padding: 16px 24px;
    background: color-mix(in srgb, var(--surface) 92%, transparent);
    border-bottom: 1px solid var(--border);
    backdrop-filter: blur(10px);
}

.topbar h1 {
    margin: 0;
    font-size: 1.35rem;
    font-weight: 800;
}

.topbar span {
    color: var(--muted);
    font-size: .9rem;
}

.top-actions,
.table-actions,
.form-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.content-wrap {
    padding: 24px;
}

.icon-btn {
    display: inline-grid;
    place-items: center;
    width: 38px;
    height: 38px;
    flex: 0 0 38px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    color: var(--text);
}

button.icon-btn {
    cursor: pointer;
}

.icon-btn:hover {
    border-color: var(--primary);
    color: var(--primary);
}

.icon-btn.danger {
    color: var(--danger);
}

.notif-dot {
    position: absolute;
    top: -7px;
    right: -7px;
    display: grid;
    place-items: center;
    min-width: 20px;
    height: 20px;
    padding: 0 5px;
    border-radius: 999px;
    background: var(--danger);
    color: #fff !important;
    font-size: .72rem !important;
    font-weight: 800;
}

.user-chip {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 38px;
    max-width: 240px;
    padding: 0 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    font-weight: 700;
}

.user-chip span {
    overflow: hidden;
    color: var(--text);
    text-overflow: ellipsis;
    white-space: nowrap;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(5, minmax(160px, 1fr));
    gap: 14px;
    margin-bottom: 18px;
}

.stat-card {
    display: grid;
    grid-template-columns: 42px 1fr;
    align-items: center;
    min-height: 104px;
    padding: 18px;
    color: var(--text);
}

.stat-card[href] {
    cursor: pointer;
}

.stat-card[href]:hover {
    border-color: var(--primary);
    color: var(--text);
    transform: translateY(-1px);
}

.stat-card i {
    display: grid;
    place-items: center;
    width: 42px;
    height: 42px;
    border-radius: 8px;
    background: color-mix(in srgb, var(--primary) 14%, transparent);
    color: var(--primary);
}

.stat-card span,
.stat-card strong {
    grid-column: 2;
}

.stat-card span {
    color: var(--muted);
    font-weight: 700;
}

.stat-card strong {
    font-size: 1.8rem;
    line-height: 1;
}

.stat-card.success i {
    background: color-mix(in srgb, var(--success) 14%, transparent);
    color: var(--success);
}

.stat-card.danger i {
    background: color-mix(in srgb, var(--danger) 14%, transparent);
    color: var(--danger);
}

.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 18px;
    margin-bottom: 18px;
}

.panel {
    padding: 18px;
}

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

.panel-head h2 {
    margin: 0;
    font-size: 1.05rem;
    font-weight: 800;
}

.filters {
    display: grid;
    grid-template-columns: repeat(6, minmax(130px, 1fr)) auto;
    gap: 10px;
    margin-bottom: 14px;
}

.filters-wide {
    grid-template-columns: repeat(5, minmax(130px, 1fr)) repeat(2, auto);
}

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

.full {
    grid-column: 1 / -1;
}

.form-control,
.form-select {
    min-height: 42px;
    border-color: var(--border);
    border-radius: 8px;
    background-color: var(--surface);
    color: var(--text);
}

.btn {
    min-height: 40px;
    border-radius: 8px;
    font-weight: 700;
}

.btn-primary {
    background: var(--primary);
    border-color: var(--primary);
}

.btn-primary:hover {
    background: var(--primary-strong);
    border-color: var(--primary-strong);
}

.table {
    --bs-table-bg: transparent;
    --bs-table-color: var(--text);
    --bs-table-border-color: var(--border);
    margin-bottom: 0;
}

.table th {
    color: var(--muted);
    font-size: .78rem;
    text-transform: uppercase;
}

.badge {
    border-radius: 999px;
    padding: .45rem .65rem;
}

.list-stack {
    display: grid;
    gap: 10px;
}

.list-row,
.muted-row {
    display: grid;
    grid-template-columns: 38px 1fr auto;
    align-items: center;
    gap: 12px;
    min-height: 58px;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text);
}

.list-row.active-chat {
    border-color: var(--primary);
    background: color-mix(in srgb, var(--primary) 8%, var(--surface));
}

.list-row i {
    color: var(--primary);
}

.chat-user-icon {
    position: relative;
    display: inline-grid;
    place-items: center;
    width: 38px;
    height: 38px;
}

.chat-red-dot {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 10px;
    height: 10px;
    border: 2px solid var(--surface);
    border-radius: 50%;
    background: var(--danger);
}

.list-row div {
    display: grid;
    gap: 2px;
}

.list-row span,
.list-row small,
.muted-row {
    color: var(--muted);
}

.summary-strip {
    display: grid;
    grid-template-columns: repeat(4, minmax(140px, 1fr));
    gap: 10px;
}

.summary-strip span {
    display: grid;
    gap: 4px;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface-2);
}

.summary-strip strong {
    color: var(--muted);
    font-size: .8rem;
    text-transform: uppercase;
}

.response-grid {
    display: grid;
    grid-template-columns: minmax(140px, .8fr) minmax(220px, 1.4fr) minmax(110px, .6fr) minmax(130px, .7fr) minmax(160px, 1fr) auto;
    gap: 8px;
    align-items: start;
}

.chat-box {
    display: grid;
    gap: 10px;
    max-height: 440px;
    overflow: auto;
    padding-right: 4px;
}

.chat-bubble {
    width: min(82%, 520px);
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface-2);
}

.chat-bubble.mine {
    justify-self: end;
    background: color-mix(in srgb, var(--primary) 10%, var(--surface));
    border-color: color-mix(in srgb, var(--primary) 32%, var(--border));
}

.chat-bubble p {
    margin: 6px 0;
}

.chat-bubble small {
    display: block;
    color: var(--muted);
}

.chat-form {
    display: grid;
    grid-template-columns: 1fr 220px auto;
    gap: 10px;
    margin-top: 14px;
    align-items: end;
}

.manual h3 {
    margin-top: 20px;
    font-size: 1rem;
    font-weight: 800;
}

.expiration-ok {
    box-shadow: inset 4px 0 0 #16a34a;
}

.expiration-warning {
    box-shadow: inset 4px 0 0 #facc15;
}

.expiration-urgent {
    box-shadow: inset 4px 0 0 #f97316;
}

.expiration-critical {
    box-shadow: inset 4px 0 0 #dc2626;
}

.expiration-expired {
    box-shadow: inset 4px 0 0 #111827;
}

.crm-tabs {
    padding-top: 18px;
}

.calendar-title {
    margin: 0 0 14px;
    font-size: 1rem;
    font-weight: 800;
}

.calendar-grid {
    display: grid;
    grid-template-columns: repeat(7, minmax(120px, 1fr));
    gap: 8px;
    width: 100%;
}

.calendar-grid > strong {
    display: block;
    min-height: 28px;
    padding: 6px 8px;
    color: var(--muted);
    font-size: .78rem;
    text-transform: uppercase;
}

.calendar-cell {
    min-height: 118px;
    padding: 10px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface-2);
}

.muted-cell {
    opacity: .45;
}

.calendar-day {
    display: block;
    margin-bottom: 8px;
    font-weight: 800;
}

.calendar-event {
    display: block;
    width: 100%;
    margin-top: 6px;
    padding: 7px 8px;
    border: 0;
    border-radius: 8px;
    color: #fff;
    font-size: .82rem;
    font-weight: 700;
    text-align: left;
}

.appointment-confirmed {
    background: var(--success);
}

.appointment-scheduled {
    background: var(--primary);
}

.appointment-postponed {
    background: var(--warning);
}

.appointment-canceled {
    background: var(--danger);
}

.appointment-finished {
    background: #111827;
}

.swal-detail {
    display: grid;
    gap: 4px;
    text-align: left;
}

.swal-detail p {
    margin: 0;
}

.appointment-history-wrap {
    max-height: 260px;
    overflow: auto;
    border: 1px solid var(--border);
    border-radius: 8px;
}

.appointment-history-table {
    table-layout: fixed;
    min-width: 760px;
}

.appointment-history-table th:nth-child(1),
.appointment-history-table td:nth-child(1) {
    width: 110px;
}

.appointment-history-table th:nth-child(2),
.appointment-history-table td:nth-child(2) {
    width: 130px;
}

.appointment-history-table th:nth-child(3),
.appointment-history-table td:nth-child(3) {
    width: 116px;
}

.history-value {
    white-space: normal;
    overflow-wrap: anywhere;
}

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

.appointment-action-grid {
    display: grid;
    grid-template-columns: minmax(260px, .8fr) minmax(360px, 1.2fr);
    gap: 12px;
    margin-top: 12px;
}

.appointment-action-card {
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface-2);
}

.appointment-action-card .form-label {
    margin-bottom: 8px;
    color: var(--muted);
    font-size: .8rem;
    font-weight: 800;
    text-transform: uppercase;
}

.appointment-inline-form,
.appointment-reschedule-form {
    display: grid;
    gap: 8px;
}

.appointment-inline-form {
    grid-template-columns: minmax(0, 1fr) auto;
}

.appointment-reschedule-form {
    grid-template-columns: minmax(130px, .8fr) minmax(110px, .7fr) minmax(180px, 1fr) auto;
}

.paid-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(260px, 1fr));
    gap: 18px;
    margin-top: 18px;
}

.paid-card {
    display: grid;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    box-shadow: var(--shadow);
}

.paid-media {
    aspect-ratio: 16 / 10;
    background: var(--surface-2);
}

.paid-media img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.paid-placeholder {
    display: grid;
    place-items: center;
    width: 100%;
    height: 100%;
    color: var(--muted);
    font-size: 2.4rem;
}

.paid-body {
    display: grid;
    gap: 14px;
    padding: 16px;
}

.paid-title-row {
    display: flex;
    justify-content: space-between;
    gap: 12px;
}

.paid-title-row small {
    color: var(--muted);
    font-weight: 800;
    text-transform: uppercase;
}

.paid-title-row h3 {
    margin: 2px 0 0;
    font-size: 1rem;
    font-weight: 800;
}

.paid-meta,
.paid-status-grid,
.paid-history-strip,
.assignment-box,
.paid-assignees {
    display: grid;
    gap: 8px;
}

.paid-meta,
.paid-status-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

.paid-status-grid.two {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.paid-history-strip {
    grid-template-columns: repeat(5, minmax(140px, 1fr));
}

.assignment-box {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.paid-assignees {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.paid-title-link {
    color: inherit;
    text-decoration: none;
}

.paid-readonly {
    display: grid;
    grid-template-columns: repeat(5, minmax(140px, 1fr));
    gap: 8px;
}

.paid-meta span,
.paid-status-grid span,
.paid-history-strip span,
.assignment-box span,
.paid-readonly span,
.paid-assignees span,
.paid-reference {
    padding: 10px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface-2);
}

.paid-meta strong,
.paid-status-grid strong,
.paid-history-strip strong,
.assignment-box strong,
.paid-readonly strong,
.paid-assignees strong {
    display: block;
    margin-bottom: 4px;
    color: var(--muted);
    font-size: .72rem;
    text-transform: uppercase;
}

.paid-copy {
    display: -webkit-box;
    min-height: 54px;
    margin: 0;
    overflow: hidden;
    color: var(--muted);
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
}

.paid-footer {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    color: var(--muted);
    font-size: .86rem;
}

.paid-reference {
    display: flex;
    align-items: center;
    gap: 12px;
}

.paid-reference i {
    color: var(--primary);
}

.paid-reference span {
    display: block;
    color: var(--muted);
}

.paid-empty {
    grid-column: 1 / -1;
    display: grid;
    place-items: center;
    min-height: 220px;
    text-align: center;
}

.paid-empty i {
    color: var(--primary);
    font-size: 2rem;
}

.empty-state {
    display: grid;
    place-items: center;
    min-height: 50vh;
    text-align: center;
}

.empty-state i {
    margin-bottom: 12px;
    color: var(--warning);
    font-size: 2rem;
}

.modal-content {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 8px;
}

@media (max-width: 1180px) {
    .stats-grid {
        grid-template-columns: repeat(2, minmax(180px, 1fr));
    }
    .filters {
        grid-template-columns: repeat(3, minmax(130px, 1fr));
    }
}

@media (max-width: 840px) {
    .app-shell {
        grid-template-columns: 1fr;
    }
    .sidebar {
        position: static;
        height: auto;
    }
    .sidebar .nav {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
    .topbar {
        align-items: flex-start;
        flex-direction: column;
    }
    .top-actions {
        flex-wrap: wrap;
    }
    .dashboard-grid,
    .form-grid,
    .summary-strip,
    .response-grid,
    .calendar-grid,
    .paid-grid,
    .paid-meta,
    .paid-status-grid,
    .paid-history-strip,
    .assignment-box,
    .paid-readonly,
    .paid-assignees,
    .appointment-action-grid,
    .appointment-inline-form,
    .appointment-reschedule-form,
    .chat-form {
        grid-template-columns: 1fr;
    }

    .calendar-cell {
        min-height: auto;
    }
}

@media (max-width: 560px) {
    .content-wrap,
    .topbar,
    .sidebar {
        padding: 14px;
    }
    .stats-grid,
    .filters {
        grid-template-columns: 1fr;
    }
    .panel {
        padding: 14px;
    }
    .panel-head {
        align-items: flex-start;
        flex-direction: column;
    }
    .user-chip {
        max-width: 100%;
    }
}
