:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --primary: #0f766e;
  --primary-text: #ecfdf5;
  --danger: #c2410c;
  --danger-text: #fff7ed;
  --secondary: #1d4ed8;
  --secondary-text: #eff6ff;
  --shadow: 0 8px 24px rgba(15, 23, 42, 0.06);
  --radius: 14px;
  --radius-sm: 10px;
  --pad: clamp(0.85rem, 2vw, 1.25rem);
  --app-max: 1320px;
  --touch: 44px;
  --gutter: max(0.75rem, env(safe-area-inset-left), env(safe-area-inset-right));
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: "DM Sans", "Noto Sans SC", sans-serif;
  color: var(--text);
  background:
    linear-gradient(180deg, #eef7f5 0%, var(--bg) 220px, var(--bg) 100%);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow-x: hidden;
}

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

.app {
  width: min(var(--app-max), calc(100% - (var(--gutter) * 2)));
  margin: 0 auto;
  padding:
    max(1rem, env(safe-area-inset-top))
    0
    calc(1.25rem + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-height: 100dvh;
}

.app-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
}

.app-header h1 {
  margin: 0;
  font-size: clamp(1.4rem, 3.2vw, 1.85rem);
  letter-spacing: -0.02em;
  line-height: 1.2;
}

.subtitle {
  margin: 0.3rem 0 0;
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.4;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(280px, 340px) minmax(0, 1fr);
  gap: 1rem;
  align-items: stretch;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: var(--pad);
  min-width: 0;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.85rem;
  min-height: 2rem;
}

.panel-head h2,
.chat-head h2 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  line-height: 1.3;
}

.pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  max-width: 55%;
  min-height: 1.7rem;
  padding: 0.2rem 0.65rem;
  border-radius: 999px;
  background: #ecfeff;
  color: #0f766e;
  border: 1px solid #99f6e4;
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-bottom: 0.8rem;
  min-width: 0;
}

.field > span {
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 500;
}

.field[hidden] {
  display: none !important;
}

.field select,
.field input[type="text"],
.field input[type="email"],
.field input[type="password"],
.chat-composer textarea,
dialog input[type="text"] {
  width: 100%;
  max-width: 100%;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 0.7rem 0.8rem;
  min-height: var(--touch);
}

.field select:focus,
.field input:focus,
.chat-composer textarea:focus,
dialog input:focus {
  outline: 2px solid rgba(15, 118, 110, 0.25);
  border-color: #5eead4;
}

.level-meter {
  width: 100%;
  height: 10px;
  border-radius: 999px;
  background: #e2e8f0;
  overflow: hidden;
  margin-bottom: 0.9rem;
}

.level-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #14b8a6, #0f766e);
  transition: width 60ms linear;
}

.btn-row {
  display: grid;
  gap: 0.55rem;
}

.primary-row {
  grid-template-columns: 1fr 1fr;
  margin-bottom: 0.55rem;
}

.secondary-row {
  grid-template-columns: repeat(3, minmax(0, 1fr));
  margin-bottom: 0.9rem;
}

.btn {
  border: 0;
  border-radius: 999px;
  min-height: var(--touch);
  padding: 0.55rem 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 120ms ease, opacity 120ms ease, background 120ms ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  white-space: nowrap;
  width: 100%;
}

.btn-compact {
  width: auto;
  min-width: auto;
  min-height: 2.25rem;
  padding: 0.4rem 0.85rem;
  flex: 0 0 auto;
}

