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

:root {
  --fs: 1;
  --primary: #0b57d0;
  --on-primary: #ffffff;
  --pc: #d3e3fd;
  --on-pc: #041e49;
  --bg: #ffffff;
  --on-bg: #1b1c1d;
  --on-var: #454746;
  --surface-2: #f0f4f9;
  --surface-3: #e9eef6;
  --divider: #e6e9ee;
  --bubble-in: #f0f4f9;
  --bubble-in-text: #1b1c1d;
  --bubble-out: #0b57d0;
  --bubble-out-text: #ffffff;
  --scrim: rgba(0, 0, 0, .42);
  --ease: cubic-bezier(.2, 0, 0, 1);
}

[data-theme="dark"] {
  --primary: #a8c7fa;
  --on-primary: #062e6f;
  --pc: #0842a0;
  --on-pc: #d3e3fd;
  --bg: #131316;
  --on-bg: #e3e3e6;
  --on-var: #c4c7cc;
  --surface-2: #26282c;
  --surface-3: #303236;
  --divider: #26282c;
  --bubble-in: #2a2c30;
  --bubble-in-text: #e3e3e6;
  --bubble-out: #0b57d0;
  --bubble-out-text: #ffffff;
  --scrim: rgba(0, 0, 0, .6);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --primary: #a8c7fa;
    --on-primary: #062e6f;
    --pc: #0842a0;
    --on-pc: #d3e3fd;
    --bg: #131316;
    --on-bg: #e3e3e6;
    --on-var: #c4c7cc;
    --surface-2: #26282c;
    --surface-3: #303236;
    --divider: #26282c;
    --bubble-in: #2a2c30;
    --bubble-in-text: #e3e3e6;
    --bubble-out: #0b57d0;
    --bubble-out-text: #ffffff;
    --scrim: rgba(0, 0, 0, .6);
  }
}

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

body {
  font-family: "Google Sans Text", "Google Sans", Roboto, "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--on-bg);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  cursor: pointer;
  padding: 0;
}

svg { width: 24px; height: 24px; fill: currentColor; flex-shrink: 0; }

#app {
  position: relative;
  height: 100dvh;
  overflow: hidden;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  color: var(--on-var);
  transition: background .15s;
}

.icon-btn:hover { background: var(--surface-3); }
.icon-btn.small { width: 38px; height: 38px; }
.icon-btn.small svg { width: 22px; height: 22px; }

.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  color: #fff;
  font-size: 20px;
  font-weight: 500;
  flex-shrink: 0;
  user-select: none;
}

.avatar svg { width: 26px; height: 26px; }

.home-screen {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  position: relative;
  background: var(--bg);
}

.home-header {
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
}

.search-bar {
  display: flex;
  align-items: center;
  gap: 4px;
  height: 48px;
  background: var(--surface-2);
  border-radius: 28px;
  padding: 0 6px 0 4px;
}

.search-bar input {
  flex: 1;
  min-width: 0;
  border: 0;
  background: none;
  outline: none;
  font: inherit;
  font-size: calc(16px * var(--fs));
  color: var(--on-bg);
}

.search-bar input::placeholder { color: var(--on-var); }

.profile-avatar {
  width: 34px;
  height: 34px;
  font-size: 15px;
  background: #7c4dff;
  border: 0;
  cursor: pointer;
}

.conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px 96px;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 16px;
  width: 100%;
  padding: 12px;
  border-radius: 16px;
  text-align: left;
  user-select: none;
  transition: background .15s;
}

.conv-item:hover { background: var(--surface-2); }
.conv-item.active { background: var(--surface-3); }

.conv-body { flex: 1; min-width: 0; }

