:root {
  --bg: #fff4e6;
  --bg-alt: #ffe8c7;
  --ink: #1a1a2e;
  --ink-soft: #4b4b66;
  --ink-dim: #888098;
  --line: rgba(26, 26, 46, 0.12);
  --line-strong: rgba(26, 26, 46, 0.28);
  --paper: #fffaf2;
  --mathias: #6366f1;
  --peter: #f97316;
  --demir: #10b981;
  --pink: #ec4899;
  --yellow: #fbbf24;
  --display: "Bricolage Grotesque", "Inter", system-ui, sans-serif;
  --hand: "Caveat", cursive;
  --mono: "JetBrains Mono", ui-monospace, monospace;
  --bar-h: 56px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; }

html {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-size-adjust: 100%;
  -webkit-text-size-adjust: 100%;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--display);
  font-weight: 400;
  font-size: 16px;
  line-height: 1.45;
  overflow: hidden;
  position: relative;
  background-image:
    radial-gradient(rgba(26, 26, 46, 0.05) 1px, transparent 1px),
    radial-gradient(rgba(26, 26, 46, 0.04) 1px, transparent 1px);
  background-size: 28px 28px, 28px 28px;
  background-position: 0 0, 14px 14px;
  overscroll-behavior: none;
  touch-action: manipulation;
}

::selection { background: var(--ink); color: var(--bg); }

/* ============================================================
   AMBIENT BACKGROUND BLOBS
   ============================================================ */
.bg-blobs {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  overflow: hidden;
}
.blob {
  position: absolute;
  width: 60vmax;
  height: 60vmax;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.36;
  mix-blend-mode: multiply;
  animation: floaty 26s ease-in-out infinite;
}
.blob--1 { background: var(--mathias); top: -25vmax; left: -20vmax; }
.blob--2 { background: var(--peter);   top: -10vmax; right: -25vmax; animation-delay: -7s; }
.blob--3 { background: var(--demir);   bottom: -25vmax; left: 10vmax; animation-delay: -14s; }
@keyframes floaty {
  0%, 100% { transform: translate3d(0, 0, 0) scale(1); }
  33%      { transform: translate3d(6vw, 4vh, 0) scale(1.08); }
  66%      { transform: translate3d(-4vw, -3vh, 0) scale(0.96); }
}

/* ============================================================
   APP SHELL — full-viewport flex column
   ============================================================ */
.app {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;            /* fallback */
  height: 100dvh;           /* dynamic viewport for mobile keyboards */
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  background: rgba(255, 250, 242, 0.6);
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}
@media (max-width: 720px) {
  .app {
    border-left: none;
    border-right: none;
    background: transparent;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }
}

/* ============================================================
   HEADER BAR
   ============================================================ */
.bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 16px;
  padding-top: env(safe-area-inset-top);
  height: calc(var(--bar-h) + env(safe-area-inset-top));
  color: #fff;
  background: linear-gradient(90deg, var(--mathias), var(--peter), var(--demir));
  border-bottom: 2px solid var(--ink);
  font-family: var(--display);
  font-weight: 700;
  z-index: 4;
  box-shadow: 0 2px 0 0 rgba(26, 26, 46, 0.06);
}
.bar__title {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  font-size: 16px;
  letter-spacing: -0.01em;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.2);
}
.bar__dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.6);
  animation: pulse-w 1.6s ease-out infinite;
}
@keyframes pulse-w {
  0%   { box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.6); }
  70%  { box-shadow: 0 0 0 12px rgba(255, 255, 255, 0); }
  100% { box-shadow: 0 0 0 0 rgba(255, 255, 255, 0); }
}
.bar__meta {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.08em;
  opacity: 0.95;
}
.bar__sep { opacity: 0.7; }
.bar__msgs { opacity: 0.7; }
.bar__live[data-state="offline"]      { color: #fff5f5; }
.bar__live[data-state="connecting"]   { color: #fff7d6; }

/* ============================================================
   FEED — scrollable messages
   ============================================================ */
.feed {
  list-style: none;
  flex: 1 1 auto;
  min-height: 0;             /* critical for flex children with overflow */
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 14px 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  scroll-behavior: smooth;
  background:
    radial-gradient(circle at 20% 20%, rgba(99, 102, 241, 0.06), transparent 40%),
    radial-gradient(circle at 80% 80%, rgba(249, 115, 22, 0.05), transparent 40%);
  -webkit-overflow-scrolling: touch;
}
.feed__empty {
  margin: auto;
  color: var(--ink-dim);
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: 0.08em;
  text-align: center;
}

/* ============================================================
   MESSAGES
   ============================================================ */
.msg {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  max-width: 86%;
  animation: msgIn 0.32s cubic-bezier(0.2, 0.9, 0.2, 1) both;
}
@keyframes msgIn {
  from { opacity: 0; transform: translateY(6px) scale(0.985); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
.msg__avatar {
  flex: 0 0 auto;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  border: 2px solid var(--ink);
  box-shadow: 1.5px 1.5px 0 0 var(--ink);
}
.msg__bubble {
  position: relative;
  background: #fff;
  border: 2px solid var(--ink);
  border-radius: 14px;
  padding: 7px 11px 8px;
  box-shadow: 2px 2px 0 0 var(--ink);
  min-width: 0;
  max-width: 100%;
}
.msg__head {
  display: flex;
  align-items: baseline;
  gap: 6px;
  margin-bottom: 2px;
}
.msg__author {
  font-weight: 700;
  font-size: 12px;
  letter-spacing: -0.01em;
}
.msg__time {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--ink-dim);
}
.msg__body {
  font-size: 15.5px;
  color: var(--ink);
  line-height: 1.4;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

/* Own messages: right-aligned dark bubble */
.msg.is-own {
  margin-left: auto;
  flex-direction: row-reverse;
}
.msg.is-own .msg__bubble {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
  box-shadow: -2px 2px 0 0 var(--mathias);
}
.msg.is-own .msg__head { justify-content: flex-end; }
.msg.is-own .msg__author { color: #fff !important; opacity: 0.85; }
.msg.is-own .msg__time   { color: rgba(255, 255, 255, 0.55); }
.msg.is-own .msg__body   { color: #fff; }

.msg.is-pending { opacity: 0.55; }
.msg.is-failed .msg__bubble {
  border-color: #ef4444;
  box-shadow: 2px 2px 0 0 #ef4444;
}
.msg.is-own.is-failed .msg__bubble { box-shadow: -2px 2px 0 0 #ef4444; }

/* ============================================================
   COMPOSER — fixed at bottom, safe-area aware
   ============================================================ */
.composer {
  flex: 0 0 auto;
  background: #fff;
  border-top: 2px solid var(--ink);
  padding: 8px 10px 10px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 8px;
  position: relative;
  z-index: 3;
  box-shadow: 0 -8px 24px -16px rgba(26, 26, 46, 0.25);
}

.composer__quick {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  padding: 2px 2px 4px;
  margin: 0 -2px;
  scrollbar-width: none;
}
.composer__quick::-webkit-scrollbar { display: none; }
.composer__quick button {
  flex: 0 0 auto;
  font: inherit;
  font-size: 13px;
  padding: 6px 11px;
  border-radius: 999px;
  border: 1.5px solid var(--line-strong);
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
  transition: transform 0.15s ease, border-color 0.2s ease, background 0.2s ease;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}
.composer__quick button:hover {
  transform: translateY(-1px);
  border-color: var(--ink);
}
.composer__quick button:active {
  transform: translateY(0);
  background: var(--bg-alt);
}

.composer__row {
  display: flex;
  align-items: stretch;
  gap: 8px;
}

.composer__id {
  flex: 0 0 auto;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 0 12px;
  height: 44px;
  max-width: 42vw;
  border-radius: 999px;
  border: 2px solid var(--ink);
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
  transition: transform 0.15s ease, background 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}
.composer__id:active { transform: translateY(1px); background: var(--bg-alt); }
.composer__id-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--mathias);
  border: 1.5px solid var(--ink);
  flex: 0 0 auto;
}
.composer__id #who-label {
  display: inline-block;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.composer__id-caret {
  font-size: 10px;
  color: var(--ink-soft);
  margin-left: -2px;
}

#chat-input {
  flex: 1 1 auto;
  min-width: 0;
  font: inherit;
  font-size: 16px;       /* prevent iOS auto-zoom on focus */
  padding: 0 14px;
  height: 44px;
  border-radius: 14px;
  border: 2px solid var(--ink);
  background: var(--paper);
  color: var(--ink);
  -webkit-appearance: none;
  appearance: none;
}
#chat-input:focus {
  outline: none;
  background: #fff;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.25);
}

.composer__send {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  border-radius: 14px;
  border: 2px solid var(--ink);
  background: var(--ink);
  color: var(--bg);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.2s ease;
  box-shadow: 2px 2px 0 0 var(--mathias);
  -webkit-tap-highlight-color: transparent;
}
.composer__send svg { transform: translate(-1px, 1px); }
.composer__send:hover {
  transform: translate(-1px, -1px);
  box-shadow: 4px 4px 0 0 var(--mathias);
}
.composer__send:active {
  transform: translate(1px, 1px);
  box-shadow: 0 0 0 0 var(--mathias);
}
.composer__send:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
  box-shadow: 2px 2px 0 0 var(--mathias);
}

.composer__status {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--ink-dim);
  letter-spacing: 0.06em;
  min-height: 14px;
  padding: 0 4px;
}
.composer__status.is-error { color: #b91c1c; }
.composer__status.is-ok    { color: var(--demir); }
.composer__status:empty    { display: none; }

/* ============================================================
   IDENTITY BOTTOM SHEET
   ============================================================ */
.sheet[hidden] { display: none !important; }
.sheet {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}
.sheet__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(26, 26, 46, 0.45);
  animation: fadeIn 0.2s ease;
}
@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}
.sheet__panel {
  position: relative;
  background: var(--paper);
  border-top: 2px solid var(--ink);
  border-radius: 22px 22px 0 0;
  padding: 10px 18px max(20px, env(safe-area-inset-bottom));
  margin: 0 auto;
  width: 100%;
  max-width: 720px;
  box-shadow: 0 -10px 40px -10px rgba(26, 26, 46, 0.4);
  animation: sheetIn 0.28s cubic-bezier(0.2, 0.9, 0.2, 1);
}
@keyframes sheetIn {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}
.sheet__handle {
  width: 44px;
  height: 5px;
  border-radius: 999px;
  background: var(--line-strong);
  margin: 4px auto 14px;
  cursor: pointer;
}
.sheet__title {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--ink-soft);
  margin-bottom: 12px;
}
.sheet__pills {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}
.pill {
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  padding: 9px 14px;
  border-radius: 999px;
  border: 2px solid var(--ink);
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  transition: transform 0.15s ease, background 0.2s ease, color 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}
.pill:hover { transform: translateY(-1px); }
.pill:active { transform: translateY(0); }
.pill.is-active {
  background: var(--ink);
  color: var(--bg);
}
.sheet__custom {
  font: inherit;
  font-size: 16px;
  padding: 12px 14px;
  border-radius: 14px;
  border: 2px solid var(--ink);
  background: #fff;
  color: var(--ink);
  width: 100%;
  margin-bottom: 12px;
}
.sheet__custom:focus {
  outline: none;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.25);
}
.sheet__done {
  width: 100%;
  font: inherit;
  font-weight: 700;
  font-size: 15px;
  padding: 14px;
  border-radius: 14px;
  border: 2px solid var(--ink);
  background: var(--ink);
  color: var(--bg);
  cursor: pointer;
  box-shadow: 4px 4px 0 0 var(--mathias);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.sheet__done:active {
  transform: translate(2px, 2px);
  box-shadow: 0 0 0 0 var(--mathias);
}

/* ============================================================
   CONFETTI
   ============================================================ */
#confetti {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 60;
}

/* ============================================================
   REDUCED MOTION
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
  .blob, .bar__dot { animation: none !important; }
}
