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

/* === Light Theme === */
:root {
    --bg: #f8f9fb;
    --bg-card: #ffffff;
    --bg-hover: #f3f4f8;
    --bg-input: #ffffff;
    --border: #e2e4ea;
    --text: #1a1d26;
    --text-muted: #6b7084;
    --primary: #4f46e5;
    --primary-hover: #4338ca;
    --primary-light: rgba(79, 70, 229, 0.06);
    --green: #16a34a;
    --yellow: #ca8a04;
    --red: #dc2626;
    --blue: #2563eb;
    --gray: #9ca3af;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
    --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
    --shadow-lg: 0 4px 16px rgba(0,0,0,0.07);
    --radius: 8px;
    --radius-lg: 12px;
}

[data-theme="dark"] {
    --bg: #0f1117;
    --bg-card: #181b23;
    --bg-hover: #1e2230;
    --bg-input: #12141c;
    --border: #2a2e3a;
    --text: #e4e6eb;
    --text-muted: #8b8fa3;
    --primary: #6366f1;
    --primary-hover: #818cf8;
    --primary-light: rgba(99, 102, 241, 0.1);
    --green: #22c55e;
    --yellow: #eab308;
    --red: #ef4444;
    --blue: #3b82f6;
    --gray: #6b7280;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.2);
    --shadow: 0 1px 3px rgba(0,0,0,0.3);
    --shadow-lg: 0 4px 16px rgba(0,0,0,0.4);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    min-height: 100vh;
}

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

/* === Topbar === */
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 32px;
    height: 52px;
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 100;
}

.logo { font-size: 17px; font-weight: 700; color: var(--text); letter-spacing: -0.3px; }
.logo:hover { color: var(--primary); }
.topbar-right { display: flex; align-items: center; gap: 8px; }

.theme-toggle {
    background: none; border: none; cursor: pointer;
    padding: 4px 8px; font-size: 22px; color: var(--text-muted); line-height: 1;
}
[data-theme="dark"] .theme-icon-light { display: none; }
[data-theme="light"] .theme-icon-dark, :root:not([data-theme="dark"]) .theme-icon-dark { display: none; }

/* === Container === */
.container { max-width: 100%; margin: 0 auto; padding: 20px 40px; }

