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

:root {
  --bg: #0f1420;
  --panel: #171e2e;
  --panel-2: #1d2638;
  --border: #2a3550;
  --text: #e8edf7;
  --muted: #8b97b1;
  --accent: #4f8cff;
  --green: #2ecc71;
  --red: #e74c3c;
  --amber: #f1c40f;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans Devanagari", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 22px; border-bottom: 1px solid var(--border); background: var(--panel);
  flex-wrap: wrap; gap: 10px;
}
.brand { display: flex; gap: 12px; align-items: center; }
.logo { font-size: 30px; }
h1 { font-size: 17px; }
.tag { background: var(--accent); color: #fff; font-size: 11px; padding: 2px 8px; border-radius: 10px; vertical-align: middle; }
header p { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.key-status { font-size: 12.5px; color: var(--muted); }
.key-status .ok { color: var(--green); }
.key-status .bad { color: var(--red); font-weight: 600; }

main {
  display: block;
  padding: 0 22px 22px; max-width: 1300px; margin: 0 auto;
}

.panel { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 16px; }
.panel h2 { font-size: 14px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); margin-bottom: 12px; }

fieldset { border: 1px solid var(--border); border-radius: 10px; padding: 12px; margin-bottom: 14px; }
legend { font-size: 12px; color: var(--accent); padding: 0 6px; font-weight: 600; }
label { display: block; font-size: 12.5px; color: var(--text); margin-bottom: 10px; }
label.row { display: flex; align-items: flex-start; gap: 8px; line-height: 1.4; }
label.row input { margin-top: 2px; }
.hint { color: var(--muted); font-size: 11px; display: block; margin-top: 2px; }
output { float: right; color: var(--accent); font-weight: 600; }
input[type="text"], select {
  width: 100%; margin-top: 4px; padding: 7px 9px; border-radius: 7px;
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text); font-size: 13px;
}
input[type="range"] { width: 100%; margin-top: 6px; accent-color: var(--accent); }
.hidden { display: none !important; }

.call { display: flex; flex-direction: column; min-height: 78vh; }
.call-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; flex-wrap: wrap; gap: 10px; }
.callee { font-size: 14px; font-weight: 600; }
.timer { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; margin-top: 3px; }

.btn {
  border: none; border-radius: 9px; padding: 10px 20px; font-size: 14px; font-weight: 700;
  cursor: pointer; color: #fff; transition: opacity .15s;
}
.btn:disabled { opacity: 0.35; cursor: not-allowed; }
.btn.start { background: var(--green); }
.btn.end { background: var(--red); margin-left: 8px; }
.btn.small { padding: 8px 14px; font-size: 13px; background: var(--accent); }

