:root {
  --bg: #eef3f2;
  --surface: #ffffff;
  --surface-2: #f4f7f6;
  --text: #13201f;
  --muted: #5f706e;
  --border: #d9e2e0;
  --accent: #0f766e;
  --accent-text: #ffffff;
  --mine: #d7f3ec;
  --mine-text: #0d2b27;
  --theirs: #ffffff;
  --btn: #e3efec;
  --btn-text: #0b5b54;
  --danger: #b42318;
  --shadow: 0 1px 2px rgba(0, 0, 0, .08);
  --radius: 16px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1416;
    --surface: #132022;
    --surface-2: #182a2d;
    --text: #e5efed;
    --muted: #8fa3a0;
    --border: #23383b;
    --accent: #2bb3a3;
    --accent-text: #04201d;
    --mine: #135049;
    --mine-text: #e6fbf6;
    --theirs: #172629;
    --btn: #1b3437;
    --btn-text: #8fe3d8;
    --danger: #f97066;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Rubik, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#app { height: 100%; }

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

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

button:disabled { opacity: .5; cursor: default; }

.primary, .secondary, .danger {
  display: block;
  width: 100%;
  padding: 12px 16px;
  border-radius: 12px;
  font-weight: 600;
}
.primary { background: var(--accent); color: var(--accent-text); }
.secondary { background: var(--btn); color: var(--btn-text); }
.danger { background: transparent; color: var(--danger); border: 1px solid var(--border); }

.splash {
  height: 100%;
  display: grid;
  place-items: center;
}
.splash img { border-radius: 18px; opacity: .9; }

/* --- Login --- */
.login {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: max(24px, env(safe-area-inset-top)) 20px max(24px, env(safe-area-inset-bottom));
}
.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border-radius: 20px;
  padding: 28px 22px;
  box-shadow: var(--shadow);
  text-align: center;
  display: grid;
  gap: 14px;
}
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0; }
.login-logo { margin: 0 auto; border-radius: 18px; }
.code-input {
  font: 600 28px/1 ui-monospace, Menlo, Consolas, monospace;
  letter-spacing: .35em;
  text-align: center;
  padding: 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  width: 100%;
}
.login-error { color: var(--danger); min-height: 1.2em; }
.hint {
  font-size: 14px;
  background: var(--surface-2);
  border-radius: 12px;
  padding: 10px 12px;
  color: var(--muted);
}

