/* ChatPro - mobile first chat UI (QQ-ish bubbles) */

:root {
  --brand: #12b7f5;
  --brand-2: #0a9fdc;
  --bg: #f2f3f5;
  --panel: #ffffff;
  --fg: #17181a;
  --sub: #8b909a;
  --line: rgba(0, 0, 0, 0.08);
  --bubble-other: #ffffff;
  --bubble-me: #12b7f5;
  --bubble-me-fg: #ffffff;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

/* Night palette. Which mode is active is decided in app.js (time of day, the
   OS setting, or an explicit choice) and expressed as data-theme on <html>,
   so the stylesheet only needs this one rule. */
:root[data-theme="dark"] {
  color-scheme: dark;
  --brand: #0f9fd8;
  --brand-2: #0b86b8;
  --bg: #101114;
  --panel: #17181c;
  --fg: #e9eaec;
  --sub: #8b909a;
  --line: rgba(255, 255, 255, 0.1);
  --bubble-other: #262830;
  --bubble-me: #0f9fd8;
  --bubble-me-fg: #ffffff;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
}

:root[data-theme="light"] {
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

/* An author rule like .field { display: flex } beats the UA sheet's
   [hidden] { display: none }, so state the intent here. */
[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
  /* The soft keyboard must not be able to scroll the page itself, otherwise
     the shell drifts out of line with the keyboard. Only .msgs ever scrolls. */
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 400 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui,
    "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

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

/* ---------------------------------------------------------------- shell -- */

.app {
  /* Pinned to the visual viewport: --vh / --vv-top are kept in sync with
     window.visualViewport, which is what actually shrinks when the keyboard
     opens (especially on iOS, where the layout viewport does not). */
  position: fixed;
  top: var(--vv-top, 0px);
  left: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  height: var(--vh, 100dvh);
  max-width: 760px;
  margin: 0 auto;
  overflow: hidden;
  background: var(--bg);
}

@media (min-width: 761px) {
  .app {
    box-shadow: 0 0 0 1px var(--line), 0 8px 40px rgba(0, 0, 0, 0.12);
  }
}

/* -------------------------------------------------------------- top bar -- */

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(8px + var(--safe-t)) calc(10px + var(--safe-r)) 8px calc(12px + var(--safe-l));
  background: linear-gradient(180deg, var(--brand), var(--brand-2));
  color: #fff;
}

.room {
  flex: 1 1 auto;
  min-width: 0;
  text-align: left;
  color: inherit;
}

.room-name,
.room-sub {
  display: block;
}

.room-name {
  font-size: 17px;
  font-weight: 600;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.room-sub {
  font-size: 12px;
  opacity: 0.88;
  line-height: 1.35;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  color: #fff;
  transition: background-color 0.15s ease;
}

.icon-btn:active {
  background: rgba(255, 255, 255, 0.22);
}

/* --------------------------------------------------------------- banner -- */

.banner {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: #fff5e6;
  color: #8a5a00;
  font-size: 13px;
  border-bottom: 1px solid var(--line);
}

.banner[hidden] {
  display: none;
}

.link-btn {
  margin-left: auto;
  flex: 0 0 auto;
  color: var(--brand-2);
  font-size: 13px;
  font-weight: 600;
  padding: 2px 4px;
}

:root[data-theme="dark"] .banner {
  background: #3a2e12;
  color: #ffd88a;
}

/* ------------------------------------------------------------- messages -- */

.msgs {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 12px calc(10px + var(--safe-r)) 14px calc(10px + var(--safe-l));
  display: flex;
  flex-direction: column;
  gap: 10px;
  scrollbar-width: thin;
}

.time {
  align-self: center;
  font-size: 12px;
  color: var(--sub);
  padding: 2px 8px;
}

.sys {
  align-self: center;
  max-width: 90%;
  font-size: 12px;
  color: var(--sub);
  background: rgba(127, 127, 127, 0.12);
  padding: 3px 10px;
  border-radius: 10px;
  text-align: center;
}

.row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  max-width: 100%;
}

.row.me {
  flex-direction: row-reverse;
}

.avatar {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  user-select: none;
  overflow: hidden;
}

.col {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
  max-width: calc(100% - 62px);
}

.row.me .col {
  align-items: flex-end;
}

.nick {
  font-size: 12px;
  color: var(--sub);
  padding: 0 2px;
  max-width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.bubble {
  position: relative;
  padding: 9px 12px;
  border-radius: 14px;
  background: var(--bubble-other);
  box-shadow: var(--shadow);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  user-select: text;
}

.row.other .bubble {
  border-top-left-radius: 4px;
}

.row.me .bubble {
  background: var(--bubble-me);
  color: var(--bubble-me-fg);
  border-top-right-radius: 4px;
}

/* Bundled artwork inside a message bubble. */
.bubble .emoji {
  width: 22px;
  height: 22px;
  margin: 0 1px;
  vertical-align: -4px;
  user-select: none;
}

.pill {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(76px + var(--safe-b));
  z-index: 5;
  padding: 6px 14px;
  border-radius: 999px;
  background: var(--brand);
  color: #fff;
  font-size: 13px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.22);
}

.pill[hidden] {
  display: none;
}

/* ------------------------------------------------------------- composer -- */

.composer {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px calc(10px + var(--safe-r)) calc(8px + var(--safe-b)) calc(10px + var(--safe-l));
  background: var(--panel);
  border-top: 1px solid var(--line);
}

/* With the keyboard up the home-indicator inset is redundant - the keyboard
   already covers that strip, and keeping the padding leaves a visible gap
   between the input box and the keyboard. */
.app.kb-open .composer {
  padding-bottom: 8px;
}

.composer textarea {
  flex: 1 1 auto;
  min-height: 38px;
  max-height: 132px;
  padding: 9px 14px;
  border: 0;
  border-radius: 19px;
  background: var(--bg);
  outline: none;
  resize: none;
  line-height: 1.35;
  /* 16px keeps iOS from zooming the page when the field is focused */
  font-size: 16px;
}

.composer textarea:disabled {
  opacity: 0.6;
}

.send {
  flex: 0 0 auto;
  height: 38px;
  padding: 0 18px;
  border-radius: 19px;
  background: var(--brand);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  transition: opacity 0.15s ease;
}

.send:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.emoji-btn {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  font-size: 23px;
  line-height: 1;
}

.emoji-btn[aria-expanded="true"] {
  background: var(--bg);
}

.emoji-btn:disabled,
.voice-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.voice-btn {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: var(--sub);
  /* A long press must not select text or pop the iOS callout. */
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.voice-btn:active,
.voice-btn.recording {
  background: var(--bg);
  color: var(--brand);
}

.voice-btn .icon-keyboard {
  display: none;
}

.voice-btn.voice-mode .icon-mic {
  display: none;
}

.voice-btn.voice-mode .icon-keyboard {
  display: block;
}

/* The bar that replaces the text field in voice mode: hold it to record. */
.hold-btn {
  flex: 1 1 auto;
  height: 38px;
  border-radius: 19px;
  background: var(--bg);
  color: var(--fg);
  font-size: 15px;
  font-weight: 500;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.hold-btn:active,
.hold-btn.recording {
  background: var(--brand);
  color: #fff;
}

.hold-btn:disabled {
  opacity: 0.45;
}

/* ------------------------------------------------------------ voice UI -- */

.voice-overlay {
  position: absolute;
  inset: 0;
  z-index: 25;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.35);
  /* The overlay must never swallow the touch that is recording: pointer
     capture stays on the bar, so no pointercancel, so no phantom cancel. */
  pointer-events: none;
  touch-action: none;
}

.voice-card {
  min-width: 172px;
  padding: 20px 22px;
  border-radius: 16px;
  background: rgba(0, 0, 0, 0.78);
  color: #fff;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.voice-badge {
  font-size: 30px;
  line-height: 1;
}

.voice-timer {
  font-size: 26px;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}

.voice-hint {
  font-size: 13px;
  opacity: 0.85;
}

.voice-overlay.cancel .voice-card {
  background: rgba(178, 34, 34, 0.9);
}

/* A voice note inside a bubble: play button, bars, duration. */
.bubble.voice {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
  cursor: pointer;
}

.voice-icon {
  flex: 0 0 auto;
  font-size: 15px;
  line-height: 1;
  width: 14px;
  text-align: center;
}

.voice-meter {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  gap: 2px;
  height: 16px;
}

.voice-meter i {
  display: block;
  width: 2px;
  border-radius: 1px;
  background: currentColor;
  opacity: 0.55;
}

.voice-sec {
  flex: 0 0 auto;
  font-size: 12px;
  opacity: 0.75;
}

/* Sits above the composer as a flex row, so the transcript shrinks instead of
   being covered - the same way QQ/WeChat handle their emoji tray. */
.emoji-panel {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(42px, 1fr));
  gap: 2px;
  max-height: 190px;
  overflow-y: auto;
  padding: 8px calc(8px + var(--safe-r)) 8px calc(8px + var(--safe-l));
  background: var(--panel);
  border-top: 1px solid var(--line);
  -webkit-overflow-scrolling: touch;
}

.emoji-panel[hidden] {
  display: none;
}

.emoji-panel button {
  height: 42px;
  border-radius: 10px;
  font-size: 24px;
  line-height: 1;
  display: grid;
  place-items: center;
}

.emoji-panel button img {
  width: 26px;
  height: 26px;
  pointer-events: none;
}

.emoji-panel button:active {
  background: rgba(127, 127, 127, 0.15);
}

/* ----------------------------------------------------------------- menu -- */

.menu {
  position: absolute;
  z-index: 15;
  top: calc(48px + var(--safe-t));
  right: calc(8px + var(--safe-r));
  min-width: 168px;
  padding: 6px;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: 0 10px 32px rgba(0, 0, 0, 0.22);
}

.menu[hidden] {
  display: none;
}

.menu-title {
  font-size: 12px;
  color: var(--sub);
  padding: 6px 10px 2px;
}

.menu-sep {
  height: 1px;
  background: var(--line);
  margin: 6px 4px;
}

.menu-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 9px 10px;
  border-radius: 8px;
  font-size: 15px;
}

.menu-item:active {
  background: rgba(127, 127, 127, 0.15);
}

.lang-list,
.theme-list {
  display: flex;
  flex-direction: column;
}

.lang-btn,
.theme-btn {
  text-align: left;
  padding: 9px 10px;
  border-radius: 8px;
  font-size: 15px;
}

.lang-btn:active,
.theme-btn:active {
  background: rgba(127, 127, 127, 0.15);
}

.lang-btn[aria-pressed="true"],
.theme-btn[aria-pressed="true"] {
  color: var(--brand);
  font-weight: 600;
}

/* ---------------------------------------------------------------- sheet -- */

.sheet {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(2px);
}

.sheet[hidden] {
  display: none;
}

.sheet-card {
  width: 100%;
  max-width: 520px;
  max-height: 100%;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 20px 18px calc(20px + var(--safe-b));
  background: var(--panel);
  border-radius: 18px 18px 0 0;
}

@media (min-width: 761px) {
  .sheet {
    align-items: center;
  }

  .sheet-card {
    border-radius: 18px;
  }
}

.sheet-title {
  margin: 0 0 2px;
  font-size: 19px;
  font-weight: 600;
  text-align: center;
}

.tabs {
  display: flex;
  gap: 6px;
  padding: 4px;
  border-radius: 12px;
  background: var(--bg);
}

.tab {
  flex: 1 1 0;
  height: 38px;
  border-radius: 9px;
  font-size: 15px;
  color: var(--sub);
}

.tab[aria-pressed="true"] {
  background: var(--panel);
  color: var(--fg);
  font-weight: 600;
  box-shadow: var(--shadow);
}

.field-note {
  font-size: 12px;
  color: var(--sub);
  padding-left: 2px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field-label {
  font-size: 13px;
  color: var(--sub);
  padding-left: 2px;
}

.field input {
  width: 100%;
  height: 46px;
  padding: 0 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg);
  outline: none;
  /* 16px avoids the iOS focus zoom */
  font-size: 16px;
}

.field input:focus {
  border-color: var(--brand);
}

.primary {
  height: 48px;
  border-radius: 12px;
  background: var(--brand);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
}

.primary:disabled {
  opacity: 0.6;
}

.err {
  margin: 0;
  font-size: 13px;
  color: #e5484d;
  text-align: center;
}

.err[hidden] {
  display: none;
}

.hint {
  margin: 0;
  font-size: 12px;
  line-height: 1.5;
  color: var(--sub);
  text-align: center;
}

.sheet-langs {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  padding-top: 2px;
}

.chip {
  padding: 5px 13px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--sub);
  font-size: 13px;
}

.chip[aria-pressed="true"] {
  border-color: var(--brand);
  color: var(--brand);
  font-weight: 600;
}

/* ---------------------------------------------------------------- toast -- */

.toast {
  position: absolute;
  left: 50%;
  bottom: calc(96px + var(--safe-b));
  z-index: 30;
  transform: translate(-50%, 8px);
  max-width: 80%;
  padding: 9px 16px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.82);
  color: #fff;
  font-size: 14px;
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none;
}

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

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
}