.conv-top {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.conv-name {
  flex: 1;
  min-width: 0;
  font-size: calc(16px * var(--fs));
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-time {
  font-size: calc(12px * var(--fs));
  color: var(--on-var);
  flex-shrink: 0;
}

.conv-bottom {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 2px;
}

.conv-snippet {
  flex: 1;
  min-width: 0;
  font-size: calc(14px * var(--fs));
  color: var(--on-var);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-snippet.typing-label {
  color: var(--primary);
  font-style: italic;
}

.conv-item.unread .conv-name,
.conv-item.unread .conv-snippet {
  font-weight: 700;
  color: var(--on-bg);
}

.conv-badge {
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--primary);
  color: var(--on-primary);
  font-size: 12px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.conv-pin {
  color: var(--on-var);
  flex-shrink: 0;
}

.conv-pin svg { width: 16px; height: 16px; }

.list-empty {
  text-align: center;
  color: var(--on-var);
  padding: 64px 24px;
  font-size: 14px;
}

.fab {
  position: absolute;
  right: 16px;
  bottom: calc(16px + env(safe-area-inset-bottom));
  height: 56px;
  padding: 0 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  border-radius: 16px;
  background: var(--pc);
  color: var(--on-pc);
  font-size: 15px;
  font-weight: 500;
  box-shadow: 0 2px 6px rgba(0, 0, 0, .25);
  transition: box-shadow .2s;
}

.fab:hover { box-shadow: 0 4px 10px rgba(0, 0, 0, .3); }

.fab .label {
  max-width: 220px;
  overflow: hidden;
  white-space: nowrap;
  transition: max-width .3s var(--ease), opacity .2s;
}

.fab.collapsed { padding: 0 16px; }
.fab.collapsed .label { max-width: 0; opacity: 0; }

.pane {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 20;
}

.chat-screen,
.new-chat-screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  transform: translateX(100%);
  transition: transform .3s var(--ease), visibility .3s;
  pointer-events: auto;
  visibility: hidden;
}

#app.show-chat .chat-screen { transform: none; visibility: visible; }
#app.show-new .new-chat-screen { transform: none; visibility: visible; }

.chat-header {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: calc(8px + env(safe-area-inset-top)) 8px 8px;
}

.chat-peer {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 12px;
}

.chat-avatar {
  width: 36px;
  height: 36px;
  font-size: 16px;
}

.chat-avatar svg { width: 20px; height: 20px; }

.chat-titles {
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.chat-name {
  font-size: calc(16px * var(--fs));
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-sub {
  font-size: calc(12px * var(--fs));
  color: var(--on-var);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-actions { display: flex; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 8px 16px;
  display: flex;
  flex-direction: column;
}

.day-sep {
  text-align: center;
  font-size: calc(12px * var(--fs));
  color: var(--on-var);
  margin: 16px 0 8px;
}

.msg {
  display: flex;
  flex-direction: column;
  margin-top: 8px;
  max-width: min(78%, 540px);
}

.msg.gp { margin-top: 2px; }
.msg.in { align-self: flex-start; align-items: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }

.bubble {
  padding: 9px 14px;
  font-size: calc(15px * var(--fs));
  line-height: 1.4;
  border-radius: 18px;
  white-space: pre-wrap;
  word-break: break-word;
  animation: pop .18s var(--ease);
}

@keyframes pop {
  from { transform: translateY(6px) scale(.97); opacity: .4; }
  to { transform: none; opacity: 1; }
}

.msg.in .bubble { background: var(--bubble-in); color: var(--bubble-in-text); }
.msg.out .bubble { background: var(--bubble-out); color: var(--bubble-out-text); }

.msg.in.gp .bubble { border-top-left-radius: 4px; }
.msg.in.gn .bubble { border-bottom-left-radius: 4px; }
.msg.out.gp .bubble { border-top-right-radius: 4px; }
.msg.out.gn .bubble { border-bottom-right-radius: 4px; }

.msg-time {
  display: none;
  font-size: calc(11px * var(--fs));
  color: var(--on-var);
  margin: 3px 6px 0;
}

.msg.show-time .msg-time { display: block; }

.msg-status {
  align-self: flex-end;
  font-size: calc(11px * var(--fs));
  color: var(--on-var);
  margin: 3px 6px 0;
}

.bubble.typing {
  display: flex;
  gap: 5px;
  align-items: center;
  padding: 14px 16px;
}

.bubble.typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--on-var);
  animation: blink 1.2s infinite both;
}

.bubble.typing span:nth-child(2) { animation-delay: .2s; }
.bubble.typing span:nth-child(3) { animation-delay: .4s; }

@keyframes blink {
  0%, 60%, 100% { opacity: .3; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 8px 10px calc(10px + env(safe-area-inset-bottom));
}

.composer-field {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: flex-end;
  background: var(--surface-2);
  border-radius: 24px;
  padding: 3px 4px;
}

.composer-field textarea {
  flex: 1;
  min-width: 0;
  border: 0;
  background: none;
  outline: none;
  resize: none;
  font: inherit;
  font-size: calc(15.5px * var(--fs));
  color: var(--on-bg);
  padding: 9px 2px;
  max-height: 120px;
}

.composer-field textarea::placeholder { color: var(--on-var); }

.send-btn {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--on-var);
  flex-shrink: 0;
  transition: background .15s, color .15s;
}

.send-btn .ic-send { display: none; }

.composer.has-text .send-btn {
  background: var(--primary);
  color: var(--on-primary);
}

.composer.has-text .ic-send { display: block; }
.composer.has-text .ic-mic { display: none; }

.new-chat-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 8px 8px;
}

.new-chat-header h1 {
  font-size: 18px;
  font-weight: 500;
  margin: 0;
}

.to-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 20px 16px;
  border-bottom: 1px solid var(--divider);
  font-size: calc(15px * var(--fs));
  color: var(--on-var);
}

.to-row input {
  flex: 1;
  min-width: 0;
  border: 0;
  background: none;
  outline: none;
  font: inherit;
  font-size: calc(16px * var(--fs));
  color: var(--on-bg);
}

.contact-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.contact-item {
  display: flex;
  align-items: center;
  gap: 16px;
  width: 100%;
  padding: 10px 12px;
  border-radius: 14px;
  text-align: left;
  transition: background .15s;
}

.contact-item:hover { background: var(--surface-2); }

.contact-item .avatar { width: 42px; height: 42px; font-size: 17px; }

.contact-item .avatar.new-num {
  background: var(--surface-3);
  color: var(--primary);
}

.contact-info { min-width: 0; }

.contact-name {
  font-size: calc(15px * var(--fs));
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.contact-phone {
  font-size: calc(13px * var(--fs));
  color: var(--on-var);
}

.section-label {
  font-size: 13px;
  font-weight: 500;
  color: var(--on-var);
  padding: 12px 16px 6px;
}

.chat-empty {
  display: none;
  position: absolute;
  inset: 0;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--on-var);
  background: var(--bg);
}

.chat-empty svg {
  width: 72px;
  height: 72px;
  opacity: .4;
}

.scrim {
  position: fixed;
  inset: 0;
  background: var(--scrim);
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s;
  z-index: 50;
}

.scrim.open { opacity: 1; pointer-events: auto; }

.drawer {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  width: min(300px, 85vw);
  background: var(--bg);
  border-radius: 0 16px 16px 0;
  transform: translateX(-105%);
  transition: transform .3s var(--ease);
  z-index: 60;
  padding: calc(16px + env(safe-area-inset-top)) 12px 16px;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

.drawer.open { transform: none; }

.drawer-title {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 20px;
  font-weight: 500;
  padding: 8px 12px 20px;
  color: var(--primary);
}

.drawer-title span { color: var(--on-bg); }

.drawer-section {
  font-size: 13px;
  font-weight: 500;
  color: var(--on-var);
  padding: 8px 12px;
}

.drawer-item {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px;
  border-radius: 24px;
  font-size: 14.5px;
  cursor: pointer;
  transition: background .15s;
  color: var(--on-bg);
  text-align: left;
}

.drawer-item:hover { background: var(--surface-2); }

.drawer-item input[type="radio"] {
  accent-color: var(--primary);
  width: 18px;
  height: 18px;
  margin: 0;
}

.drawer-item svg { color: var(--on-var); }

.drawer-divider {
  height: 1px;
  background: var(--divider);
  margin: 8px 4px;
}

.drawer-footer {
  margin-top: auto;
  font-size: 12px;
  color: var(--on-var);
  padding: 16px 12px 4px;
}

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg);
  border-radius: 28px 28px 0 0;
  transform: translateY(105%);
  transition: transform .3s var(--ease);
  z-index: 60;
  padding: 8px 16px calc(20px + env(safe-area-inset-bottom));
  max-width: 480px;
  margin: 0 auto;
}

