:root {
  --bg: #f4f1ff;
  --surface: #ffffff;
  --text: #2d2a3e;
  --muted: #7a7590;
  --primary: #6c5ce7;
  --primary-2: #a29bfe;
  --accent: #fd79a8;
  --ok: #00b894;
  --user: #6c5ce7;
  --user-text: #ffffff;
  --bot: #ffffff;
  --border: #e3ddff;
  --shadow: 0 4px 16px rgba(108, 92, 231, .15);
  --radius: 20px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1830;
    --surface: #26223f;
    --text: #f1eeff;
    --muted: #a8a2c6;
    --bot: #2f2a4d;
    --border: #3b355e;
    --shadow: 0 4px 16px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 18px/1.5 "Nunito", "Segoe UI", system-ui, -apple-system, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button, select, input, textarea { font: inherit; color: inherit; }

.app {
  max-width: 820px;
  margin: 0 auto;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: 0 16px env(safe-area-inset-bottom);
}

/* Header */
.top { display: flex; align-items: center; justify-content: space-between; padding: 12px 0 8px; gap: 8px; }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.mascot { font-size: 40px; display: inline-block; transition: transform .3s; }
.mascot.talking { animation: bob .6s ease-in-out infinite; }
.mascot.thinking { animation: tilt 1.2s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-6px) scale(1.05); } }
@keyframes tilt { 25% { transform: rotate(-10deg); } 75% { transform: rotate(10deg); } }
.title { font-weight: 800; font-size: 22px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-actions { display: flex; gap: 6px; align-items: center; }
.lang { border: 2px solid var(--border); background: var(--surface); border-radius: 12px; padding: 6px 8px; font-size: 16px; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 46px; height: 46px; flex: none;
  border: 2px solid var(--border); background: var(--surface);
  border-radius: 14px; font-size: 22px; cursor: pointer;
}
.icon-btn:active { transform: scale(.94); }
.icon-btn.small { width: 30px; height: 30px; font-size: 14px; border-radius: 50%; }

/* Modes */
.modes { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 8px; }
.mode {
  border: 2px solid var(--border); background: var(--surface); border-radius: 16px;
  padding: 10px; font-size: 17px; cursor: pointer;
}
.mode.active { background: var(--primary); border-color: var(--primary); color: #fff; box-shadow: var(--shadow); }

/* Chat */
.chat { flex: 1; overflow-y: auto; padding: 8px 2px 16px; display: flex; flex-direction: column; gap: 12px; scroll-behavior: smooth; }
.msg { max-width: 88%; padding: 12px 16px; border-radius: var(--radius); box-shadow: var(--shadow); word-wrap: break-word; }
.msg.user { align-self: flex-end; background: var(--user); color: var(--user-text); border-bottom-right-radius: 6px; }
.msg.assistant { align-self: flex-start; background: var(--bot); border-bottom-left-radius: 6px; }
.msg.error { align-self: center; background: #ffeaa7; color: #6b4f00; font-size: 16px; }
.msg.error .details { display: block; margin-top: 6px; font-size: 12px; opacity: .75; word-break: break-word; }
.msg img { max-width: 100%; max-height: 240px; border-radius: 12px; display: block; margin-bottom: 6px; }
.msg p { margin: 0 0 8px; } .msg p:last-child { margin-bottom: 0; }
.msg ul, .msg ol { margin: 4px 0 8px; padding-left: 24px; }
.msg code { background: rgba(108,92,231,.12); padding: 1px 6px; border-radius: 6px; }
.msg .meta { display: flex; gap: 8px; align-items: center; margin-top: 6px; font-size: 13px; color: var(--muted); }
.msg .meta button { border: 0; background: none; cursor: pointer; font-size: 18px; padding: 0; }
.welcome { text-align: center; color: var(--muted); margin: auto 0; padding: 24px 8px; }
.welcome .big { font-size: 64px; }
.welcome h1 { color: var(--text); font-size: 26px; margin: 8px 0; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 16px; }
.chip { border: 2px solid var(--border); background: var(--surface); border-radius: 999px; padding: 8px 14px; cursor: pointer; font-size: 16px; }

.typing span { display: inline-block; width: 9px; height: 9px; margin: 0 2px; border-radius: 50%; background: var(--primary-2); animation: dot 1s infinite; }
.typing span:nth-child(2) { animation-delay: .15s; } .typing span:nth-child(3) { animation-delay: .3s; }
@keyframes dot { 50% { transform: translateY(-6px); opacity: .5; } }

.status { min-height: 24px; text-align: center; color: var(--muted); font-size: 16px; padding: 0 8px; }

/* Composer */
.composer { padding: 6px 0 12px; display: flex; flex-direction: column; gap: 10px; }
.composer .row { display: flex; gap: 8px; align-items: flex-end; }
textarea {
  flex: 1; min-width: 0; resize: none; max-height: 140px;
  border: 2px solid var(--border); border-radius: 16px; background: var(--surface);
  padding: 10px 14px; outline: none;
}
textarea:focus { border-color: var(--primary); }
.send { background: var(--primary); border-color: var(--primary); color: #fff; }
.attachment { position: relative; align-self: flex-start; }
.attachment img { height: 72px; border-radius: 12px; border: 2px solid var(--border); }
.attachment .icon-btn { position: absolute; top: -10px; right: -10px; }

.mic {
  display: flex; align-items: center; justify-content: center; gap: 12px;
  width: 100%; padding: 16px; border: 0; border-radius: 999px; cursor: pointer;
  background: linear-gradient(135deg, var(--primary), var(--accent)); color: #fff;
  font-size: 20px; font-weight: 800; box-shadow: var(--shadow);
  user-select: none;
}
.mic-icon { font-size: 30px; }
.mic.listening { background: linear-gradient(135deg, #e17055, #d63031); animation: pulse 1.2s infinite; }
.mic.speaking { background: linear-gradient(135deg, var(--ok), #00cec9); }
.mic:disabled { opacity: .5; }
@keyframes pulse { 0%,100% { box-shadow: 0 0 0 0 rgba(214,48,49,.5); } 50% { box-shadow: 0 0 0 14px rgba(214,48,49,0); } }

/* Settings */
dialog { border: 0; border-radius: 24px; padding: 0; width: min(440px, calc(100vw - 32px)); background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(20, 16, 40, .5); }
.settings { padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.settings h2 { margin: 0; }
.settings label { display: flex; flex-direction: column; gap: 4px; font-size: 16px; }
.settings label.check { flex-direction: row; align-items: center; gap: 10px; }
.settings select, .settings input[type=password] { border: 2px solid var(--border); border-radius: 12px; padding: 8px; background: var(--bg); }
.settings input[type=checkbox] { width: 22px; height: 22px; accent-color: var(--primary); }
.settings .hint { font-size: 14px; color: var(--muted); margin: 0; white-space: pre-line; }
.settings menu { margin: 0; padding: 0; display: flex; justify-content: flex-end; }
.primary { background: var(--primary); color: #fff; border: 0; border-radius: 12px; padding: 10px 20px; cursor: pointer; font-weight: 700; }

@media (max-width: 480px) {
  body { font-size: 17px; }
  .title { font-size: 18px; }
  .mascot { font-size: 32px; }
  .icon-btn { width: 42px; height: 42px; font-size: 20px; }
}