.btn:hover:not(:disabled) {
  transform: translateY(-1px);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

.btn-primary {
  background: var(--primary);
  color: var(--primary-text);
}

.btn-danger {
  background: var(--danger);
  color: var(--danger-text);
}

.btn-secondary {
  background: var(--secondary);
  color: var(--secondary-text);
}

.btn-ghost {
  background: #f1f5f9;
  color: #334155;
  border: 1px solid var(--line);
}

.upload-box {
  display: grid;
  gap: 0.65rem;
  padding: 0.85rem;
  border: 1px dashed #cbd5e1;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  margin-bottom: 0.75rem;
  min-width: 0;
}

.upload-box .field {
  margin-bottom: 0;
}

.upload-box input[type="file"] {
  width: 100%;
  max-width: 100%;
  font-size: 0.86rem;
  color: var(--muted);
  padding: 0.15rem 0;
}

.upload-box .btn {
  width: 100%;
}

.status {
  margin: 0;
  color: #0f766e;
  font-size: 0.9rem;
  line-height: 1.45;
  word-break: break-word;
}

.status.inline {
  margin-top: 0;
  color: var(--muted);
}

.note {
  margin: 0.45rem 0 0;
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.45;
}

.control-panel .status {
  margin-top: 0.15rem;
}

.transcript-panel {
  display: flex;
  flex-direction: column;
  min-height: 420px;
  height: 100%;
}

.transcript {
  flex: 1;
  min-height: 280px;
  max-height: min(62vh, 640px);
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  -webkit-overflow-scrolling: touch;
}

.empty-hint,
.chat-empty {
  margin: auto;
  color: var(--muted);
  text-align: center;
  padding: 1.5rem 0.75rem;
}

.transcript-item {
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 0.7rem 0.8rem;
  background: var(--surface-2);
}

.transcript-meta {
  font-size: 0.78rem;
  color: #0f766e;
  font-weight: 600;
  margin-bottom: 0.25rem;
  word-break: break-word;
}

.chat-panel {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  flex: 1 1 auto;
  min-height: min(68vh, 820px);
}

.chat-head {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-bottom: 0;
  flex: 0 0 auto;
}

.chat-head-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  min-height: 2.25rem;
}

.chat-messages-wrap {
  position: relative;
  min-width: 0;
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat-messages {
  flex: 1 1 auto;
  min-height: min(52vh, 620px);
  max-height: min(70vh, 900px);
  height: min(70vh, 900px);
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  padding: 0.85rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: #f8fafc;
  scroll-behavior: auto;
  overflow-anchor: none;
  -webkit-overflow-scrolling: touch;
}

.chat-jump-bottom {
  position: absolute;
  right: 0.75rem;
  bottom: 0.75rem;
  z-index: 2;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.96);
  color: var(--text);
  font: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.45rem 0.8rem;
  min-height: 2.1rem;
  box-shadow: 0 4px 14px rgba(15, 23, 42, 0.12);
  cursor: pointer;
}

.chat-jump-bottom.hidden {
  display: none;
}

.chat-jump-bottom:hover {
  background: #fff;
}

.chat-bubble {
  max-width: min(720px, 100%);
  width: fit-content;
  border-radius: 14px;
  padding: 0.75rem 0.85rem;
  line-height: 1.45;
  animation: chatIn 160ms ease-out;
  overflow-wrap: anywhere;
}

