:root {
  --bg: #0b1016;
  --surface: #121922;
  --surface-2: #192230;
  --border: #2a3647;
  --text: #eef4fb;
  --text-2: #9fb0c5;
  --accent: #60d6f0;
  --danger: #ff6e7a;
  --warn: #ffbd59;
  --green: #50d890;
  --purple: #b18cff;
  --sidebar-width: 220px;
  --radius: 14px;
  --shadow: 0 20px 45px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(circle at top right, rgba(96, 214, 240, 0.12), transparent 22%),
    radial-gradient(circle at bottom left, rgba(177, 140, 255, 0.08), transparent 24%),
    var(--bg);
  color: var(--text);
}

body {
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
  line-height: 1.45;
}

a {
  color: inherit;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.hidden {
  display: none !important;
}

.app-shell {
  min-height: 100vh;
}

.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: var(--sidebar-width);
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 24px;
  padding: 24px 18px;
  background: rgba(9, 14, 20, 0.96);
  border-right: 1px solid var(--border);
  backdrop-filter: blur(12px);
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.brand-icon {
  width: 46px;
  height: 46px;
  display: grid;
  place-items: center;
  border-radius: 16px;
  color: var(--accent);
  background: linear-gradient(145deg, rgba(96, 214, 240, 0.16), rgba(177, 140, 255, 0.18));
  border: 1px solid rgba(96, 214, 240, 0.2);
}

.brand-icon svg {
  width: 28px;
  height: 28px;
}

.sidebar-brand h1 {
  margin: 0;
  font-size: 1.1rem;
}

.sidebar-brand p {
  margin: 2px 0 0;
  color: var(--text-2);
  font-size: 0.85rem;
}

.sidebar-nav {
  display: grid;
  gap: 8px;
}

.nav-link,
.footer-link,
.icon-btn,
.btn,
.pagination-btn {
  border: 1px solid transparent;
  transition: 160ms ease;
}

.nav-link,
.footer-link {
  width: 100%;
  border-radius: 12px;
  padding: 11px 13px;
  text-align: left;
  background: transparent;
  color: var(--text-2);
}

.nav-link:hover,
.footer-link:hover,
.nav-link.active {
  background: var(--surface-2);
  color: var(--text);
  border-color: rgba(96, 214, 240, 0.15);
}

.sidebar-footer {
  margin-top: auto;
  display: grid;
  gap: 6px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
}

.main-shell {
  min-height: 100vh;
  margin-left: var(--sidebar-width);
}

.mobile-topbar {
  display: none;
}

.main-content {
  padding: 28px;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 22px;
}

.page-header h2,
.section-title {
  margin: 0;
}

.page-header p,
.section-subtitle,
.empty-state p,
.ai-header p {
  margin: 6px 0 0;
  color: var(--text-2);
}

.card {
  background: linear-gradient(180deg, rgba(18, 25, 34, 0.96), rgba(16, 22, 30, 0.96));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.panel {
  padding: 20px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}

.stat-card {
  padding: 18px;
}

.stat-card .stat-label {
  color: var(--text-2);
  font-size: 0.9rem;
}

.stat-card .stat-value {
  margin-top: 12px;
  font-size: 1.85rem;
  font-weight: 700;
}

.stat-card .stat-meta {
  margin-top: 10px;
  font-size: 0.85rem;
  color: var(--text-2);
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.filter-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.table-wrap {
  overflow: auto;
}

.table {
  width: 100%;
  min-width: 760px;
  border-collapse: collapse;
}

.table th,
.table td {
  padding: 14px 16px;
  text-align: left;
  border-bottom: 1px solid rgba(42, 54, 71, 0.7);
}

.table thead th {
  color: var(--text-2);
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.table tbody tr:nth-child(odd) {
  background: rgba(255, 255, 255, 0.015);
}

.table tbody tr:hover {
  background: rgba(96, 214, 240, 0.06);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.form-grid-3 {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
}

.field {
  display: grid;
  gap: 8px;
}

.field.full {
  grid-column: 1 / -1;
}

label {
  color: var(--text-2);
  font-size: 0.9rem;
}

input,
select,
textarea {
  width: 100%;
  padding: 11px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(25, 34, 48, 0.94);
  color: var(--text);
  outline: none;
}

input:focus,
select:focus,
textarea:focus {
  border-color: rgba(96, 214, 240, 0.6);
  box-shadow: 0 0 0 3px rgba(96, 214, 240, 0.15);
}

textarea {
  resize: vertical;
  min-height: 90px;
}

.checkbox-row,
.inline-checks,
.inline-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}

.inline-checks label,
.checkbox-row label {
  display: inline-flex;
  gap: 8px;
  align-items: center;
}

.inline-checks input,
.checkbox-row input {
  width: auto;
}

.btn,
.pagination-btn,
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 14px;
  border-radius: 12px;
  background: var(--surface-2);
  color: var(--text);
  border-color: rgba(255, 255, 255, 0.06);
}

.btn:hover,
.pagination-btn:hover,
.icon-btn:hover {
  transform: translateY(-1px);
  border-color: rgba(96, 214, 240, 0.3);
}

.btn-primary {
  background: linear-gradient(135deg, rgba(96, 214, 240, 0.95), rgba(48, 164, 189, 0.95));
  color: #031017;
  border-color: rgba(96, 214, 240, 0.4);
}

.btn-danger {
  background: rgba(255, 110, 122, 0.15);
  color: var(--danger);
  border-color: rgba(255, 110, 122, 0.25);
}

.btn-sm {
  padding: 7px 10px;
  font-size: 0.88rem;
  border-radius: 10px;
}

.muted {
  color: var(--text-2);
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 5px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: capitalize;
}

.badge-draft,
.badge-completed,
.badge-closed {
  background: rgba(107, 114, 128, 0.16);
  color: #cbd5e1;
}

.badge-pending {
  background: rgba(245, 158, 11, 0.16);
  color: #fbbf24;
}

.badge-approved {
  background: rgba(34, 197, 94, 0.16);
  color: #4ade80;
}

.badge-in-progress {
  background: rgba(59, 130, 246, 0.16);
  color: #60a5fa;
}

.badge-billed {
  background: rgba(167, 139, 250, 0.16);
  color: #c4b5fd;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin-top: 14px;
}

.metric {
  padding: 14px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.04);
}

.metric strong {
  display: block;
  margin-top: 6px;
  font-size: 1.1rem;
}

.detail-grid {
  display: grid;
  grid-template-columns: 1.2fr 0.8fr;
  gap: 16px;
}

.info-list {
  display: grid;
  gap: 12px;
}

.info-row {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  padding-bottom: 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.info-row span:first-child {
  color: var(--text-2);
}

.line-items {
  display: grid;
  gap: 10px;
}

.line-item-row {
  display: grid;
  grid-template-columns: 1fr 2fr 0.7fr 0.8fr 0.8fr auto;
  gap: 10px;
  align-items: end;
}

.line-item-total {
  padding: 11px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.03);
  min-height: 46px;
  display: flex;
  align-items: center;
}

.grand-total {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 14px;
  padding: 14px 16px;
  border-radius: 12px;
  background: rgba(96, 214, 240, 0.07);
  border: 1px solid rgba(96, 214, 240, 0.18);
  font-size: 1rem;
}

.logo-upload-row {
  display: grid;
  grid-template-columns: 140px minmax(0, 1fr);
  gap: 14px;
  align-items: stretch;
}

.logo-preview {
  min-height: 86px;
  display: grid;
  place-items: center;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.03);
}

.logo-preview img {
  display: block;
  max-width: 100%;
  max-height: 82px;
  object-fit: contain;
}

.logo-placeholder {
  color: var(--text-2);
  font-size: 0.85rem;
}

.logo-upload-controls {
  display: grid;
  gap: 10px;
  align-content: start;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 400;
  background: rgba(3, 8, 14, 0.8);
  backdrop-filter: blur(8px);
}

.modal-overlay.open {
  display: flex;
}

.modal {
  width: min(960px, 100%);
  max-height: 88vh;
  overflow: auto;
  background: linear-gradient(180deg, rgba(18, 25, 34, 0.98), rgba(13, 18, 24, 0.98));
  border: 1px solid var(--border);
  border-radius: 22px;
  box-shadow: var(--shadow);
}

.modal-header,
.modal-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 18px 20px;
}

