:root {
  --bg: var(--tg-theme-bg-color, #f4f6f8);
  --surface: var(--tg-theme-secondary-bg-color, #ffffff);
  --surface-raised: color-mix(in srgb, var(--surface) 94%, var(--text) 6%);
  --text: var(--tg-theme-text-color, #17202a);
  --muted: var(--tg-theme-hint-color, #697684);
  --accent: var(--tg-theme-button-color, #2b78e4);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: #d9485f;
  --success: #16834a;
  --warning: #b85f12;
  --line: color-mix(in srgb, var(--muted) 23%, transparent);
  --shadow: 0 14px 38px rgba(18, 34, 52, .08);
  color: var(--text);
  background: var(--bg);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  font-synthesis: none;
}

* { box-sizing: border-box; }
html { min-width: 320px; background: var(--bg); }
body { margin: 0; min-height: 100vh; overflow-x: hidden; background: var(--bg); }
body.dialog-open { overflow: hidden; }
button, select, textarea, input { font: inherit; color: inherit; }
button { min-height: 48px; border: 0; cursor: pointer; }
button:disabled { cursor: wait; opacity: .56; }
button, input, select, textarea { -webkit-tap-highlight-color: transparent; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, audio:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 34%, transparent);
  outline-offset: 2px;
}
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(16px + env(safe-area-inset-top)) 18px 12px;
  background: color-mix(in srgb, var(--bg) 91%, transparent);
  backdrop-filter: blur(18px);
}
h1 { margin: 3px 0 0; font-size: clamp(23px, 6vw, 28px); line-height: 1.08; letter-spacing: -.025em; }
h2 { margin: 4px 0 12px; font-size: 18px; line-height: 1.25; letter-spacing: -.015em; }
h3 { margin: 0; font-size: 15px; line-height: 1.35; }
p { line-height: 1.5; }
.eyebrow { color: var(--accent); font-size: 10px; font-weight: 850; letter-spacing: .16em; }
.meta { color: var(--muted); font-size: 10px; font-weight: 750; letter-spacing: .08em; text-transform: uppercase; }
.muted { color: var(--muted); font-size: 13px; }
.center { text-align: center; }
.danger-text { color: var(--danger) !important; }

.status {
  flex: 0 0 auto;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: 12px;
}
.status.online { color: var(--success); }
.status.offline { color: var(--warning); }

main {
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  padding: 8px 16px calc(96px + env(safe-area-inset-bottom));
}
.view { display: none; }
.view.active { display: block; animation: view-in .18s ease-out; }
@keyframes view-in { from { opacity: 0; transform: translateY(4px); } }

.field-label { display: block; margin: 10px 2px 7px; color: var(--muted); font-size: 12px; font-weight: 750; }
select, textarea, input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 15px;
  background: var(--surface);
}
select { min-height: 50px; padding: 0 42px 0 14px; }
textarea { min-height: 126px; padding: 14px; resize: vertical; line-height: 1.5; }
input { min-height: 48px; padding: 0 14px; }
select:focus, textarea:focus, input:focus { border-color: var(--accent); }

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  margin: 14px 0 12px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--surface-raised);
}
.segmented button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border-radius: 12px;
  background: transparent;
  color: var(--muted);
  font-weight: 760;
}
.segmented button.active { background: var(--surface); color: var(--accent); box-shadow: 0 3px 12px rgba(22, 38, 58, .08); }
.segmented svg { width: 19px; height: 19px; }

