:root {
  --color-fg: #2b2927;
  --color-bg: #cbcaf6;
  --font: 'IBM Plex Mono', monospace;
  --font-header: 'Stylish', cursive;
  --font-normal: 400;
  --font-bold: 700;
  --radius: 4px;
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 16px;
  --space-lg: 32px;
  --space-xl: 64px;
  --text-sm: clamp(14px, 0.15vw + 13.5px, 15px);
  --text-body: clamp(16px, 0.2vw + 15.5px, 18px);
  --text-header-md: clamp(22px, 0.8vw + 18px, 26px);
  --text-header-lg: clamp(30px, 1.2vw + 23px, 36px);
}

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

html, body {
  height: 100%;
  background: var(--color-bg);
  color: var(--color-fg);
  font-family: var(--font);
  font-weight: var(--font-normal);
  font-size: var(--text-body);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3,
.dashboard-header .editable-name {
  font-family: var(--font-header);
}

#app {
  position: relative;
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

#ascii-layer {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
}

#ascii-layer > .ascii-viewport {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  margin: 0;
  border: none;
  border-radius: 0;
  padding: 0;
  text-align: left;
  font-size: clamp(5px, 0.75vw, 9px);
  line-height: clamp(5px, 0.75vw, 9px);
  text-rendering: geometricPrecision;
  pointer-events: auto;
}

#screen-container {
  position: relative;
  z-index: 1;
  flex: 1;
  display: flex;
  flex-direction: column;
}

a {
  color: var(--color-fg);
  text-decoration: underline;
}

/* --- Buttons --- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-sm);
  background: var(--color-fg);
  color: var(--color-bg);
  border: none;
  border-radius: var(--radius);
  font-family: var(--font-header);
  font-weight: var(--font-bold);
  font-size: var(--text-body);
  padding: var(--space-sm) var(--space-md);
  cursor: pointer;
  transition: opacity 0.15s;
}

.btn:hover {
  opacity: 0.85;
}

.btn:focus-visible {
  outline: 2px solid var(--color-fg);
  outline-offset: 2px;
}

.btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.btn--secondary {
  background: transparent;
  color: var(--color-fg);
  border: 1px solid color-mix(in srgb, var(--color-fg) 30%, transparent);
}

.btn--danger {
  background: transparent;
  color: var(--color-fg);
  border: 1px solid color-mix(in srgb, var(--color-fg) 30%, transparent);
  opacity: 0.6;
}

/* --- Inputs --- */

.input {
  background: transparent;
  color: var(--color-fg);
  border: 1px solid color-mix(in srgb, var(--color-fg) 20%, transparent);
  border-radius: var(--radius);
  font-family: var(--font);
  font-weight: var(--font-normal);
  font-size: var(--text-body);
  padding: var(--space-sm) var(--space-md);
  width: 100%;
  transition: border-color 0.15s;
}

.input:focus {
  outline: none;
  border-color: var(--color-fg);
}

.input::placeholder {
  color: color-mix(in srgb, var(--color-fg) 30%, transparent);
}

/* --- Typography --- */

.text-md { font-size: var(--text-body); }
.text-sm { font-size: var(--text-sm); }
.text-lg {
  font-family: var(--font-header);
  font-size: var(--text-header-md);
}
.text-xl {
  font-family: var(--font-header);
  font-size: var(--text-header-lg);
}
.bold {
  font-family: var(--font-header);
  font-weight: var(--font-bold);
}
.secondary { opacity: 0.5; }
.muted { opacity: 0.3; }

/* --- Layout --- */

.screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-lg);
  text-align: center;
  gap: var(--space-md);
}

.screen--top {
  justify-content: flex-start;
  text-align: left;
  align-items: stretch;
}

/* --- Dividers --- */

.divider {
  border: none;
  border-top: 1px solid color-mix(in srgb, var(--color-fg) 10%, transparent);
  width: 100%;
}

/* --- Tabs --- */

.tabs {
  display: flex;
  gap: 0;
  align-items: center;
  border-bottom: 1px solid color-mix(in srgb, var(--color-fg) 10%, transparent);
}

.tabs-main {
  display: flex;
  gap: 0;
}

