/* ============================================================================
   Pall — web app chat. Full-bleed, light, Apple white-monochrome "Liquid Glass",
   ported 1:1 from the iOS PALView layout and adjusted for the browser.
   Reuses the design tokens from styles.css (:root --ink, --accent, fonts, 8pt grid).
   ============================================================================ */

.pall-body {
  margin: 0;
  height: 100dvh;
  overflow: hidden;
  background:
    radial-gradient(120% 80% at 50% -10%, #f7f8fb 0%, #ffffff 46%) fixed,
    var(--paper);
  color: var(--ink);
  font-family: var(--font-sans);
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

/* ----------------------------- Stages (gate / picker) ----------------------
   Pre-chat flow, mirroring the app's sign-in → profile pick. The top bar + chat
   stay hidden until a profile is chosen (driven by body[data-stage]). */
.pall-stage {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(120% 80% at 50% -10%, #f7f8fb 0%, #ffffff 46%), var(--paper);
  overflow-y: auto;
}
.pall-body[data-stage="gate"] #gate { display: flex; }
.pall-body[data-stage="picker"] #picker { display: flex; }
.pall-body[data-stage="app"] .pall-stage { display: none; }
.pall-body:not([data-stage="app"]) .pall-top,
.pall-body:not([data-stage="app"]) .pall-main { display: none; }

.pall-gate {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 12px;
  max-width: 380px;
  width: 100%;
}
.pall-gate__mark { width: auto; opacity: 0.96; margin-bottom: 4px; }
.pall-gate__title { font-family: var(--font-serif); font-size: 2rem; margin: 0; letter-spacing: -0.02em; }
.pall-gate__sub { color: var(--muted); margin: 0 0 8px; line-height: 1.5; }
.pall-gate__form { display: flex; flex-direction: column; gap: 10px; width: 100%; }
.pall-gate__input {
  width: 100%;
  text-align: center;
  font-size: 1.1rem;
  letter-spacing: 0.12em;
  padding: 14px 16px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: #fff;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.pall-gate__input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(0, 113, 227, 0.15); }
.pall-gate__form .btn { width: 100%; }
.pall-gate__err { color: #b3261e; font-size: var(--t-small); margin: 0; min-height: 1em; }
.pall-gate__back { color: var(--muted); font-size: var(--t-small); margin-top: 12px; cursor: pointer; background: none; border: 0; }
.pall-gate__back:hover { color: var(--ink); }

.pall-pick { width: 100%; max-width: 560px; text-align: center; }
.pall-pick__title { font-family: var(--font-serif); font-size: 1.9rem; margin: 0 0 4px; letter-spacing: -0.02em; }
.pall-pick__sub { color: var(--muted); margin: 0 0 24px; }
.pall-pick__grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 14px;
}
.pall-user {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 18px 12px;
  border-radius: 18px;
  border: 0.5px solid var(--line);
  background: rgba(255, 255, 255, 0.7);
  cursor: pointer;
  transition: transform 0.12s, box-shadow 0.15s, border-color 0.15s;
}
.pall-user:hover { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08); border-color: var(--accent); }
.pall-user:active { transform: scale(0.97); }
.pall-user__face {
  position: relative;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  overflow: hidden;
  display: grid;
  place-items: center;
  font-size: 1.3rem;
  font-weight: 700;
  color: #fff;
  background: var(--accent);
}
/* Layered character avatar — matches the iOS AvatarView: full-frame monochrome
   PNGs stacked back→front at the same size, clipped to a circle. */
.pall-user__face.has-avatar { background: #f3f4f6; }
.pall-avatar { position: absolute; inset: 0; }
.pall-avatar img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  image-rendering: auto;
}
.pall-user__name { font-size: var(--t-small); font-weight: 600; color: var(--ink); }
.pall-user__role { font-size: var(--t-xs); color: var(--faint); }

/* ----------------------------- Top bar -------------------------------------
   Brand floats left, the Pano/Sohbet switch sits dead-centre, controls right. */
.pall-top {
  position: relative;
  z-index: 20;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: var(--s-3);
  padding: 10px clamp(12px, 3vw, 22px);
}
.pall-brand {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  text-decoration: none;
  color: var(--ink);
  justify-self: start;
}
.pall-brand img { height: 24px; width: auto; border-radius: 6px; display: block; object-fit: contain; }
.pall-brand__wm {
  font-weight: 700;
  letter-spacing: 0.14em;
  font-size: 0.95rem;
}
.pall-top__right {
  justify-self: end;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.pall-iconbtn {
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 0.5px solid rgba(0, 0, 0, 0.08);
  background: rgba(255, 255, 255, 0.6);
  -webkit-backdrop-filter: blur(18px) saturate(180%);
  backdrop-filter: blur(18px) saturate(180%);
  color: var(--ink);
  cursor: pointer;
  transition: background 0.18s, transform 0.12s;
}
.pall-iconbtn:hover { background: rgba(255, 255, 255, 0.9); }
.pall-iconbtn:active { transform: scale(0.94); }
.pall-iconbtn svg { width: 19px; height: 19px; }

/* The segmented Pano/Sohbet switch — glass pill with a sliding thumb. */
.pall-switch {
  position: relative;
  justify-self: center;
  display: inline-flex;
  padding: 4px;
  border-radius: var(--r-pill);
  background: rgba(120, 120, 128, 0.12);
  -webkit-backdrop-filter: blur(16px);
  backdrop-filter: blur(16px);
  border: 0.5px solid rgba(0, 0, 0, 0.05);
}
.pall-switch button {
  position: relative;
  z-index: 2;
  appearance: none;
  border: 0;
  background: transparent;
  cursor: pointer;
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--muted);
  padding: 6px 18px;
  border-radius: var(--r-pill);
  transition: color 0.22s;
}
.pall-switch button.on { color: var(--ink); }
.pall-switch__thumb {
  position: absolute;
  z-index: 1;
  top: 4px;
  bottom: 4px;
  left: 4px;
  width: calc(50% - 4px);
  border-radius: var(--r-pill);
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 0 0 0.5px rgba(0, 0, 0, 0.04);
  transition: transform 0.32s cubic-bezier(0.32, 0.72, 0, 1);
}
.pall-switch[data-active="pano"] .pall-switch__thumb { transform: translateX(100%); }

/* ----------------------------- Layout --------------------------------------- */
.pall-main { position: relative; flex: 1; min-height: 0; }
.pall-screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.pall-screen[hidden] { display: none; }

.pall-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  scroll-behavior: smooth;
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 26px, #000 calc(100% - 16px), transparent 100%);
  mask-image: linear-gradient(to bottom, transparent 0, #000 26px, #000 calc(100% - 16px), transparent 100%);
}

/* ----------------------------- Empty state ---------------------------------- */
.pall-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: clamp(40px, 9vh, 96px) 20px 24px;
  gap: 16px;
}
.pall-empty.gone { display: none; }
.pall-empty__mark { height: 64px; width: auto; border-radius: 16px; opacity: 0.96; object-fit: contain; }
.pall-empty__tag {
  margin: 0;
  color: var(--faint);
  font-size: 0.95rem;
}
.pall-chips {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  max-width: 540px;
  margin-top: 6px;
}
.pall-chip {
  appearance: none;
  font: inherit;
  font-size: 0.9rem;
  color: var(--ink);
  cursor: pointer;
  padding: 9px 14px;
  border-radius: var(--r-pill);
  border: 0.5px solid rgba(0, 0, 0, 0.1);
  background: rgba(255, 255, 255, 0.7);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  transition: background 0.16s, transform 0.12s;
}
.pall-chip:hover { background: #fff; }
.pall-chip:active { transform: scale(0.97); }

/* ----------------------------- Thread --------------------------------------- */
.pall-thread {
  display: flex;
  flex-direction: column;
  gap: 16px;
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  padding: 8px clamp(16px, 4vw, 24px) 4px;
}
.pall-bottom-spacer { height: 8px; }

.pall-turn { display: flex; flex-direction: column; }
.pall-turn.user { align-items: flex-end; }
.pall-turn.assistant { align-items: stretch; }

/* User = right-side glass bubble. */
.pall-bubble {
  max-width: min(82%, 560px);
  padding: 11px 16px;
  border-radius: 22px;
  background: rgba(255, 255, 255, 0.72);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
  border: 0.5px solid rgba(0, 0, 0, 0.07);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.45;
}
.pall-bubble img.pall-att {
  display: block;
  max-width: 220px;
  border-radius: 12px;
  margin-bottom: 8px;
}

/* Assistant = left-aligned rich markdown, full width. */
.pall-answer {
  line-height: 1.55;
  font-size: var(--t-body);
  color: var(--ink);
}
.pall-answer > :first-child { margin-top: 0; }
.pall-answer > :last-child { margin-bottom: 0; }
.pall-answer h2 { font-size: 1.18rem; font-weight: 700; margin: 1.1em 0 0.4em; letter-spacing: -0.01em; }
.pall-answer h3 { font-size: 1.02rem; font-weight: 700; margin: 0.95em 0 0.35em; color: var(--ink-2); }
.pall-answer p { margin: 0.5em 0; }
.pall-answer strong { font-weight: 700; }
.pall-answer ul, .pall-answer ol { margin: 0.45em 0; padding-left: 1.35em; }
.pall-answer li { margin: 0.28em 0; }
/* The → "pinpoint" line — Pall's most-important callout, set apart. */
.pall-answer .pin {
  display: flex;
  gap: 8px;
  align-items: baseline;
  margin: 0.45em 0;
  padding: 8px 12px;
  border-radius: 12px;
  background: rgba(0, 113, 227, 0.06);
}
.pall-answer .pin::before {
  content: "→";
  color: var(--accent);
  font-weight: 700;
  flex: none;
}
.pall-ico {
  height: 1.05em;
  width: 1.05em;
  vertical-align: -0.16em;
  margin: 0 0.08em;
  display: inline-block;
}

/* Smart underline — Pall underlines the single most important phrase. */
.pall-answer .pall-u { text-decoration: underline; text-decoration-color: var(--accent); text-underline-offset: 3px; text-decoration-thickness: 2px; }

/* Sub-agent badges — each tool call shows as a named agent (branch tree). */
.pall-agents { display: flex; flex-wrap: wrap; gap: 6px; margin: 2px 0; }
.pall-agents:empty { display: none; }
.pall-agent {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: var(--t-xs); color: var(--muted);
  padding: 4px 10px; border-radius: var(--r-pill);
  border: 0.5px solid var(--line); background: rgba(255, 255, 255, 0.7);
}
.pall-agent b { color: var(--ink); font-weight: 600; }
.pall-agent__d { color: var(--faint); }
.pall-agent__dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); animation: pulse 1.6s ease-in-out infinite; }

/* Clarifying-question option chips. */
.pall-choices { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.pall-choice {
  appearance: none; font: inherit; font-size: 0.9rem; cursor: pointer;
  padding: 9px 15px; border-radius: var(--r-pill);
  border: 1px solid var(--accent); color: var(--accent); background: transparent;
  transition: background 0.15s, color 0.15s, transform 0.12s;
}
.pall-choice:hover { background: var(--accent); color: #fff; }
.pall-choice:active { transform: scale(0.96); }

/* Word-by-word reveal for the streaming answer. */
.pall-w { opacity: 0; animation: pallWordIn 0.32s ease forwards; }
@keyframes pallWordIn { from { opacity: 0; transform: translateY(3px); } to { opacity: 1; transform: none; } }

/* Per-message action row (copy) fading in under each finished answer. */
.pall-actions { display: flex; gap: 6px; margin-top: 6px; }
.pall-act {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--faint);
  cursor: pointer;
  padding: 5px;
  border-radius: 8px;
  display: inline-grid;
  place-items: center;
  transition: color 0.15s, background 0.15s;
}
.pall-act:hover { color: var(--ink); background: rgba(0, 0, 0, 0.04); }
.pall-act svg { width: 16px; height: 16px; }

/* Thinking row: the REAL Pall scribble Lottie + a shimmering one-line caption.
   The same five hand-drawn scribbles the app rotates through (PALScribbleView). */
.pall-thinking { display: flex; align-items: center; gap: 10px; padding: 2px 0; }
.pall-scribble {
  width: 30px;
  height: 30px;
  flex: none;
  display: inline-flex;
}
.pall-scribble svg { width: 100% !important; height: 100% !important; display: block; }
/* Fallback when Lottie is unavailable or reduced-motion is on: the old conic dot. */
.pall-scribble--css {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: conic-gradient(from 0deg, var(--line), var(--ink), var(--line));
  -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 3px), #000 calc(100% - 3px));
  mask: radial-gradient(farthest-side, transparent calc(100% - 3px), #000 calc(100% - 3px));
  animation: pallSpin 0.9s linear infinite;
}
@media (prefers-reduced-motion: reduce) { .pall-scribble--css { animation: none; } }
@keyframes pallSpin { to { transform: rotate(360deg); } }
.pall-think-cap {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 70%;
  background: linear-gradient(100deg, var(--muted) 30%, var(--ink) 50%, var(--muted) 70%);
  background-size: 220% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: pallShimmer 1.5s linear infinite;
  direction: rtl; /* keep the freshest words visible (head-truncation like the app) */
  text-align: left;
}
@keyframes pallShimmer { to { background-position: -220% 0; } }

/* ----------------------------- Composer ------------------------------------- */
.pall-composer {
  flex: none;
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  padding: 8px clamp(16px, 4vw, 24px) max(14px, env(safe-area-inset-bottom));
}
.pall-attachrow { display: flex; gap: 8px; flex-wrap: wrap; padding: 0 4px 8px; }
.pall-attachrow[hidden] { display: none; }
.pall-attchip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 5px 6px 5px 5px;
  border-radius: var(--r-pill);
  background: rgba(255, 255, 255, 0.8);
  border: 0.5px solid rgba(0, 0, 0, 0.08);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  font-size: 0.82rem;
  max-width: 200px;
}
.pall-attchip img { width: 26px; height: 26px; border-radius: 6px; object-fit: cover; }
.pall-attchip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pall-attchip button { appearance: none; border: 0; background: transparent; cursor: pointer; color: var(--faint); padding: 2px; line-height: 0; }
.pall-attchip button:hover { color: var(--ink); }

.pall-capsule {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 6px 6px 6px 8px;
  border-radius: 26px;
  background: rgba(255, 255, 255, 0.78);
  -webkit-backdrop-filter: blur(24px) saturate(180%);
  backdrop-filter: blur(24px) saturate(180%);
  border: 0.5px solid rgba(0, 0, 0, 0.09);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.07), 0 1px 2px rgba(0, 0, 0, 0.04);
}
.pall-plus, .pall-send {
  flex: none;
  appearance: none;
  border: 0;
  cursor: pointer;
  display: inline-grid;
  place-items: center;
  border-radius: 50%;
  transition: background 0.16s, transform 0.12s, opacity 0.16s;
}
.pall-plus {
  width: 36px;
  height: 36px;
  background: transparent;
  color: var(--ink);
}
.pall-plus:hover { background: rgba(0, 0, 0, 0.06); }
.pall-plus svg { width: 20px; height: 20px; }
.pall-plus:active { transform: scale(0.92); }

