:root {
  --bg: #f3f5f7;
  --panel: #ffffff;
  --text: #17201f;
  --muted: #667370;
  --line: #dfe6e4;
  --green: #087f5b;
  --green-dark: #056146;
  --green-soft: #e6f5ef;
  --yellow: #aa6f00;
  --yellow-soft: #fff4cf;
  --blue: #2463ab;
  --blue-soft: #eaf2ff;
  --red: #aa3434;
  --red-soft: #fdecec;
  --surface-subtle: #f9fbfa;
  --surface-hover: #f4faf7;
  --field-bg: #ffffff;
  --field-border: #cad5d2;
  --code-bg: #edf2f0;
  --message-bg: #f8faf9;
  --shadow: 0 12px 30px rgba(25, 48, 43, .07);
}

:root[data-theme="dark"] {
  --bg: #0f1413;
  --panel: #18201e;
  --text: #e7eeeb;
  --muted: #9aaba6;
  --line: #2d3a37;
  --green: #37d39a;
  --green-dark: #63dfb1;
  --green-soft: #143a2e;
  --yellow: #f0bd51;
  --yellow-soft: #3b3018;
  --blue: #79aef2;
  --blue-soft: #182f4d;
  --red: #f08b8b;
  --red-soft: #432222;
  --surface-subtle: #141b19;
  --surface-hover: #202b28;
  --field-bg: #111816;
  --field-border: #3a4945;
  --code-bg: #25312e;
  --message-bg: #121917;
  --shadow: 0 14px 34px rgba(0, 0, 0, .3);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.5 Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif; }
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; }