.modal-header {
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.modal-footer {
  border-top: 1px solid rgba(255, 255, 255, 0.06);
}

.modal-body {
  padding: 20px;
}

.alert-stack {
  position: fixed;
  top: 18px;
  right: 18px;
  z-index: 450;
  display: grid;
  gap: 10px;
}

.alert {
  min-width: 240px;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid rgba(96, 214, 240, 0.2);
  background: rgba(17, 24, 33, 0.96);
  box-shadow: var(--shadow);
}

.alert-error {
  border-color: rgba(255, 110, 122, 0.25);
}

.ai-toggle {
  position: fixed;
  right: 22px;
  bottom: 22px;
  z-index: 310;
  width: 58px;
  height: 58px;
  border: none;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--purple));
  color: #041015;
  font-weight: 800;
  box-shadow: var(--shadow);
}

.ai-panel {
  position: fixed;
  right: 22px;
  bottom: 92px;
  z-index: 320;
  width: 360px;
  max-width: calc(100vw - 24px);
  padding: 14px;
  border-radius: 22px;
  border: 1px solid var(--border);
  background: rgba(12, 17, 23, 0.98);
  box-shadow: var(--shadow);
}

.ai-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 12px;
}

.ai-messages {
  display: grid;
  gap: 10px;
  max-height: 320px;
  overflow: auto;
  margin-bottom: 12px;
}