#input {
  flex: 1;
  min-width: 0;
  appearance: none;
  border: 0;
  outline: 0;
  background: transparent;
  resize: none;
  font: inherit;
  font-size: var(--t-body);
  color: var(--ink);
  line-height: 1.4;
  padding: 8px 2px;
  max-height: 140px;
}
#input::placeholder { color: var(--faint); }

.pall-send {
  width: 36px;
  height: 36px;
  background: var(--accent);
  color: #fff;
}
.pall-send:hover { background: var(--accent-hover); }
.pall-send:active { transform: scale(0.92); }
.pall-send:disabled { background: rgba(120, 120, 128, 0.2); color: rgba(255, 255, 255, 0.85); cursor: default; transform: none; }
.pall-send svg { width: 18px; height: 18px; }

.pall-disclaimer {
  margin: 8px 6px 0;
  text-align: center;
  font-size: var(--t-xs);
  color: var(--faint);
}

/* ----------------------------- Pano (live board) ---------------------------- */
.pall-pano-scroll { flex: 1; min-height: 0; overflow-y: auto; }
.pall-board {
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
  padding: 16px clamp(16px, 4vw, 24px) 32px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 12px;
  align-content: start;
}
.pall-board__msg {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--faint);
  padding: 48px 16px;
}
.pall-card {
  border: 0.5px solid rgba(0, 0, 0, 0.08);
  background: rgba(255, 255, 255, 0.7);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
  border-radius: 18px;
  padding: 16px 18px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}
