:root {
  --bg: #f6f4f2;
  --surface: #ffffff;
  --surface-2: #faf8f6;
  --ink: #1f2430;
  --ink-2: #5b6170;
  --ink-3: #9096a3;
  --line: #e7e2dd;
  --brand: #b0466b;
  --brand-soft: #f6e6ec;
  --teal: #0f8a7e;
  --teal-soft: #e2f3f0;
  --green: #18a058;
  --green-dark: #128048;
  --red: #d93a3a;
  --red-soft: #fde8e8;
  --amber: #e39b21;
  --amber-soft: #fff4dd;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(31, 36, 48, 0.05), 0 8px 24px rgba(31, 36, 48, 0.06);
  --mono: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  --internals-w: 420px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: 'Vazirmatn', Tahoma, sans-serif;
  font-size: 15px;
  line-height: 1.7;
  min-height: 100vh;
  transition: padding 0.2s ease;
}
button { font-family: inherit; cursor: pointer; }

/* ───── header ───── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 28px;
  max-width: 1280px;
  margin: 0 auto;
}
.brand { display: flex; align-items: center; gap: 14px; }
.mark { width: 44px; height: 44px; color: var(--brand); flex: none; }
.brand h1 { font-size: 20px; margin: 0; font-weight: 800; letter-spacing: -0.2px; }
.brand .sub { margin: 0; color: var(--ink-2); font-size: 13px; }

.ghost-btn {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink-2);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 13px;
}
.ghost-btn { white-space: nowrap; }
.ghost-btn:hover { color: var(--ink); border-color: var(--ink-3); }
.ghost-btn[aria-pressed='true'] { background: var(--ink); color: #fff; border-color: var(--ink); }
.ghost-btn.small { padding: 2px 9px; }

/* ───── layout ───── */
.layout {
  display: grid;
  grid-template-columns: minmax(320px, 400px) 1fr;
  gap: 24px;
  max-width: 1280px;
  margin: 0 auto;
  padding: 0 28px 40px;
  align-items: start;
}
body.with-internals { padding-left: var(--internals-w); }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; padding: 0 16px 32px; }
  .topbar { padding: 14px 16px; }
}

/* ───── call card ───── */
.call-card {
  background: var(--surface);
  border-radius: 24px;
  box-shadow: var(--shadow);
  padding: 22px;
  min-height: 520px;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 16px;
}
@media (max-width: 900px) { .call-card { position: static; min-height: 0; } }

.idle-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 18px;
  padding: 40px 8px;
}
.call-btn {
  width: 128px;
  height: 128px;
  border-radius: 50%;
  border: 0;
  background: var(--green);
  color: #fff;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 2px;
  font-size: 20px;
  font-weight: 700;
  box-shadow: 0 10px 30px rgba(24, 160, 88, 0.35);
  transition: transform 0.15s ease, background 0.15s ease;
  animation: breathe 2.6s ease-in-out infinite;
}
.call-btn svg { width: 40px; height: 40px; }
.call-btn:hover { background: var(--green-dark); transform: scale(1.04); }
.call-btn:active { transform: scale(0.97); }
@keyframes breathe {
  0%, 100% { box-shadow: 0 10px 30px rgba(24, 160, 88, 0.35), 0 0 0 0 rgba(24, 160, 88, 0.25); }
  50% { box-shadow: 0 10px 30px rgba(24, 160, 88, 0.35), 0 0 0 16px rgba(24, 160, 88, 0); }
}
.hint { color: var(--ink-2); margin: 0; max-width: 280px; }
.error { color: var(--red); background: var(--red-soft); padding: 8px 12px; border-radius: 10px; margin: 0; }

.live-view { display: flex; flex-direction: column; flex: 1; gap: 14px; min-height: 0; }
.call-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
}
.state-label { font-weight: 700; font-size: 17px; }
body[data-state='LISTENING'] .state-label { color: var(--teal); }
body[data-state='END'] .state-label { color: var(--ink-2); }
.call-meta { color: var(--ink-3); font-size: 13px; font-variant-numeric: tabular-nums; }