.ai-bubble {
  max-width: 88%;
  padding: 10px 12px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.05);
}

.ai-bubble.user {
  margin-left: auto;
  background: rgba(96, 214, 240, 0.16);
}

.ai-input-row {
  display: grid;
  gap: 10px;
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 16px;
}

.pagination-btn[disabled] {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.empty-state {
  padding: 40px 22px;
  text-align: center;
}

.kbd {
  padding: 2px 7px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.06);
}

@media (max-width: 1100px) {
  .stats-grid,
  .metric-grid,
  .detail-grid,
  .form-grid,
  .form-grid-3,
  .line-item-row {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .line-item-row > .field:nth-child(2),
  .line-item-row > .line-item-total {
    grid-column: span 2;
  }
}

@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
    transition: transform 180ms ease;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .main-shell {
    margin-left: 0;
  }

  .mobile-topbar {
    position: sticky;
    top: 0;
    z-index: 210;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    background: rgba(11, 16, 22, 0.92);
    border-bottom: 1px solid var(--border);
    backdrop-filter: blur(12px);
  }

  .mobile-brand {
    font-weight: 700;
  }

  .main-content {
    padding: 18px;
  }

  .stats-grid,
  .metric-grid,
  .detail-grid,
  .form-grid,
  .form-grid-3,
  .line-item-row {
    grid-template-columns: 1fr;
  }

  .line-item-row > .field,
  .line-item-row > .line-item-total,
  .logo-upload-row {
    grid-column: auto;
  }

  .logo-upload-row {
    grid-template-columns: 1fr;
  }

  .page-header,
  .toolbar,
  .modal-header,
  .modal-footer,
  .grand-total {
    flex-direction: column;
    align-items: stretch;
  }

  .ai-toggle {
    right: 16px;
    bottom: 16px;
  }

  .ai-panel {
    right: 12px;
    left: 12px;
    bottom: 86px;
    width: auto;
    max-width: none;
  }
}