.tab {
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--color-fg);
  opacity: 0.5;
  font-family: var(--font-header);
  font-weight: var(--font-normal);
  font-size: var(--text-body);
  padding: var(--space-sm) var(--space-md);
  cursor: pointer;
  transition: opacity 0.15s, border-color 0.15s;
}

.tab--icon {
  margin-left: auto;
  padding-left: var(--space-sm);
  padding-right: var(--space-sm);
  font-size: 20px;
  line-height: 1;
}

.tab:hover {
  opacity: 0.75;
}

.tab--active {
  opacity: 1;
  font-weight: var(--font-bold);
  border-bottom-color: var(--color-fg);
}

.tab:focus-visible {
  outline: 2px solid var(--color-fg);
  outline-offset: -2px;
}

/* --- Error --- */

.error {
  border: 1px solid color-mix(in srgb, var(--color-fg) 30%, transparent);
  border-radius: var(--radius);
  padding: var(--space-sm) var(--space-md);
  font-size: var(--text-body);
  opacity: 0.8;
}

/* --- ASCII camera --- */

.ascii-viewport {
  display: block;
  font-family: 'PingFang SC', 'Noto Sans Mono CJK SC', 'Microsoft YaHei',
               'WenQuanYi Micro Hei Mono', monospace;
  font-size: 6px;
  line-height: 6px;
  letter-spacing: 0;
  white-space: pre;
  overflow: hidden;
  border: 1px solid color-mix(in srgb, var(--color-fg) 15%, transparent);
  border-radius: var(--radius);
  padding: var(--space-sm);
  max-width: 100%;
}

@media (min-width: 640px) {
  .ascii-viewport {
    font-size: 8px;
    line-height: 8px;
  }
}

/* --- Chat --- */

.chat-log {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  padding: var(--space-md) 0;
}

.chat-msg {
  max-width: 80%;
  padding: var(--space-sm) var(--space-md);
  border-radius: var(--radius);
  font-size: var(--text-body);
  word-wrap: break-word;
}

.chat-msg--user {
  align-self: flex-end;
  background: var(--color-fg);
  color: var(--color-bg);
}

.chat-msg--twin {
  align-self: flex-start;
  border: 1px solid color-mix(in srgb, var(--color-fg) 20%, transparent);
}

.chat-input-row {
  display: flex;
  gap: var(--space-sm);
}

.chat-input-row .input {
  flex: 1;
}

/* --- Character profile --- */

.character-profile {
  width: 100%;
  resize: vertical;
  min-height: 220px;
  line-height: 1.6;
}

/* --- Wallet --- */

.wallet-balance {
  font-family: var(--font-header);
  font-size: var(--text-header-lg);
  font-weight: var(--font-bold);
  padding: var(--space-sm) 0 var(--space-md);
}

.finance-panel {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  border: 1px solid color-mix(in srgb, var(--color-fg) 15%, transparent);
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--color-bg) 94%, transparent);
  padding: var(--space-md);
}

.tx-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
}

.tx-item {
  display: flex;
  justify-content: space-between;
  font-size: var(--text-body);
  padding: var(--space-xs) 0;
  border-bottom: 1px solid color-mix(in srgb, var(--color-fg) 5%, transparent);
}

/* --- Inline editable --- */

.editable-name {
  background: none;
  border: none;
  border-bottom: 1px solid color-mix(in srgb, var(--color-fg) 20%, transparent);
  color: var(--color-fg);
  font-family: var(--font-header);
  font-weight: var(--font-bold);
  font-size: var(--text-header-lg);
  padding: var(--space-xs) 0;
  outline: none;
  transition: border-color 0.15s;
}

.editable-name:focus {
  border-bottom-color: var(--color-fg);
}

/* --- Welcome screen --- */

.welcome-screen {
  position: relative;
  isolation: isolate;
  width: 100vw;
  height: 100vh;
  height: 100svh;
  padding: 0;
  overflow: hidden;
}

.welcome-content {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  text-align: center;
  padding: calc(var(--space-lg) + env(safe-area-inset-top)) var(--space-lg) calc(var(--space-lg) + env(safe-area-inset-bottom));
}