/* --- Chat layout --- */
.chat {
  height: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: relative;
  z-index: 2;
}
.avatar { width: 38px; height: 38px; border-radius: 50%; }
.title { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.title strong { font-size: 17px; }
.title small { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.icon-btn {
  width: 40px;
  height: 40px;
  flex: 0 0 40px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: var(--muted);
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg { width: 24px; height: 24px; fill: currentColor; }
.icon-btn.send { color: var(--accent); }
[dir="rtl"] .icon-btn.send svg { transform: scaleX(-1); }
.bell { color: var(--accent); }

.banner {
  background: #b54708;
  color: #fff;
  text-align: center;
  font-size: 14px;
  padding: 6px 12px;
}

.messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 10px 6px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.empty {
  margin: auto;
  max-width: 320px;
  text-align: center;
  color: var(--muted);
  padding: 24px;
}

.day {
  align-self: center;
  font-size: 12px;
  color: var(--muted);
  background: var(--surface);
  border-radius: 999px;
  padding: 3px 10px;
  margin: 8px 0 4px;
  box-shadow: var(--shadow);
}

.load-older {
  align-self: center;
  font-size: 14px;
  color: var(--accent);
  padding: 6px 12px;
}

.msg {
  display: flex;
  flex-direction: column;
  max-width: min(88%, 560px);
  gap: 4px;
}
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.bot { align-self: flex-start; align-items: stretch; }

.bubble {
  background: var(--theirs);
  border-radius: var(--radius);
  padding: 8px 12px 6px;
  box-shadow: var(--shadow);
  overflow-wrap: anywhere;
  position: relative;
}
.msg.user .bubble { background: var(--mine); color: var(--mine-text); }
.bubble .text { white-space: pre-wrap; }
.bubble .time {
  display: block;
  font-size: 11px;
  color: var(--muted);
  text-align: end;
  margin-top: 2px;
}
.bubble a { color: var(--accent); }
.bubble a.cmd { text-decoration: none; font-weight: 500; }
.bubble code, .bubble pre {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 14px;
  background: var(--surface-2);
  border-radius: 6px;
}
.bubble code { padding: 1px 4px; }
.bubble pre { padding: 8px; margin: 4px 0; white-space: pre-wrap; direction: ltr; text-align: left; }
.bubble blockquote { margin: 4px 0; padding-inline-start: 10px; border-inline-start: 3px solid var(--accent); }
.spoiler { background: var(--muted); color: transparent; border-radius: 4px; }
.spoiler:active { color: inherit; background: none; }

.media { display: block; margin: -2px -6px 6px; }
.media img, video.media {
  display: block;
  max-width: 100%;
  max-height: 340px;
  border-radius: 12px;
}
audio { display: block; max-width: 240px; height: 40px; margin-bottom: 4px; }
.file {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 0;
}
.file svg { width: 18px; height: 18px; fill: currentColor; }

/* --- Buttons under messages (inline keyboard) --- */
.inline-kb, .reply-kb { display: flex; flex-direction: column; gap: 4px; }
.kb-row { display: flex; gap: 4px; }
.kb-btn {
  flex: 1 1 0;
  min-width: 0;
  padding: 9px 8px;
  border-radius: 10px;
  background: var(--btn);
  color: var(--btn-text);
  font-size: 14px;
  font-weight: 500;
  text-align: center;
  text-decoration: none;
  overflow-wrap: anywhere;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  transition: opacity .15s;
}
.kb-btn:active { opacity: .7; }
.kb-btn svg { width: 16px; height: 16px; fill: currentColor; }
.kb-btn.busy { opacity: .55; position: relative; }
.kb-btn.busy::after {
  content: '';
  width: 12px;
  height: 12px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.reply-kb {
  padding: 6px 10px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  max-height: 34vh;
  overflow-y: auto;
}

.typing {
  align-self: flex-start;
  display: flex;
  gap: 4px;
  margin: 0 16px 6px;
  padding: 10px 12px;
  background: var(--theirs);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  width: fit-content;
}
.typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  animation: blink 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* --- Composer --- */
.composer, .recording {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  padding: 6px 6px calc(env(safe-area-inset-bottom) + 6px);
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.composer textarea {
  flex: 1;
  min-width: 0;
  resize: none;
  font: inherit;
  font-size: 16px; /* 16px stops iOS from zooming in on focus */
  line-height: 1.35;
  padding: 9px 14px;
  margin: 0 4px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  max-height: 140px;
  outline: none;
}
.composer textarea:focus { border-color: var(--accent); }

.recording { align-items: center; gap: 8px; }
.rec-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #e5484d;
  animation: blink 1s infinite;
}
.rec-timer { font-variant-numeric: tabular-nums; font-weight: 600; }
.rec-label { flex: 1; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* --- Toast --- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 84px);
  transform: translate(-50%, 20px);
  max-width: calc(100% - 32px);
  background: rgba(20, 30, 30, .92);
  color: #fff;
  padding: 10px 16px;
  border-radius: 12px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 30;
  text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* --- Bottom sheets --- */
.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .35);
  opacity: 0;
  transition: opacity .2s;
  z-index: 10;
}
.backdrop.show { opacity: 1; }
.sheet {
  position: fixed;
  inset-inline: 0;
  bottom: 0;
  margin: 0 auto;
  max-width: 640px;
  max-height: 82vh;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  transform: translateY(100%);
  transition: transform .22s ease-out;
  z-index: 11;
  padding-bottom: env(safe-area-inset-bottom);
}
.sheet.show { transform: none; }
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 10px 6px 18px;
  font-size: 17px;
}
[dir="rtl"] .sheet-head { padding: 10px 18px 6px 10px; }
.sheet-body {
  overflow-y: auto;
  padding: 4px 16px 18px;
  display: grid;
  gap: 12px;
}
.search {
  width: 100%;
  font: inherit;
  padding: 10px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
}
.cmd-list { display: flex; flex-direction: column; }
.cmd-item {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  padding: 10px 4px;
  border-bottom: 1px solid var(--border);
  text-align: start;
}
.cmd-name { color: var(--accent); font-weight: 600; font-size: 15px; }
.cmd-desc { color: var(--muted); font-size: 14px; }

.setting {
  display: grid;
  gap: 8px;
  padding: 12px;
  background: var(--surface-2);
  border-radius: 14px;
}
.setting p { margin: 0; }
.setting.toggle {
  grid-template-columns: 1fr auto;
  align-items: center;
}
.setting.toggle > span { display: grid; gap: 2px; }
.setting input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--accent); }
