:root {
  --bg: #0c1017;
  --surface: #151c28;
  --surface2: #1e2838;
  --surface3: #263246;
  --border: #2e3d54;
  --text: #eef2f8;
  --muted: #8fa3be;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --primary-soft: rgba(59, 130, 246, 0.15);
  --danger: #ef4444;
  --success: #22c55e;
  --warning: #f59e0b;
  --radius: 12px;
  --radius-sm: 8px;
  --font: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --header-h: 56px;
  --drawer-w: 280px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  line-height: 1.45;
}

.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 0.9rem; }
.error { color: var(--danger); margin-top: 0.75rem; font-size: 0.9rem; }

/* --- Login --- */
.page-login {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 1rem;
  padding-bottom: calc(1rem + var(--safe-bottom));
  background: radial-gradient(ellipse at top, #1a2744 0%, var(--bg) 55%);
}
.login-card {
  background: var(--surface);
  padding: 1.75rem;
  border-radius: var(--radius);
  width: 100%;
  max-width: 400px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.login-card h1 { font-size: 1.4rem; margin-bottom: 0.25rem; font-weight: 700; }
label { display: block; margin: 1rem 0 0.4rem; font-size: 0.82rem; font-weight: 500; color: var(--muted); }
input, textarea, select {
  width: 100%;
  padding: 0.7rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface2);
  color: var(--text);
  font: inherit;
  font-size: 16px;
  transition: border-color 0.15s;
  color-scheme: dark;
}
select option,
select optgroup {
  background: var(--surface2);
  color: var(--text);
}
input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
textarea { resize: vertical; min-height: 80px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  min-height: 42px;
  padding: 0.55rem 1rem;
  border: none;
  border-radius: var(--radius-sm);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  background: var(--surface2);
  color: var(--text);
  transition: filter 0.15s, background 0.15s;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.btn:hover { filter: brightness(1.08); }
.btn:active { filter: brightness(0.95); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; pointer-events: none; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { background: transparent; border: 1px solid var(--border); }
.btn-sm { min-height: 34px; padding: 0.3rem 0.7rem; font-size: 0.85rem; }
.btn-lg { min-height: 48px; padding: 0.75rem 1.5rem; font-size: 1rem; }
.btn-block { width: 100%; }
.btn.danger, .danger { background: var(--danger); color: #fff; }
.login-card .btn-primary { width: 100%; margin-top: 1.25rem; }

.contact-card textarea,
.contact-card select {
  width: 100%;
  margin-top: 0.35rem;
  padding: 0.65rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font: inherit;
  resize: vertical;
  color-scheme: dark;
}

.contact-card select { margin-bottom: 0.5rem; }

.success {
  color: #4ade80;
  font-size: 0.9rem;
  margin-top: 0.75rem;
}
.code-hint { margin-bottom: 0.75rem; }
.code-timer { font-size: 0.85rem; margin: 0.5rem 0 0; }
#login-code {
  font-size: 1.5rem;
  letter-spacing: 0.35em;
  text-align: center;
  font-family: ui-monospace, monospace;
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text);
  cursor: pointer;
  flex-shrink: 0;
  touch-action: manipulation;
}
.btn-icon:hover { background: var(--surface2); }

/* Burger icon */
.burger { flex-direction: column; gap: 5px; }
.burger span {
  display: block;
  width: 20px;
  height: 2px;
  background: currentColor;
  border-radius: 1px;
  transition: transform 0.2s, opacity 0.2s;
}
body.drawer-open .burger span:nth-child(1) { transform: translateY(7px) rotate(45deg); }
body.drawer-open .burger span:nth-child(2) { opacity: 0; }
body.drawer-open .burger span:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }

/* --- Drawer --- */
.drawer-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 300;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
}
body.drawer-open .drawer-overlay {
  opacity: 1;
  pointer-events: auto;
}

.drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: var(--drawer-w);
  max-width: 88vw;
  background: var(--surface);
  border-right: 1px solid var(--border);
  z-index: 400;
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  padding-bottom: var(--safe-bottom);
}
body.drawer-open .drawer { transform: translateX(0); }

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1rem 0.75rem;
  border-bottom: 1px solid var(--border);
}
.drawer-brand { font-weight: 700; font-size: 1.1rem; }
#drawer-close { display: none; }

