:root {
  color-scheme: light;
  --surface: #e5ecf1;
  --surface-soft: rgba(255, 255, 255, 0.68);
  --surface-solid: #f8fafb;
  --blue: #1976d2;
  --blue-strong: #0f67bc;
  --green: #31b86b;
  --danger: #bd2e2e;
  --ink: #17212b;
  --muted: #536879;
  --line: rgba(30, 63, 86, 0.11);
  --incoming: #ffffff;
  --outgoing: #1976d2;
  --shadow: 0 1px 2px rgba(20, 45, 65, 0.11);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  background: var(--surface);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

button, textarea, input { font: inherit; }
button { -webkit-user-select: none; user-select: none; }
button:disabled { cursor: default; }

.app-shell {
  width: 100%;
  height: 100dvh;
  max-width: 760px;
  margin: 0 auto;
  overflow: hidden;
  background: var(--surface);
}

.view {
  width: 100%;
  height: 100%;
  min-height: 0;
  background: var(--surface);
}

.home-view {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}

.chat-view {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
}

.topbar {
  min-height: calc(58px + var(--safe-top));
  padding: calc(7px + var(--safe-top)) 10px 7px;
  display: grid;
  align-items: center;
  gap: 4px;
  background: rgba(229, 236, 241, 0.96);
  backdrop-filter: saturate(145%) blur(18px);
  -webkit-backdrop-filter: saturate(145%) blur(18px);
  z-index: 5;
}

.home-topbar { grid-template-columns: minmax(0, 1fr) 44px; }
.chat-topbar { grid-template-columns: 44px minmax(0, 1fr) 44px; }
.topbar-spacer { width: 44px; height: 44px; }

.icon-button {
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: var(--blue);
  background: transparent;
  cursor: pointer;
}

.icon-button:active { background: rgba(25, 118, 210, 0.10); }
.icon-button svg, .send-button svg {
  width: 23px;
  height: 23px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-button:focus-visible,
.send-button:focus-visible,
.primary-action:focus-visible,
.notification-recovery:focus-visible,
.conversation-card:focus-visible,
.conversation-end:focus-visible {
  outline: 3px solid rgba(25, 118, 210, 0.32);
  outline-offset: 2px;
}

.identity {
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.home-identity {
  justify-content: flex-start;
  padding-left: 4px;
}

.avatar, .empty-avatar {
  position: relative;
  width: 40px;
  height: 40px;
  flex: 0 0 40px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #fff;
  background: var(--blue);
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.03em;
}

.presence {
  position: absolute;
  right: -1px;
  bottom: 1px;
  width: 11px;
  height: 11px;
  border: 2px solid var(--surface);
  border-radius: 50%;
  background: var(--green);
}

.identity-copy {
  min-width: 0;
  display: flex;
  flex-direction: column;
  line-height: 1.15;
}

.identity-copy strong {
  overflow: hidden;
  color: var(--ink);
  font-size: 16px;
  font-weight: 650;
  letter-spacing: -0.01em;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.identity-copy span {
  margin-top: 3px;
  color: var(--blue);
  font-size: 12px;
  white-space: nowrap;
}

.home-main {
  min-height: 0;
  padding: 10px 12px calc(18px + var(--safe-bottom));
  overflow-x: hidden;
  overflow-y: auto;
}

.notification-recovery {
  width: 100%;
  min-height: 46px;
  margin: 0 0 10px;
  padding: 10px 13px;
  border: 0;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.72);
  cursor: pointer;
}

.notification-recovery span { color: var(--muted); font-size: 14px; }
.notification-recovery strong { color: var(--blue); font-size: 14px; }

.conversation-list {
  display: grid;
  gap: 8px;
}

.conversation-item {
  position: relative;
  min-height: 72px;
  border-radius: 14px;
  overflow: hidden;
  background: var(--danger);
  touch-action: pan-y;
}

.conversation-end {
  position: absolute;
  inset: 0 0 0 auto;
  width: 82px;
  min-height: 72px;
  padding: 0;
  border: 0;
  color: #fff;
  background: var(--danger);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.conversation-card {
  position: relative;
  z-index: 1;
  width: 100%;
  min-height: 72px;
  padding: 14px 15px;
  border: 0;
  border-radius: 14px;
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr) 14px;
  align-items: center;
  gap: 11px;
  color: var(--ink);
  background: var(--surface-soft);
  box-shadow: var(--shadow);
  text-align: left;
  cursor: pointer;
  transform: translate3d(0, 0, 0);
  transition: transform 170ms cubic-bezier(.2, .75, .25, 1), background 120ms ease;
  will-change: transform;
}

.conversation-card:active { background: rgba(255, 255, 255, 0.82); }
.conversation-mark {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #fff;
  background: var(--blue);
  font-size: 14px;
  font-weight: 700;
}
.conversation-copy { min-width: 0; display: grid; gap: 5px; }
.conversation-copy strong {
  overflow: hidden;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.018em;
  line-height: 1.2;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conversation-copy small {
  overflow: hidden;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conversation-chevron { color: rgba(23, 33, 43, 0.34); font-size: 22px; line-height: 1; }
.conversation-title {
  overflow: hidden;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.018em;
  line-height: 1.2;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conversation-detail { color: var(--muted); font-size: 13px; line-height: 1.25; }
.conversation-time { align-self: start; padding-top: 1px; color: var(--muted); font-size: 12px; white-space: nowrap; }

.empty-state {
  min-height: 56vh;
  padding: 52px 24px;
  display: grid;
  align-content: center;
  justify-items: center;
  text-align: center;
}
.empty-avatar { width: 54px; height: 54px; margin-bottom: 18px; font-size: 22px; }
.empty-state h1 { margin: 0; font-size: 22px; font-weight: 650; letter-spacing: -0.025em; }
.empty-state p { max-width: 285px; margin: 9px 0 22px; color: var(--muted); font-size: 14px; line-height: 1.5; }
.list-status { margin: 28px 0; color: var(--muted); font-size: 13px; text-align: center; }

.primary-action {
  width: 100%;
  min-height: 46px;
  padding: 10px 15px;
  border: 0;
  border-radius: 12px;
  color: #fff;
  background: var(--blue);
  font-weight: 650;
  cursor: pointer;
}
.primary-action:active { background: var(--blue-strong); }
.compact-action { width: auto; min-width: 120px; }

.conversation {
  position: relative;
  min-height: 0;
  padding: 12px 10px 18px;
  overflow-x: hidden;
  overflow-y: auto;
  scroll-behavior: smooth;
  scrollbar-width: thin;
  background: var(--surface);
}

.day-pill {
  position: sticky;
  top: 2px;
  z-index: 2;
  width: max-content;
  margin: 0 auto 12px;
  padding: 5px 11px;
  border-radius: 999px;
  color: #fff;
  background: rgba(83, 112, 132, 0.62);
  font-size: 12px;
  font-weight: 600;
}

.chat-empty {
  min-height: 48vh;
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 14px;
}
.history-status { margin: 24px auto; color: var(--muted); font-size: 13px; text-align: center; }

.message-row {
  display: flex;
  margin: 4px 0;
  animation: message-in 160ms ease-out both;
}
.message-row.incoming { justify-content: flex-start; }
.message-row.outgoing { justify-content: flex-end; }
.message-row.segmented { margin: 2px 0; }
.message-row.segment-start { margin-top: 4px; }
.message-row.segment-end { margin-bottom: 4px; }

.bubble {
  position: relative;
  max-width: min(84%, 580px);
  min-width: 66px;
  padding: 8px 11px 18px;
  border-radius: 15px;
  box-shadow: var(--shadow);
  overflow-wrap: anywhere;
}
.incoming .bubble { color: var(--ink); background: var(--incoming); border-bottom-left-radius: 5px; }
.incoming.segment-start .bubble { padding-bottom: 8px; border-bottom-left-radius: 8px; }
.incoming.segment-middle .bubble { padding-bottom: 8px; border-top-left-radius: 8px; border-bottom-left-radius: 8px; }
.incoming.segment-end .bubble { border-top-left-radius: 8px; }
.outgoing .bubble { color: #fff; background: var(--outgoing); border-bottom-right-radius: 5px; }

.message-text { white-space: pre-wrap; font-size: 15.5px; line-height: 1.38; letter-spacing: -0.006em; }
.message-meta { position: absolute; right: 9px; bottom: 4px; color: var(--muted); font-size: 10px; line-height: 1; white-space: nowrap; }
.outgoing .message-meta { color: rgba(255, 255, 255, 0.76); }

.typing .bubble { padding: 12px 15px; min-width: 62px; }
.typing-dots { display: flex; align-items: center; gap: 4px; height: 12px; }
.typing-dots i { width: 7px; height: 7px; border-radius: 50%; background: #91a1ad; animation: typing 1.15s infinite ease-in-out; }
.typing-dots i:nth-child(2) { animation-delay: 120ms; }
.typing-dots i:nth-child(3) { animation-delay: 240ms; }

.composer {
  background: rgba(229, 236, 241, 0.96);
  backdrop-filter: saturate(145%) blur(18px);
  -webkit-backdrop-filter: saturate(145%) blur(18px);
  z-index: 5;
}
.composer-inner { min-height: 58px; display: flex; align-items: flex-end; gap: 8px; padding: 8px 9px; }
#message-input {
  width: 100%;
  min-height: 42px;
  max-height: 132px;
  resize: none;
  overflow-y: auto;
  padding: 10px 13px;
  color: var(--ink);
  caret-color: var(--blue);
  background: var(--incoming);
  border: 1px solid var(--line);
  border-radius: 21px;
  outline: none;
  line-height: 20px;
}
#message-input::placeholder { color: #8a9aa7; }
#message-input:focus { border-color: rgba(25, 118, 210, 0.50); }

.send-button {
  width: 42px;
  height: 42px;
  flex: 0 0 42px;
  border: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #fff;
  background: var(--blue);
  cursor: pointer;
  transition: transform 120ms ease, opacity 120ms ease, background 120ms ease;
}
.send-button svg { width: 22px; height: 22px; transform: translate(-1px, 1px); }
.send-button:active { transform: scale(0.92); background: var(--blue-strong); }
.send-button:disabled { opacity: 0.35; transform: none; }
.safe-space { height: var(--safe-bottom); }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(82px + var(--safe-bottom));
  z-index: 30;
  max-width: calc(100% - 32px);
  transform: translateX(-50%);
  padding: 9px 13px;
  border-radius: 10px;
  color: #fff;
  background: rgba(25, 42, 54, 0.88);
  box-shadow: 0 6px 22px rgba(0, 0, 0, 0.18);
  font-size: 13px;
  text-align: center;
}

.login-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 22px;
  background: rgba(10, 24, 34, 0.42);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.login-card { width: min(100%, 360px); padding: 28px; border-radius: 18px; color: var(--ink); background: var(--incoming); box-shadow: 0 18px 56px rgba(11,31,45,.22); }
.login-card .avatar { margin: 0 auto 14px; }
.login-card h1 { margin: 0; text-align: center; font-size: 24px; }
.login-card > p { margin: 8px 0 22px; color: var(--muted); text-align: center; line-height: 1.45; }
.login-card label { display: block; margin-bottom: 7px; font-size: 13px; font-weight: 650; }
.login-card input { width: 100%; min-height: 46px; margin-bottom: 14px; padding: 10px 12px; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 11px; outline: none; }
.login-card input:focus { border-color: var(--blue); }
.login-card .login-error { margin: -4px 0 12px; color: var(--danger); font-size: 13px; text-align: left; }

@keyframes typing { 0%, 60%, 100% { transform: translateY(0); opacity: .45; } 30% { transform: translateY(-4px); opacity: 1; } }
@keyframes message-in { from { opacity: 0; transform: translateY(5px) scale(.985); } to { opacity: 1; transform: none; } }

@media (min-width: 760px) {
  body { padding: 18px 0; }
  .app-shell { height: calc(100dvh - 36px); border-radius: 18px; box-shadow: 0 0 32px rgba(16, 38, 55, 0.15); }
}

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