* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}

html,
body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

@supports (height: 100dvh) {
  html,
  body {
    height: 100dvh;
  }
}

body {
  background: #1a0f12;
  color: #f5e6e8;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  font-size: 15px;
  overflow-x: hidden;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: max(0.75rem, env(safe-area-inset-top))
    max(0.75rem, env(safe-area-inset-right))
    max(0.75rem, env(safe-area-inset-bottom))
    max(0.75rem, env(safe-area-inset-left));
  background: rgba(10, 5, 8, 0.82);
  backdrop-filter: blur(4px);
}

.overlay[hidden] {
  display: none;
}

.modal {
  width: min(100%, 420px);
  max-height: min(92dvh, calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 1.5rem));
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 1.25rem 1.35rem 1.35rem;
  border: 1px solid #6b2d3a;
  border-radius: 14px;
  background: linear-gradient(160deg, #2a1218 0%, #1f0e14 100%);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
}

.modal h2 {
  margin: 0 0 0.35rem;
  font-size: 1.25rem;
  font-weight: 700;
}

.modal-lead {
  margin: 0 0 1rem;
  color: #c9a0a8;
  font-size: 0.9rem;
}

.modal--setup {
  width: min(100%, 520px);
}

.setup-view[hidden] {
  display: none;
}

.btn-back {
  display: inline-block;
  margin: 0 0 0.75rem;
  padding: 0;
  border: none;
  background: none;
  color: #c9a0a8;
  font: inherit;
  font-size: 0.82rem;
  cursor: pointer;
}

.btn-back:hover {
  color: #e8c0c8;
}

.custom-title {
  margin: 0 0 0.35rem;
  font-size: 1.25rem;
  font-weight: 700;
}

.preset-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.55rem;
  margin-bottom: 0.55rem;
}

.preset-section-label {
  grid-column: 1 / -1;
  margin: 0.35rem 0 0.15rem;
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #9a7078;
}

.preset-section-label:first-child {
  margin-top: 0;
}

.preset-card {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  width: 100%;
  margin: 0;
  padding: 0;
  border: 1px solid #6b3a45;
  border-radius: 12px;
  background: rgba(20, 10, 14, 0.65);
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
  overflow: hidden;
  transition:
    border-color 0.12s ease,
    transform 0.12s ease,
    box-shadow 0.12s ease;
}

.preset-card:hover {
  border-color: #c43d55;
  transform: translateY(-1px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.25);
}

.preset-card-art {
  display: block;
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  background: #1a0f12;
}

.preset-card-body {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.5rem 0.55rem 0.6rem;
}

.preset-card-name {
  font-size: 0.88rem;
  font-weight: 700;
  color: #f5e6e8;
}

.preset-card-tag {
  font-size: 0.72rem;
  color: #c9a0a8;
}

.preset-card--custom {
  flex-direction: row;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 0.85rem;
  border-style: dashed;
}

.preset-card-art--custom {
  flex-shrink: 0;
  width: 3rem;
  height: 3rem;
  display: grid;
  place-items: center;
  aspect-ratio: auto;
  border-radius: 10px;
  background: linear-gradient(135deg, rgba(214, 61, 82, 0.25), rgba(255, 159, 74, 0.15));
  font-size: 1.35rem;
  color: #ff8a9a;
}

.preset-confirm-hero {
  display: flex;
  align-items: center;
  gap: 0.85rem;
  margin-bottom: 1rem;
}

.preset-confirm-hero > div {
  min-width: 0;
  flex: 1;
}

.preset-confirm-img {
  flex-shrink: 0;
  width: 5.5rem;
  height: 5.5rem;
  border-radius: 12px;
  object-fit: cover;
  border: 1px solid #6b3a45;
  background: #140a0e;
}

.preset-confirm-name {
  margin: 0 0 0.2rem;
  font-size: 1.2rem;
  font-weight: 700;
}

.preset-confirm-tag {
  margin: 0;
  font-size: 0.85rem;
  color: #c9a0a8;
}

/* Desktop: one row per section (4 across) in a wider setup modal. */
@media (min-width: 640px) {
  .modal--setup {
    width: min(100%, 760px);
  }

  .preset-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

.setup-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.setup-form label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.82rem;
  color: #e8b8c0;
}

.optional {
  color: #9a7078;
  font-weight: 400;
}

.setup-form input,
.setup-form select,
.setup-form textarea {
  min-height: 44px;
  padding: 0.55rem 0.65rem;
  border: 1px solid #6b3a45;
  border-radius: 8px;
  background: #140a0e;
  color: #fff5f7;
  font: inherit;
}

.setup-form input:focus,
.setup-form select:focus,
.setup-form textarea:focus {
  outline: 2px solid #c43d55;
  outline-offset: 1px;
}

.setup-error {
  margin: 0;
  color: #ff8a8a;
  font-size: 0.85rem;
}