/* === Buttons === */
.btn {
    display: inline-flex; align-items: center; gap: 6px;
    padding: 7px 14px; border: 1px solid var(--border); border-radius: var(--radius);
    background: var(--bg-card); color: var(--text); font-size: 13px;
    cursor: pointer; transition: all 0.15s; text-decoration: none;
}
.btn:hover { background: var(--bg-hover); border-color: var(--text-muted); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

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

.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-full { width: 100%; justify-content: center; }
.btn-send { padding: 9px 28px; font-size: 14px; font-weight: 600; }

.btn-icon {
    display: inline-flex; align-items: center; justify-content: center;
    width: 34px; height: 34px; padding: 0; border: 1px solid var(--border);
    border-radius: var(--radius); background: var(--bg-card); color: var(--text-muted);
    cursor: pointer; transition: all 0.15s; flex-shrink: 0;
}
.btn-icon:hover { background: var(--bg-hover); color: var(--text); }
.btn-icon:disabled { opacity: 0.4; }
.btn-icon.spinning svg { animation: spin 0.8s linear infinite; }

.btn-replied {
    background: none; border: 1px solid var(--green); color: var(--green);
    font-size: 12px; padding: 4px 10px; border-radius: var(--radius); cursor: pointer;
}
.btn-replied:hover { background: var(--green); color: #fff; }
.btn-replied:disabled { opacity: 0.5; cursor: default; background: var(--green); color: #fff; }

.btn-spam {
    background: none; border: 1px solid var(--red); color: var(--red);
    font-size: 12px; padding: 4px 10px; border-radius: var(--radius); cursor: pointer;
}
.btn-spam:hover { background: var(--red); color: #fff; }

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

.btn-spinner {
    display: inline-block; width: 12px; height: 12px;
    border: 2px solid rgba(255,255,255,0.3); border-top-color: #fff;
    border-radius: 50%; animation: spin 0.6s linear infinite;
    vertical-align: middle;
}

.btn:not(.btn-primary) .btn-spinner { border-color: rgba(0,0,0,0.15); border-top-color: var(--primary); }

/* === Flash === */
.flash-messages { margin-bottom: 16px; max-width: 600px; margin-left: auto; margin-right: auto; }
.flash { padding: 10px 14px; border-radius: var(--radius); margin-bottom: 8px; font-size: 13px; }
.flash-error { background: rgba(220,38,38,0.08); border: 1px solid var(--red); color: var(--red); }
.flash-success { background: rgba(22,163,74,0.08); border: 1px solid var(--green); color: var(--green); }

/* === Login === */
.login-wrapper { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 24px; }
.login-card {
    background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius-lg);
    padding: 48px 40px; width: 100%; max-width: 380px; box-shadow: var(--shadow-lg);
}
.login-card { text-align: center; }
.login-card h1 { font-size: 22px; font-weight: 700; margin-bottom: 4px; }
.login-subtitle { color: var(--text-muted); margin-bottom: 28px; font-size: 14px; }
.login-card .form-group { text-align: center; }
.login-card .form-group label { text-align: center; }
.login-card .totp-input { margin: 0 auto; }
.login-card .back-link { margin-top: 16px; }

/* === Forms === */
.form-group { margin-bottom: 14px; position: relative; }
.form-group label { display: block; font-size: 13px; color: var(--text-muted); margin-bottom: 5px; font-weight: 500; }
.form-group input, .input-full {
    width: 100%; padding: 10px 14px; background: var(--bg-input); border: 1px solid var(--border);
    border-radius: var(--radius); color: var(--text); font-size: 14px; font-family: inherit;
}
.form-group input:focus, .input-full:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light); }
textarea.input-full { resize: vertical; min-height: 80px; line-height: 1.6; }

/* ========================================
   DASHBOARD
   ======================================== */

.dashboard { max-width: 100%; margin: 0 auto; }

.dash-top {
    display: flex; align-items: center; justify-content: space-between;
    margin-bottom: 20px; gap: 16px;
}

.dash-title { font-size: 24px; font-weight: 800; letter-spacing: -0.5px; }

.dash-top-right { display: flex; align-items: center; gap: 8px; }

.search-form {
    position: relative; display: flex; align-items: center;
}
.search-icon { position: absolute; left: 10px; color: var(--text-muted); pointer-events: none; }
.search-input {
    padding: 8px 12px 8px 32px; background: var(--bg-card); border: 1px solid var(--border);
    border-radius: var(--radius); color: var(--text); font-size: 13px; width: 220px;
}
.search-input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light); width: 280px; transition: width 0.2s; }

/* === Tab Bar === */
.tab-bar {
    display: flex; align-items: center; justify-content: space-between;
    border-bottom: 1px solid var(--border); margin-bottom: 16px; gap: 16px; flex-wrap: wrap;
}
.tab-bar-left { display: flex; gap: 0; }
.tab-bar-right { display: flex; }

