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

:root {
  --bg: #f5f5f5;
  --surface: #ffffff;
  --border: #e5e7eb;
  --text: #111827;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --sidebar-bg: #111827;
  --sidebar-text: #d1d5db;
  --sidebar-active: #ffffff;
  --sidebar-hover: rgba(255,255,255,.08);
  --sidebar-w: 220px;
  --red: #dc2626; --red-bg: #fef2f2; --red-border: #fecaca;
  --orange: #d97706; --orange-bg: #fffbeb; --orange-border: #fde68a;
  --green: #16a34a; --green-bg: #f0fdf4; --green-border: #bbf7d0;
  --blue: #2563eb; --blue-bg: #eff6ff; --blue-border: #bfdbfe;
  --purple: #7c3aed; --purple-bg: #f5f3ff; --purple-border: #ddd6fe;
  --gray-bg: #f3f4f6; --gray-border: #e5e7eb;
}

body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; color: var(--text); font-size: 14px; line-height: 1.5; }
a { text-decoration: none; color: inherit; }

/* ── Layouts ── */
.auth-layout { background: var(--bg); min-height: 100vh; display: flex; align-items: center; justify-content: center; }
.app-layout   { display: flex; min-height: 100vh; background: var(--bg); }

/* ── Auth ── */
.auth-wrap { width: 100%; display: flex; align-items: center; justify-content: center; padding: 20px; }
.auth-box  { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 40px 36px; width: 100%; max-width: 360px; }
.auth-logo { font-size: 15px; font-weight: 700; margin-bottom: 20px; display: flex; align-items: center; gap: 8px; }
.auth-box h2 { font-size: 20px; margin-bottom: 20px; }
.auth-box form { display: flex; flex-direction: column; gap: 12px; }
.auth-box input { padding: 10px 12px; border: 1px solid var(--border); border-radius: 6px; font-size: 14px; outline: none; }
.auth-box input:focus { border-color: var(--accent); }
.auth-box button { padding: 10px; background: var(--accent); color: #fff; border: none; border-radius: 6px; font-size: 14px; font-weight: 500; cursor: pointer; }
.auth-box button:hover { background: var(--accent-hover); }

/* ── Sidebar ── */
.sidebar {
  width: var(--sidebar-w); background: var(--sidebar-bg);
  flex-shrink: 0; display: flex; flex-direction: column;
  transition: transform .25s ease;
}
.sidebar-logo {
  padding: 20px 20px 16px; font-size: 15px; font-weight: 700;
  color: #fff; display: flex; align-items: center; gap: 8px;
  border-bottom: 1px solid rgba(255,255,255,.08);
}
.sidebar-sub { font-size: 11px; font-weight: 400; color: var(--sidebar-text); margin-left: 2px; }
.sidebar-nav { flex: 1; padding: 8px 0; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 20px; color: var(--sidebar-text);
  font-size: 13px; cursor: pointer; transition: background .1s; position: relative;
}
.nav-item:hover  { background: var(--sidebar-hover); color: var(--sidebar-active); }
.nav-item.active { background: rgba(255,255,255,.12); color: var(--sidebar-active); font-weight: 500; }
.nav-icon { width: 16px; text-align: center; font-size: 12px; }
.sidebar-footer { padding: 12px 0 16px; border-top: 1px solid rgba(255,255,255,.08); }
.badge-count { margin-left: auto; background: var(--red); color: #fff; font-size: 10px; font-weight: 700; padding: 1px 6px; border-radius: 10px; }

/* Hamburger — mobile only */
.hamburger {
  display: none; background: none; border: none; color: #fff;
  font-size: 20px; cursor: pointer; margin-left: auto; padding: 2px 4px; line-height: 1;
}
/* Overlay behind sidebar on mobile */
.sidebar-overlay {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 90;
}
body.sidebar-open .sidebar-overlay { display: block; }

/* ── Logo dot ── */
.logo-dot { width: 8px; height: 8px; background: var(--accent); border-radius: 50%; display: inline-block; flex-shrink: 0; }

/* ── Mobile top bar (hidden on desktop) ── */
.mobile-topbar {
  display: none; position: sticky; top: 0; z-index: 80;
  background: var(--sidebar-bg); color: #fff;
  padding: 12px 16px; align-items: center; gap: 12px;
  border-bottom: 1px solid rgba(255,255,255,.1);
}
.mobile-topbar-title { font-size: 15px; font-weight: 700; flex: 1; }
.mobile-hamburger {
  background: none; border: none; color: #fff;
  font-size: 22px; cursor: pointer; padding: 0; line-height: 1;
}

/* ── Main content ── */
.main-content { flex: 1; padding: 28px 32px; overflow-y: auto; min-width: 0; }

/* ── Page header ── */
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 24px; gap: 12px; flex-wrap: wrap; }
.page-header-left { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.page-header-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.page-header h1 { font-size: 22px; font-weight: 700; }

/* ── Stats ── */
.stats-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 14px; margin-bottom: 28px; }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 18px 20px; text-decoration: none; color: var(--text); transition: box-shadow .15s; }
.stat-card:hover { box-shadow: 0 2px 8px rgba(0,0,0,.06); }
.stat-card--orange { border-left: 3px solid var(--orange); }
.stat-card--green  { border-left: 3px solid var(--green); }
.stat-card--blue   { border-left: 3px solid var(--accent); }
.stat-value { font-size: 28px; font-weight: 700; line-height: 1; }
.stat-label { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* ── Section header ── */
.section-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.section-header h2 { font-size: 16px; font-weight: 600; }

/* ── Statistics period selector ── */
.stat-period-btns { display: flex; gap: 6px; }

/* ── Buttons ── */
.btn { display: inline-flex; align-items: center; padding: 7px 14px; border-radius: 6px; font-size: 13px; font-weight: 500; cursor: pointer; border: 1px solid transparent; white-space: nowrap; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost   { background: transparent; border-color: var(--border); color: var(--text); }
.btn-ghost:hover { background: var(--bg); }
.btn-success { background: var(--green);  color: #fff; }
.btn-danger  { background: var(--red);    color: #fff; }
.btn-warning { background: var(--orange); color: #fff; }
.btn-ai-on  { background: var(--green-bg); color: var(--green); border-color: var(--green-border); font-weight: 600; }
.btn-ai-off { background: var(--gray-bg);  color: var(--muted); border-color: var(--gray-border);  font-weight: 600; }

/* ── Table ── */
.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; min-width: 560px; }
.data-table th { padding: 11px 14px; text-align: left; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); background: var(--bg); border-bottom: 1px solid var(--border); white-space: nowrap; }
.data-table td { padding: 11px 14px; border-bottom: 1px solid var(--border); }
.data-table tr:last-child td { border-bottom: none; }
.data-table tr.clickable { cursor: pointer; }
.data-table tr.clickable:hover td { background: var(--bg); }
.empty-row { text-align: center; color: var(--muted); padding: 32px; }

/* ── Filters ── */
.filters { margin-bottom: 16px; }
.filter-form { display: flex; gap: 8px; flex-wrap: wrap; }
.filter-input  { padding: 7px 12px; border: 1px solid var(--border); border-radius: 6px; font-size: 13px; outline: none; min-width: 200px; flex: 1; }
.filter-input:focus { border-color: var(--accent); }
.filter-select { padding: 7px 10px; border: 1px solid var(--border); border-radius: 6px; font-size: 13px; outline: none; background: var(--surface); }

/* ── Pagination ── */
.pagination { display: flex; align-items: center; gap: 12px; margin-top: 16px; flex-wrap: wrap; }

/* ── Tags ── */
.tag { display: inline-block; padding: 2px 8px; border-radius: 4px; font-size: 11px; font-weight: 600; }
.tag--red    { background: var(--red-bg);    color: var(--red);    border: 1px solid var(--red-border); }
.tag--orange { background: var(--orange-bg); color: var(--orange); border: 1px solid var(--orange-border); }
.tag--blue   { background: var(--blue-bg);   color: var(--blue);   border: 1px solid var(--blue-border); }
.tag--green  { background: var(--green-bg);  color: var(--green);  border: 1px solid var(--green-border); }
.tag--purple { background: var(--purple-bg); color: var(--purple); border: 1px solid var(--purple-border); }
.tag--gray   { background: var(--gray-bg);   color: var(--muted);  border: 1px solid var(--gray-border); }
.tag-list { display: flex; flex-wrap: wrap; gap: 4px; }

/* ── Status badges ── */
.status { display: inline-block; padding: 2px 9px; border-radius: 20px; font-size: 11px; font-weight: 600; text-transform: capitalize; }
.status--blue   { background: var(--blue-bg);   color: var(--blue); }
.status--orange { background: var(--orange-bg); color: var(--orange); }
.status--green  { background: var(--green-bg);  color: var(--green); }
.status--gray   { background: var(--gray-bg);   color: var(--muted); }

/* ── Ticket detail layout ── */
.ticket-layout { display: grid; grid-template-columns: 1fr 280px; gap: 20px; align-items: start; }
.ticket-main { display: flex; flex-direction: column; gap: 16px; }
.ticket-meta { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.meta-section { padding: 14px 16px; border-bottom: 1px solid var(--border); }
.meta-section:last-child { border-bottom: none; }
.meta-title { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-bottom: 8px; }
.meta-row { display: flex; justify-content: space-between; gap: 10px; font-size: 13px; padding: 3px 0; }
.meta-row span:first-child { color: var(--muted); flex-shrink: 0; }
.meta-row span:last-child  { text-align: right; word-break: break-all; }
.meta-note { font-size: 13px; color: var(--muted); }

/* ── API call log ── */
.api-call { padding: 6px 0; border-bottom: 1px solid var(--border); }
.api-call:last-child { border-bottom: none; }
.api-call-name { font-size: 12px; font-weight: 600; }
.api-call--ok   .api-call-name { color: var(--green); }
.api-call--fail .api-call-name { color: var(--red); }
.api-call-time, .api-call-msg { font-size: 11px; }

/* ── Conversation bubbles ── */
.thread { display: flex; flex-direction: column; gap: 14px; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 20px; }
.bubble-row { display: flex; gap: 10px; align-items: flex-start; }
.bubble-row.mine { flex-direction: row-reverse; }
.avatar { width: 32px; height: 32px; border-radius: 50%; background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center; font-size: 13px; font-weight: 700; flex-shrink: 0; }
.bubble-row.mine .avatar { background: #374151; }
.bubble { max-width: 74%; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.bubble-row.mine .bubble { background: var(--blue-bg); border-color: var(--blue-border); }
.bubble--ai { border-left: 3px solid var(--accent); }
.bubble-meta { display: flex; align-items: center; gap: 8px; padding: 8px 12px 5px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.bubble-from { font-size: 12px; font-weight: 600; }
.bubble-date { font-size: 11px; color: var(--muted); margin-left: auto; }
.ai-chip { font-size: 10px; font-weight: 700; background: var(--accent); color: #fff; padding: 1px 6px; border-radius: 4px; }
.bubble-body { padding: 10px 12px; }
.bubble-body pre { white-space: pre-wrap; word-break: break-word; font-family: inherit; font-size: 13px; }

/* ── Rendered markdown inside bubbles ── */
.md-body { font-size: 13px; line-height: 1.6; word-break: break-word; }
.md-body p  { margin: 0 0 8px; }
.md-body p:last-child { margin-bottom: 0; }
.md-body strong { font-weight: 700; }
.md-body em     { font-style: italic; }
.md-body ul, .md-body ol { padding-left: 18px; margin: 4px 0 8px; }
.md-body li     { margin-bottom: 2px; }
.md-body a      { color: var(--accent); text-decoration: underline; word-break: break-all; }
.md-body a:hover { color: var(--accent-hover); }
.md-body code   { background: var(--gray-bg); border-radius: 3px; padding: 1px 4px; font-size: 12px; font-family: "SF Mono","Fira Code",monospace; }
.md-body hr     { border: none; border-top: 1px solid var(--border); margin: 10px 0; }

/* ── Bubble attachments (images + files) ── */
.bubble-attachments { display: flex; flex-wrap: wrap; gap: 8px; padding: 8px 12px 12px; border-top: 1px solid var(--border); }
.bubble-img-link { display: block; border-radius: 6px; overflow: hidden; border: 1px solid var(--border); flex-shrink: 0; max-width: 100%; }
.bubble-img { display: block; max-width: 320px; max-height: 240px; width: auto; height: auto; object-fit: contain; cursor: zoom-in; transition: opacity .15s; }
.bubble-img:hover { opacity: .9; }
.bubble-file-link {
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 12px; text-decoration: none; color: var(--text);
  transition: border-color .15s, background .15s; max-width: 260px;
}
.bubble-file-link:hover { border-color: var(--accent); background: var(--blue-bg); }
.bubble-file-icon { font-size: 11px; font-weight: 700; background: var(--accent); color: #fff; border-radius: 4px; padding: 2px 5px; flex-shrink: 0; }
.bubble-file-name { font-size: 12px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bubble-file-size { font-size: 11px; color: var(--muted); flex-shrink: 0; margin-left: auto; }

/* ── Reply box ── */
.reply-box { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 16px; }
.reply-box.hidden { display: none; }
.reply-label { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin-bottom: 10px; }
.reply-box textarea { width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 6px; font-size: 14px; font-family: inherit; resize: vertical; outline: none; }
.reply-box textarea:focus { border-color: var(--accent); }
.reply-box button { margin-top: 10px; }
.reply-input { width: 100%; padding: 8px 12px; border: 1px solid var(--border); border-radius: 6px; font-size: 13px; outline: none; margin-bottom: 8px; }
.reply-input:focus { border-color: var(--accent); }

/* ── Queue ── */
.queue-list { display: flex; flex-direction: column; gap: 10px; }
.queue-card { display: block; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 16px; transition: box-shadow .15s; }
.queue-card:hover { box-shadow: 0 2px 8px rgba(0,0,0,.07); border-color: var(--accent); }
.queue-card-top { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; flex-wrap: wrap; }
.queue-age { margin-left: auto; font-size: 12px; }
.queue-from    { font-size: 13px; color: var(--muted); }
.queue-subject { font-size: 14px; font-weight: 500; margin-top: 2px; }
.queue-reason  { font-size: 12px; color: var(--orange); margin-top: 6px; }

/* ── Empty state ── */
.empty-state { display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 80px 20px; }
.empty-icon { font-size: 48px; margin-bottom: 12px; color: var(--green); }
.empty-text { font-size: 16px; color: var(--muted); }

/* ── Settings ── */
.settings-form { max-width: 640px; display: flex; flex-direction: column; gap: 24px; }
.settings-section { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 20px; }
.settings-section h2 { font-size: 15px; font-weight: 600; margin-bottom: 14px; }
.settings-input { width: 100%; padding: 9px 12px; border: 1px solid var(--border); border-radius: 6px; font-size: 13px; outline: none; margin-top: 8px; }
.settings-input:focus { border-color: var(--accent); }
.settings-hint { font-size: 12px; color: var(--muted); margin-top: 8px; }

/* ── Toggle ── */
.toggle-label { display: flex; align-items: center; gap: 12px; cursor: pointer; }
.toggle-label input { display: none; }
.toggle-track { width: 42px; height: 24px; background: var(--border); border-radius: 12px; position: relative; flex-shrink: 0; transition: background .2s; }
.toggle-track::after { content: ''; width: 18px; height: 18px; background: #fff; border-radius: 50%; position: absolute; top: 3px; left: 3px; transition: transform .2s; box-shadow: 0 1px 3px rgba(0,0,0,.2); }
input:checked + .toggle-track { background: var(--accent); }
input:checked + .toggle-track::after { transform: translateX(18px); }
.toggle-text { font-size: 14px; }

/* ── Alert ── */
.alert { padding: 10px 14px; border-radius: 6px; font-size: 13px; margin-bottom: 16px; border: 1px solid; background: var(--red-bg); border-color: var(--red-border); color: var(--red); }
.alert--success { background: var(--green-bg); border-color: var(--green-border); color: var(--green); }

/* ── Misc ── */
.msg-count { display: inline-block; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 1px 8px; font-size: 12px; font-weight: 600; color: var(--muted); min-width: 24px; text-align: center; }
.mono { font-family: "SF Mono", "Fira Code", monospace; font-size: 12px; }
.muted  { color: var(--muted); }
.hidden { display: none !important; }
.empty  { text-align: center; color: var(--muted); padding: 32px; }


/* ════════════════════════════════════════════
   RESPONSIVE BREAKPOINTS
   ════════════════════════════════════════════ */

/* ── Large screens (1280px+): unconstrained width ── */
@media (min-width: 1280px) {
  .main-content { max-width: 1200px; }
  .stats-grid { grid-template-columns: repeat(5, 1fr); }
}

/* ── Medium-large (1024px – 1279px) ── */
@media (max-width: 1279px) {
  .main-content { padding: 24px 24px; }
  .stats-grid { grid-template-columns: repeat(3, 1fr); }
}

/* ── Tablet (768px – 1023px): narrow sidebar ── */
@media (max-width: 1023px) {
  :root { --sidebar-w: 180px; }
  .main-content { padding: 20px 20px; }
  .stats-grid { grid-template-columns: repeat(3, 1fr); gap: 10px; }
  .ticket-layout { grid-template-columns: 1fr 240px; gap: 14px; }
  .bubble { max-width: 82%; }
}

/* ── Mobile (< 768px): sidebar becomes a drawer ── */
@media (max-width: 767px) {
  /* Show mobile top bar, hide desktop sidebar */
  .mobile-topbar { display: flex; }

  .sidebar {
    position: fixed; top: 0; left: 0; height: 100vh;
    z-index: 100; transform: translateX(-100%);
  }
  body.sidebar-open .sidebar { transform: translateX(0); }
  .sidebar-overlay { display: none; }
  body.sidebar-open .sidebar-overlay { display: block; }

  /* Main content fills full width (no sidebar margin) */
  .app-layout { flex-direction: column; }
  .main-content { padding: 16px; }

  /* Stats: 2 columns on mobile */
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .stat-value { font-size: 22px; }

  /* Page header: stack actions below title on very small screens */
  .page-header { flex-direction: column; align-items: flex-start; gap: 10px; }
  .page-header h1 { font-size: 18px; }

  /* Ticket detail: single column */
  .ticket-layout { grid-template-columns: 1fr; }
  /* Move metadata panel above conversation on mobile */
  .ticket-meta { order: -1; }

  /* Bubbles: wider on mobile */
  .bubble { max-width: 90%; }

  /* Filters: stack vertically */
  .filter-form { flex-direction: column; }
  .filter-input { min-width: unset; }

  /* Section header: wrap */
  .section-header { flex-wrap: wrap; gap: 8px; }

  /* Auth box: full width on small screens */
  .auth-box { padding: 28px 20px; }

  /* Settings */
  .settings-form { max-width: 100%; }

  /* Hide less important table columns on mobile */
  .data-table .col-created,
  .data-table .col-tag { display: none; }
}

/* ── Very small phones (< 480px) ── */
@media (max-width: 479px) {
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .stat-card { padding: 14px; }
  .main-content { padding: 12px; }
  .thread { padding: 12px; }
  .bubble-img { max-width: 220px; max-height: 180px; }
}