.drawer-nav {
  flex: 1;
  overflow-y: auto;
  padding: 0.75rem 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.nav-btn {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  width: 100%;
  padding: 0.75rem 1rem;
  border: none;
  border-radius: var(--radius-sm);
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 0.95rem;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
  min-height: 48px;
  touch-action: manipulation;
}
.nav-btn:hover { background: var(--surface2); color: var(--text); }
.nav-btn.active {
  background: var(--primary-soft);
  color: var(--text);
  box-shadow: inset 3px 0 0 var(--primary);
}
.nav-icon { font-size: 1.15rem; width: 1.5rem; text-align: center; flex-shrink: 0; }

.drawer-foot {
  padding: 1rem;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}
.staff-pill {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
  padding: 0.4rem 0.6rem;
  background: var(--surface2);
  border-radius: var(--radius-sm);
  text-align: center;
}

/* --- App shell --- */
.app-shell {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 200;
  height: var(--header-h);
  padding: 0 0.75rem 0 0.5rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: rgba(12, 16, 23, 0.92);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.header-titles { min-width: 0; flex: 1; }
.header-eyebrow {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  line-height: 1.2;
}
.header-title {
  font-size: 1.1rem;
  font-weight: 700;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.main {
  flex: 1;
  min-width: 0;
  padding: 1rem;
  max-width: 1400px;
  width: 100%;
  margin: 0 auto;
}

.view { display: none; }
.view.active { display: block; }

.page-intro { margin-bottom: 1.25rem; }
.page-intro h2 { font-size: 1.35rem; font-weight: 700; margin-bottom: 0.25rem; }
.card-title { font-size: 1rem; font-weight: 600; margin-bottom: 0.75rem; }

.section-label {
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin-bottom: 0.5rem;
}

/* --- Tickets layout --- */
.tickets-layout {
  display: grid;
  grid-template-columns: minmax(280px, 340px) 1fr;
  gap: 1rem;
  height: calc(100dvh - var(--header-h) - 2rem);
}
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.2);
}
.panel-section { padding: 0.85rem 1rem; border-bottom: 1px solid var(--border); }
.actions-section { padding-bottom: 0.65rem; }

.filter-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}
.chip {
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--muted);
  font: inherit;
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  min-height: 32px;
  touch-action: manipulation;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.chip:hover { color: var(--text); border-color: var(--muted); }
.chip.active {
  background: var(--primary-soft);
  border-color: var(--primary);
  color: var(--text);
}