/* Dispatch Board */
.dispatch-board{display:grid;grid-template-columns:repeat(3,1fr);gap:16px;margin-top:16px}
.dispatch-column{background:#0f1117;border:1px solid #2a2d42;border-radius:8px;padding:12px;min-height:300px}
.dispatch-column h3{color:#9fb0c5;font-size:13px;text-transform:uppercase;letter-spacing:1px;margin:0 0 12px 0;padding-bottom:8px;border-bottom:1px solid #2a2d42}
.dispatch-card{background:#16181f;border:1px solid #2a2d42;border-radius:6px;padding:12px;margin-bottom:8px;cursor:pointer;transition:border-color .15s}
.dispatch-card:hover{border-color:#CCFF00}
.dispatch-card .job-number{color:#CCFF00;font-weight:600;font-size:13px}
.dispatch-card .customer{color:#e8edf5;font-size:12px;margin:4px 0}
.dispatch-card .details{color:#9fb0c5;font-size:11px;display:flex;gap:8px;flex-wrap:wrap}
.dispatch-card .details span{display:flex;align-items:center;gap:4px}
.dispatch-card .actions{display:flex;gap:6px;margin-top:8px;flex-wrap:wrap}
.dispatch-card .actions button{font-size:11px;padding:4px 8px;border-radius:4px;border:1px solid #2a2d42;background:transparent;color:#e8edf5;cursor:pointer}
.dispatch-card .actions button:hover{border-color:#CCFF00;color:#CCFF00}
.dispatch-card .tech-name{color:#60a5fa;font-size:11px}
.dispatch-card .time{color:#9fb0c5;font-size:10px}
.dispatch-card.completed{opacity:0.6}
.dispatch-stats{display:flex;gap:24px;margin-bottom:16px;padding:12px 16px;background:#0f1117;border:1px solid #2a2d42;border-radius:8px}
.dispatch-stats .stat{display:flex;flex-direction:column}
.dispatch-stats .stat .value{font-size:22px;font-weight:700;color:#CCFF00}
.dispatch-stats .stat .label{font-size:11px;color:#9fb0c5;text-transform:uppercase;letter-spacing:.5px}
.badge-dispatched{background:rgba(96,165,250,0.15);color:#60a5fa}
.badge-in-progress{background:rgba(251,191,36,0.15);color:#fbbf24}
.tech-list{display:flex;flex-direction:column;gap:8px}
.tech-row{display:flex;justify-content:space-between;align-items:center;padding:10px 12px;background:#0f1117;border:1px solid #2a2d42;border-radius:6px}
.tech-row .tech-info{display:flex;flex-direction:column;gap:2px}
.tech-row .tech-info .name{color:#e8edf5;font-weight:500;font-size:13px}
.tech-row .tech-info .details{color:#9fb0c5;font-size:11px}
.tech-row .tech-actions{display:flex;gap:6px}

/* Dispatch Chat */
.dispatch-chat-panel {
  position: fixed;
  right: 22px;
  bottom: 92px;
  z-index: 330;
  display: flex;
  flex-direction: column;
  width: min(520px, calc(100vw - 44px));
  height: min(660px, calc(100vh - 116px));
  border: 1px solid var(--border);
  border-radius: 18px;
  background: rgba(11, 16, 22, 0.98);
  box-shadow: var(--shadow);
  overflow: hidden;
  opacity: 1;
  transform: translateX(0);
  transition: opacity 180ms ease, transform 180ms ease;
}

.dispatch-chat-panel.hidden {
  display: flex !important;
  pointer-events: none;
  opacity: 0;
  transform: translateX(calc(100% + 28px));
}

.dispatch-chat-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  padding: 14px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  background: rgba(18, 25, 34, 0.96);
}

.dispatch-chat-header p {
  margin: 4px 0 0;
  color: var(--text-2);
  font-size: 0.86rem;
}

.dispatch-chat-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.dispatch-chat-body {
  min-height: 0;
  flex: 1;
  display: grid;
  grid-template-columns: 190px minmax(0, 1fr);
}

.conv-list {
  min-height: 0;
  overflow: auto;
  padding: 10px;
  border-right: 1px solid rgba(255, 255, 255, 0.06);
  background: #0b1016;
}

.conv-item {
  width: 100%;
  display: grid;
  gap: 3px;
  padding: 10px;
  margin-bottom: 8px;
  text-align: left;
  border: 1px solid rgba(42, 54, 71, 0.8);
  border-radius: 8px;
  background: #121922;
  color: var(--text);
}

.conv-item:hover,
.conv-item.active {
  border-color: #CCFF00;
  background: rgba(204, 255, 0, 0.08);
}

.conv-name {
  font-weight: 700;
  font-size: 0.9rem;
}

.conv-preview,
.conv-time,
.conv-empty,
.msg-empty {
  color: var(--text-2);
  font-size: 0.8rem;
}

.conv-preview {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dispatch-thread-pane {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.msg-thread {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: auto;
  padding: 14px;
}

.msg-row {
  display: flex;
}

.msg-row.dispatch {
  justify-content: flex-end;
}

.msg-bubble {
  max-width: 86%;
  padding: 10px 12px;
  border: 1px solid rgba(42, 54, 71, 0.9);
  border-radius: 12px;
  background: #192230;
  color: var(--text);
  overflow-wrap: anywhere;
}

.msg-bubble.dispatch {
  border-color: rgba(204, 255, 0, 0.42);
  background: rgba(204, 255, 0, 0.16);
  color: #f5ffd8;
}

.msg-bubble span {
  display: block;
  margin-top: 6px;
  color: var(--text-2);
  font-size: 0.72rem;
}

.msg-input-area {
  display: flex;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
  background: rgba(18, 25, 34, 0.96);
}

.msg-input-area input {
  min-width: 0;
}

.msg-input-area button {
  flex: 0 0 auto;
}

@media (max-width: 768px) {
  .dispatch-chat-panel {
    right: 12px;
    left: 12px;
    bottom: 86px;
    width: auto;
    height: min(680px, calc(100vh - 108px));
  }

  .dispatch-chat-body {
    grid-template-columns: 1fr;
    grid-template-rows: 152px minmax(0, 1fr);
  }

  .conv-list {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    overflow-y: hidden;
    border-right: 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  }

  .conv-item {
    min-width: 170px;
    margin-bottom: 0;
  }
}