.tab {
    padding: 10px 20px; font-size: 14px; font-weight: 500; color: var(--text-muted);
    background: none; border: none; border-bottom: 2px solid transparent;
    margin-bottom: -1px; cursor: pointer; transition: all 0.15s;
    display: inline-flex; align-items: center; gap: 8px;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

.tab-badge {
    display: none; align-items: center; justify-content: center;
    background: var(--red); color: #fff; font-size: 11px; font-weight: 700;
    min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
}

/* === Filter Pills === */
.filter-pills { display: flex; gap: 4px; background: var(--bg); border-radius: var(--radius); padding: 3px; }
.pill {
    padding: 5px 14px; border-radius: 6px; font-size: 12px; font-weight: 500;
    color: var(--text-muted); border: none; background: none; cursor: pointer;
    display: inline-flex; align-items: center; gap: 5px;
}
.pill:hover { color: var(--text); }
.pill.active { color: var(--primary); background: var(--bg-card); box-shadow: var(--shadow-sm); font-weight: 600; }
.pill-count { font-size: 11px; color: var(--text-muted); }

/* === Ticket List === */
.ticket-list { display: flex; flex-direction: column; gap: 1px; }

.ticket-row {
    display: flex; align-items: center; gap: 14px;
    padding: 12px 16px; background: var(--bg-card); border: 1px solid var(--border);
    border-radius: var(--radius); color: var(--text); transition: all 0.12s;
    cursor: pointer; text-decoration: none; margin-bottom: 2px;
}
.ticket-row:hover { background: var(--bg-hover); border-color: var(--text-muted); box-shadow: var(--shadow-sm); transform: translateY(-1px); }
.ticket-row.read { border-left: 3px solid transparent; }
.ticket-row.unread { border-left: 3px solid var(--primary); background: var(--primary-light); }
.ticket-row.unread .ticket-subject-line { font-weight: 700; }
.ticket-row.replied { border-left: 3px solid var(--green); }
.ticket-row.spam-row { border-left: 3px solid var(--red); color: var(--text-muted); }
.ticket-row.closed-row { border-left: 3px solid var(--gray); color: var(--text-muted); }

.ticket-dot { flex-shrink: 0; width: 8px; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; }
.dot-green { background: var(--green); }
.dot-yellow { background: var(--yellow); }
.dot-red { background: var(--red); }
.dot-blue { background: var(--blue); }
.dot-gray { background: var(--gray); }

.ticket-main { flex: 1; min-width: 0; }
.ticket-subject-line {
    font-size: 13px; font-weight: 600; margin-bottom: 3px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    display: flex; align-items: center; gap: 8px;
}
.ticket-preview { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ticket-meta { flex-shrink: 0; text-align: right; min-width: 140px; }
/* Status Badges in ticket list */
.status-badge {
    display: inline-flex; align-items: center; gap: 3px;
    font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 4px;
    margin-bottom: 3px;
}
.status-badge.replied { color: #fff; background: var(--green); font-size: 12px; padding: 3px 10px; }
.status-badge.draft { color: var(--yellow); background: rgba(202,138,4,0.1); }
.status-badge.closed { color: var(--gray); background: rgba(156,163,175,0.1); }
.status-badge.spam-badge { color: var(--red); background: rgba(220,38,38,0.1); }

.ticket-from { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 180px; margin-left: auto; }
.ticket-date { font-size: 11px; color: var(--gray); margin-top: 2px; }

/* Category Tags */
.cat-tag {
    display: inline-flex; align-items: center;
    font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.3px;
    padding: 1px 6px; border-radius: 4px; flex-shrink: 0;
    color: var(--cat-color); background: color-mix(in srgb, var(--cat-color) 10%, transparent);
    border: 1px solid color-mix(in srgb, var(--cat-color) 25%, transparent);
}

.empty-state { text-align: center; padding: 48px; color: var(--text-muted); font-size: 14px; }
.loading { text-align: center; padding: 48px; color: var(--text-muted); font-size: 14px; }
.loading::after {
    content: ''; display: inline-block; width: 14px; height: 14px;
    border: 2px solid var(--border); border-top-color: var(--primary);
    border-radius: 50%; animation: spin 0.8s linear infinite; margin-left: 8px; vertical-align: middle;
}

.pagination { display: flex; align-items: center; justify-content: center; gap: 12px; margin-top: 16px; padding: 12px 0; }
.page-info { font-size: 13px; color: var(--text-muted); }

/* ========================================
   TICKET DETAIL
   ======================================== */

.ticket-detail { max-width: none; }
.ticket-detail-header {
    display: flex; align-items: center; gap: 12px; margin-bottom: 16px;
}
.ticket-nav { display: flex; gap: 4px; }
.ticket-nav .btn-icon { text-decoration: none; font-size: 14px; }
.ticket-nav .btn-icon.disabled { opacity: 0.3; pointer-events: none; }
.ticket-detail-title { flex: 1; font-size: 19px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.status-select {
    padding: 5px 10px; background: var(--bg-input); border: 1px solid var(--border);
    border-radius: var(--radius); color: var(--text); font-size: 12px; cursor: pointer;
}

.ticket-split { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; }
.ticket-left, .ticket-right { display: flex; flex-direction: column; gap: 12px; }
.ticket-body-scroll { max-height: calc(100vh - 180px); overflow-y: auto; }

.card {
    background: var(--bg-card); border: 1px solid var(--border);
    border-radius: var(--radius-lg); overflow: hidden; box-shadow: var(--shadow-sm);
}
.card-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 12px 16px; border-bottom: 1px solid var(--border); flex-wrap: wrap; gap: 8px;
}
.card-header.compact { padding: 10px 16px; }
.card-header h2 { font-size: 18px; font-weight: 700; }
.card-header h3 { font-size: 16px; font-weight: 600; }
.card-body { padding: 16px; }

.ticket-info-row { font-size: 13px; color: var(--text-muted); display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.ticket-info-row strong { color: var(--text); }
.label { color: var(--gray); font-weight: 500; }
.sep { color: var(--border); }
.ml { margin-left: 12px; }

.ticket-message { white-space: pre-wrap; font-family: inherit; font-size: 15px; line-height: 1.7; color: var(--text); }
.ticket-message-html { margin-bottom: 12px; }
.email-iframe-full { width: 100%; min-height: 200px; border: none; background: #fff; }

.quoted-section {
    margin-top: 12px; border-top: 1px solid var(--border); padding-top: 8px;
}
.quoted-section summary {
    font-size: 12px; color: var(--text-muted); cursor: pointer; padding: 4px 0;
}
.quoted-text { color: var(--text-muted); font-size: 13px; }

.plain-text-toggle { margin-top: 8px; font-size: 12px; color: var(--text-muted); cursor: pointer; }
.plain-text-toggle summary { padding: 4px 0; }
.plain-text-toggle pre { margin-top: 8px; }

.translation-box {
    margin-top: 14px; padding: 14px; border-radius: var(--radius);
    background: rgba(79,70,229,0.04); border: 1px solid var(--primary);
}
[data-theme="dark"] .translation-box { background: rgba(99,102,241,0.08); }
.translation-header { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; color: var(--primary); margin-bottom: 8px; }
/* === Attachments === */
.attachments-section { margin-top: 16px; }
.attachments-header {
    font-size: 12px; font-weight: 600; text-transform: uppercase;
    letter-spacing: 0.5px; color: var(--text-muted); margin-bottom: 8px;
}
.attachments-list { display: flex; flex-wrap: wrap; gap: 8px; }

.attachment-item {
    display: flex; align-items: center; gap: 8px;
    padding: 8px 12px; border: 1px solid var(--border); border-radius: var(--radius);
    text-decoration: none; color: var(--text); background: var(--bg);
    transition: all 0.15s; max-width: 250px;
}
.attachment-item:hover { border-color: var(--primary); background: var(--primary-light); }

.attachment-thumb {
    width: 48px; height: 48px; object-fit: cover; border-radius: 4px;
}
.attachment-icon { color: var(--text-muted); flex-shrink: 0; }
.attachment-info { min-width: 0; }
.attachment-name {
    font-size: 13px; font-weight: 500;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.attachment-size { font-size: 11px; color: var(--text-muted); }

/* Upload area */
.upload-area { display: flex; align-items: center; gap: 8px; margin-top: 10px; flex-wrap: wrap; }

.uploaded-files { display: flex; flex-wrap: wrap; gap: 6px; }

.uploaded-file {
    display: inline-flex; align-items: center; gap: 4px;
    padding: 3px 8px; background: var(--primary-light); border: 1px solid var(--primary);
    border-radius: 4px; font-size: 12px; color: var(--primary);
}
.remove-file { cursor: pointer; font-size: 14px; font-weight: 700; margin-left: 2px; }
.remove-file:hover { color: var(--red); }

.message-actions { margin-top: 12px; display: flex; gap: 8px; }

/* Reply */
.instruction-row { display: flex; gap: 8px; margin-bottom: 10px; align-items: center; }
.instruction-row input { flex: 1; font-size: 14px; padding: 9px 12px; }

.reply-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.reply-actions-top { display: flex; gap: 8px; }
.draft-progress {
    width: 100%; height: 3px; background: var(--border); border-radius: 2px;
    margin-top: 8px; overflow: hidden;
}
.draft-progress-bar {
    height: 100%; width: 30%; background: var(--primary); border-radius: 2px;
    animation: progress-slide 1.5s ease-in-out infinite;
}
@keyframes progress-slide {
    0% { transform: translateX(-100%); width: 30%; }
    50% { width: 60%; }
    100% { transform: translateX(400%); width: 30%; }
}

.toast {
    position: absolute; bottom: 4px; right: 4px;
    padding: 4px 12px; border-radius: 6px;
    font-size: 12px; font-weight: 500;
    animation: toastIn 0.3s ease;
    pointer-events: none; z-index: 10;
}
.toast-success { background: var(--green); color: #fff; }
.toast-error { background: var(--red); color: #fff; }
.toast-fade { opacity: 0; transition: opacity 0.4s ease; }
@keyframes toastIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }

.shortcut-hint {
    font-size: 10px; color: var(--text-muted); background: var(--bg);
    padding: 2px 7px; border-radius: 4px; border: 1px solid var(--border);
}

/* Previous Replies */
.reply-item { padding: 12px 0; border-bottom: 1px solid var(--border); }
.reply-item:last-child { border-bottom: none; }
.reply-meta { display: flex; justify-content: space-between; font-size: 11px; color: var(--text-muted); margin-bottom: 6px; }
.reply-body { white-space: pre-wrap; font-family: inherit; font-size: 12px; color: var(--text); line-height: 1.6; }

/* Customer History */
.customer-history { max-height: 300px; overflow-y: auto; padding: 6px 14px !important; }
.history-item {
    display: block; padding: 7px 8px; border-radius: 6px;
    text-decoration: none; color: var(--text); border-bottom: 1px solid var(--border);
}
.history-item:last-child { border-bottom: none; }
.history-item:hover { background: var(--primary-light); }
.history-subject { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.history-meta { display: flex; gap: 10px; font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.history-date { flex-shrink: 0; }
.history-preview { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.history-pagination { display: flex; align-items: center; justify-content: center; gap: 8px; padding: 6px 14px; border-top: 1px solid var(--border); }

/* === Modal === */
.modal-overlay {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.4); backdrop-filter: blur(4px);
    display: flex; align-items: center; justify-content: center; z-index: 1000;
}

.modal-box {
    background: var(--bg-card); border: 1px solid var(--border);
    border-radius: var(--radius-lg); padding: 28px 32px;
    min-width: 360px; max-width: 480px; box-shadow: var(--shadow-lg);
}

.modal-title { font-size: 18px; font-weight: 700; margin-bottom: 12px; }
.modal-text { font-size: 14px; color: var(--text-muted); margin-bottom: 24px; line-height: 1.5; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; }

.modal-large { min-width: 500px; max-width: 700px; max-height: 80vh; display: flex; flex-direction: column; }
.modal-preview-text {
    white-space: pre-wrap; font-family: inherit; font-size: 14px;
    line-height: 1.7; color: var(--text); overflow-y: auto;
    flex: 1; margin-bottom: 16px; padding: 16px;
    background: var(--bg); border-radius: var(--radius); border: 1px solid var(--border);
}

/* === Settings === */
.settings-page { max-width: 600px; margin: 0 auto; }
.settings-page ~ .flash-messages, .settings-page .flash { max-width: 600px; margin-left: auto; margin-right: auto; }
.settings-header { display: flex; align-items: center; gap: 16px; margin-bottom: 24px; }
.settings-header h1 { font-size: 22px; font-weight: 700; }
.settings-grid { display: flex; flex-direction: column; gap: 16px; }

.totp-input {
    font-size: 24px !important; letter-spacing: 8px; text-align: center;
    font-family: monospace; max-width: 220px; display: block; margin: 0 auto;
}

.totp-setup {
    display: flex; align-items: center; gap: 20px;
    padding: 16px; background: var(--bg); border-radius: var(--radius); border: 1px solid var(--border);
}

.qr-container { flex-shrink: 0; border-radius: var(--radius); overflow: hidden; }
.qr-container img { display: block; }

.totp-secret {
    display: block; font-size: 14px; padding: 8px 12px; background: var(--bg-card);
    border: 1px solid var(--border); border-radius: var(--radius); word-break: break-all;
    user-select: all; cursor: pointer;
}

.totp-manual { flex: 1; }

.back-link { display: block; text-align: center; margin-top: 16px; font-size: 13px; color: var(--text-muted); }

/* === Send Success Overlay === */
.send-success-overlay {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(255,255,255,0.9); backdrop-filter: blur(4px);
    display: flex; align-items: center; justify-content: center; z-index: 2000;
    animation: fadeIn 0.3s ease;
}
[data-theme="dark"] .send-success-overlay { background: rgba(15,17,23,0.9); }

.send-success-box { text-align: center; }
.send-success-text { font-size: 22px; font-weight: 700; color: var(--green); margin-top: 16px; }
.send-success-sub { font-size: 14px; color: var(--text-muted); margin-top: 6px; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* === Responsive === */
@media (max-width: 1024px) {
    .ticket-split { grid-template-columns: 1fr; }
    .ticket-body-scroll { max-height: none; }
}
@media (max-width: 768px) {
    .container { padding: 12px 16px; }
    .topbar { padding: 0 16px; }
    .ticket-meta { display: none; }
    .search-input { width: 150px; }
    .dash-top { flex-direction: column; align-items: flex-start; }
    .tab-bar { flex-direction: column; align-items: flex-start; }
}