.composer { animation: view-in .16s ease-out; }
.wide { width: 100%; }
.primary, .secondary, .danger {
  border-radius: 14px;
  padding: 0 16px;
  font-weight: 780;
}
.primary { background: var(--accent); color: var(--accent-text); }
.secondary { border: 1px solid var(--line); background: var(--surface); color: var(--text); }
.danger { background: var(--danger); color: #fff; }
#send-button { margin-top: 12px; }

.voice-stage {
  display: flex;
  min-height: 290px;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px 12px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--surface);
  text-align: center;
}
.voice-stage .wide { max-width: 360px; }
.voice-orb {
  display: grid;
  width: 76px;
  height: 76px;
  margin-bottom: 12px;
  place-items: center;
  border-radius: 50%;
  background: color-mix(in srgb, var(--accent) 11%, var(--surface));
  color: var(--accent);
}
.voice-orb svg { width: 34px; height: 34px; }
.recording-dot { width: 15px; height: 15px; border-radius: 50%; background: var(--danger); box-shadow: 0 0 0 8px color-mix(in srgb, var(--danger) 12%, transparent); animation: pulse 1.35s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 14px color-mix(in srgb, var(--danger) 4%, transparent); } }
.timer { margin-top: 24px; font-variant-numeric: tabular-nums; font-size: 42px; font-weight: 720; letter-spacing: -.04em; }
.two-actions { display: grid; width: 100%; max-width: 420px; grid-template-columns: 1fr 1.2fr; gap: 10px; margin-top: 16px; }
audio { width: 100%; max-width: 430px; margin: 12px 0; }
.progress-wrap { width: 100%; max-width: 430px; margin-top: 18px; text-align: left; }
.progress-label { display: flex; justify-content: space-between; margin-bottom: 7px; color: var(--muted); font-size: 12px; }
progress { width: 100%; height: 9px; accent-color: var(--accent); }

.receipt {
  display: grid;
  grid-template-columns: 42px 1fr;
  gap: 10px 12px;
  margin-top: 16px;
  padding: 15px;
  border: 1px solid color-mix(in srgb, var(--success) 28%, var(--line));
  border-radius: 18px;
  background: color-mix(in srgb, var(--success) 7%, var(--surface));
}
.receipt-icon { display: grid; width: 38px; height: 38px; place-items: center; border-radius: 50%; background: var(--success); color: #fff; }
.receipt-icon svg { width: 21px; }
.receipt-body h2 { margin: 4px 0; }
.receipt-body p { margin: 4px 0; font-size: 13px; }
.receipt .wide { grid-column: 1 / -1; }
.receipt-stage { color: var(--accent); font-weight: 760; }
.pipeline-steps {
  display: grid;
  grid-column: 1 / -1;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin: 8px 0 2px;
  padding: 0;
  list-style: none;
}
.pipeline-steps li { display: grid; gap: 5px; color: var(--muted); font-size: 10px; text-align: center; }
.pipeline-steps li span { height: 4px; border-radius: 999px; background: var(--line); }
.pipeline-steps li.complete { color: var(--success); }
.pipeline-steps li.complete span { background: var(--success); }
.pipeline-steps li.active { color: var(--accent); font-weight: 760; }
.pipeline-steps li.active span { background: var(--accent); }
.pipeline-error {
  grid-column: 1 / -1;
  margin: 4px 0;
  padding: 10px 12px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--danger) 10%, var(--surface));
  color: var(--danger);
  font-size: 12px;
}

