:root {
  color-scheme: light;
  --selected: #edf3ff;
  --accent-soft: #eaf0fd;
  --note-bg: #fff7df;
  --note-text: #805516;
  --bg: #eef2f7;
  --surface: #ffffff;
  --surface-soft: #f6f8fb;
  --line: #dfe5ec;
  --text: #172033;
  --muted: #718096;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #b42318;
  --shadow: 0 18px 50px rgba(22, 32, 51, .12);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, input, select, textarea { font: inherit; }
button, select { cursor: pointer; }
.hidden { display: none !important; }

.login-view {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(circle at 15% 20%, rgba(37, 99, 235, .16), transparent 34%),
    radial-gradient(circle at 85% 80%, rgba(15, 118, 110, .12), transparent 30%),
    #edf2f8;
}
.login-card {
  width: min(100%, 390px);
  display: grid;
  gap: 18px;
  padding: 34px;
  border: 1px solid rgba(255, 255, 255, .8);
  border-radius: 22px;
  background: rgba(255, 255, 255, .94);
  box-shadow: var(--shadow);
}
.login-card h1 { margin: 0 0 4px; font-size: 25px; }
.login-card p { margin: 0; color: var(--muted); }
.login-card label { display: grid; gap: 7px; font-weight: 600; }
.login-card input {
  width: 100%;
  padding: 12px 13px;
  border: 1px solid var(--line);
  border-radius: 10px;
  outline: none;
}
.login-card input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37, 99, 235, .12); }
.primary, .send-button {
  border: 0;
  border-radius: 10px;
  background: var(--primary);
  color: #fff;
  font-weight: 700;
}
.primary { padding: 12px; }
.primary:hover, .send-button:hover { background: var(--primary-dark); }
.primary:disabled, .send-button:disabled { cursor: default; opacity: .5; }
.error { color: var(--danger); font-size: 13px; }