.sheet.open { transform: none; }

.sheet-handle {
  width: 32px;
  height: 4px;
  border-radius: 2px;
  background: var(--on-var);
  opacity: .4;
  margin: 6px auto 10px;
}

.sheet-item {
  display: flex;
  align-items: center;
  gap: 16px;
  width: 100%;
  padding: 14px 12px;
  border-radius: 14px;
  font-size: 15px;
  text-align: left;
  transition: background .15s;
}

.sheet-item:hover { background: var(--surface-2); }
.sheet-item svg { color: var(--on-var); }
.sheet-item.danger, .sheet-item.danger svg { color: #dc362e; }

[data-theme="dark"] .sheet-item.danger,
[data-theme="dark"] .sheet-item.danger svg { color: #f2b8b5; }

.dialog-wrap {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--scrim);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
  z-index: 70;
  padding: 24px;
}

.dialog-wrap.open { opacity: 1; pointer-events: auto; }

.dialog {
  background: var(--bg);
  border-radius: 28px;
  padding: 24px;
  max-width: 340px;
  width: 100%;
  transform: scale(.95);
  transition: transform .2s var(--ease);
}

.dialog-wrap.open .dialog { transform: none; }

.dialog h2 { font-size: 19px; font-weight: 500; margin: 0 0 10px; }
.dialog p { font-size: 14px; color: var(--on-var); margin: 0 0 20px; line-height: 1.45; }

.setting-label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
  color: var(--on-bg);
  margin: 10px 0 12px;
}

