/* ===== CSS Reset & Variables ===== */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --bg: #0f1117;
    --surface: #1a1d27;
    --surface2: #22263a;
    --border: #2e3250;
    --accent: #6c63ff;
    --accent-hover: #857dff;
    --success: #22c55e;
    --danger: #ef4444;
    --warning: #f59e0b;
    --text: #e2e8f0;
    --text-muted: #8892a4;
    --user-bubble: #6c63ff22;
    --bot-bubble: #22263a;
    --font: 'Segoe UI', system-ui, -apple-system, sans-serif;
    --radius: 10px;
    --shadow: 0 4px 24px #0005;
}

body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ===== NAV ===== */
nav {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0 1.5rem;
    display: flex;
    align-items: center;
    gap: 1rem;
    height: 56px;
    position: sticky;
    top: 0;
    z-index: 100;
}

nav .logo {
    font-weight: 700;
    font-size: 1.15rem;
    color: var(--accent);
    text-decoration: none;
    margin-right: auto;
    letter-spacing: -0.3px;
}

nav a {
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.9rem;
    padding: 0.35rem 0.8rem;
    border-radius: 6px;
    transition: color 0.15s, background 0.15s;
}

nav a:hover,
nav a.active {
    color: var(--text);
    background: var(--surface2);
}

/* ===== LAYOUT ===== */
.container {
    max-width: 1100px;
    margin: 0 auto;
    padding: 2rem 1.5rem;
    flex: 1;
}

.page-title {
    font-size: 1.5rem;
    font-weight: 700;
    margin-bottom: 0.4rem;
}

.page-sub {
    color: var(--text-muted);
    font-size: 0.92rem;
    margin-bottom: 2rem;
}

/* ===== CARDS ===== */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem;
    margin-bottom: 1.5rem;
}

.card-title {
    font-weight: 600;
    margin-bottom: 1rem;
    font-size: 1rem;
    color: var(--text);
}

/* ===== GRID ===== */
.grid {
    display: grid;
    gap: 1rem;
}

.grid-2 {
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
}

.grid-3 {
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
}

/* ===== STAT TILE ===== */
.stat {
    text-align: center;
    padding: 1.25rem;
}

.stat-value {
    font-size: 2rem;
    font-weight: 700;
    color: var(--accent);
}

.stat-label {
    font-size: 0.82rem;
    color: var(--text-muted);
    margin-top: 0.25rem;
}

/* ===== STATUS BADGE ===== */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.78rem;
    font-weight: 600;
    padding: 0.2rem 0.65rem;
    border-radius: 99px;
}

.badge-green {
    background: #22c55e22;
    color: var(--success);
}

.badge-red {
    background: #ef444422;
    color: var(--danger);
}

.badge-yellow {
    background: #f59e0b22;
    color: var(--warning);
}

.badge-purple {
    background: #6c63ff22;
    color: var(--accent);
}

/* ===== FORMS ===== */
.form-group {
    margin-bottom: 1.1rem;
}

label {
    display: block;
    font-size: 0.83rem;
    color: var(--text-muted);
    margin-bottom: 0.35rem;
    font-weight: 500;
}

input[type=text],
input[type=password],
select,
textarea {
    width: 100%;
    background: var(--surface2);
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: 7px;
    padding: 0.55rem 0.85rem;
    font-size: 0.92rem;
    font-family: var(--font);
    outline: none;
    transition: border-color 0.15s;
}

input:focus,
select:focus,
textarea:focus {
    border-color: var(--accent);
}

textarea {
    resize: vertical;
    min-height: 90px;
}

input[type=text][readonly] {
    opacity: 0.6;
    cursor: default;
}

/* ===== BUTTONS ===== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.55rem 1.2rem;
    border: none;
    border-radius: 7px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.15s, transform 0.1s;
    text-decoration: none;
}

.btn:active {
    transform: scale(0.97);
}

.btn-primary {
    background: var(--accent);
    color: #fff;
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.btn-secondary {
    background: var(--surface2);
    color: var(--text);
    border: 1px solid var(--border);
}

.btn-secondary:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.btn-danger {
    background: #ef444422;
    color: var(--danger);
    border: 1px solid #ef444433;
}

.btn-danger:hover {
    background: #ef444433;
}

.btn-sm {
    padding: 0.35rem 0.8rem;
    font-size: 0.82rem;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ===== TABLE ===== */
table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

th {
    text-align: left;
    padding: 0.6rem 0.85rem;
    color: var(--text-muted);
    font-size: 0.8rem;
    font-weight: 600;
    border-bottom: 1px solid var(--border);
}

td {
    padding: 0.7rem 0.85rem;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}

tr:last-child td {
    border-bottom: none;
}

tr:hover td {
    background: var(--surface2);
}

/* ===== CHAT ===== */
.chat-layout {
    display: flex;
    gap: 1rem;
    height: calc(100vh - 130px);
}

.chat-sidebar {
    width: 240px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
}

.chat-user-list {
    flex: 1;
    overflow-y: auto;
}

.chat-user-item {
    padding: 0.65rem 0.85rem;
    border-radius: 7px;
    cursor: pointer;
    transition: background 0.15s;
    font-size: 0.9rem;
    margin-bottom: 0.15rem;
}

.chat-user-item:hover {
    background: var(--surface2);
}

.chat-user-item.selected {
    background: var(--accent);
    color: #fff;
}

.chat-user-item .uphone {
    font-size: 0.78rem;
    opacity: 0.7;
}

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

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 0.75rem;
}

.msg-row {
    display: flex;
}

.msg-row.user {
    justify-content: flex-end;
}

.msg-row.assistant {
    justify-content: flex-start;
}

.msg-bubble {
    max-width: 70%;
    padding: 0.65rem 1rem;
    border-radius: 14px;
    font-size: 0.93rem;
    line-height: 1.55;
    word-break: break-word;
}

.msg-row.user .msg-bubble {
    background: var(--accent);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.msg-row.assistant .msg-bubble {
    background: var(--bot-bubble);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

.msg-meta {
    font-size: 0.72rem;
    color: var(--text-muted);
    margin-top: 0.2rem;
    text-align: right;
}

.msg-row.assistant .msg-meta {
    text-align: left;
}

.chat-input-row {
    display: flex;
    gap: 0.5rem;
}

.chat-input-row input {
    flex: 1;
}

/* ===== TOAST ===== */
#toast {
    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    background: var(--surface2);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 0.7rem 1.2rem;
    border-radius: 8px;
    font-size: 0.9rem;
    box-shadow: var(--shadow);
    opacity: 0;
    transition: opacity 0.3s;
    z-index: 999;
    pointer-events: none;
}

#toast.show {
    opacity: 1;
}

#toast.success {
    border-color: var(--success);
    color: var(--success);
}

#toast.error {
    border-color: var(--danger);
    color: var(--danger);
}

/* ===== SCROLLBAR ===== */
::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 99px;
}

/* ===== SPINNER ===== */
.spinner {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 2px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
    vertical-align: middle;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.empty-state {
    text-align: center;
    color: var(--text-muted);
    padding: 3rem 1rem;
    font-size: 0.92rem;
}