.mic { display: flex; align-items: center; gap: 8px; color: var(--ink-3); opacity: 0.55; transition: opacity 0.2s; }
.mic svg { width: 18px; height: 18px; }
.mic.live { color: var(--teal); opacity: 1; }
.meter { width: 72px; height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; direction: ltr; }
.meter-fill { height: 100%; width: 0; background: currentColor; border-radius: 3px; transition: width 0.06s linear; }

.conversation {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  max-height: 340px;
  min-height: 120px;
}
.line { display: flex; flex-direction: column; max-width: 88%; }
.line .speaker { font-size: 11.5px; color: var(--ink-3); margin: 0 4px 2px; }
.line .bubble { padding: 8px 13px; border-radius: 14px; font-size: 14.5px; }
.line.agent { align-self: flex-start; }
.line.agent .bubble { background: var(--surface-2); border: 1px solid var(--line); border-start-start-radius: 4px; }
.line.division { align-self: flex-start; }
.line.division .bubble { background: var(--teal-soft); color: #0b5d55; border-start-start-radius: 4px; }
.line.caller { align-self: flex-end; align-items: flex-end; }
.line.caller .bubble { background: var(--brand); color: #fff; border-start-end-radius: 4px; min-width: 60px; }
.line.caller .partial { opacity: 0.6; }
.line.caller.pending .bubble:has(.final:empty):has(.partial:empty)::before {
  content: attr(data-placeholder);
  opacity: 0.75;
  animation: pulse-text 1.4s ease-in-out infinite;
}
@keyframes pulse-text { 50% { opacity: 0.35; } }

.call-actions { margin-top: auto; display: flex; justify-content: center; padding-top: 8px; }
.hangup-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--red);
  color: #fff;
  border: 0;
  border-radius: 999px;
  padding: 10px 26px;
  font-size: 15px;
  font-weight: 700;
  box-shadow: 0 6px 18px rgba(217, 58, 58, 0.3);
}
.hangup-btn svg { width: 22px; height: 22px; }
.hangup-btn:hover { background: #bf2f2f; }
.new-call-btn {
  background: var(--green);
  color: #fff;
  border: 0;
  border-radius: 999px;
  padding: 10px 30px;
  font-size: 15px;
  font-weight: 700;
}
.new-call-btn:hover { background: var(--green-dark); }

/* ───── report ───── */
.report { border-top: 1px solid var(--line); padding-top: 14px; }
.report h3 { margin: 0 0 6px; font-size: 16px; }
.outcome { margin: 0 0 10px; font-weight: 500; }
.outcome.completed { color: var(--green-dark); }
.outcome.hangup, .outcome.no_input { color: var(--ink-2); }
.outcome.error { color: var(--red); }
.report-grid { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0; font-size: 14px; }
.report-grid dt { color: var(--ink-3); white-space: nowrap; }
.report-grid dd { margin: 0; }
.conf-row { display: flex; align-items: center; gap: 8px; }
.conf { flex: 1; max-width: 160px; height: 8px; background: var(--line); border-radius: 4px; overflow: hidden; }
.conf-fill { height: 100%; background: var(--teal); border-radius: 4px; }
.conf-num { font-variant-numeric: tabular-nums; color: var(--ink-2); font-size: 13px; }
.urgent-pill { background: var(--red); color: #fff; padding: 1px 10px; border-radius: 999px; font-size: 12.5px; font-weight: 700; }

/* ───── division tiles ───── */
.divisions h2 { font-size: 15px; color: var(--ink-2); font-weight: 500; margin: 4px 4px 12px; }
.division-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
@media (max-width: 1100px) { .division-grid { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 520px) { .division-grid { grid-template-columns: 1fr; } }
.operator-slot { margin-top: 14px; display: flex; }
.operator-slot .tile { flex: 0 1 calc((100% - 28px) / 3); min-height: 0; padding: 12px 16px; }
@media (max-width: 1100px) { .operator-slot .tile { flex-basis: calc((100% - 14px) / 2); } }
@media (max-width: 520px) { .operator-slot .tile { flex-basis: 100%; } }

.tile {
  position: relative;
  background: var(--surface);
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  min-height: 118px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  transition: opacity 0.3s, border-color 0.3s, background 0.3s, transform 0.3s, box-shadow 0.3s;
}
.tile-name { font-weight: 700; font-size: 15px; line-height: 1.5; }
.tile-tag { color: var(--ink-3); font-size: 12.5px; line-height: 1.6; }
.tile-status { margin-top: auto; display: flex; align-items: center; gap: 6px; font-size: 12.5px; font-weight: 500; min-height: 20px; }
.tile-status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line); }
.urgent-badge {
  display: none;
  position: absolute;
  top: 12px;
  inset-inline-end: 12px;
  background: var(--red);
  color: #fff;
  font-size: 12px;
  font-weight: 800;
  padding: 1px 10px;
  border-radius: 999px;
}

.divisions.thinking .tile { animation: think 1.6s ease-in-out infinite; }
.divisions.thinking .tile:nth-child(3n + 2) { animation-delay: 0.2s; }
.divisions.thinking .tile:nth-child(3n + 3) { animation-delay: 0.4s; }
@keyframes think { 50% { border-color: #d6cfc8; background: var(--surface-2); } }

.divisions.has-choice .tile:not(.chosen) { opacity: 0.42; }
.tile.ringing {
  border-color: var(--amber);
  background: var(--amber-soft);
  animation: ring-pulse 1s ease-in-out infinite;
}
.tile.ringing .dot { background: var(--amber); }
.tile.ringing .tile-status { color: #9a6410; }
@keyframes ring-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(227, 155, 33, 0.45); transform: scale(1); }
  50% { box-shadow: 0 0 0 10px rgba(227, 155, 33, 0); transform: scale(1.02); }
}
.tile.connected {
  border-color: var(--teal);
  background: var(--teal-soft);
  box-shadow: 0 8px 22px rgba(15, 138, 126, 0.18);
}
.tile.connected .dot { background: var(--teal); }
.tile.connected .tile-status { color: var(--teal); }
.tile.urgent { border-color: var(--red); }
.tile.urgent .urgent-badge { display: inline-block; }
.tile.urgent.connected { background: #fff3f3; }

/* ───── internals (English, LTR) ───── */
.internals {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: var(--internals-w);
  background: #161a22;
  color: #d6dae3;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.5;
  display: flex;
  flex-direction: column;
  z-index: 10;
  box-shadow: 4px 0 24px rgba(0, 0, 0, 0.15);
}
@media (max-width: 900px) {
  .internals { top: auto; width: 100%; height: 50vh; }
  body.with-internals { padding-left: 0; padding-bottom: 50vh; }
}
.internals-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid #2a303c;
}
.internals-head .ghost-btn { margin-left: auto; background: transparent; color: #aab1bf; border-color: #3a4150; }
.internals-body { overflow-y: auto; padding: 4px 14px 20px; }
.internals h3 { font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: #7c8597; margin: 16px 0 6px; font-weight: 600; }
.internals pre { margin: 0; white-space: pre-wrap; word-break: break-word; font-family: var(--mono); }
.internals .muted { color: #7c8597; }
#int-log, #int-tokens { max-height: 260px; overflow-y: auto; background: #0f1218; padding: 8px; border-radius: 6px; }
#int-timings { border-collapse: collapse; width: 100%; }
#int-timings td { padding: 2px 0; border-bottom: 1px solid #232835; }
#int-timings td.num { text-align: right; color: #f0c674; font-variant-numeric: tabular-nums; }
.router-turn { border-left: 2px solid #3a4150; padding-left: 8px; margin-bottom: 12px; }
.router-turn .strong { color: #8ae0c5; font-weight: 600; }
.router-turn .reasoning { color: #c5cad6; margin: 4px 0; font-family: system-ui, sans-serif; font-size: 12.5px; }
.router-turn .overrides { color: #f0a36e; }
.router-turn pre { background: #0f1218; padding: 8px; border-radius: 6px; margin-top: 6px; }

@media (max-width: 520px) {
  .brand h1 { font-size: 17px; }
  .mark { width: 36px; height: 36px; }
  .tile { min-height: 0; padding: 12px 14px; }
  .call-card { padding: 18px 16px; }
}
