:root {
  --bg: #0f1419;
  --surface: #1a212b;
  --surface-2: #232c39;
  --border: #2c3744;
  --text: #e6edf3;
  --text-dim: #8b96a5;
  --accent: #4f8cff;
  --accent-glow: rgba(79, 140, 255, 0.4);
  --customer: #2a3441;
  --agent: #2d3a2a;
  --agent-border: #5a8a3a;
  --warning: #d9a236;
  --warning-glow: rgba(217, 162, 54, 0.35);
  --success: #4ec27a;
  --radius: 10px;
  --shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

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

.brand .dot {
  width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 12px var(--accent-glow);
}

.brand h1 { margin: 0; font-size: 16px; font-weight: 600; }
.brand .subtitle { color: var(--text-dim); font-size: 13px; }

.controls { display: flex; gap: 8px; }

/* Nav tabs */
.nav-tabs {
  display: flex;
  gap: 4px;
  background: var(--bg);
  padding: 4px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

.nav-tab {
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-dim);
  text-decoration: none;
  transition: all 0.15s ease;
}

.nav-tab:hover { color: var(--text); background: var(--surface-2); }

.nav-tab.active {
  background: var(--accent);
  color: white;
}

.btn-primary, .btn-secondary, .btn-warning {
  padding: 8px 16px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid transparent;
  transition: all 0.15s ease;
}

.btn-primary { background: var(--accent); color: white; }
.btn-primary:hover:not(:disabled) { background: #6a9eff; }

.btn-warning { background: var(--warning); color: #1a1300; }
.btn-warning:hover:not(:disabled) { background: #e6b04a; }

.btn-primary:disabled, .btn-warning:disabled {
  background: #3a4452;
  color: var(--text-dim);
  cursor: not-allowed;
}

.btn-secondary {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}
.btn-secondary:hover { background: var(--surface-2); }

/* Layout — 3 columns */
.layout {
  display: grid;
  grid-template-columns: 1.4fr 0.9fr 1.1fr;
  gap: 14px;
  padding: 14px;
  height: calc(100vh - 65px);
}

.pane {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}

.pane-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
  flex-shrink: 0;
}

.pane-header h2 {
  margin: 0;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
}

.ticket-hint {
  font-size: 11px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--warning);
  color: #1a1300;
  letter-spacing: 0.3px;
  animation: hintPulse 1.6s ease-in-out infinite;
}

.ticket-hint.hidden { display: none; }

@keyframes hintPulse {
  0%, 100% { box-shadow: 0 0 0 0 var(--warning-glow); }
  50%      { box-shadow: 0 0 0 6px transparent; }
}

.btn-warning.pulse {
  animation: btnPulse 1.6s ease-in-out infinite;
}

@keyframes btnPulse {
  0%, 100% { box-shadow: 0 0 0 0 var(--warning-glow); }
  50%      { box-shadow: 0 0 0 8px transparent; }
}

.step-pill {
  font-size: 11px;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--bg);
  color: var(--text-dim);
  border: 1px solid var(--border);
}

/* Chat (left) */
.chat {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
}

.bubble {
  max-width: 88%;
  padding: 10px 14px;
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-wrap: break-word;
  animation: slideIn 0.25s ease;
}

@keyframes slideIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.bubble-customer {
  align-self: flex-start;
  background: var(--customer);
  border-left: 3px solid var(--text-dim);
}

.bubble-customer .label,
.bubble-agent .label {
  display: block;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
  margin-bottom: 4px;
  font-weight: 600;
}

.bubble-agent {
  align-self: flex-end;
  background: var(--agent);
  border-right: 3px solid var(--agent-border);
}

.bubble-agent .label { color: var(--success); }

.cursor::after {
  content: "▋";
  display: inline-block;
  margin-left: 2px;
  animation: blink 0.8s infinite;
  color: var(--accent);
}

@keyframes blink { 50% { opacity: 0; } }

/* Middle: System tiles stack */
.systems-stack {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 14px;
  overflow-y: auto;
  min-height: 0;
}

.system-tile {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto;
  gap: 4px 10px;
  transition: all 0.3s ease;
  flex-shrink: 0;
}

.system-tile .tile-icon {
  grid-row: 1 / 3;
  align-self: center;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 1px;
  color: var(--text-dim);
  background: var(--bg);
  padding: 6px 8px;
  border-radius: 4px;
  min-width: 38px;
  text-align: center;
}

.system-tile .tile-name { font-weight: 600; font-size: 13px; }

.system-tile .tile-data {
  grid-column: 2;
  font-size: 11px;
  color: var(--text-dim);
  line-height: 1.4;
}

.system-tile .tile-data:empty { display: none; }

.system-tile .tile-data div {
  padding: 2px 0;
  border-top: 1px dotted var(--border);
  margin-top: 2px;
}

.system-tile.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-glow), 0 0 20px var(--accent-glow);
  animation: pulse 1.4s ease-in-out;
}