.setup-status {
  display: flex;
  align-items: flex-start;
  gap: 0.65rem;
  padding: 0.7rem 0.75rem;
  border: 1px solid #6b3a45;
  border-radius: 8px;
  background: rgba(20, 10, 14, 0.65);
}

.setup-status[hidden] {
  display: none;
}

.setup-status-text {
  margin: 0;
  color: #e8c0c8;
  font-size: 0.85rem;
  line-height: 1.45;
}

.spinner {
  flex-shrink: 0;
  width: 1.1rem;
  height: 1.1rem;
  margin-top: 0.1rem;
  border: 2px solid #6b3a45;
  border-top-color: #ff8a9a;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

#btnSetup,
#btnPresetStart {
  margin-top: 0.25rem;
  min-height: 48px;
  padding: 0.65rem 1rem;
  border: none;
  border-radius: 8px;
  background: linear-gradient(135deg, #d63d52, #a82840);
  color: #fff;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

#btnSetup:disabled,
#btnPresetStart:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.setup-consent {
  margin: 0.5rem 0 0;
  font-size: 0.72rem;
  line-height: 1.45;
  color: #9a7078;
  text-align: center;
}

.link-quiet {
  display: inline;
  padding: 0;
  border: none;
  background: none;
  color: #c9a0a8;
  font: inherit;
  font-size: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
  cursor: pointer;
}

.link-quiet:hover {
  color: #e8b8c0;
}

.privacy-overlay {
  z-index: 30;
}

.privacy-modal {
  width: min(100%, 440px);
}

.privacy-body {
  color: #d8b0b8;
  font-size: 0.82rem;
  line-height: 1.55;
}

.privacy-body h3 {
  margin: 1rem 0 0.35rem;
  font-size: 0.88rem;
  font-weight: 600;
  color: #e8c0c8;
}

.privacy-body p {
  margin: 0 0 0.65rem;
}

.privacy-updated {
  margin-top: 0.75rem !important;
  font-size: 0.75rem;
  color: #8a6068;
}

.btn-privacy-close {
  width: 100%;
  margin-top: 0.75rem;
  min-height: 44px;
  padding: 0.6rem 1rem;
  border: 1px solid #6b3a45;
  border-radius: 8px;
  background: transparent;
  color: #ffb8c4;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.app-footer {
  flex-shrink: 0;
  padding: 0.35rem max(0.75rem, env(safe-area-inset-right))
    max(0.35rem, env(safe-area-inset-bottom))
    max(0.75rem, env(safe-area-inset-left));
  border-top: 1px solid #2a1218;
  background: #1a0f12;
}

.fine-print {
  margin: 0;
  font-size: 0.68rem;
  line-height: 1.4;
  color: #8a6068;
  text-align: center;
}

.fine-print .link-quiet {
  font-size: 0.68rem;
  color: #9a7078;
}

.app {
  display: flex;
  flex-direction: column;
  height: var(--app-height, 100dvh);
  max-height: var(--app-height, 100dvh);
  overflow: hidden;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

.brand {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.85rem 1rem 0.65rem;
  border-bottom: 1px solid #3d1a22;
  background: #220f15;
}

.brand-link {
  text-decoration: none;
  color: inherit;
  cursor: pointer;
  transition: opacity 0.12s ease;
}

.brand-link:hover {
  opacity: 0.88;
}

.brand-emoji {
  font-size: 2.4rem;
  line-height: 1;
}

.brand-title {
  margin: 0;
  font-size: 2.5rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  background: linear-gradient(90deg, #ff6b7a, #ff9f4a);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.chat-panel {
  display: flex;
  flex: 1;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.log {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 1rem 1.25rem;
}

.line {
  margin: 0 0 0.85rem;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}

.line .who {
  color: #ff8a9a;
  font-weight: 600;
  margin-right: 0.35rem;
}

.line.user .who {
  color: #8ab4ff;
}

.line.error {
  color: #ff8a8a;
}

.line.thinking {
  color: #c9a0a8;
  font-style: italic;
  animation: thinking-pulse 1.4s ease-in-out infinite;
}

.line.thinking .who {
  color: #ff8a9a;
}

@keyframes thinking-pulse {
  0%,
  100% {
    opacity: 0.55;
  }
  50% {
    opacity: 1;
  }
}

.app--booting .chat-panel {
  position: relative;
}

.app--booting .log .line.assistant {
  position: relative;
  z-index: 2;
}

.app--booting .log .line.thinking {
  position: relative;
  z-index: 2;
}

.app--booting .input-row,
.app--booting .image-preview {
  filter: blur(6px);
  opacity: 0.55;
  pointer-events: none;
}

.input-row {
  display: flex;
  flex-shrink: 0;
  gap: 0.5rem;
  padding: 0.75rem max(0.75rem, env(safe-area-inset-right))
    max(0.75rem, env(safe-area-inset-bottom))
    max(0.75rem, env(safe-area-inset-left));
  border-top: 1px solid #3d1a22;
  background: #220f15;
}

#input {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  padding: 0.6rem 0.7rem;
  border: 1px solid #6b3a45;
  border-radius: 8px;
  background: #140a0e;
  color: #fff5f7;
  font: inherit;
  font-size: 16px;
}

#input:disabled {
  opacity: 0.5;
}

#btnSend {
  flex-shrink: 0;
  min-height: 44px;
  min-width: 4.5rem;
  padding: 0.6rem 1.1rem;
  border: none;
  border-radius: 8px;
  background: linear-gradient(135deg, #d63d52, #a82840);
  color: #fff;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

#btnSend:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-attach {
  flex-shrink: 0;
  min-height: 44px;
  min-width: 44px;
  padding: 0.6rem;
  border: 1px solid #6b3a45;
  border-radius: 8px;
  background: #140a0e;
  color: #fff5f7;
  font-size: 1.2rem;
  line-height: 1;
  cursor: pointer;
}

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

.image-preview {
  position: relative;
  flex-shrink: 0;
  padding: 0.5rem 0.75rem 0;
  align-self: flex-start;
}

.image-preview-img {
  max-height: 96px;
  max-width: 160px;
  border-radius: 10px;
  border: 1px solid #6b3a45;
  display: block;
}

.image-preview-remove {
  position: absolute;
  top: 0.25rem;
  right: 0.5rem;
  width: 24px;
  height: 24px;
  border: none;
  border-radius: 50%;
  background: rgba(20, 10, 14, 0.85);
  color: #fff;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
}

.line--image .chat-image {
  display: block;
  margin-top: 0.35rem;
  max-width: min(280px, 70%);
  max-height: 320px;
  border-radius: 12px;
  border: 1px solid #6b3a45;
}

.line--image .line-caption {
  display: block;
  margin-top: 0.3rem;
}

@media (max-width: 480px) {
  body {
    font-size: 14px;
  }

  .overlay {
    align-items: center;
    padding: max(0.75rem, env(safe-area-inset-top))
      max(0.75rem, env(safe-area-inset-right))
      max(0.75rem, env(safe-area-inset-bottom))
      max(0.75rem, env(safe-area-inset-left));
  }

  .overlay.overlay--sheet {
    align-items: end;
    padding: 0;
  }

  .modal {
    width: min(calc(100% - 0.5rem), 420px);
    max-height: min(92dvh, calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 1.5rem));
    border-radius: 14px;
    border-bottom: 1px solid #6b2d3a;
    padding: 1rem 1rem max(1rem, env(safe-area-inset-bottom));
  }

  .modal.modal--sheet {
    width: 100%;
    max-height: min(96dvh, calc(100dvh - env(safe-area-inset-top)));
    border-radius: 16px 16px 0 0;
    border-bottom: none;
    padding: 1rem 1rem max(1rem, env(safe-area-inset-bottom));
  }

  .modal h2 {
    font-size: 1.1rem;
  }

  .modal-lead {
    font-size: 0.85rem;
    margin-bottom: 0.75rem;
  }

  .preset-confirm-hero {
    gap: 0.75rem;
    margin-bottom: 0.85rem;
  }

  .preset-confirm-img {
    width: 4.5rem;
    height: 4.5rem;
  }

  .preset-confirm-name {
    font-size: 1.05rem;
    line-height: 1.25;
  }

  .preset-confirm-tag {
    font-size: 0.8rem;
    line-height: 1.35;
  }

  .setup-form {
    gap: 0.55rem;
  }

  .setup-form label {
    font-size: 0.78rem;
    gap: 0.25rem;
  }

  .setup-form input,
  .setup-form select {
    font-size: 16px;
    padding: 0.55rem 0.65rem;
  }

  .brand {
    padding: 0.5rem max(0.75rem, env(safe-area-inset-left)) 0.4rem
      max(0.75rem, env(safe-area-inset-right));
    gap: 0.35rem;
  }

  .brand-title {
    font-size: 1.55rem;
  }

  .brand-emoji {
    font-size: 1.55rem;
  }

  .log {
    padding: 0.65rem max(0.75rem, env(safe-area-inset-left))
      0.65rem max(0.75rem, env(safe-area-inset-right));
  }

  .line {
    margin-bottom: 0.6rem;
    line-height: 1.45;
    font-size: 0.95rem;
  }

  .input-row {
    gap: 0.4rem;
    padding: 0.55rem max(0.65rem, env(safe-area-inset-right))
      max(0.55rem, env(safe-area-inset-bottom))
      max(0.65rem, env(safe-area-inset-left));
  }

  #btnSend {
    padding: 0.55rem 0.75rem;
    font-size: 0.9rem;
    min-width: 4rem;
  }

  #btnSetup,
  #btnPresetStart {
    width: 100%;
    padding: 0.75rem 1rem;
  }
}

@media (max-width: 360px) {
  .brand-title {
    font-size: 1.35rem;
  }

  .brand-emoji {
    font-size: 1.35rem;
  }

  #btnSend {
    min-width: 3.5rem;
    padding-inline: 0.6rem;
  }
}
