:root {
  --bg: #F5F3EE;
  --surface: #FFFFFF;
  --surface-2: #ECE9E2;
  --text: #1E2227;
  --muted: #69707B;
  --border: #E1DDD4;
  --accent: #3867D6;
  --on-accent: #FFFFFF;
  --danger: #C8433F;
  --shadow: 0 1px 2px rgba(30, 34, 39, .06), 0 3px 10px rgba(30, 34, 39, .05);
  --radius: 20px;
  color-scheme: light;
}
:root[data-child="katya"] { --accent: #7650D2; }
:root[data-child="platon"] { --accent: #2A78CC; }

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121417;
    --surface: #1C2026;
    --surface-2: #272C34;
    --text: #E9EBEE;
    --muted: #9AA2AD;
    --border: #2C323A;
    --accent: #5B82E6;
    --danger: #E5716C;
    --shadow: none;
    color-scheme: dark;
  }
  :root[data-child="katya"] { --accent: #8A67E3; }
  :root[data-child="platon"] { --accent: #3D8BE0; }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

#app {
  height: var(--app-h, 100dvh);
  display: flex;
  flex-direction: column;
}
.screen { flex: 1; display: flex; flex-direction: column; min-height: 0; }

button { font: inherit; color: inherit; cursor: pointer; }
.icon-btn {
  width: 44px; height: 44px; flex: none;
  display: grid; place-items: center;
  border: none; border-radius: 50%;
  background: transparent; color: var(--text);
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn[aria-pressed="true"] { color: var(--accent); }
.icon-btn svg, .action-btn svg { width: 24px; height: 24px; }
.action-btn {
  width: 46px; height: 46px; flex: none;
  display: grid; place-items: center;
  border: none; border-radius: 50%;
  background: var(--accent); color: var(--on-accent);
}
.action-btn:active { filter: brightness(.92); }
.action-btn:disabled, .action-btn.wait { opacity: .4; }
.text-btn { border: none; background: none; color: var(--muted); padding: 10px 12px; border-radius: 12px; }
.text-btn:active { background: var(--surface-2); }
.muted { color: var(--muted); }
.small { font-size: 14px; }

/* ---- login */
.login-wrap {
  width: 100%; max-width: 420px; margin: 0 auto;
  padding: calc(48px + env(safe-area-inset-top)) 24px 24px;
  text-align: center; overflow-y: auto;
}
.logo { width: 88px; height: 88px; border-radius: 22px; box-shadow: var(--shadow); }
.login-wrap h1 { font-size: 34px; margin: 14px 0 2px; letter-spacing: -.01em; }
.login-wrap p { margin: 0; }
.child-list { display: grid; gap: 14px; margin: 28px 0 16px; }
.child-card {
  display: flex; align-items: center; gap: 16px;
  padding: 14px 18px; border-radius: 22px;
  background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow);
  font-size: 22px; font-weight: 650; text-align: left;
}
.child-card:active { background: var(--surface-2); }
.avatar {
  width: 56px; height: 56px; border-radius: 50%; flex: none;
  display: grid; place-items: center;
  background: #3867D6; color: #fff; font-weight: 700; font-size: 24px;
}
.avatar.katya { background: #7650D2; }
.avatar.platon { background: #2A78CC; }
.avatar.big { width: 76px; height: 76px; font-size: 32px; }

/* ---- pin */
.pin-top { padding: calc(6px + env(safe-area-inset-top)) 6px 0; }
.pin-wrap {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  padding: 0 24px calc(16px + env(safe-area-inset-bottom)); gap: 8px; overflow-y: auto;
}
.pin-wrap h2 { font-size: 22px; margin: 6px 0 0; text-align: center; }
.pin-dots { display: flex; gap: 14px; height: 22px; margin: 10px 0 0; align-items: center; }
.pin-dots span { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--muted); }
.pin-dots span.on { background: var(--accent); border-color: var(--accent); }
.pin-error { min-height: 22px; margin: 4px 0; color: var(--danger); font-size: 15px; text-align: center; }
.keypad { display: grid; grid-template-columns: repeat(3, 76px); gap: 14px 22px; margin-top: 4px; }
.key {
  height: 76px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
  font-size: 30px; font-weight: 500; box-shadow: var(--shadow);
  display: grid; place-items: center;
}
.key:active { background: var(--surface-2); }
.key.fn { background: transparent; border-color: transparent; box-shadow: none; }
.key.ok { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.key svg { width: 28px; height: 28px; }
.shake { animation: shake .35s; }
@keyframes shake { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-10px); } 75% { transform: translateX(10px); } }

/* ---- chat */
.topbar {
  display: flex; align-items: center; gap: 2px;
  padding: calc(6px + env(safe-area-inset-top)) 6px 6px 14px;
  background: var(--bg); border-bottom: 1px solid var(--border);
}
.brand { flex: 1; display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-icon { width: 36px; height: 36px; border-radius: 10px; }
.title { font-weight: 700; font-size: 18px; line-height: 1.1; }
.sub { font-size: 13px; color: var(--muted); }

.messages {
  flex: 1; min-height: 0;
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 10px 0 14px;
}
.msg { display: flex; flex-direction: column; margin: 6px 12px; }
.msg.me { align-items: flex-end; }
.msg.bot { align-items: flex-start; }
.bubble {
  max-width: min(86%, 600px);
  padding: 9px 14px; border-radius: var(--radius);
  overflow-wrap: anywhere; box-shadow: var(--shadow);
}
.me .bubble { background: var(--accent); color: var(--on-accent); border-bottom-right-radius: 6px; }
.bot .bubble { background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 6px; }
.bubble .plain { white-space: pre-wrap; }
.bubble .voice-line { white-space: pre-wrap; }
.bubble .voice-line.pending { opacity: .75; font-style: italic; }
.thumbs { display: grid; gap: 4px; margin: -3px -8px 6px; }
.thumbs.n2, .thumbs.n3, .thumbs.n4 { grid-template-columns: 1fr 1fr; }
.thumbs img {
  width: 100%; max-height: 220px; object-fit: cover;
  border-radius: 14px; display: block; background: rgba(0, 0, 0, .08);
}
.thumbs.n2 img, .thumbs.n3 img, .thumbs.n4 img { height: 120px; }
.bubble.only-images { padding: 4px; background: transparent; box-shadow: none; }
.bubble.only-images .thumbs { margin: 0; }

.md p { margin: 0 0 .55em; }
.md p:last-child, .md ul:last-child, .md ol:last-child, .md pre:last-child { margin-bottom: 0; }
.md ul, .md ol { margin: .1em 0 .55em; padding-left: 1.3em; }
.md li { margin: .15em 0; }
.md .h { font-weight: 700; }
.md code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .9em;
  background: var(--surface-2); padding: .05em .35em; border-radius: 5px;
}
.md pre {
  background: var(--surface-2); padding: 8px 10px; border-radius: 10px;
  overflow-x: auto; margin: .3em 0 .55em;
}
.md pre code { background: none; padding: 0; white-space: pre; }
.md a { color: var(--accent); }

.bot-actions { display: flex; gap: 4px; margin: 2px 0 0 2px; }
.listen {
  display: inline-flex; align-items: center; gap: 6px;
  border: none; background: none; color: var(--muted);
  font-size: 14px; padding: 6px 10px; border-radius: 14px;
}
.listen svg { width: 16px; height: 16px; }
.listen:active { background: var(--surface-2); }
.listen.playing { color: var(--accent); }
.listen.loading { opacity: .6; }

.typing { display: flex; align-items: center; gap: 5px; min-height: 24px; }
.typing .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite both; }
.typing .dot:nth-child(2) { animation-delay: .2s; }
.typing .dot:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.status { color: var(--muted); font-size: 14px; margin-top: 4px; }
.typing + .status { margin: 0 0 0 6px; }
.sys { text-align: center; color: var(--danger); font-size: 15px; margin: 8px 20px; }
.sys.note { color: var(--muted); }
.stop-link:disabled { opacity: .5; }
.day { text-align: center; color: var(--muted); font-size: 13px; margin: 14px 0 4px; }

.empty { text-align: center; padding: 36px 24px 12px; color: var(--muted); }
.empty img { width: 64px; height: 64px; border-radius: 16px; }
.empty .hello { font-size: 22px; color: var(--text); font-weight: 700; margin: 12px 0 6px; }
.empty p { margin: 0 auto; max-width: 320px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 18px; }
.chip {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 18px; padding: 8px 14px; font-size: 15px;
}
.chip:active { background: var(--surface-2); }

.previews { display: flex; gap: 8px; padding: 8px 12px 0; overflow-x: auto; background: var(--bg); }
.preview { position: relative; width: 64px; height: 64px; flex: none; border-radius: 12px; overflow: hidden; }
.preview img { width: 100%; height: 100%; object-fit: cover; display: block; }
.preview button {
  position: absolute; top: 3px; right: 3px; width: 24px; height: 24px;
  display: grid; place-items: center; padding: 0;
  border: none; border-radius: 50%; background: rgba(0, 0, 0, .6); color: #fff;
}
.preview button svg { width: 14px; height: 14px; }

.composer {
  background: var(--bg); border-top: 1px solid var(--border);
  padding: 8px 8px max(8px, env(safe-area-inset-bottom));
}
:root.kb .composer { padding-bottom: 8px; }
.compose-row { display: flex; align-items: flex-end; gap: 4px; }
.compose-row textarea {
  flex: 1; min-width: 0; resize: none;
  border: 1px solid var(--border); border-radius: 23px;
  background: var(--surface); color: var(--text);
  font: inherit; font-size: 17px; line-height: 1.35;
  padding: 11px 14px; max-height: 150px; outline: none;
}
.compose-row textarea:focus { border-color: var(--accent); }
.rec-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; min-height: 46px; }
.rec-indicator { display: flex; align-items: center; gap: 10px; font-variant-numeric: tabular-nums; font-size: 18px; }
.rec-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--danger); animation: pulse 1s infinite; }
.rec-label { color: var(--muted); font-size: 15px; }
.rec-row.arming .rec-dot { background: var(--muted); animation: none; }
.rec-row.arming #recTime { display: none; }
.rec-row.live .rec-label { color: var(--danger); font-weight: 600; }
@keyframes pulse { 50% { opacity: .3; } }