.topbar { min-height: 68px; display: flex; align-items: center; gap: 24px; padding: 0 4vw; background: #112a25; color: #fff; box-shadow: 0 4px 18px rgba(0,0,0,.12); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 17px; }
.brand-mark { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 10px; background: #24b47e; color: #08251b; }
nav { display: flex; align-self: stretch; align-items: center; gap: 4px; }
.nav-link { height: 100%; display: flex; align-items: center; padding: 0 15px; color: #c5d5d0; border-bottom: 3px solid transparent; }
.nav-link:hover, .nav-link.active { color: #fff; border-bottom-color: #37d39a; }
.shift-form { display: flex; align-items: center; gap: 8px; margin-left: auto; color: #b9cec8; font-size: 12px; font-weight: 700; white-space: nowrap; }
.shift-form select { width: 145px; padding: 7px 9px; border-color: #45635b; background: #173b33; color: #fff; }
.theme-toggle { padding: 7px 10px; border: 1px solid #45635b; border-radius: 8px; background: #173b33; color: #d7e5e1; cursor: pointer; font-size: 12px; font-weight: 700; white-space: nowrap; }
.theme-toggle:hover { border-color: #61a08e; color: #fff; }
.prototype { padding: 5px 9px; border: 1px solid #5d7770; border-radius: 6px; color: #b9cec8; font-size: 11px; letter-spacing: .12em; }

.page { width: min(1500px, 94vw); margin: 30px auto 60px; }
footer { padding: 25px; color: var(--muted); text-align: center; font-size: 13px; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 8px; font-size: clamp(28px, 3vw, 40px); line-height: 1.1; }
h2 { font-size: 19px; }
h3 { margin-top: 28px; font-size: 15px; }
.eyebrow { margin-bottom: 5px; color: var(--green); font-size: 12px; font-weight: 800; letter-spacing: .12em; text-transform: uppercase; }
.page-heading, .ticket-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 25px; margin-bottom: 24px; }
.page-heading p:last-child, .ticket-header p:last-child { color: var(--muted); white-space: pre-line; }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); }
.button { display: inline-flex; justify-content: center; align-items: center; min-height: 42px; padding: 0 16px; border: 1px solid var(--line); border-radius: 9px; background: var(--field-bg); color: var(--text); cursor: pointer; font-weight: 700; }
.button:hover { border-color: #a9b9b4; }
.button.primary { border-color: var(--green); background: var(--green); color: #fff; }
.button.primary:hover { background: var(--green-dark); }
.button.ghost { background: transparent; }
.notice { margin-bottom: 18px; padding: 13px 16px; border-radius: 10px; background: var(--green-soft); color: var(--green-dark); }
.warning-notice { background: var(--yellow-soft); color: #745000; }

.metrics { display: grid; grid-template-columns: repeat(5, minmax(140px, 1fr)); gap: 12px; margin-bottom: 18px; }
.metric { display: flex; justify-content: space-between; align-items: center; min-height: 82px; padding: 16px 18px; border: 1px solid var(--line); border-radius: 12px; background: var(--panel); }
.metric span { color: var(--muted); font-weight: 700; }
.metric strong { font-size: 28px; }
.metric:hover { transform: translateY(-1px); box-shadow: var(--shadow); }
.metric-new strong { color: var(--yellow); }
.metric-in_progress strong { color: var(--blue); }
.metric-done strong { color: var(--green); }
.metric-invalid strong { color: var(--red); }

.filters-panel { margin-bottom: 18px; padding: 14px; }
.filters { display: grid; grid-template-columns: minmax(220px, 2fr) repeat(3, minmax(150px, 1fr)) auto auto; gap: 10px; }
input, select, textarea { width: 100%; padding: 10px 12px; border: 1px solid var(--field-border); border-radius: 8px; background: var(--field-bg); color: var(--text); outline: none; }
input:focus, select:focus, textarea:focus { border-color: var(--green); box-shadow: 0 0 0 3px rgba(8,127,91,.1); }
textarea { resize: vertical; }
label { display: grid; gap: 6px; color: var(--muted); font-size: 13px; font-weight: 700; }

.table-panel { overflow: hidden; }
.table-hint { display: flex; justify-content: space-between; gap: 16px; padding: 10px 15px; border-bottom: 1px solid var(--line); background: var(--surface-subtle); color: var(--muted); font-size: 12px; }
#auto-refresh-state { color: var(--green); white-space: nowrap; }
.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th { padding: 12px 15px; background: var(--surface-subtle); color: var(--muted); text-align: left; font-size: 12px; letter-spacing: .04em; text-transform: uppercase; }
td { padding: 15px; border-top: 1px solid var(--line); vertical-align: top; }
tbody tr:hover { background: var(--surface-hover); }
tbody tr[data-ticket-row] { cursor: context-menu; }
td small { display: block; margin-top: 5px; color: var(--muted); }
.ticket-number { color: var(--muted); font-weight: 800; }
.priority-cell { width: 125px; }
.priority-select { width: 118px; min-width: 118px; padding: 6px 8px; border: 0; border-radius: 999px; cursor: pointer; font-size: 12px; font-weight: 800; }
.priority-select-low { background: #eef1f2; color: #5d696d; }
.priority-select-normal { background: var(--blue-soft); color: var(--blue); }
.priority-select-high { background: var(--yellow-soft); color: var(--yellow); }
.priority-select-urgent { background: var(--red-soft); color: var(--red); }
.priority-select:disabled { cursor: wait; opacity: .65; }
.ticket-title-link { display: inline-block; color: var(--green-dark); text-decoration: underline; text-decoration-color: transparent; text-underline-offset: 3px; }
.ticket-title-link:hover { color: var(--green); text-decoration-color: currentColor; }
.summary { max-width: 560px; white-space: pre-line; }
time { color: var(--muted); font-size: 12px; white-space: nowrap; }
.pagination { display: flex; justify-content: space-between; align-items: center; gap: 16px; min-height: 62px; padding: 11px 15px; border-top: 1px solid var(--line); background: var(--surface-subtle); }
.pagination-total { color: var(--muted); font-size: 13px; }
.pagination-links { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 5px; }
.pagination-link { display: inline-flex; align-items: center; justify-content: center; min-width: 36px; min-height: 36px; padding: 0 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--field-bg); color: var(--text); font-size: 13px; font-weight: 700; }
.pagination-link:hover, .pagination-link.current { border-color: var(--green); background: var(--green); color: #fff; }
.pagination-link.disabled { pointer-events: none; opacity: .45; }
.pagination-gap { display: inline-flex; align-items: center; padding: 0 3px; color: var(--muted); }
.empty { padding: 45px; color: var(--muted); text-align: center; }
.source, .status, .priority { display: inline-flex; align-items: center; padding: 4px 8px; border-radius: 999px; font-size: 12px; font-weight: 800; white-space: nowrap; }
.source-whatsapp { background: var(--green-soft); color: var(--green-dark); }
.source-telegram_manual { background: var(--blue-soft); color: var(--blue); }
.status-new { background: var(--yellow-soft); color: var(--yellow); }
.status-in_progress { background: var(--blue-soft); color: var(--blue); }
.status-done { background: var(--green-soft); color: var(--green-dark); }
.status-invalid { background: var(--red-soft); color: var(--red); }
.priority-low { background: #eef1f2; color: #5d696d; }
.priority-normal { background: var(--blue-soft); color: var(--blue); }
.priority-high { background: var(--yellow-soft); color: var(--yellow); }
.priority-urgent { background: var(--red-soft); color: var(--red); }

.manual-panel { display: grid; grid-template-columns: minmax(260px, .7fr) 2fr; gap: 40px; margin-top: 18px; padding: 24px; }
.manual-panel p:last-child { color: var(--muted); }
.manual-form { display: grid; grid-template-columns: repeat(2, 1fr); gap: 13px; }
.manual-form .wide { grid-column: 1 / -1; }
.manual-form .button { align-self: end; }

.back { display: inline-block; margin-bottom: 18px; color: var(--green); font-weight: 700; }
.ticket-header { padding: 23px 25px; border-radius: 15px; background: #173b33; color: #fff; }
.ticket-header .eyebrow { color: #55dba8; }
.ticket-header p:last-child { margin-bottom: 0; color: #c6d9d3; white-space: pre-line; }
.ticket-badges { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }
.ticket-layout { display: grid; grid-template-columns: minmax(0, 2fr) minmax(300px, .85fr); gap: 18px; }
.ticket-main, .actions-panel, .history-panel { padding: 24px; }
.ticket-main-heading { display: flex; justify-content: space-between; align-items: center; gap: 14px; }
.ticket-main-heading h2 { margin-bottom: 0; }
.whatsapp-button { border-color: #1da851; background: #25d366; color: #073b1c; }
.whatsapp-button:hover { border-color: #178d44; background: #20bd5a; }
.button.disabled { cursor: not-allowed; opacity: .55; }
.direct-chat-note { margin-top: 8px; }
.detail-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; margin: 18px 0; }
.detail-grid > div { display: grid; gap: 4px; padding: 13px; border-radius: 9px; background: var(--surface-subtle); }
.detail-grid span { color: var(--muted); font-size: 12px; }
.bin-grid { grid-template-columns: repeat(3, 1fr); border-top: 1px solid var(--line); padding-top: 18px; }
.message { margin: 0; padding: 16px; overflow: auto; border: 1px solid var(--line); border-radius: 10px; background: var(--message-bg); font: 14px/1.6 ui-monospace, SFMono-Regular, Consolas, monospace; white-space: pre-wrap; }
.attachment { margin-top: 12px; padding: 12px; border-radius: 8px; background: var(--blue-soft); color: var(--blue); }
.actions-panel form { display: grid; gap: 14px; }
.status-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.status-action { padding: 10px; border: 0; border-radius: 8px; cursor: pointer; font-weight: 800; }
.action-new { background: var(--yellow-soft); color: var(--yellow); }
.action-in_progress { background: var(--blue-soft); color: var(--blue); }
.action-done { background: var(--green-soft); color: var(--green-dark); }
.action-invalid { background: var(--red-soft); color: var(--red); }
.muted { margin: 12px 0 0; color: var(--muted); font-size: 13px; }
.history-panel { margin-top: 18px; }
.classification-panel { margin-top: 18px; padding: 24px; }
.classification-panel form { display: grid; gap: 12px; }
.reply-panel { margin-top: 18px; padding: 24px; }
.reply-panel form { display: grid; gap: 12px; margin-top: 14px; }
.quick-replies { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; }
.quick-replies .button { min-width: 0; padding: 0 8px; }
.history-panel ul { margin: 0; padding: 0; list-style: none; }
.history-panel li { display: grid; gap: 3px; padding: 12px 0; border-top: 1px solid var(--line); }
.history-panel li:first-child { border-top: 0; }
.history-panel small { color: var(--muted); }

.outbox-list { display: grid; gap: 14px; }
.outbox-card { padding: 20px; }
.outbox-card > div { display: flex; justify-content: space-between; align-items: center; gap: 15px; }
.outbox-card h2 { margin: 16px 0 4px; }
.outbox-card details { margin-top: 14px; }
.outbox-card summary { color: var(--green); cursor: pointer; font-weight: 700; }
.outbox-card details .message { margin-top: 10px; }
.auto-reply-phone { color: var(--muted); font-weight: 700; }
.delivery-error { margin: 10px 0 0; padding: 10px 12px; border-radius: 8px; background: var(--red-soft); color: var(--red); font-size: 13px; }

.integration-status, .setup-steps { padding: 24px; }
.integration-status > div { display: grid; gap: 4px; padding: 12px 0; border-top: 1px solid var(--line); }
.integration-status > div:first-of-type { border-top: 0; }
.integration-status span { color: var(--muted); font-size: 12px; }
.integration-status code, .setup-steps code { overflow-wrap: anywhere; }
.setup-steps { margin-top: 18px; }
.setup-steps ol { margin: 0; padding-left: 22px; }
.setup-steps li { margin: 9px 0; }
code { padding: 2px 5px; border-radius: 5px; background: var(--code-bg); color: var(--green-dark); font: 13px/1.5 ui-monospace, SFMono-Regular, Consolas, monospace; }

.rules-panel { padding: 24px; }
.rules-grid { display: grid; grid-template-columns: repeat(2, minmax(260px, 1fr)); gap: 16px; }
.rules-actions { display: flex; justify-content: flex-end; margin-top: 18px; }

.context-menu { position: fixed; z-index: 1000; width: 250px; padding: 7px; border: 1px solid var(--field-border); border-radius: 12px; background: var(--panel); box-shadow: 0 18px 45px rgba(15, 35, 30, .22); }
.context-heading { display: grid; gap: 2px; padding: 8px 10px 10px; border-bottom: 1px solid var(--line); }
.context-heading small { color: var(--muted); }
.context-heading strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.context-menu button { display: flex; align-items: center; gap: 10px; width: 100%; margin-top: 3px; padding: 10px; border: 0; border-radius: 8px; background: var(--panel); color: var(--text); text-align: left; cursor: pointer; }
.context-menu button:hover { background: var(--surface-hover); }
.context-menu.busy { pointer-events: none; opacity: .65; }
.context-dot { width: 9px; height: 9px; border-radius: 50%; }
.dot-new { background: #d99a13; }
.dot-in_progress { background: #3f80c9; }
.dot-done { background: #159269; }
.dot-invalid { background: #c65050; }

.whatsapp-heading { align-items: center; }
.connection-badge { display: inline-flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: 999px; font-size: 12px; font-weight: 800; white-space: nowrap; }
.connection-badge::before { width: 8px; height: 8px; border-radius: 50%; background: currentColor; content: ""; }
.connection-badge.online { background: var(--green-soft); color: var(--green-dark); }
.connection-badge.offline { background: var(--red-soft); color: var(--red); }
.chat-shell { display: grid; grid-template-columns: minmax(280px, 340px) minmax(0, 1fr); height: min(720px, calc(100vh - 215px)); min-height: 540px; gap: 14px; }
.chat-sidebar, .chat-main { min-height: 0; overflow: hidden; }
.chat-sidebar { display: grid; grid-template-rows: auto auto minmax(0, 1fr); padding: 14px; }
.chat-sidebar-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.chat-sidebar-head h2 { margin: 0; }
.icon-button { border: 0; background: transparent; color: var(--green); cursor: pointer; font-weight: 800; }
.chat-sidebar input { margin: 12px 0; }
.chat-list { min-height: 0; overflow-y: auto; border-top: 1px solid var(--line); }
.chat-list-item { display: grid; gap: 5px; width: 100%; padding: 12px 10px; border: 0; border-bottom: 1px solid var(--line); background: transparent; color: var(--text); text-align: left; cursor: pointer; }
.chat-list-item:hover { background: var(--surface-hover); }
.chat-list-item.active { background: var(--green-soft); }
.chat-list-top, .chat-list-bottom { display: flex; align-items: center; justify-content: space-between; gap: 10px; min-width: 0; }
.chat-list-top strong, .chat-list-bottom > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-list-top time { flex: 0 0 auto; }
.chat-list-bottom { color: var(--muted); font-size: 12px; }
.chat-unread { min-width: 21px; padding: 2px 6px; border-radius: 999px; background: var(--green); color: #fff; text-align: center; }
.chat-main { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; }
.chat-header { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 16px 18px; border-bottom: 1px solid var(--line); }
.chat-header small { color: var(--muted); }
.chat-header h2 { margin: 2px 0 0; }
.chat-memory-note { padding: 5px 8px; border-radius: 7px; background: var(--surface-subtle); color: var(--muted); font-size: 11px; font-weight: 700; }
.chat-messages { min-height: 0; padding: 18px; overflow-y: auto; background: var(--surface-subtle); }
.chat-message-row { display: flex; margin: 6px 0; }
.chat-message-row.outgoing { justify-content: flex-end; }
.chat-bubble { display: grid; gap: 5px; max-width: min(72%, 660px); padding: 9px 12px; border: 1px solid var(--line); border-radius: 12px 12px 12px 3px; background: var(--panel); white-space: pre-wrap; overflow-wrap: anywhere; }
.chat-message-row.outgoing .chat-bubble { border-color: transparent; border-radius: 12px 12px 3px 12px; background: var(--green-soft); }
.chat-message-meta { display: flex; justify-content: flex-end; gap: 7px; align-items: center; color: var(--muted); }
.chat-bubble time, .chat-delivery { font-size: 10px; }
.chat-message-row.outgoing .chat-delivery { color: var(--green); font-weight: 700; }
.chat-composer { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: end; gap: 10px; padding: 13px; border-top: 1px solid var(--line); }
.chat-composer textarea { min-height: 46px; max-height: 130px; }
.chat-placeholder { margin: auto; padding: 24px; color: var(--muted); text-align: center; }
.chat-page-note { text-align: center; }
:root[data-theme="dark"] .priority-select-low,
:root[data-theme="dark"] .priority-low { background: #2a3335; color: #bdc8ca; }

@media (max-width: 1000px) {
  .metrics { grid-template-columns: repeat(2, 1fr); }
  .filters { grid-template-columns: 1fr 1fr; }
  .filters input { grid-column: 1 / -1; }
  .manual-panel, .ticket-layout { grid-template-columns: 1fr; }
  .topbar { gap: 16px; }
  .chat-shell { grid-template-columns: 280px minmax(0, 1fr); }
}

@media (max-width: 700px) {
  .topbar { flex-wrap: wrap; gap: 8px; padding: 12px 4vw 0; }
  nav { order: 3; width: 100%; overflow-x: auto; }
  .nav-link { min-height: 46px; white-space: nowrap; }
  .shift-form { margin-left: auto; }
  .theme-toggle { margin-left: auto; }
  .prototype { display: none; }
  .page { width: 94vw; margin-top: 22px; }
  .page-heading { display: grid; }
  .metrics, .filters, .manual-form, .detail-grid, .bin-grid, .rules-grid { grid-template-columns: 1fr; }
  .filters input, .manual-form .wide { grid-column: auto; }
  .manual-panel { gap: 10px; padding: 18px; }
  .ticket-header { display: grid; }
  .ticket-main-heading { display: grid; }
  .pagination { align-items: stretch; flex-direction: column; }
  .pagination-links { justify-content: flex-start; }
  .chat-shell { grid-template-columns: 1fr; height: auto; }
  .chat-sidebar { height: 300px; }
  .chat-main { height: 560px; }
  .chat-bubble { max-width: 88%; }
}