.app { height: 100%; display: flex; flex-direction: column; }
.topbar {
  min-height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 12px 22px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.brand { display: flex; align-items: center; gap: 11px; }
.brand > div:last-child { display: grid; }
.brand strong { font-size: 15px; }
.brand span { color: var(--muted); font-size: 11px; }
.topbar-actions { display: flex; align-items: center; gap: 22px; }
.project-filter { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; }
.project-filter select, .status-select {
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--surface);
  color: var(--text);
  padding: 8px 10px;
}
.operator { display: flex; align-items: center; gap: 10px; }
.operator span { font-weight: 600; }
.ghost, .icon-button, .back-button {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
}
.ghost { padding: 7px 10px; border-radius: 9px; }
.icon-button, .back-button { width: 34px; height: 34px; border-radius: 9px; font-size: 18px; }
.global-error { padding: 9px 20px; background: #fef3f2; color: var(--danger); border-bottom: 1px solid #fecdca; }

.workspace {
  min-height: 0;
  flex: 1;
  display: grid;
  grid-template-columns: 370px minmax(0, 1fr);
  margin: 16px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--surface);
  box-shadow: 0 8px 30px rgba(22, 32, 51, .06);
}
.tickets-pane { min-height: 0; display: flex; flex-direction: column; border-right: 1px solid var(--line); }
.pane-head { display: flex; align-items: center; justify-content: space-between; padding: 17px; border-bottom: 1px solid var(--line); }
.pane-head > div { display: flex; align-items: baseline; gap: 8px; }
.pane-head h2 { margin: 0; font-size: 17px; }
.pane-head span { color: var(--muted); font-size: 12px; }
.status-filters { display: flex; gap: 6px; padding: 11px 12px; overflow-x: auto; border-bottom: 1px solid var(--line); }
.filter-chip {
  flex: 0 0 auto;
  padding: 6px 9px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: 11px;
}
.filter-chip.active { border-color: #172033; background: #172033; color: #fff; }
.tickets-list { min-height: 0; flex: 1; overflow-y: auto; }
.ticket-card { width: 100%; display: grid; gap: 7px; padding: 14px 16px; border: 0; border-bottom: 1px solid var(--line); background: #fff; text-align: left; }
.ticket-card:hover { background: var(--surface-soft); }
.ticket-card.active { background: #edf4ff; box-shadow: inset 3px 0 var(--primary); }
.ticket-row { display: flex; justify-content: space-between; align-items: flex-start; gap: 9px; }
.ticket-title { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 700; }
.ticket-email, .ticket-details, .ticket-date { color: var(--muted); font-size: 11px; }
.ticket-email { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ticket-details { color: #8492a6; }
.badge { flex: 0 0 auto; padding: 3px 7px; border-radius: 999px; background: #edf2f7; color: #526176; font-size: 10px; font-weight: 700; }
.badge.open { background: #e8f3ff; color: #175cd3; }
.badge.inprogress { background: #eef4ff; color: #3538cd; }
.badge.waitinguser { background: #fff7e6; color: #b54708; }
.badge.resolved { background: #ecfdf3; color: #027a48; }
.badge.closed { background: #f2f4f7; color: #667085; }
.list-state { padding: 36px 16px; text-align: center; color: var(--muted); }

.chat-pane { min-width: 0; min-height: 0; background: var(--surface-soft); }
.chat-pane.empty { display: grid; place-items: center; }
.empty-chat { text-align: center; color: var(--muted); }
.empty-chat h2 { margin: 10px 0 5px; color: var(--text); font-size: 18px; }
.empty-chat p { margin: 0; }
.empty-icon { font-size: 26px; }
.active-chat { height: 100%; display: flex; flex-direction: column; }
.chat-head { min-height: 72px; display: flex; align-items: center; gap: 12px; padding: 13px 17px; border-bottom: 1px solid var(--line); background: var(--surface); }
.back-button { display: none; }
.chat-title { min-width: 0; flex: 1; display: grid; gap: 3px; }
.chat-title strong, .chat-title span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-title span { color: var(--muted); font-size: 11px; }
.messages-list { min-height: 0; flex: 1; display: flex; flex-direction: column; gap: 11px; padding: 18px; overflow-y: auto; }
.message { max-width: min(75%, 680px); display: grid; gap: 4px; }
.message.guest { align-self: flex-start; }
.message.operator { align-self: flex-end; }
.message.internal { align-self: flex-end; }
.message-meta { padding: 0 4px; color: var(--muted); font-size: 10px; }
.message.operator .message-meta, .message.internal .message-meta { text-align: right; }
.message-body { padding: 10px 13px; border: 1px solid var(--line); border-radius: 13px; background: #fff; white-space: pre-wrap; word-break: break-word; }
.message.operator .message-body { border-color: #172033; background: #172033; color: #fff; }
.message.internal .message-body { border-color: #f6c344; background: #fffaeb; color: #7a2e0e; }
.attachments { display: grid; gap: 6px; margin-top: 8px; }
.attachment { display: block; max-width: 320px; padding: 8px; border-radius: 9px; background: rgba(255,255,255,.12); color: inherit; text-decoration: none; }
.attachment img, .attachment video { display: block; max-width: 100%; max-height: 250px; border-radius: 7px; }
.composer { border-top: 1px solid var(--line); background: var(--surface); padding: 11px 14px; }
.composer-row { display: flex; align-items: flex-end; gap: 9px; }
.attach-button { width: 38px; height: 38px; flex: 0 0 auto; display: grid; place-items: center; border: 1px solid var(--line); border-radius: 10px; cursor: pointer; font-size: 21px; }
.composer textarea { min-height: 38px; max-height: 120px; flex: 1; resize: none; padding: 9px 11px; border: 1px solid var(--line); border-radius: 10px; outline: none; }
.composer textarea:focus { border-color: var(--primary); }
.internal-note { display: flex; align-items: center; gap: 5px; padding: 9px 5px; color: var(--muted); font-size: 11px; cursor: pointer; }
.send-button { height: 38px; padding: 0 15px; }
.selected-files { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 9px; }
.file-pill { border: 1px solid var(--line); border-radius: 999px; background: var(--surface-soft); padding: 5px 9px; color: var(--text); font-size: 11px; }

@media (max-width: 760px) {
  .topbar { align-items: flex-start; padding: 10px 12px; }
  .brand span, .project-filter > span, .operator > span { display: none; }
  .topbar-actions { gap: 8px; }
  .workspace { grid-template-columns: 1fr; margin: 0; border: 0; border-radius: 0; }
  .tickets-pane { border-right: 0; }
  .chat-pane { display: none; }
  .workspace.chat-open .tickets-pane { display: none; }
  .workspace.chat-open .chat-pane { display: block; }
  .back-button { display: block; }
  .message { max-width: 88%; }
  .internal-note span { display: none; }
}
/* Project customer card */
#customer-dialog { width: min(1000px, 94vw); max-height: 90vh; border: 1px solid #dce1e8; border-radius: 16px; padding: 24px; color: #172033; }
#customer-dialog::backdrop { background: #15203888; }
#customer-dialog header, .customer-actions { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.customer-fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; margin-bottom: 16px; }
.customer-fields label { display: grid; gap: 6px; }
.customer-fields input { width: 100%; padding: 10px; border: 1px solid #ccd3df; border-radius: 6px; }
.customer-actions { justify-content: flex-start; margin-top: 20px; }
.customer-table { overflow: auto; }
.customer-table table { border-collapse: collapse; width: 100%; font-size: 13px; }
.customer-table th, .customer-table td { padding: 8px; border-bottom: 1px solid #e0e5ec; text-align: left; }
#customer-error { color: #b42318; }
#customer-result { white-space: pre-wrap; overflow-wrap: anywhere; }
#staff-dialog { width: min(820px, 94vw); max-height: 90vh; padding: 24px; border: 1px solid var(--line); border-radius: 16px; background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
#staff-dialog::backdrop { background: #15203888; }
#staff-dialog[open] { animation: support-enter .2s ease-out; }
#staff-dialog header { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
#staff-dialog h2 { font-size: 20px; margin: 0; }
#staff-dialog h3 { font-size: 16px; }
.staff-help, .staff-row span { color: var(--muted); font-size: 12px; }
.staff-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.staff-row > div { display: grid; gap: 4px; min-width: 0; overflow-wrap: anywhere; }
#staff-list { max-height: 230px; overflow: auto; }
#staff-dialog fieldset { border: 1px solid var(--line); border-radius: 8px; }
#staff-projects { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 10px; max-height: 180px; overflow: auto; }
#staff-projects label { display: flex; align-items: center; gap: 8px; overflow-wrap: anywhere; }

/* Shared theme and compact operator workspace. */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #10151e;
  --surface: #19212e;
  --surface-soft: #141c27;
  --line: #2c384b;
  --text: #e6edf7;
  --muted: #9eacc2;
  --primary: #638fff;
  --primary-dark: #4b7beb;
  --danger: #ff9d9d;
  --selected: #223451;
  --accent-soft: #273a5c;
  --note-bg: #382d1c;
  --note-text: #f4d496;
  --shadow: 0 18px 50px #0004;
}
body { overflow: hidden; }
button, input, select, textarea { color: var(--text); }
input, select, textarea { background: var(--surface); }
button { transition: background-color .15s, border-color .15s; }
button:disabled { opacity: .5; cursor: default; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 3px; }
* { scrollbar-width: thin; scrollbar-color: var(--line) transparent; }
.login-view { position: relative; background: var(--bg); overflow: auto; height: 100%; }
.login-card { background: var(--surface); border-color: var(--line); border-radius: 16px; }
.theme-toggle { width: 36px; height: 36px; flex: 0 0 auto; display: grid; place-items: center; padding: 8px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); color: var(--text); }
.theme-toggle svg { width: 18px; height: 18px; }
.theme-toggle:hover { background: var(--surface-soft); border-color: var(--muted); }
.refresh-icon { display: block; width: 18px; height: 18px; margin: auto; }
.send-button { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-width: 132px; }
.message-arriving { animation: message-arrive .24s ease-out; transform-origin: bottom right; }
.message-arriving.guest { transform-origin: bottom left; }
@keyframes message-arrive {
  from { opacity: 0; transform: translateY(10px) scale(.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
.is-refreshing .refresh-icon { animation: support-spin .75s linear infinite; }
#refresh-button.is-refreshing { opacity: 1; color: var(--primary); background: var(--accent-soft); }
@keyframes support-spin { to { transform: rotate(360deg); } }
@keyframes support-enter {
  from { opacity: 0; transform: translateY(8px) scale(.99); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes support-backdrop { from { opacity: 0; } to { opacity: 1; } }
.ghost, .icon-button, .theme-toggle, .send-button, .primary, .filter-chip, #customer-dialog button {
  transition: background-color .18s ease, color .18s ease, border-color .18s ease, box-shadow .18s ease, transform .12s ease;
}
.ghost:hover, .icon-button:hover, #customer-dialog button:hover { background: var(--accent-soft); }
.ghost:active, .icon-button:active, .theme-toggle:active, .send-button:active, .primary:active, .filter-chip:active, #customer-dialog button:active {
  transform: scale(.96);
}
.ticket-card { transition: background-color .18s ease, box-shadow .18s ease; }
.project-group-heading { transition: background-color .18s ease, color .18s ease; }
.project-group-heading:hover { background: var(--accent-soft); color: var(--primary); }
.login-card, .empty-chat { animation: support-enter .24s ease-out; }
#customer-dialog[open] { animation: support-enter .2s ease-out; }
#customer-dialog[open]::backdrop { animation: support-backdrop .2s ease-out; }
.composer textarea, .customer-fields input, .login-card input {
  transition: border-color .18s ease, box-shadow .18s ease, background-color .18s ease;
}
.customer-fields input:focus, .composer textarea:focus { box-shadow: 0 0 0 3px var(--accent-soft); }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
.login-theme { position: absolute; top: 20px; right: 20px; }
.topbar { min-height: 72px; flex-shrink: 0; padding: 14px 24px; }
.brand strong { font-size: 19px; letter-spacing: -.4px; }
.brand span { font-size: 12px; }
.topbar-actions { gap: 16px; }
.project-filter select { max-width: 220px; }
.operator span { max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; }
.workspace { grid-template-columns: minmax(310px, 350px) minmax(0, 1fr); margin: 16px 20px 20px; border-radius: 12px; box-shadow: none; }
.tickets-pane { min-width: 0; background: var(--surface); }
.pane-head { padding: 18px 16px; }
.pane-head h2 { font-size: 16px; }
#ticket-count { border-radius: 6px; background: var(--surface-soft); padding: 2px 7px; }
.status-filters { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; overflow: visible; padding: 12px; }
.filter-chip { border-radius: 7px; padding: 7px 4px; font-size: 12px; color: var(--muted); }
.filter-chip.active { background: var(--accent-soft); border-color: var(--primary); color: var(--primary); }
.project-group-heading { position: sticky; top: 0; z-index: 1; display: flex; align-items: center; gap: 8px; width: 100%; padding: 10px 16px; text-align: left; overflow-wrap: anywhere; border: 0; border-bottom: 1px solid var(--line); background: var(--surface-soft); color: var(--text); font-size: 12px; font-weight: 700; }
.project-group-heading span { margin-left: auto; padding: 1px 7px; border-radius: 5px; background: var(--surface); color: var(--muted); }
.ticket-card { background: var(--surface); color: var(--text); gap: 6px; padding: 14px 16px; }
.ticket-card.active { background: var(--selected); }
.ticket-title { font-size: 13px; font-weight: 600; }
.ticket-email { font-size: 12px; }
.ticket-details { display: flex; align-items: center; flex-wrap: wrap; gap: 7px; color: var(--muted); }
.project-label { display: inline-block; width: fit-content; max-width: 100%; padding: 2px 7px; border-radius: 5px; background: var(--accent-soft); color: var(--primary); font-size: 11px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ticket-date { font-size: 10px; }
.badge { border-radius: 5px; font-size: 10px; font-weight: 600; }
:root[data-theme="dark"] .badge.open, :root[data-theme="dark"] .badge.inprogress { background: #263e60; color: #a9c6ff; }
:root[data-theme="dark"] .badge.waitinguser { background: #453720; color: #f3d39a; }
:root[data-theme="dark"] .badge.resolved { background: #1d4035; color: #8ddbb5; }
:root[data-theme="dark"] .badge.closed { background: #303a4b; color: #bac6d9; }
.load-more { margin: 10px 12px; flex-shrink: 0; }
.chat-head { padding: 18px 22px; min-height: 100px; flex-wrap: wrap; gap: 12px; }
.chat-title { flex-basis: 200px; gap: 6px; }
.chat-title strong { font-size: 15px; }
.chat-title .project-label { color: var(--primary); }
.chat-title span { font-size: 12px; }
.chat-head > button, .chat-head > select { flex-shrink: 0; font-size: 12px; }
.messages-list { padding: 24px; gap: 18px; }
.message { max-width: min(85%, 720px); }
.message-meta { font-size: 11px; }
.message-body { background: var(--surface); padding: 12px 15px; border-radius: 10px; line-height: 1.6; }
.message.operator .message-body { background: var(--accent-soft); color: var(--text); border-color: var(--line); }
.message.internal .message-body { background: var(--note-bg); color: var(--note-text); border-color: var(--note-text); }
.composer { padding: 16px 20px; }
.composer textarea { min-width: 0; background: var(--surface-soft); color: var(--text); }
.internal-note { font-size: 12px; }
.global-error { background: var(--note-bg); border-color: var(--line); color: var(--danger); }
#customer-dialog { background: var(--surface); color: var(--text); border-color: var(--line); padding: 28px; box-shadow: var(--shadow); }
#customer-dialog h2 { margin: 0; font-size: 20px; }
#customer-dialog h3 { font-size: 14px; padding-top: 20px; border-top: 1px solid var(--line); }
#customer-dialog button { padding: 9px 13px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface-soft); color: var(--text); }
#customer-dialog #customer-save { background: var(--primary); color: #fff; border-color: transparent; }
#customer-info { color: var(--muted); }
#customer-error { color: var(--danger); }
.customer-fields label { color: var(--muted); font-size: 12px; }
.customer-fields input { background: var(--surface-soft); color: var(--text); border-color: var(--line); border-radius: 8px; font-size: 14px; }
.customer-actions { flex-wrap: wrap; }
.customer-table th, .customer-table td { border-color: var(--line); padding: 10px; white-space: nowrap; }
.customer-table th { color: var(--muted); font-size: 11px; background: var(--surface-soft); }
#customer-result:not(:empty) { padding: 14px; border-radius: 8px; background: var(--surface-soft); }
@media (max-width: 1000px) {
  .operator > span { display: none; }
  .workspace { margin: 10px; grid-template-columns: 310px minmax(0, 1fr); }
  .chat-title { flex-basis: calc(100% - 50px); }
}
@media (max-width: 760px) {
  .topbar { min-height: auto; padding: 12px; gap: 12px; flex-wrap: wrap; align-items: center; }
  .brand strong { font-size: 16px; }
  .topbar-actions { flex-wrap: wrap; gap: 8px; }
  .project-filter select { max-width: 145px; }
  .theme-toggle { padding: 8px; }
  .workspace { grid-template-columns: 1fr; margin: 0; }
  .chat-head { padding: 14px; }
  .chat-title { flex-basis: calc(100% - 60px); }
  .messages-list { padding: 16px; }
  .composer { padding: 12px; }
  .composer-row { flex-wrap: wrap; }
  .composer textarea { flex-basis: calc(100% - 50px); }
  .send-button { margin-left: auto; }
  #customer-dialog { padding: 18px; }
}