.welcome-title {
  font-family: var(--font-header);
  font-size: clamp(48px, 8vw, 120px);
  line-height: 0.95;
  text-align: center;
  min-height: 0.95em;
}

.overlay-panel {
  width: min(560px, 100%);
  border: 1px solid color-mix(in srgb, var(--color-fg) 20%, transparent);
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--color-bg) 92%, transparent);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-md);
  padding: var(--space-lg);
}

.overlay-shell {
  opacity: 0;
  transform: translateY(30px) scale(0.98);
  filter: blur(8px);
  pointer-events: none;
  transition: opacity 700ms cubic-bezier(0.22, 1, 0.36, 1),
    transform 850ms cubic-bezier(0.22, 1, 0.36, 1),
    filter 850ms cubic-bezier(0.22, 1, 0.36, 1);
}

.overlay-shell.is-visible {
  opacity: 1;
  transform: translateY(0) scale(1);
  filter: blur(0);
  pointer-events: auto;
}

.overlay-shell.is-exiting {
  opacity: 0;
  transform: translateY(14px) scale(0.985);
  filter: blur(6px);
  pointer-events: none;
}

.overlay-panel--compact {
  width: min(460px, 100%);
}

.welcome-panel {
  width: min(480px, 100%);
  align-items: stretch;
  gap: var(--space-sm);
  text-align: left;
  padding: var(--space-md);
}

.welcome-heading-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-sm);
}

.welcome-heading {
  line-height: 1.05;
  min-height: 1.2em;
}

.welcome-help-toggle {
  width: 32px;
  height: 32px;
  border-radius: 999px;
  border: 1px solid color-mix(in srgb, var(--color-fg) 30%, transparent);
  background: transparent;
  color: var(--color-fg);
  font-family: var(--font-header);
  font-size: 20px;
  line-height: 1;
  flex-shrink: 0;
  cursor: pointer;
  transition: background-color 0.2s, border-color 0.2s;
}

.welcome-help-toggle:hover {
  background: color-mix(in srgb, var(--color-fg) 8%, transparent);
}

.welcome-help-toggle:focus-visible {
  outline: 2px solid var(--color-fg);
  outline-offset: 2px;
}

.welcome-help-wrap {
  display: grid;
  grid-template-rows: 0fr;
  opacity: 0;
  transition: grid-template-rows 0.28s ease, opacity 0.2s ease;
}

.welcome-help-wrap.is-open {
  grid-template-rows: 1fr;
  opacity: 1;
}

.welcome-help-body {
  overflow: hidden;
}

.welcome-help-text {
  max-width: none;
}

.welcome-actions {
  display: flex;
  gap: var(--space-sm);
  align-items: stretch;
}

.welcome-actions > .btn {
  flex: 1 1 220px;
  min-height: 56px;
  padding: var(--space-sm) var(--space-md);
}

.welcome-secondary-actions {
  flex: 1 1 220px;
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
}

.welcome-secondary-actions .btn {
  width: 100%;
  flex: 0 0 auto;
  min-height: 48px;
  padding: var(--space-sm) var(--space-md);
}

.btn--login {
  align-self: flex-end;
  flex: none;
  min-height: 0;
  padding: 2px var(--space-sm);
  font-size: var(--text-sm);
}

.recording-panel {
  align-items: stretch;
  gap: var(--space-sm);
  text-align: left;
  padding: var(--space-md);
}

.recording-heading-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-sm);
}

.recording-heading {
  line-height: 1.05;
  min-height: 2.2em;
}

.birthing-heading {
  min-height: 1.2em;
}

.typewriter-animated {
  transition: opacity 0.16s ease, transform 0.16s ease, filter 0.16s ease;
}

.typewriter-animated.is-switching {
  opacity: 0;
  transform: translateY(-5px);
  filter: blur(3px);
}

.typewriter-animated.is-typing::after {
  content: '_';
  display: inline-block;
  margin-left: 2px;
  opacity: 0.8;
  animation: typewriter-caret-blink 0.7s steps(1, end) infinite;
}

@keyframes typewriter-caret-blink {
  0%, 50% { opacity: 0.8; }
  51%, 100% { opacity: 0; }
}