.pall-card.span2 { grid-column: span 2; }
.pall-card__label { font-size: var(--t-xs); font-weight: 600; letter-spacing: 0.02em; color: var(--faint); text-transform: uppercase; margin: 0 0 6px; }
.pall-card__v { font-size: 2rem; font-weight: 700; letter-spacing: -0.02em; line-height: 1.05; color: var(--ink); }
.pall-card__v .u { font-size: 1rem; font-weight: 600; color: var(--muted); margin-left: 2px; }
.pall-card__sub { font-size: var(--t-small); color: var(--muted); margin-top: 4px; }
.pall-card__v.accent { color: var(--accent); }
.pall-card__bar { height: 6px; border-radius: 3px; background: var(--line-2); margin-top: 10px; overflow: hidden; }
.pall-card__bar i { display: block; height: 100%; border-radius: 3px; background: var(--accent); }
.pall-chip-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.pall-zone { font-size: var(--t-xs); font-weight: 600; padding: 4px 9px; border-radius: var(--r-pill); border: 0.5px solid var(--line); }
.pall-zone.sev-critical { background: #fdecec; color: #b3261e; border-color: #f5c6c4; }
.pall-zone.sev-high { background: #fff2e8; color: #b35a00; border-color: #ffd7b8; }
.pall-zone.sev-medium { background: #fff8e1; color: #8a6d00; border-color: #f0e0a0; }
.pall-zone.sev-low, .pall-zone.sev-ok { background: #eaf7ee; color: #1b7f37; border-color: #c4e8cf; }
.pall-card ul { margin: 6px 0 0; padding-left: 1.1em; }
.pall-card li { font-size: var(--t-small); color: var(--ink-2); margin: 3px 0; }

/* ----------------------------- Toast ---------------------------------------- */
.pall-toast {
  position: fixed;
  left: 50%;
  bottom: 110px;
  transform: translateX(-50%) translateY(10px);
  z-index: 40;
  padding: 10px 18px;
  border-radius: var(--r-pill);
  background: rgba(255, 255, 255, 0.85);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
  border: 0.5px solid rgba(0, 0, 0, 0.08);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
  font-size: 0.9rem;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
}
.pall-toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.pall-toast[hidden] { display: block; } /* keep transition; visibility via .show */

@media (prefers-reduced-motion: reduce) {
  .pall-w { animation: none; opacity: 1; }
  .pall-scribble { animation-duration: 1.6s; }
  .pall-think-cap { animation: none; -webkit-text-fill-color: var(--muted); }
  .pall-scroll { scroll-behavior: auto; }
}

@media (max-width: 520px) {
  .pall-brand__wm { display: none; }
  .pall-switch button { padding: 6px 14px; }
}