.queue-section, .subsection { margin-top: 26px; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.section-head h2, .subsection h2 { margin-bottom: 10px; }
.counter { min-width: 30px; padding: 5px 9px; border-radius: 999px; background: var(--surface); color: var(--muted); text-align: center; font-size: 12px; }
.queue, .cards { display: grid; gap: 10px; }
.queue-empty, .empty {
  padding: 22px 16px;
  border: 1px dashed var(--line);
  border-radius: 16px;
  color: var(--muted);
  text-align: center;
  font-size: 13px;
}
.queue-item {
  display: grid;
  grid-template-columns: 36px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 11px;
  border: 1px solid var(--line);
  border-radius: 15px;
  background: var(--surface);
}
.queue-kind { display: grid; width: 36px; height: 36px; place-items: center; border-radius: 11px; background: color-mix(in srgb, var(--accent) 10%, var(--surface)); color: var(--accent); }
.queue-kind svg { width: 19px; }
.queue-copy { min-width: 0; }
.queue-copy strong, .queue-copy span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.queue-copy strong { font-size: 13px; }
.queue-copy span { margin-top: 3px; color: var(--muted); font-size: 11px; }
.queue-actions { display: flex; gap: 4px; }
.queue-actions button { min-width: 40px; min-height: 40px; padding: 0; border-radius: 11px; background: var(--surface-raised); color: var(--accent); }
.queue-actions svg { width: 18px; }
.queue-item.failed { border-color: color-mix(in srgb, var(--danger) 35%, var(--line)); }
.queue-item.synced { border-color: color-mix(in srgb, var(--success) 32%, var(--line)); }

.icon-button {
  display: grid;
  width: 48px;
  min-width: 48px;
  padding: 0;
  place-items: center;
  border-radius: 14px;
  background: transparent;
  color: var(--accent);
}
.search { position: relative; display: block; margin: 4px 0 10px; }
.search svg { position: absolute; top: 13px; left: 14px; width: 20px; color: var(--muted); pointer-events: none; }
.search input { padding-left: 43px; }
.filter-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 14px; }

.card {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--surface);
  box-shadow: 0 7px 24px rgba(20, 35, 55, .035);
}
.card.clickable { cursor: pointer; }
.card.clickable:hover { border-color: color-mix(in srgb, var(--accent) 34%, var(--line)); }
.card-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.card-title { min-width: 0; }
.card p { margin: 7px 0 0; color: var(--muted); font-size: 13px; }
.card-status { flex: 0 0 auto; padding: 5px 8px; border-radius: 999px; background: var(--surface-raised); color: var(--muted); font-size: 10px; font-weight: 720; }
.card-status.ready { background: color-mix(in srgb, var(--success) 11%, var(--surface)); color: var(--success); }
.card-status.error { background: color-mix(in srgb, var(--danger) 10%, var(--surface)); color: var(--danger); }
.card-status.processing { background: color-mix(in srgb, var(--accent) 10%, var(--surface)); color: var(--accent); }
.card-actions { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 12px; }
.card-actions button { min-height: 42px; padding: 0 12px; border-radius: 12px; background: color-mix(in srgb, var(--accent) 9%, var(--surface)); color: var(--accent); font-size: 12px; font-weight: 760; }

.tabbar {
  position: fixed;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  padding: 7px 8px calc(7px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(18px);
}
.tabbar button {
  display: flex;
  min-height: 51px;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 3px;
  border-radius: 12px;
  background: transparent;
  color: var(--muted);
  font-size: 10px;
}
.tabbar button svg { width: 21px; height: 21px; }
.tabbar button.active { color: var(--accent); }

.detail-view {
  position: fixed;
  inset: 0;
  z-index: 30;
  overflow: hidden;
  background: var(--bg);
  animation: detail-in .2s ease-out;
}
@keyframes detail-in { from { transform: translateX(20px); opacity: 0; } }
.detail-header {
  display: grid;
  grid-template-columns: 48px 1fr;
  align-items: center;
  gap: 8px;
  padding: calc(12px + env(safe-area-inset-top)) 14px 10px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 94%, transparent);
  backdrop-filter: blur(18px);
}
.detail-header h1 { font-size: 21px; }
.detail-scroll { height: calc(100vh - 78px - env(safe-area-inset-top)); overflow-y: auto; padding: 14px 16px calc(32px + env(safe-area-inset-bottom)); }
.detail-panel { width: 100%; max-width: 720px; margin: 0 auto; }
.detail-block { margin-bottom: 12px; padding: 14px; border: 1px solid var(--line); border-radius: 17px; background: var(--surface); }
.detail-block h2 { margin-bottom: 8px; }
.detail-content { white-space: pre-wrap; overflow-wrap: anywhere; color: var(--text); font-size: 14px; line-height: 1.55; }
.facts { display: grid; gap: 1px; margin: 12px 0; overflow: hidden; border: 1px solid var(--line); border-radius: 15px; background: var(--line); }
.facts div { display: grid; grid-template-columns: minmax(105px, .8fr) 1.3fr; gap: 12px; padding: 11px 12px; background: var(--surface); }
.facts dt { color: var(--muted); font-size: 12px; }
.facts dd { margin: 0; overflow-wrap: anywhere; font-size: 12px; font-weight: 650; }
.list { display: grid; gap: 8px; padding: 0; list-style: none; }
.list li { padding: 11px 12px; border-radius: 13px; background: var(--surface-raised); font-size: 13px; }
.evidence { cursor: pointer; }
.evidence:hover { color: var(--accent); }
.pill-row { display: flex; flex-wrap: wrap; gap: 7px; }
.pill { padding: 7px 9px; border: 1px solid var(--line); border-radius: 999px; background: var(--surface-raised); font-size: 11px; }

