/* Heirloom voice — browser client styles.
   Palette: navy #0A2240, gold #C49A3C, teal #1A6B5C, bg #F5F1EB. */

:root {
  --navy: #0A2240;
  --navy-soft: #1c365c;
  --gold: #C49A3C;
  --gold-soft: #d6b25c;
  --teal: #1A6B5C;
  --teal-soft: #2a8975;
  --bg: #F5F1EB;
  --bg-card: #FFFDF8;
  --line: #e5dccc;
  --line-strong: #c9bea7;
  --text: #1a1a1a;
  --text-muted: #5a5a5a;
  --danger: #a83333;
  --success: #2f7a4f;
  --shadow: 0 2px 6px rgba(10, 34, 64, 0.06), 0 8px 24px rgba(10, 34, 64, 0.05);
  --radius: 10px;
  --radius-small: 6px;
  --serif: "Iowan Old Style", "Hoefler Text", Georgia, "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 {
  font-family: var(--serif);
  color: var(--navy);
  font-weight: 600;
  letter-spacing: -0.01em;
}

h1 { font-size: 1.75rem; margin: 0 0 0.25rem 0; }
h2 { font-size: 1.15rem; margin: 1.5rem 0 0.75rem 0; }

a { color: var(--teal); }

.muted { color: var(--text-muted); font-size: 0.95rem; margin: 0; }
.hidden { display: none !important; }

/* ----- Header ----- */
.app-header {
  background: var(--navy);
  color: #fff;
  border-bottom: 3px solid var(--gold);
}
.app-header-inner {
  max-width: 1100px;
  margin: 0 auto;
  padding: 1rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}
.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}
.brand-mark {
  width: 28px;
  height: 28px;
  background: var(--gold);
  border-radius: 50%;
  box-shadow: inset 0 0 0 3px var(--navy), 0 0 0 2px var(--gold);
}
.brand-text { display: flex; flex-direction: column; line-height: 1.1; }
.brand-title {
  font-family: var(--serif);
  font-size: 1.4rem;
  letter-spacing: 0.02em;
}
.brand-sub {
  font-size: 0.75rem;
  color: var(--gold-soft);
  text-transform: uppercase;
  letter-spacing: 0.15em;
}

.tabs {
  display: flex;
  gap: 0.25rem;
}
.tab {
  background: transparent;
  border: 0;
  color: rgba(255, 255, 255, 0.7);
  font: inherit;
  padding: 0.55rem 1rem;
  border-radius: var(--radius-small);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.tab:hover { color: #fff; background: rgba(255, 255, 255, 0.06); }
.tab[aria-selected="true"] {
  color: var(--navy);
  background: var(--gold);
  font-weight: 600;
}

/* ----- Main + Cards ----- */
.app-main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 2rem 1.5rem 4rem;
}

.panel-head { margin-bottom: 1.25rem; }
.panel-head.row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.card {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem;
}

/* ----- Forms / fields ----- */
.field { display: flex; flex-direction: column; gap: 0.35rem; margin-bottom: 1rem; }
.field.full { width: 100%; }
.field label { font-weight: 600; font-size: 0.9rem; color: var(--navy); }

.field input[type="text"],
.field input[type="number"],
.field select,
.field textarea {
  font: inherit;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--line-strong);
  background: #fff;
  border-radius: var(--radius-small);
  color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.field input:focus,
.field select:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(26, 107, 92, 0.15);
}

textarea.prompt {
  font-family: var(--mono);
  font-size: 0.85rem;
  line-height: 1.55;
  white-space: pre-wrap;
  resize: vertical;
  min-height: 200px;
}

.checkbox {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 500;
  color: var(--text);
  margin-top: 1.6rem;
}
.field.check { margin-bottom: 1rem; }

.section-heading {
  border-top: 1px solid var(--line);
  padding-top: 1rem;
  margin-top: 1.5rem;
}
.section-heading:first-of-type { border-top: 0; padding-top: 0; margin-top: 0; }

.grid.two {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0 1rem;
}
@media (max-width: 640px) {
  .grid.two { grid-template-columns: 1fr; }
}