@keyframes chatIn {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.chat-bubble.user {
  align-self: flex-end;
  background: #1d4ed8;
  color: #eff6ff;
}

.chat-bubble.assistant {
  align-self: flex-start;
  width: min(720px, 100%);
  background: #ffffff;
  border: 1px solid var(--line);
  color: var(--text);
}

.chat-role {
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  margin-bottom: 0.3rem;
  opacity: 0.8;
  font-weight: 700;
}

.search-badge {
  display: inline-flex;
  align-items: center;
  margin: 0 0 0.5rem;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
  color: #075985;
  background: #e0f2fe;
  border: 1px solid #7dd3fc;
}

.search-badge.done {
  color: #065f46;
  background: #d1fae5;
  border-color: #6ee7b7;
}

.search-badge.hidden {
  display: none;
}

.think-block {
  margin-bottom: 0.55rem;
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  background: #f8fafc;
  overflow: hidden;
}

.think-block summary {
  cursor: pointer;
  list-style: none;
  padding: 0.5rem 0.65rem;
  color: #64748b;
  font-size: 0.86rem;
  font-weight: 600;
  user-select: none;
}

.think-block summary::-webkit-details-marker {
  display: none;
}

.think-block summary::before {
  content: "▸ ";
}

.think-block[open] summary::before {
  content: "▾ ";
}

.think-content {
  padding: 0 0.7rem 0.65rem;
  color: #64748b;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: 0.86rem;
  border-top: 1px dashed #e2e8f0;
}

.answer-content {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.answer-content h3 {
  margin: 0.65rem 0 0.3rem;
  font-size: 0.98rem;
}

.answer-content h3:first-child {
  margin-top: 0;
}

.answer-content p {
  margin: 0.25rem 0 0.45rem;
}

.answer-content ul {
  margin: 0.15rem 0 0.45rem 1.1rem;
  padding: 0;
}

.chat-composer {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.55rem;
  align-items: stretch;
  background: var(--surface);
  padding-top: 0.15rem;
  flex: 0 0 auto;
}

.chat-composer textarea {
  resize: vertical;
  min-height: 72px;
  max-height: 160px;
  width: 100%;
}

.chat-composer-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.chat-composer-actions .btn {
  width: 100%;
}

#chatVoiceBtn.recording {
  background: #dc2626;
  color: #fff;
}

.streaming-cursor::after {
  content: "▍
  animation: blink 1s step-end infinite;
  color: var(--primary);
}

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

dialog {
  width: min(520px, calc(100vw - 1.5rem));
  max-height: min(88vh, 720px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 0;
  box-shadow: var(--shadow);
  margin: auto;
}

dialog::backdrop {
  background: rgba(15, 23, 42, 0.35);
}

.dialog-body {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1rem;
  max-height: min(88vh, 720px);
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.dialog-hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.45;
}

.dialog-body h3,
.dialog-body h4 {
  margin: 0;
}

.enroll-actions {
  grid-template-columns: 1fr 1fr;
}

.speaker-list-block {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  min-width: 0;
}

#speakerList {
  margin: 0;
  padding: 0;
  list-style: none;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.speaker-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.5rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: #fff;
  color: var(--text);
  min-width: 0;
}

.speaker-name {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
  flex: 1;
}

.speaker-delete-btn {
  flex: 0 0 auto;
  width: auto;
  min-height: 2.1rem;
  padding: 0.3rem 0.7rem;
  font-size: 0.8rem;
  color: #b91c1c;
}

.speaker-empty {
  color: var(--muted);
  padding: 0.25rem 0;
}

/* Wide desktop */
@media (min-width: 1100px) {
  .workspace {
    grid-template-columns: 360px minmax(0, 1fr);
  }

  .transcript-panel {
    min-height: 480px;
  }

  .transcript {
    max-height: 560px;
  }

  .chat-panel {
    min-height: min(72vh, 920px);
  }

  .chat-messages {
    min-height: min(58vh, 720px);
    max-height: min(74vh, 980px);
    height: min(74vh, 980px);
  }

  .chat-composer {
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: end;
  }

  .chat-composer-actions {
    grid-template-columns: 1fr;
    width: 132px;
  }
}

/* Tablet / phone: single column */
@media (max-width: 900px) {
  .app {
    width: min(100% - (var(--gutter) * 2), var(--app-max));
    padding-top: max(0.85rem, env(safe-area-inset-top));
    gap: 0.85rem;
  }

  .workspace {
    grid-template-columns: 1fr;
  }

  .transcript-panel {
    min-height: 0;
  }

  .transcript {
    min-height: 140px;
    max-height: 28vh;
  }

  .chat-panel {
    min-height: 62vh;
  }

  .chat-messages {
    min-height: 48vh;
    max-height: 62vh;
    height: 58vh;
  }

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

  .secondary-row .btn:last-child {
    grid-column: 1 / -1;
  }

  .pill {
    max-width: 48%;
  }
}

/* Narrow phones */
@media (max-width: 420px) {
  .btn {
    padding: 0.55rem 0.7rem;
    font-size: 0.92rem;
  }

  .primary-row,
  .secondary-row,
  .enroll-actions,
  .chat-composer-actions {
    grid-template-columns: 1fr;
  }

  .secondary-row .btn:last-child {
    grid-column: auto;
  }

  .chat-bubble {
    max-width: 100%;
  }

  .chat-jump-bottom {
    right: 0.55rem;
    bottom: 0.55rem;
  }

  dialog {
    width: calc(100vw - 1rem);
  }
}

/* Prefer less hover motion on touch devices */
@media (hover: none) {
  .btn:hover:not(:disabled) {
    transform: none;
  }
}

/* Full-screen processing lock for long ASR / summary generation */
.processing-overlay {
  position: fixed;
  inset: 0;
  z-index: 4000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.25rem;
  background: rgba(15, 23, 42, 0.48);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.processing-overlay.hidden {
  display: none;
}

.processing-card {
  width: min(420px, 100%);
  padding: 1.5rem 1.35rem 1.35rem;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 20px 48px rgba(15, 23, 42, 0.22);
  text-align: center;
  border: 1px solid var(--line);
}

.processing-spinner {
  width: 2.75rem;
  height: 2.75rem;
  margin: 0 auto 1rem;
  border-radius: 50%;
  border: 3px solid #ccfbf1;
  border-top-color: var(--primary);
  animation: processing-spin 0.85s linear infinite;
}

.processing-title {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text);
}

.processing-detail {
  margin: 0.55rem 0 0;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.45;
  min-height: 2.7em;
}

.processing-progress-track {
  margin-top: 1.1rem;
  height: 6px;
  border-radius: 999px;
  background: #e2e8f0;
  overflow: hidden;
}

.processing-progress-bar {
  height: 100%;
  width: 8%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--primary), #14b8a6);
  transition: width 0.25s ease;
}