.dialog-layer {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 14px 14px calc(14px + env(safe-area-inset-bottom));
  background: rgba(7, 15, 25, .56);
  backdrop-filter: blur(7px);
  animation: fade-in .16s ease-out;
}
@keyframes fade-in { from { opacity: 0; } }
.dialog-card {
  width: 100%;
  max-width: 560px;
  max-height: min(88vh, 760px);
  overflow-y: auto;
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 24px;
  background: var(--surface);
  box-shadow: var(--shadow);
  animation: sheet-in .2s ease-out;
}
@keyframes sheet-in { from { transform: translateY(18px); } }
.dialog-card h2 { margin-top: 7px; font-size: 21px; }
.dialog-actions { display: grid; grid-template-columns: 1fr 1.25fr; gap: 9px; margin-top: 18px; }
.check-list { display: grid; gap: 8px; padding-left: 20px; color: var(--muted); font-size: 13px; line-height: 1.45; }
.safety-note { display: grid; grid-template-columns: 34px 1fr; gap: 9px; margin: 14px 0; padding: 12px; border-radius: 14px; background: color-mix(in srgb, var(--success) 8%, var(--surface)); color: var(--success); }
.safety-note p { margin: 0; color: var(--text); font-size: 13px; }

#toast {
  position: fixed;
  right: 16px;
  bottom: calc(88px + env(safe-area-inset-bottom));
  left: 16px;
  z-index: 70;
  max-width: 520px;
  margin: auto;
  padding: 12px 14px;
  border-radius: 14px;
  background: #17202a;
  color: #fff;
  opacity: 0;
  pointer-events: none;
  transform: translateY(8px);
  transition: opacity .18s ease, transform .18s ease;
}
#toast.show { opacity: 1; transform: none; }

@media (min-width: 600px) {
  .topbar { max-width: 800px; margin: auto; }
  .tabbar { right: auto; left: 50%; width: min(720px, 100%); transform: translateX(-50%); border: 1px solid var(--line); border-bottom: 0; border-radius: 20px 20px 0 0; }
  .dialog-layer { align-items: center; }
}

@media (max-width: 374px) {
  main { padding-right: 12px; padding-left: 12px; }
  .filter-grid, .two-actions, .dialog-actions { grid-template-columns: 1fr; }
  .facts div { grid-template-columns: 1fr; gap: 4px; }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: var(--tg-theme-bg-color, #0f141a);
    --surface: var(--tg-theme-secondary-bg-color, #182029);
    --surface-raised: color-mix(in srgb, var(--surface) 90%, #ffffff 10%);
    --text: var(--tg-theme-text-color, #f1f5f8);
    --muted: var(--tg-theme-hint-color, #96a4b2);
    --line: color-mix(in srgb, var(--muted) 27%, transparent);
    --shadow: 0 18px 46px rgba(0, 0, 0, .28);
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}

/* Accessibility contract: min-height: 48px for primary touch targets. */