.ticket-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
  -webkit-overflow-scrolling: touch;
}
.ticket-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0.85rem 0.9rem;
  background: none;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  color: var(--text);
  font: inherit;
  cursor: pointer;
  margin-bottom: 4px;
  min-height: 48px;
  touch-action: manipulation;
  transition: background 0.12s, border-color 0.12s;
}
.ticket-item:hover { background: var(--surface2); border-color: var(--border); }
.ticket-item.active {
  background: var(--primary-soft);
  border-color: var(--primary);
}
.ti-label { display: block; font-weight: 600; font-size: 0.95rem; margin-bottom: 0.15rem; }
.ti-meta { font-size: 0.8rem; color: var(--muted); }
.ticket-item.active .ti-meta { color: #93c5fd; }

.pager {
  padding: 0.65rem 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.pager-info { font-size: 0.85rem; color: var(--muted); min-width: 5rem; text-align: center; }
.card-pager { margin-top: 0.75rem; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--border); }

.panel-head {
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 0.65rem;
}
.panel-head-detail { flex-wrap: nowrap; }
.detail-head-text { flex: 1; min-width: 0; display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.detail-head-text h2 { font-size: 1.05rem; font-weight: 700; }
.back-btn { display: none; flex-shrink: 0; }

.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  color: var(--muted);
  padding: 2.5rem 1.5rem;
  text-align: center;
}
.empty-icon { font-size: 2.5rem; opacity: 0.5; }

#ticket-detail { flex: 1; display: flex; flex-direction: column; overflow: hidden; min-height: 0; }

.meta {
  padding: 0.85rem 1rem;
  font-size: 0.88rem;
  border-bottom: 1px solid var(--border);
}
.meta-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.65rem 1rem;
}
.meta-item { display: flex; flex-direction: column; gap: 0.15rem; }
.meta-item.full { grid-column: 1 / -1; }
.meta-key { font-size: 0.72rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.meta-val { font-size: 0.9rem; word-break: break-word; }
.meta-val code { font-size: 0.85em; background: var(--surface2); padding: 0.1rem 0.35rem; border-radius: 4px; }

.badge {
  font-size: 0.72rem;
  font-weight: 600;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  flex-shrink: 0;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.badge-open { background: rgba(34, 197, 94, 0.15); color: #4ade80; }
.badge-closed { background: var(--surface3); color: var(--muted); }

.actions { display: flex; flex-wrap: wrap; gap: 0.4rem; }

.messages-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 0.65rem 1rem 0;
}
.messages-label { padding: 0 0 0.4rem; }
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem 0;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  -webkit-overflow-scrolling: touch;
  min-height: 100px;
}
.msg {
  max-width: 82%;
  padding: 0.65rem 0.9rem;
  border-radius: 14px;
  background: var(--surface2);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
}
.msg.user { align-self: flex-start; border-bottom-left-radius: 4px; }
.msg.admin { align-self: flex-end; background: #1a3a5c; border-bottom-right-radius: 4px; }
.msg.internal {
  align-self: center;
  max-width: 94%;
  background: rgba(245, 158, 11, 0.1);
  border: 1px dashed rgba(245, 158, 11, 0.35);
}
.msg-head { font-size: 0.72rem; color: var(--muted); margin-bottom: 0.3rem; }
.msg-body { white-space: pre-wrap; word-break: break-word; font-size: 0.92rem; }
.msg-media { display: block; max-width: 100%; max-height: 260px; border-radius: var(--radius-sm); margin-top: 0.35rem; }
.msg-file { display: inline-block; margin-top: 0.35rem; color: #60a5fa; font-weight: 500; }
.msg-caption { margin-top: 0.35rem; font-size: 0.88rem; white-space: pre-wrap; }

.composer {
  padding: 0.85rem 1rem;
  padding-bottom: calc(0.85rem + var(--safe-bottom));
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  background: var(--surface);
}
.composer .section-label { margin-bottom: 0.4rem; }
.composer-actions {
  display: flex;
  gap: 0.45rem;
  margin-top: 0.65rem;
  flex-wrap: wrap;
  align-items: center;
}
.composer-select { flex: 1 1 140px; min-width: 0; font-size: 0.9rem; }
.composer input.hidden { display: none; }
.file-name { font-size: 0.82rem; color: var(--muted); max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.15rem;
  margin-bottom: 0.85rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}
.card-list .card:last-child { margin-bottom: 0; }
.qr-card pre {
  margin: 0.6rem 0;
  font-size: 0.85rem;
  white-space: pre-wrap;
  color: var(--muted);
  background: var(--surface2);
  padding: 0.65rem;
  border-radius: var(--radius-sm);
}
.search-bar { display: flex; gap: 0.65rem; align-items: stretch; }
.search-bar input { flex: 1; min-width: 0; }
.export-card { text-align: center; padding: 2rem; }

.syslog-item.level-error { border-left: 3px solid var(--danger); }
.syslog-item.level-warning { border-left: 3px solid var(--warning); }

.toast {
  position: fixed;
  bottom: calc(1.25rem + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  max-width: min(400px, calc(100vw - 2rem));
  width: max-content;
  background: var(--surface2);
  border: 1px solid var(--border);
  padding: 0.75rem 1.25rem;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 500;
  font-size: 0.9rem;
}
.toast.error { border-color: var(--danger); }

/* Desktop: drawer pinned */
@media (min-width: 1024px) {
  body { padding-left: var(--drawer-w); }
  .drawer {
    transform: translateX(0);
    box-shadow: none;
  }
  .drawer-overlay { display: none; }
  #drawer-close { display: none; }
  #menu-toggle { display: none; }
  .app-header { padding-left: 1rem; }
}

@media (max-width: 1023px) {
  #drawer-close { display: inline-flex; }
  body.drawer-open { overflow: hidden; }
}

@media (max-width: 768px) {
  .main { padding: 0.75rem; }
  .tickets-layout {
    grid-template-columns: 1fr;
    height: calc(100dvh - var(--header-h) - 1.5rem);
    gap: 0.65rem;
  }
  .tickets-layout .detail-panel { display: none; }
  .tickets-layout.show-detail .list-panel { display: none; }
  .tickets-layout.show-detail .detail-panel { display: flex; }
  .back-btn { display: inline-flex; }
  .meta-grid { grid-template-columns: 1fr; }
  .msg { max-width: 90%; }
  .composer-actions .btn-primary { flex: 1 1 100%; order: 10; }
  .search-bar { flex-direction: column; }
  .search-bar .btn { width: 100%; }
  .login-card { padding: 1.25rem; }
  #login-code { font-size: 1.25rem; letter-spacing: 0.25em; }
}

@media (max-width: 400px) {
  .filter-chips .chip { font-size: 0.78rem; padding: 0.3rem 0.55rem; }
  .actions .btn { flex: 1 1 calc(50% - 0.25rem); font-size: 0.8rem; }
}