.recording-help-toggle {
  width: 32px;
  height: 32px;
  border-radius: 999px;
  border: 1px solid color-mix(in srgb, var(--color-fg) 30%, transparent);
  background: transparent;
  color: var(--color-fg);
  font-family: var(--font-header);
  font-size: 20px;
  line-height: 1;
  flex-shrink: 0;
  cursor: pointer;
  transition: background-color 0.2s, border-color 0.2s;
}

.recording-help-toggle:hover {
  background: color-mix(in srgb, var(--color-fg) 8%, transparent);
}

.recording-help-toggle:focus-visible {
  outline: 2px solid var(--color-fg);
  outline-offset: 2px;
}

.recording-help-wrap {
  display: grid;
  grid-template-rows: 0fr;
  opacity: 0;
  transition: grid-template-rows 0.28s ease, opacity 0.2s ease;
}

.recording-help-wrap.is-open {
  grid-template-rows: 1fr;
  opacity: 1;
}

.recording-help-body {
  overflow: hidden;
}

.recording-help-text {
  max-width: none;
}

.rec-status {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-sm);
  min-height: 1.5em;
}

.rec-controls--compact {
  gap: var(--space-sm);
}

.rec-controls--compact .btn {
  flex: 1 1 180px;
}

.question-textarea {
  width: 100%;
  min-height: 132px;
  resize: vertical;
  line-height: 1.5;
}

.pricing-panel {
  width: min(640px, 100%);
  align-items: stretch;
  gap: var(--space-sm);
  text-align: left;
  padding: var(--space-md);
  font-family: var(--font);
}

.pricing-heading {
  min-height: 1.2em;
}

.pricing-options {
  display: grid;
  gap: var(--space-sm);
}

.pricing-option {
  background: transparent;
  color: var(--color-fg);
  border: 1px solid color-mix(in srgb, var(--color-fg) 20%, transparent);
  border-radius: var(--radius);
  padding: var(--space-sm) var(--space-md);
  font-family: var(--font);
  font-size: var(--text-body);
  font-weight: var(--font-normal);
  text-align: left;
  cursor: pointer;
  transition: border-color 0.2s, background-color 0.2s, opacity 0.2s;
}

.pricing-option:hover {
  opacity: 0.9;
}

.pricing-option--selected {
  border-color: var(--color-fg);
  background: color-mix(in srgb, var(--color-fg) 8%, transparent);
}

.pricing-option-row {
  display: flex;
  justify-content: space-between;
  gap: var(--space-sm);
}

.pricing-option-price {
  font-family: var(--font-header);
}

.pricing-option-copy {
  margin-top: var(--space-xs);
  font-family: var(--font);
}

.pricing-signup {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.pricing-status {
  min-height: 1.4em;
  font-family: var(--font);
}

.stripe-embed-wrap {
  position: relative;
  width: 100%;
  max-width: 600px;
  margin: 0 auto;
  padding: var(--space-md);
}

.stripe-embed-back {
  margin-bottom: var(--space-sm);
}

.stripe-embed {
  min-height: 300px;
}

.verify-panel {
  width: min(520px, 100%);
  align-items: stretch;
  gap: var(--space-sm);
  text-align: left;
  padding: var(--space-md);
}

.verify-code {
  text-align: center;
  letter-spacing: 0.18em;
  font-family: var(--font);
}

/* --- Recording controls --- */

.recording-screen {
  position: relative;
  isolation: isolate;
  width: 100vw;
  height: 100vh;
  height: 100svh;
  padding: 0;
  overflow: hidden;
}

.recording-content {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-lg);
  text-align: center;
}

.birthing-content {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-md);
  text-align: center;
  padding: var(--space-lg);
}

.rec-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: var(--space-md);
}

.rec-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--color-fg);
  animation: pulse 1s ease-in-out infinite;
}

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

.rec-timer {
  font-family: var(--font-header);
  font-size: var(--text-body);
  font-weight: var(--font-bold);
  opacity: 0.7;
}

/* --- WhatsApp QR --- */

.qr-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-md);
  padding: var(--space-lg) 0;
}

.qr-container canvas {
  border: 1px solid color-mix(in srgb, var(--color-fg) 15%, transparent);
  border-radius: var(--radius);
}