.status-bar { display: flex; align-items: center; gap: 14px; margin-bottom: 12px; }
.pill { font-size: 12px; font-weight: 700; padding: 5px 14px; border-radius: 999px; white-space: nowrap; }
.pill.idle      { background: #2a3550; color: var(--muted); }
.pill.speaking  { background: rgba(79,140,255,.18); color: var(--accent); }
.pill.listening { background: rgba(46,204,113,.15); color: var(--green); animation: pulse 1.4s infinite; }
.pill.thinking  { background: rgba(241,196,15,.15); color: var(--amber); }
@keyframes pulse { 50% { opacity: .55; } }

.meter { flex: 1; height: 8px; background: var(--panel-2); border-radius: 4px; overflow: hidden; position: relative; }
#micLevel { height: 100%; width: 0%; background: linear-gradient(90deg, var(--green), var(--amber)); transition: width .08s linear; }
.th-marker { position: absolute; top: 0; left: 0; width: 2px; height: 100%; background: var(--red); opacity: .8; }

.transcript {
  flex: 1; overflow-y: auto; background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 10px; padding: 14px; display: flex; flex-direction: column; gap: 10px; min-height: 300px;
}
.placeholder { color: var(--muted); font-size: 13.5px; line-height: 1.6; margin: auto; max-width: 480px; text-align: center; }
.bubble { max-width: 78%; padding: 10px 14px; border-radius: 14px; font-size: 14.5px; line-height: 1.55; }
.bubble .who { display: block; font-size: 10.5px; font-weight: 700; letter-spacing: .8px; text-transform: uppercase; margin-bottom: 3px; opacity: .7; }
.bubble.bot  { align-self: flex-start; background: #24304a; border-bottom-left-radius: 4px; }
.bubble.user { align-self: flex-end; background: #2e4a33; border-bottom-right-radius: 4px; }
.bubble.sys  { align-self: center; background: transparent; color: var(--muted); font-size: 12px; padding: 2px; }
.bubble.sys.recording { width: 100%; max-width: 460px; text-align: center; }
.bubble.sys.recording audio { display: block; width: 100%; margin-top: 6px; height: 32px; }

.error { margin-top: 10px; background: rgba(231,76,60,.12); border: 1px solid rgba(231,76,60,.4); color: #ff9d92; padding: 10px 14px; border-radius: 9px; font-size: 13px; }

.manual-row { display: flex; gap: 8px; margin-top: 12px; }
.manual-row input { flex: 1; margin-top: 0; }

.history { display: flex; flex-direction: column; gap: 6px; max-height: 220px; overflow-y: auto; }
.history-item {
  text-align: left; font-size: 11.5px; padding: 7px 9px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text);
  font-variant-numeric: tabular-nums;
}
.history-item:hover { border-color: var(--accent); background: rgba(79,140,255,.08); }

/* ---------- Latency debug panel ---------- */
.lat-panel { display: flex; flex-direction: column; gap: 8px; max-height: 260px; overflow-y: auto; font-variant-numeric: tabular-nums; }
.lat-row { display: flex; flex-direction: column; gap: 3px; }
.lat-head { font-size: 11.5px; color: var(--text); }
.lat-head .hint { display: inline; font-size: 10.5px; }
.lat-avg { font-size: 11.5px; color: var(--muted); padding-bottom: 4px; border-bottom: 1px solid var(--border); }
.lat-bar { display: flex; height: 8px; border-radius: 4px; overflow: hidden; background: var(--panel-2); }
.lat-seg { display: inline-block; height: 100%; min-width: 2px; }
.lat-legend { display: flex; flex-wrap: wrap; gap: 4px 10px; font-size: 10.5px; color: var(--muted); margin-bottom: 8px; }
.lat-legend span { display: inline-flex; align-items: center; gap: 4px; }
.lat-legend .lat-seg { width: 10px; height: 8px; border-radius: 2px; }
.lat-wait { background: #6b7280; }
.lat-stt  { background: #4f8cff; }
.lat-llm  { background: #a78bfa; }
.lat-tts  { background: #f59e0b; }
.lat-net  { background: #34d399; }

/* ===================== Tabs & layout (portal v2) ===================== */
body { background: radial-gradient(1200px 600px at 80% -10%, #16203a 0%, var(--bg) 55%); }
header { position: sticky; top: 0; z-index: 20; backdrop-filter: blur(6px); background: rgba(23,30,46,.92); }

.tabs {
  display: flex; gap: 4px; max-width: 1300px; margin: 0 auto;
  padding: 14px 22px 0;
}
.tab {
  background: transparent; border: 1px solid transparent; border-bottom: none;
  color: var(--muted); padding: 10px 20px; border-radius: 11px 11px 0 0;
  font-size: 13.5px; font-weight: 600; cursor: pointer; transition: color .15s, background .15s;
}
.tab:hover { color: var(--text); background: rgba(79,140,255,.07); }
.tab.active {
  background: var(--panel); border-color: var(--border);
  color: var(--text); box-shadow: 0 -2px 0 var(--accent) inset;
}
.tabpage { display: none; }
.tabpage.active { display: block; animation: fadein .18s ease; }
@keyframes fadein { from { opacity: 0; transform: translateY(3px); } }

.tabpage > .panel, .call-grid { border-top-left-radius: 0; }
.panel { box-shadow: 0 6px 24px rgba(0,0,0,.25); }
.panel h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); margin-bottom: 12px; }

/* Call tab: transcript + side column */
.call-grid { display: grid; grid-template-columns: 1fr 330px; gap: 16px; align-items: start; }
.sidecol { display: flex; flex-direction: column; gap: 16px; }
@media (max-width: 950px) { .call-grid { grid-template-columns: 1fr; } }

/* Configuration tab: masonry columns — cards stack without row-height voids */
.config-grid { columns: 340px 3; column-gap: 14px; }
.config-grid fieldset {
  display: inline-block; width: 100%; break-inside: avoid;
  margin: 0 0 14px; background: var(--panel-2); padding: 14px;
}
.config-grid fieldset > label:last-child,
.config-grid fieldset > span.hint:last-child { margin-bottom: 0; }
/* checkbox rows: keep inline outputs inline (the floated "(ambient: –)" used
   to split across lines) and align text with the checkbox */
label.row { align-items: baseline; }
label.row output { float: none; font-weight: 600; }
label.row .hint { white-space: nowrap; }

/* Script tab */
.script-panel { max-width: 900px; margin: 0 auto; }
.script-intro { font-size: 12.5px; line-height: 1.55; margin-bottom: 14px; }
textarea {
  width: 100%; margin-top: 6px; padding: 12px 14px; border-radius: 9px;
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, "Noto Sans Devanagari", monospace;
  font-size: 12.5px; line-height: 1.55; resize: vertical;
}
textarea:focus, input:focus, select:focus { outline: none; border-color: var(--accent); }
textarea::placeholder { color: #5a668a; }
.script-actions { display: flex; align-items: center; gap: 10px; margin-top: 12px; }

/* Property tab: unit configuration rows */
.prop-identity { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 10px; }
.prop-units { display: flex; flex-direction: column; gap: 8px; margin-bottom: 4px; }
.prop-unit { display: grid; grid-template-columns: 110px 170px 150px 1fr 30px; gap: 8px; align-items: center; }
.prop-unit-head { margin: 4px 0 2px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; }
.prop-unit input {
  width: 100%; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); font-size: 12.5px;
}
.prop-unit .prop-del {
  background: none; border: none; color: var(--muted); cursor: pointer; font-size: 14px; padding: 4px;
}
.prop-unit .prop-del:hover { color: #e66; }

/* Script section cards */
.script-toolbar { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.script-view-label { font-size: 13px; color: var(--muted); flex: 1; }
.script-cards { display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px; }
.script-card {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; transition: border-color .15s, opacity .15s;
}
.script-card.dragging { opacity: .5; }
.script-card.dragover { border-color: var(--accent); }
.script-card-head { display: flex; align-items: center; gap: 6px; }
.script-grip { color: var(--muted); cursor: grab; font-size: 13px; padding: 2px 4px; user-select: none; }
.script-title {
  flex: 1; background: transparent; border: 1px solid transparent; border-radius: 7px;
  color: var(--accent); font-weight: 600; font-size: 12.5px; padding: 5px 8px;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, "Noto Sans Devanagari", monospace;
}
.script-title:hover, .script-title:focus { border-color: var(--border); background: var(--panel); }
.script-icon-btn {
  border: 1px solid transparent; background: transparent; color: var(--muted);
  border-radius: 7px; width: 26px; height: 26px; cursor: pointer; font-size: 11px;
}
.script-icon-btn:hover { color: var(--text); border-color: var(--border); background: var(--panel); }
.script-icon-btn:last-child:hover { color: #ff9d92; border-color: var(--red); background: rgba(231,76,60,.08); }
.script-card.collapsed .script-body-rows { display: none; }

/* structured body rows: prose, • bullets, POSITIVE/NEGATIVE branches */
.script-body-rows { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.script-line { display: flex; align-items: flex-start; gap: 8px; }
.script-line textarea.script-line-text {
  flex: 1; margin-top: 0; padding: 8px 10px; resize: none; overflow: hidden;
  background: var(--panel); min-height: 34px;
}
.script-badge {
  border: 1px solid transparent; border-radius: 999px; cursor: pointer;
  font-size: 10.5px; font-weight: 700; letter-spacing: .4px; padding: 6px 10px;
  margin-top: 3px; white-space: nowrap; min-width: 82px; text-align: center;
}
.script-badge.pos { color: var(--green); background: rgba(46,204,113,.12); border-color: rgba(46,204,113,.35); }
.script-badge.neg { color: #ff9d92; background: rgba(231,76,60,.12); border-color: rgba(231,76,60,.35); }
.script-badge:hover { filter: brightness(1.25); }
.script-dot { color: var(--muted); padding: 8px 2px 0 6px; font-size: 14px; line-height: 1; }
.endcall-btn {
  border: 1px solid var(--border); background: transparent; color: var(--muted);
  border-radius: 999px; font-size: 10.5px; font-weight: 600; padding: 6px 10px;
  margin-top: 3px; white-space: nowrap; cursor: pointer;
}
.endcall-btn:hover { color: var(--amber); border-color: rgba(241,196,15,.5); }
.endcall-btn.on {
  color: var(--amber); background: rgba(241,196,15,.12); border-color: rgba(241,196,15,.45);
}
.script-line-del {
  border: 1px solid transparent; background: transparent; color: transparent;
  border-radius: 7px; width: 24px; height: 28px; cursor: pointer; font-size: 11px; margin-top: 3px;
}
.script-line:hover .script-line-del { color: var(--muted); }
.script-line-del:hover { color: #ff9d92 !important; border-color: var(--red); background: rgba(231,76,60,.08); }
.script-line-add { display: flex; gap: 6px; margin-top: 2px; }
.script-line-add .btn { font-size: 10.5px; padding: 4px 10px; }
.btn.ghost { background: transparent; border: 1px solid var(--border); color: var(--muted); }
.btn.ghost:hover { color: var(--text); border-color: var(--accent); }
.btn.small.wide { width: 100%; margin-top: 4px; }
.btn:hover:not(:disabled) { filter: brightness(1.1); }

/* history rows with delete */
.history-row { display: flex; gap: 6px; align-items: stretch; }
.history-row .history-item { flex: 1; }
.history-del {
  border: 1px solid var(--border); background: transparent; color: var(--muted);
  border-radius: 8px; width: 28px; cursor: pointer; font-size: 12px;
}
.history-del:hover { color: #ff9d92; border-color: var(--red); background: rgba(231,76,60,.08); }
.history-audio {
  border: 1px solid var(--border); background: transparent; color: var(--muted);
  border-radius: 8px; width: 28px; cursor: pointer; font-size: 12px;
}
.history-audio:hover { color: var(--text); border-color: var(--accent); background: rgba(79,140,255,.08); }
/* inline recording player under a history row */
.history-player { display: flex; gap: 6px; align-items: center; padding: 2px 0 2px 6px; }
.history-player audio { flex: 1; min-width: 0; height: 30px; }
.history-player .history-del { align-self: stretch; }
/* delete button inside the transcript's recording bubble */
.rec-del {
  border: none; background: transparent; color: var(--muted); font-size: 11px;
  cursor: pointer; padding: 0 2px; text-decoration: underline dotted;
}
.rec-del:hover { color: #ff9d92; }
.history-clear {
  border: none; background: transparent; color: var(--muted); font-size: 11px;
  cursor: pointer; text-align: right; padding: 0 2px 4px; align-self: flex-end;
}
.history-clear:hover { color: #ff9d92; }