.form-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1.5rem;
  padding-top: 1rem;
  border-top: 1px solid var(--line);
}

/* Segmented mode field */
fieldset.segmented {
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-small);
  padding: 0.5rem;
  display: inline-flex;
  gap: 0.25rem;
  margin: 0;
  background: #fff;
}
fieldset.segmented legend {
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--navy);
  padding: 0 0.35rem;
}
fieldset.segmented label {
  position: relative;
  cursor: pointer;
}
fieldset.segmented input[type="radio"] {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
fieldset.segmented label span {
  display: inline-block;
  padding: 0.4rem 0.8rem;
  border-radius: 4px;
  font-size: 0.9rem;
  color: var(--text-muted);
  transition: background 0.15s, color 0.15s;
}
fieldset.segmented input[type="radio"]:checked + span {
  background: var(--navy);
  color: #fff;
}
fieldset.segmented label:hover span { color: var(--navy); }
fieldset.segmented input[type="radio"]:checked + span { color: #fff; }

/* ----- Interaction tab ----- */
.control-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 1.25rem;
  margin-bottom: 1.5rem;
}
.session-controls {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
  align-items: center;
  margin-bottom: 1.25rem;
}

.status-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.75rem 1rem;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius-small);
  font-family: var(--mono);
  font-size: 0.9rem;
}
.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #aaa;
  transition: background 0.2s, box-shadow 0.2s;
}
.status-dot[data-state="idle"] { background: #aaa; }
.status-dot[data-state="connecting"] { background: var(--gold); animation: pulse 1.2s infinite; }
.status-dot[data-state="listening"] { background: var(--teal); box-shadow: 0 0 0 4px rgba(26, 107, 92, 0.2); }
.status-dot[data-state="speaking"] { background: var(--gold); box-shadow: 0 0 0 4px rgba(196, 154, 60, 0.25); }
.status-dot[data-state="ended"] { background: var(--text-muted); }
.status-dot[data-state="error"] { background: var(--danger); }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

.transcript {
  margin-top: 1.25rem;
  min-height: 80px;
  padding: 1rem;
  background: var(--bg);
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius-small);
  font-size: 0.95rem;
}
.transcript .placeholder { color: var(--text-muted); margin: 0; font-style: italic; }
.transcript .line { margin: 0.25rem 0; }
.transcript .line.user { color: var(--navy); }
.transcript .line.assistant { color: var(--teal); }
.transcript .line .role-tag {
  display: inline-block;
  min-width: 5rem;
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

#remote-audio { display: none; }

/* ----- Buttons ----- */
.btn {
  font: inherit;
  font-weight: 600;
  border: 1px solid transparent;
  padding: 0.55rem 1.1rem;
  border-radius: var(--radius-small);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.05s;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary {
  background: var(--navy);
  color: #fff;
}
.btn-primary:hover:not(:disabled) { background: var(--navy-soft); }

.btn-ghost {
  background: transparent;
  color: var(--navy);
  border-color: var(--line-strong);
}
.btn-ghost:hover:not(:disabled) {
  background: #fff;
  border-color: var(--navy);
}

.btn-large {
  padding: 0.75rem 1.5rem;
  font-size: 1.05rem;
}
.btn-small {
  padding: 0.35rem 0.7rem;
  font-size: 0.85rem;
}

/* ----- Loading / empty ----- */
.loading, .empty {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--text-muted);
  font-style: italic;
}

/* ----- Database table ----- */
.conv-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.93rem;
}
.conv-table th, .conv-table td {
  text-align: left;
  padding: 0.7rem 0.6rem;
  border-bottom: 1px solid var(--line);
}
.conv-table th {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  font-weight: 600;
}
.conv-table tbody tr {
  cursor: pointer;
  transition: background 0.1s;
}
.conv-table tbody tr:hover { background: var(--bg); }
.conv-table td.mode { font-weight: 600; color: var(--navy); }
.conv-table td .pill {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--bg);
  color: var(--navy);
  border: 1px solid var(--line-strong);
}
.conv-table td .open-link {
  color: var(--teal);
  font-weight: 600;
  font-size: 0.85rem;
}