/* --- Loading / Birthing --- */

.birthing-status {
  font-size: var(--text-body);
  opacity: 0.5;
  min-height: 1.5em;
}

/* --- Dashboard layout --- */

.dashboard {
  display: flex;
  flex-direction: column;
  flex: 1;
  max-width: 640px;
  width: 100%;
  margin: 0 auto;
  padding: var(--space-lg) var(--space-md);
  gap: var(--space-md);
}

.settings-panel {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
}

.settings-section {
  border: 1px solid color-mix(in srgb, var(--color-fg) 15%, transparent);
  border-radius: var(--radius);
  padding: var(--space-md);
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.dashboard-header {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  transition: gap 0.35s ease;
}

.profile-image {
  width: 96px;
  height: 72px;
  border-radius: 0;
  object-fit: contain;
  padding: var(--space-xs);
  flex-shrink: 0;
  border: none;
  background: none;
  cursor: pointer;
  transition: transform 0.35s ease, opacity 0.25s ease;
}

.profile-image:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--color-fg) 50%, transparent);
  outline-offset: 2px;
}

.profile-image--active {
  opacity: 0.8;
}

.profile-panel {
  max-height: 0;
  overflow: hidden;
  opacity: 0;
  transform: translateY(-8px);
  transition: max-height 0.45s ease, opacity 0.35s ease, transform 0.45s ease;
}

.profile-panel--open {
  max-height: 70vh;
  opacity: 1;
  transform: translateY(0);
}

.profile-image-large {
  width: 100%;
  max-height: 68vh;
  object-fit: contain;
  display: block;
  padding: var(--space-sm) 0;
  cursor: pointer;
}

.tabs,
.tab-content {
  transition: transform 0.45s ease, opacity 0.35s ease;
}

.dashboard--photo-expanded .tabs,
.dashboard--photo-expanded .tab-content {
  transform: translateY(4px);
  opacity: 0.96;
}

.tab-content {
  flex: 1;
  display: flex;
  flex-direction: column;
}

/* --- Connection status --- */

.status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: var(--space-xs);
}

.status-dot--off {
  border: 1px solid color-mix(in srgb, var(--color-fg) 30%, transparent);
}

.status-dot--on {
  background: var(--color-fg);
}

/* --- Deposit form --- */

.deposit-form {
  display: flex;
  gap: var(--space-sm);
  align-items: flex-end;
}

.deposit-form .input {
  flex: 1;
}

/* --- Responsive --- */

@media (max-width: 480px) {
  .screen {
    padding: var(--space-md);
  }

  .recording-screen {
    padding: 0;
  }

  .recording-content {
    padding: var(--space-md);
  }

  .overlay-panel {
    padding: var(--space-md);
  }

  .welcome-actions > .btn {
    flex-basis: 100%;
  }

  .welcome-secondary-actions {
    flex-basis: 100%;
  }

  .welcome-secondary-actions .btn--login {
    flex-basis: auto;
    width: auto;
  }

  .rec-controls--compact .btn {
    flex-basis: 100%;
  }

  .dashboard {
    padding: var(--space-md) var(--space-sm);
  }

  .ascii-viewport {
    font-size: 5px;
    line-height: 5px;
  }

}

/* ── Login form on welcome ── */
.welcome-login-form {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

/* ── Wallet QR ── */
.wallet-qr-section {
  text-align: center;
  padding: var(--space-sm) 0;
}
.wallet-qr {
  border: 2px solid rgba(255,255,255,0.1);
  border-radius: 8px;
  background: #fff;
  padding: 8px;
}
.wallet-address {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  opacity: 0.7;
  transition: opacity 0.15s;
}
.wallet-address:hover {
  opacity: 1;
}

/* ── Chat typing indicator ── */
.chat-msg--typing {
  opacity: 0.5;
  animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 0.3; }
  50% { opacity: 0.7; }
}

/* ── Auth form ── */
.auth-form-fields {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}
.auth-form-fields .btn--secondary {
  font-size: 0.85em;
  opacity: 0.7;
}
.auth-form-fields .btn--secondary:hover {
  opacity: 1;
}