/* ---- overlays */
.sheet { position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, .35); display: flex; align-items: flex-end; }
.sheet-panel {
  width: 100%; max-width: 560px; margin: 0 auto;
  background: var(--surface); border-radius: 20px 20px 0 0;
  padding: 10px 12px calc(12px + env(safe-area-inset-bottom));
}
.sheet-title { font-weight: 650; padding: 10px 16px 2px; }
.sheet-text { color: var(--muted); padding: 2px 16px 8px; font-size: 15px; }
.sheet-item {
  display: block; width: 100%; text-align: left;
  padding: 15px 16px; border: none; background: none; border-radius: 12px; font-size: 18px;
}
.sheet-item:active { background: var(--surface-2); }
.sheet-item.danger { color: var(--danger); }
.sheet-item.primary { color: var(--accent); font-weight: 650; }

.viewer { position: fixed; inset: 0; z-index: 30; background: rgba(0, 0, 0, .94); display: grid; place-items: center; }
.viewer img { max-width: 100%; max-height: 100%; object-fit: contain; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(96px + env(safe-area-inset-bottom)); z-index: 40;
  max-width: 86vw; padding: 10px 16px; border-radius: 14px;
  background: rgba(30, 34, 39, .94); color: #fff; font-size: 15px; text-align: center;
}

@media (prefers-reduced-motion: reduce) {
  .typing .dot, .rec-dot, .shake { animation: none; }
}