/* Conversation detail */
.conv-detail-head {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
}
.conv-meta {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.35rem 1rem;
  padding: 0.75rem 1rem;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius-small);
  margin: 0 0 1.25rem 0;
  font-size: 0.9rem;
}
.conv-meta dt { font-weight: 600; color: var(--navy); }
.conv-meta dd { margin: 0; color: var(--text); word-break: break-word; }
.conv-meta dd.prompt-snippet {
  font-family: var(--mono);
  font-size: 0.8rem;
  white-space: pre-wrap;
  max-height: 8rem;
  overflow: auto;
  background: #fff;
  padding: 0.5rem;
  border: 1px solid var(--line);
  border-radius: 4px;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
.bubble {
  max-width: 80%;
  padding: 0.7rem 0.95rem;
  border-radius: 14px;
  font-size: 0.95rem;
  line-height: 1.45;
  white-space: pre-wrap;
  word-wrap: break-word;
  box-shadow: 0 1px 2px rgba(10, 34, 64, 0.04);
}
.bubble .ts {
  display: block;
  margin-top: 0.35rem;
  font-size: 0.72rem;
  color: rgba(0,0,0,0.45);
  font-family: var(--mono);
}
.bubble.user {
  align-self: flex-end;
  background: var(--navy);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.bubble.user .ts { color: rgba(255,255,255,0.6); }
.bubble.assistant {
  align-self: flex-start;
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--text);
  border-bottom-left-radius: 4px;
}
.bubble.system {
  align-self: center;
  background: transparent;
  color: var(--text-muted);
  font-style: italic;
  font-size: 0.85rem;
  text-align: center;
  border: 0;
}

/* ----- Toasts ----- */
.toast-host {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  z-index: 1000;
  pointer-events: none;
}
.toast {
  background: var(--navy);
  color: #fff;
  padding: 0.6rem 0.9rem;
  border-radius: var(--radius-small);
  box-shadow: var(--shadow);
  font-size: 0.9rem;
  max-width: 320px;
  pointer-events: auto;
  animation: toast-in 0.2s ease-out;
}
.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

@media (max-width: 640px) {
  .app-header-inner { padding: 0.75rem 1rem; }
  .app-main { padding: 1.25rem 1rem 3rem; }
  .card { padding: 1.1rem; }
  .brand-sub { display: none; }
  .tabs { width: 100%; }
  .tab { flex: 1; text-align: center; }
}

/* --------------------------------------------------------------------- */
/* Mic-check card (Interaction tab)                                       */
/* --------------------------------------------------------------------- */
.mic-check-card { margin-bottom: 1.25rem; }
.mic-check-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}
.mic-check-title {
  font-family: var(--serif);
  color: var(--navy);
  font-size: 1.15rem;
  font-weight: 600;
  margin: 0 0 0.2rem 0;
}
.mic-check-actions { display: flex; gap: 0.5rem; }

.mic-check-body { margin-top: 1.25rem; }
.mic-check-grid {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.5rem 1rem;
  align-items: center;
  margin: 0 0 1rem 0;
  font-size: 0.92rem;
}
.mic-check-grid dt {
  font-weight: 600;
  color: var(--navy);
  font-size: 0.85rem;
}
.mic-check-grid dd { margin: 0; color: var(--text); }
.mic-check-grid select {
  font: inherit;
  padding: 0.4rem 0.55rem;
  border: 1px solid var(--line-strong);
  background: #fff;
  border-radius: var(--radius-small);
  color: var(--text);
  width: 100%;
  max-width: 28rem;
}

.mic-check-grid dd .status-yes {
  display: inline-block;
  font-weight: 600;
  color: var(--success);
}
.mic-check-grid dd .status-no {
  display: inline-block;
  font-weight: 600;
  color: var(--danger);
}
.mic-check-grid dd .status-warn {
  display: inline-block;
  font-weight: 600;
  color: var(--gold);
}