.setting-label span:last-child { color: var(--on-var); }

#fontRange {
  width: 100%;
  accent-color: var(--primary);
  margin: 0 0 6px;
}

.font-preview {
  background: var(--bubble-in);
  color: var(--bubble-in-text);
  border-radius: 18px;
  padding: 9px 14px;
  font-size: calc(15px * var(--fs));
  line-height: 1.4;
  width: fit-content;
  margin: 10px 0 6px;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.text-btn {
  padding: 10px 14px;
  border-radius: 20px;
  color: var(--primary);
  font-size: 14px;
  font-weight: 500;
  transition: background .15s;
}

.text-btn:hover { background: var(--surface-2); }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(90px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px);
  background: #3c4043;
  color: #fff;
  font-size: 14px;
  padding: 12px 20px;
  border-radius: 10px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s, transform .25s;
  z-index: 80;
  max-width: 90vw;
  text-align: center;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }

@media (min-width: 900px) {
  #app {
    display: grid;
    grid-template-columns: minmax(320px, 400px) 1fr;
  }

  .home-screen {
    grid-column: 1;
    border-right: 1px solid var(--divider);
    height: 100dvh;
  }

  .pane {
    position: relative;
    grid-column: 2;
    pointer-events: auto;
  }

  .chat-screen,
  .new-chat-screen {
    transition: none;
    transform: none;
  }

  .chat-screen { visibility: hidden; }
  #app.has-active .chat-screen { visibility: visible; }
  .new-chat-screen { visibility: hidden; z-index: 5; }
  #app.show-new .new-chat-screen { visibility: visible; }

  #app:not(.has-active):not(.show-new) .chat-empty { display: flex; }

  .back-btn { display: none; }

  .messages { padding: 16px 10%; }
}