.processing-progress-bar.indeterminate {
  width: 36%;
  animation: processing-bar-slide 1.35s ease-in-out infinite;
}

@keyframes processing-spin {
  to {
    transform: rotate(360deg);
  }
}

@keyframes processing-bar-slide {
  0% {
    transform: translateX(-120%);
  }
  100% {
    transform: translateX(320%);
  }
}

body.processing-locked {
  overflow: hidden;
}

body.processing-locked .app {
  pointer-events: none;
  user-select: none;
}

.account-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--muted);
  font-size: 0.9rem;
  min-width: 0;
}

.account-bar span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 16rem;
}

.auth-gate {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: max(1rem, env(safe-area-inset-top)) var(--gutter) calc(1rem + env(safe-area-inset-bottom));
}

.auth-gate[hidden],
#app[hidden] {
  display: none !important;
}

.auth-card {
  width: min(440px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem 1.25rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.auth-card [hidden] {
  display: none !important;
}

.auth-card .field {
  margin-bottom: 0;
}

.auth-kicker {
  margin: 0;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-size: 0.75rem;
  color: var(--primary);
}

.auth-card h1 {
  margin: 0;
  font-size: 1.8rem;
}

.auth-lede,
.auth-setup {
  margin: 0;
  color: var(--muted);
  line-height: 1.45;
}

.auth-setup {
  color: #9a3412;
}

.auth-message {
  min-height: 1.2em;
  margin: 0;
  line-height: 1.4;
}

.auth-message.ok {
  color: var(--primary);
}

.auth-message.bad {
  color: var(--danger);
}

.auth-links {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.45rem;
  margin: 0;
}

.auth-text-link {
  border: 0;
  background: none;
  padding: 0;
  color: var(--primary);
  font: inherit;
  font-size: 0.92rem;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.auth-text-link:hover {
  color: #115e59;
}

.auth-card .btn-primary {
  min-height: var(--touch);
  width: 100%;
}