.level-meter {
  display: inline-block;
  vertical-align: middle;
  width: 16rem;
  max-width: 100%;
  height: 14px;
  background: var(--bg);
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  overflow: hidden;
  position: relative;
}
.level-meter-fill {
  height: 100%;
  width: 0%;
  background: var(--teal);
  border-radius: 999px;
  transition: width 90ms linear, background-color 200ms ease;
}
.level-value {
  display: inline-block;
  margin-left: 0.6rem;
  vertical-align: middle;
  font-family: var(--mono);
  font-size: 0.85rem;
  color: var(--text-muted);
  min-width: 3em;
}

.mic-error {
  margin: 0 0 1rem 0;
  padding: 0.6rem 0.8rem;
  border-radius: var(--radius-small);
  background: rgba(168, 51, 51, 0.08);
  color: var(--danger);
  font-size: 0.92rem;
  border: 1px solid rgba(168, 51, 51, 0.18);
}

.mic-check-actions-row {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

/* --------------------------------------------------------------------- */
/* Live connection panel (Interaction tab, during session)                */
/* --------------------------------------------------------------------- */
.connection-panel {
  margin-top: 1rem;
  padding: 0.7rem 0.9rem;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius-small);
  font-family: var(--mono);
  font-size: 0.83rem;
  line-height: 1.55;
}
.connection-row { color: var(--text); }
.connection-row.muted { color: var(--text-muted); }

/* --------------------------------------------------------------------- */
/* Database tab status chips                                              */
/* --------------------------------------------------------------------- */
.status-chip {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--line-strong);
  white-space: nowrap;
}
.status-chip.status-ok {
  background: rgba(47, 122, 79, 0.12);
  color: var(--success);
  border-color: rgba(47, 122, 79, 0.35);
}
.status-chip.status-error {
  background: rgba(168, 51, 51, 0.12);
  color: var(--danger);
  border-color: rgba(168, 51, 51, 0.35);
}
.status-chip.status-warn {
  background: rgba(196, 154, 60, 0.15);
  color: #8a6a1f;
  border-color: rgba(196, 154, 60, 0.4);
}

/* Flag summary <dl> in conversation detail */
.conv-flags {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(8rem, 1fr));
  gap: 0.4rem 0.75rem;
  padding: 0.6rem 0.9rem;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius-small);
  margin: 0 0 1rem 0;
  font-size: 0.85rem;
}
.conv-flags .flag {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}
.conv-flags .flag-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  font-weight: 600;
}
.conv-flags .flag-value {
  font-family: var(--mono);
  font-size: 0.85rem;
  color: var(--navy);
}
.conv-flags .flag-value.yes { color: var(--success); }
.conv-flags .flag-value.no  { color: var(--danger); }

/* --------------------------------------------------------------------- */
/* Login view (shown until /api/auth/me succeeds)                         */
/* --------------------------------------------------------------------- */
.login-view {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem 1rem;
  background: var(--bg, #F5F1EB);
}
.login-card {
  width: 100%;
  max-width: 24rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}
.brand-vertical {
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 0.6rem;
}
.brand-vertical .brand-text { align-items: center; }
.login-heading {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 600;
  text-align: center;
  color: var(--navy, #0A2240);
}
.login-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.login-form .field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}
.login-form input[type="text"],
.login-form input[type="password"] {
  width: 100%;
  box-sizing: border-box;
}
.login-error {
  margin: 0;
  padding: 0.55rem 0.7rem;
  border-radius: 0.4rem;
  background: rgba(192, 60, 60, 0.08);
  color: var(--danger, #B23A3A);
  font-size: 0.92rem;
}

/* --------------------------------------------------------------------- */
/* Header user / sign-out (right side of the top bar)                     */
/* --------------------------------------------------------------------- */
.header-user {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-left: auto;
}
.header-username {
  font-size: 0.9rem;
  color: var(--muted, #6b6b6b);
  white-space: nowrap;
}
@media (max-width: 640px) {
  .header-username { display: none; }
  .header-user { margin-left: 0; }
}