.system-tile.has-data { border-color: var(--agent-border); }

.system-tile.has-data .tile-icon {
  background: var(--agent);
  color: var(--success);
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 2px var(--accent-glow), 0 0 20px var(--accent-glow); }
  50%      { box-shadow: 0 0 0 4px var(--accent-glow), 0 0 32px var(--accent-glow); }
}

/* Right: Context log */
.context-log {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
}

.ctx-entry {
  background: var(--bg);
  border-left: 2px solid var(--accent);
  border-radius: 4px;
  padding: 10px 12px;
  animation: slideIn 0.25s ease;
}

.ctx-entry .ctx-step {
  font-size: 10px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-weight: 700;
  margin-bottom: 4px;
}

.ctx-entry .ctx-insight {
  font-size: 12px;
  color: var(--text);
  margin-bottom: 6px;
  white-space: pre-wrap;
}

.ctx-entry .ctx-data {
  margin-top: 6px;
  border-top: 1px dotted var(--border);
  padding-top: 6px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.ctx-entry .ctx-data .ctx-source {
  font-size: 10px;
  font-weight: 700;
  color: var(--success);
  letter-spacing: 0.5px;
  text-transform: uppercase;
}

.ctx-entry .ctx-data .ctx-line {
  font-size: 11px;
  color: var(--text-dim);
  padding-left: 8px;
  border-left: 2px solid var(--agent-border);
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  animation: fadeIn 0.2s ease;
  padding: 20px;
}

.modal.hidden { display: none; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.modal-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 640px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
}

.modal-card h2 { margin: 0 0 4px 0; font-size: 20px; }
.modal-sub { color: var(--text-dim); margin: 0 0 20px 0; font-size: 13px; }

.ticket {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin: 0 0 16px 0;
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 8px 16px;
}

.ticket dt {
  font-size: 12px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.ticket dd { margin: 0; font-weight: 500; }
.ticket dd.criticality { color: var(--warning); }
.ticket dd.muted { color: var(--text-dim); font-style: italic; font-weight: 400; }

.ticket-context {
  margin-bottom: 20px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 14px;
}

.ticket-context summary {
  cursor: pointer;
  font-size: 12px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 4px 0;
  user-select: none;
}

.ticket-context ol {
  margin: 8px 0 4px 0;
  padding-left: 22px;
  font-size: 12px;
  color: var(--text);
}

.ticket-context ol li {
  margin-bottom: 6px;
  line-height: 1.4;
}

.ticket-context ol li .src {
  color: var(--success);
  font-weight: 600;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* === Email-Routing Demo === */

.email-stream {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
}

.email-card {
  background: var(--customer);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius);
  padding: 14px 16px;
  animation: slideIn 0.25s ease;
}

.email-meta-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 16px;
  font-size: 12px;
  margin-bottom: 10px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}

.email-meta-grid div { color: var(--text); }

.email-meta-grid span {
  display: inline-block;
  width: 56px;
  color: var(--text-dim);
  text-transform: uppercase;
  font-size: 10px;
  letter-spacing: 0.5px;
}

.email-body {
  margin: 0;
  font-family: inherit;
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
  color: var(--text);
}

.analysis-card {
  background: var(--bg);
  border-left: 3px solid var(--success);
  border-radius: var(--radius);
  padding: 10px 14px;
  animation: slideIn 0.25s ease;
}

.analysis-card-final {
  border-left-color: var(--warning);
  background: linear-gradient(90deg, rgba(217,162,54,0.1), var(--bg) 60%);
}

.analysis-step {
  font-size: 10px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-weight: 700;
}

.analysis-title {
  font-size: 13px;
  font-weight: 600;
  margin: 4px 0 6px 0;
  color: var(--text);
}

.analysis-insight {
  font-size: 12px;
  color: var(--text-dim);
  white-space: pre-wrap;
  line-height: 1.5;
}

.analysis-mcp {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px dotted var(--border);
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.analysis-source {
  font-size: 10px;
  font-weight: 700;
  color: var(--success);
  letter-spacing: 0.5px;
  text-transform: uppercase;
  margin-top: 4px;
}

.analysis-line {
  font-size: 11px;
  color: var(--text-dim);
  padding-left: 8px;
  border-left: 2px solid var(--agent-border);
}

/* Department list (right pane) */
.dept-list {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
}

.dept-row {
  display: grid;
  grid-template-columns: 36px 1fr auto;
  gap: 12px;
  align-items: center;
  padding: 12px;
  background: var(--bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  transition: all 0.3s ease;
}

.dept-row.leader {
  border-color: var(--accent);
}

.dept-row.winner {
  border-color: var(--success);
  background: rgba(78, 194, 122, 0.08);
  box-shadow: 0 0 0 2px rgba(78, 194, 122, 0.25);
}

.dept-id {
  font-weight: 700;
  font-size: 16px;
  background: var(--surface-2);
  border-radius: 6px;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text);
  transition: all 0.3s ease;
}

.dept-row.winner .dept-id { background: var(--success); color: #0d2014; }

.dept-info {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.dept-name {
  font-size: 12px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.dept-bar {
  height: 6px;
  background: var(--surface-2);
  border-radius: 3px;
  overflow: hidden;
}

.dept-bar-fill {
  height: 100%;
  background: var(--accent);
  width: 25%;
  transition: width 0.7s cubic-bezier(0.4, 0, 0.2, 1), background 0.3s;
}

.dept-row.winner .dept-bar-fill { background: var(--success); }

.dept-pct {
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--text-dim);
  min-width: 42px;
  text-align: right;
  transition: color 0.3s;
}

.dept-row.leader .dept-pct { color: var(--accent); }
.dept-row.winner .dept-pct { color: var(--success); }

/* Critical badge + wide modal + email-original block */
.modal-card-wide { max-width: 720px; }

.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.crit-badge {
  background: #d92d2d;
  color: white;
  font-weight: 700;
  font-size: 12px;
  padding: 5px 12px;
  border-radius: 6px;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  box-shadow: 0 0 0 2px rgba(217, 45, 45, 0.25);
  animation: critPulse 1.6s ease-in-out infinite;
}

@keyframes critPulse {
  0%, 100% { box-shadow: 0 0 0 2px rgba(217, 45, 45, 0.25); }
  50%      { box-shadow: 0 0 0 6px rgba(217, 45, 45, 0.05); }
}

.ticket dd.highlight-success {
  color: var(--success);
  font-weight: 700;
}

.ticket-original {
  background: var(--bg);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 20px;
}

.ticket-original-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--accent);
  margin-bottom: 8px;
}

.ticket-original-meta {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 16px;
  font-size: 11px;
  color: var(--text);
  margin-bottom: 10px;
  padding-bottom: 10px;
  border-bottom: 1px dotted var(--border);
}

.ticket-original-meta span {
  color: var(--text-dim);
  margin-right: 4px;
}

.ticket-original-body {
  margin: 0;
  font-family: inherit;
  font-size: 12px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
  color: var(--text);
}

.ticket-reply {
  background: var(--bg);
  border: 1px solid var(--border);
  border-left: 3px solid var(--success);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 20px;
}

.ticket-reply-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--success);
  margin-bottom: 8px;
}

.ticket-reply-body {
  margin: 0;
  font-family: inherit;
  font-size: 12px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-wrap: break-word;
  color: var(--text);
}

.ticket-reply-body.muted {
  color: var(--text-dim);
  font-style: italic;
}

.ticket-context-block {
  background: var(--bg);
  border: 1px solid var(--border);
  border-left: 3px solid var(--text-dim);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 20px;
}

.ticket-context-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
  margin-bottom: 10px;
}

.ticket-context-list {
  margin: 0;
  padding-left: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.ticket-context-list li {
  font-size: 12px;
  line-height: 1.5;
  color: var(--text);
  padding-left: 4px;
}

.ticket-context-list li .ctx-step-badge {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.5px;
  background: var(--surface-2);
  color: var(--text-dim);
  padding: 2px 6px;
  border-radius: 4px;
  margin-right: 6px;
}

.ticket-context-list li .src {
  color: var(--success);
  font-weight: 600;
}

/* Scrollbar */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }
