/* Starkeep Чат — тема «Полночь». Светлая/тёмная — по настройке телефона. */
:root {
  --bg: #F3F4F9;
  --panel: #FFFFFF;
  --panel-2: #EEF0F7;
  --line: rgba(18, 26, 58, .08);
  --text: #121A3A;
  --muted: #6A7299;
  --accent: #F2B705;
  --accent-soft: #FFF1C2;
  --accent-ink: #121A3A;
  --bubble-in: #FFFFFF;
  --bubble-out: #FFF1C2;
  --bubble-out-text: #121A3A;
  --tick: #8A90B0;
  --tick-read: #2F6BFF;
  --link: #2F5BD8;
  --danger: #D93A3A;
  --shadow: 0 1px 2px rgba(18, 26, 58, .08);
  --chat-bg: #E9ECF5;
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0B1026;
    --panel: #111836;
    --panel-2: #18214A;
    --line: rgba(255, 255, 255, .07);
    --text: #EEF1FF;
    --muted: #8E96C2;
    --accent: #F7C948;
    --accent-soft: rgba(247, 201, 72, .14);
    --accent-ink: #0B1026;
    --bubble-in: #18214A;
    --bubble-out: #2A3570;
    --bubble-out-text: #EEF1FF;
    --tick: #8E96C2;
    --tick-read: #F7C948;
    --link: #9DB6FF;
    --shadow: none;
    --chat-bg: #0B1026;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
a { color: var(--link); }
.muted { color: var(--muted); }
.error { color: var(--danger); margin: 0; }
.hint { color: var(--muted); font-size: 14px; margin: 0; }

svg.i { width: 24px; height: 24px; display: block; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- каркас ---------- */
#app {
  position: fixed; inset: 0;
  display: grid;
  grid-template-columns: 1fr;
  height: 100dvh;
}
#app.booting > * { visibility: hidden; }
.pane { min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--panel); }
#chat { position: relative; background: var(--chat-bg); }

/* телефон: показываем либо список, либо переписку */
#app:not(.chat-open) #chat { display: none; }
#app.chat-open #list { display: none; }
.no-chat { display: none; }

/* компьютер/планшет: две колонки */
@media (min-width: 820px) {
  #app { grid-template-columns: minmax(300px, 380px) 1fr; }
  #app:not(.chat-open) #chat, #app.chat-open #list { display: flex; }
  #list { border-right: 1px solid var(--line); }
  #btn-back { display: none; }
  #app:not(.chat-open) #chat > :not(.no-chat) { display: none !important; }
  #app:not(.chat-open) .no-chat { display: grid; place-items: center; flex: 1; color: var(--muted); }
}
#app.logged-out { grid-template-columns: 1fr; }
#app.logged-out .pane { display: none !important; }

/* ---------- вход ---------- */
.screen { display: grid; place-items: center; padding: 24px; padding-top: calc(24px + var(--safe-top)); overflow: auto; }
.login-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 14px; }
.login-card .logo img { width: 72px; height: 72px; border-radius: 20px; display: block; }
.login-card h1 { margin: 4px 0 0; font-size: 28px; letter-spacing: -.02em; }
.login-card p { margin: 0; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.login-card input {
  height: 48px; padding: 0 14px; border-radius: 14px; border: 1px solid var(--line);
  background: var(--panel); font-size: 17px; outline: none;
}
.login-card input:focus { border-color: var(--accent); }

.btn { height: 48px; padding: 0 20px; border-radius: 14px; font-weight: 600; background: var(--panel-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; }
.btn.small { height: 36px; padding: 0 14px; border-radius: 10px; font-size: 14px; }

/* ---------- шапки ---------- */
.bar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; padding-top: calc(10px + var(--safe-top));
  min-height: calc(60px + var(--safe-top));
  background: var(--panel); border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.bar h2 { margin: 0 0 0 8px; font-size: 24px; letter-spacing: -.02em; flex: 1; }
.bar-actions { display: flex; gap: 4px; }
.icon-btn { width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; color: var(--text); flex-shrink: 0; }
.icon-btn:active { background: var(--panel-2); }
.icon-btn.send { background: var(--accent); color: var(--accent-ink); border-radius: 50%; }
.icon-btn.send:disabled { opacity: .45; }

.banner { margin: 10px 12px 0; padding: 10px 12px; border-radius: 12px; background: var(--accent-soft); font-size: 14px; display: flex; gap: 10px; align-items: center; }
.banner > span { flex: 1; }
.banner.warn { background: rgba(217, 58, 58, .12); color: var(--danger); }

/* ---------- список чатов ---------- */
.chat-list { list-style: none; margin: 0; padding: 6px 0 calc(6px + var(--safe-bottom)); overflow-y: auto; flex: 1; }
.chat-item { display: flex; gap: 12px; align-items: center; padding: 10px 16px; cursor: pointer; }
.chat-item:active, .chat-item.active { background: var(--panel-2); }
.chat-item .meta { flex: 1; min-width: 0; display: grid; grid-template-columns: 1fr auto; row-gap: 2px; column-gap: 8px; }
.chat-item .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .time { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 3px; }
.chat-item .preview { color: var(--muted); font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .badge { min-width: 22px; height: 22px; padding: 0 7px; border-radius: 11px; background: var(--accent); color: var(--accent-ink); font-size: 13px; font-weight: 700; display: grid; place-items: center; justify-self: end; }
.empty { padding: 40px 24px; text-align: center; color: var(--muted); display: flex; flex-direction: column; gap: 14px; align-items: center; }

.avatar {
  width: 48px; height: 48px; border-radius: 50%; flex-shrink: 0;
  display: grid; place-items: center; font-weight: 700; font-size: 19px; color: #fff;
  background: var(--av, #5B6BD6);
}
.avatar.emoji { background: var(--panel-2); font-size: 26px; }
.avatar.sm { width: 38px; height: 38px; font-size: 16px; }
.avatar.sm.emoji { font-size: 21px; }

/* ---------- шапка переписки ---------- */
.chat-head { flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; text-align: left; padding: 2px 4px; border-radius: 12px; }
.chat-head-text { min-width: 0; display: flex; flex-direction: column; }
.chat-head .title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head .sub { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head .sub.typing { color: var(--accent); }
@media (prefers-color-scheme: light) { .chat-head .sub.typing { color: #B88700; } }

/* ---------- сообщения ---------- */
.messages { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 8px 10px 12px; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.older { text-align: center; color: var(--muted); font-size: 13px; padding: 8px; }
.day { text-align: center; margin: 14px 0 8px; }
.day span { font-size: 13px; color: var(--muted); background: var(--panel); padding: 3px 10px; border-radius: 10px; box-shadow: var(--shadow); }

.msg { display: flex; flex-direction: column; align-items: flex-start; margin-top: 2px; max-width: 100%; }
.msg.out { align-items: flex-end; }
.msg.first { margin-top: 8px; }
.msg .sender { font-size: 13px; font-weight: 600; margin: 0 0 2px 12px; color: var(--av, var(--muted)); }
.bubble {
  max-width: min(78%, 520px);
  padding: 7px 10px 6px 12px;
  border-radius: var(--radius);
  background: var(--bubble-in);
  box-shadow: var(--shadow);
  position: relative;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}
.msg.out .bubble { background: var(--bubble-out); color: var(--bubble-out-text); }
.msg.in.last .bubble { border-bottom-left-radius: 6px; }
.msg.out.last .bubble { border-bottom-right-radius: 6px; }
.bubble .text { display: inline; }
/* место под время, чтобы оно не налезало на текст */
.bubble .text::after { content: ""; display: inline-block; width: var(--stamp-w, 44px); }
.msg.out .bubble .text::after { --stamp-w: 64px; }
.stamp {
  position: absolute; right: 9px; bottom: 5px;
  display: inline-flex; align-items: center; gap: 3px;
  font-size: 11.5px; color: var(--muted); line-height: 1; white-space: nowrap;
}
.msg.out .stamp { color: color-mix(in srgb, var(--bubble-out-text) 55%, transparent); }
.tick { width: 18px; height: 12px; display: block; fill: none; stroke: var(--tick); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tick.read { stroke: var(--tick-read); }
.tick.clock { width: 12px; height: 12px; }
.msg.failed .bubble { outline: 2px solid var(--danger); cursor: pointer; }
.msg .fail-note { font-size: 12px; color: var(--danger); margin: 2px 6px; }

.bubble.big-emoji { background: none !important; box-shadow: none; padding: 0 2px 16px; font-size: 44px; line-height: 1.15; }
.bubble.big-emoji .stamp { background: var(--panel); padding: 3px 6px; border-radius: 8px; bottom: 0; }
.bubble.big-emoji .text::after { display: none; }

/* медиа в пузыре */
.bubble.media-bubble { padding: 3px; overflow: hidden; }
.media {
  position: relative; display: block; border-radius: 15px; overflow: hidden;
  width: min(260px, 64vw); max-height: 360px;
  background: var(--panel-2); cursor: pointer;
}
.media .ph { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; filter: blur(14px); transform: scale(1.15); }
.media .full { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0; transition: opacity .2s; }
.media .full.on { opacity: 1; }
.media .overlay {
  position: absolute; inset: 0; display: grid; place-items: center; text-align: center;
  color: #fff; font-size: 13px; padding: 12px; background: rgba(0, 0, 0, .25);
}
.media .overlay .pill { background: rgba(0, 0, 0, .55); padding: 6px 10px; border-radius: 12px; }
.media .play { width: 52px; height: 52px; border-radius: 50%; background: rgba(0, 0, 0, .5); display: grid; place-items: center; }
.media .play svg { width: 26px; height: 26px; fill: #fff; stroke: none; margin-left: 3px; }
.media .dur { position: absolute; left: 8px; top: 8px; font-size: 12px; color: #fff; background: rgba(0, 0, 0, .5); padding: 2px 6px; border-radius: 8px; }
.media-bubble .stamp { color: #fff !important; background: rgba(0, 0, 0, .45); padding: 3px 6px; border-radius: 9px; right: 9px; bottom: 9px; }
.media-bubble .tick { stroke: #fff; }
.media-bubble .tick.read { stroke: var(--accent); }
.spinner { width: 28px; height: 28px; border: 3px solid rgba(255, 255, 255, .35); border-top-color: #fff; border-radius: 50%; animation: spin 1s linear infinite; margin: 0 auto 6px; }
@keyframes spin { to { transform: rotate(360deg); } }

.sticker { position: relative; padding-bottom: 18px; }
.sticker img { width: 140px; height: 140px; object-fit: contain; display: block; }
.sticker .stamp { background: var(--panel); padding: 3px 6px; border-radius: 8px; bottom: 0; right: 0; }

.to-bottom {
  position: absolute; right: 14px; bottom: calc(80px + var(--safe-bottom)); width: 44px; height: 44px; border-radius: 50%;
  background: var(--panel); box-shadow: 0 2px 8px rgba(0, 0, 0, .2); display: grid; place-items: center; z-index: 2;
}

/* ---------- поле ввода ---------- */
.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 8px 8px calc(8px + var(--safe-bottom)); flex-shrink: 0; }
.composer-row { display: flex; align-items: flex-end; gap: 6px; }
.input-wrap { flex: 1; display: flex; align-items: flex-end; background: var(--panel-2); border-radius: 22px; min-height: 40px; padding-left: 14px; }
#input {
  flex: 1; border: 0; outline: none; background: none; resize: none;
  font-size: 16px; line-height: 22px; padding: 9px 0; max-height: 140px; min-height: 40px;
}
.emoji-btn { color: var(--muted); }

.emoji-panel { height: 260px; display: flex; flex-direction: column; margin: -8px -8px 8px; border-bottom: 1px solid var(--line); }
.emoji-tabs { display: flex; gap: 4px; padding: 6px 8px; }
.emoji-tabs button { padding: 6px 12px; border-radius: 10px; font-size: 14px; color: var(--muted); }
.emoji-tabs button.active { background: var(--panel-2); color: var(--text); font-weight: 600; }
.emoji-grid, .sticker-grid { flex: 1; overflow-y: auto; padding: 0 6px 8px; display: grid; }
.emoji-grid { grid-template-columns: repeat(auto-fill, minmax(42px, 1fr)); }
.emoji-grid h4 { grid-column: 1 / -1; margin: 8px 6px 4px; font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.emoji-grid button { font-size: 28px; height: 44px; border-radius: 10px; }
.emoji-grid button:active { background: var(--panel-2); }
.sticker-grid { grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 6px; align-content: start; }
.sticker-grid button { aspect-ratio: 1; border-radius: 12px; padding: 6px; }
.sticker-grid button:active { background: var(--panel-2); }
.sticker-grid img { width: 100%; height: 100%; object-fit: contain; }
.sticker-grid .empty { grid-column: 1 / -1; font-size: 14px; padding: 24px 12px; }

/* ---------- просмотр ---------- */
.viewer { position: fixed; inset: 0; z-index: 50; background: #000; display: flex; flex-direction: column; color: #fff; }
.viewer-bar { display: flex; align-items: center; gap: 8px; padding: 8px; padding-top: calc(8px + var(--safe-top)); }
.viewer-bar .icon-btn { color: #fff; }
.viewer-bar span { flex: 1; text-align: center; font-size: 14px; opacity: .8; }
.viewer-body { flex: 1; min-height: 0; display: grid; place-items: center; padding-bottom: var(--safe-bottom); }
.viewer-body img, .viewer-body video { max-width: 100%; max-height: 100%; object-fit: contain; }

/* ---------- листы ---------- */
.sheet-wrap { position: fixed; inset: 0; z-index: 40; background: rgba(5, 8, 20, .5); display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  width: 100%; max-width: 520px; max-height: 88dvh; overflow-y: auto;
  background: var(--panel); border-radius: 22px 22px 0 0;
  padding: 8px 16px calc(16px + var(--safe-bottom));
  animation: up .2s ease-out;
}
@media (min-width: 820px) { .sheet-wrap { align-items: center; } .sheet { border-radius: 22px; } }
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
.sheet .grabber { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 10px; }
.sheet h3 { margin: 4px 0 12px; font-size: 20px; }
.sheet .row { display: flex; align-items: center; gap: 12px; padding: 10px 4px; border-radius: 12px; width: 100%; text-align: left; }
.sheet button.row:active { background: var(--panel-2); }
.sheet .row .grow { flex: 1; min-width: 0; }
.sheet .row small { display: block; color: var(--muted); font-size: 13px; }
.sheet .section { margin: 14px 0 6px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.sheet .field { width: 100%; height: 46px; padding: 0 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel-2); font-size: 16px; outline: none; }
.sheet .actions { display: flex; gap: 10px; margin-top: 16px; }
.sheet .actions .btn { flex: 1; }
.check { width: 24px; height: 24px; border-radius: 50%; border: 2px solid var(--muted); display: grid; place-items: center; flex-shrink: 0; }
.row.selected .check { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.check svg { width: 14px; height: 14px; stroke-width: 3; opacity: 0; }
.row.selected .check svg { opacity: 1; }
.emoji-pick { display: flex; flex-wrap: wrap; gap: 6px; }
.emoji-pick button { width: 44px; height: 44px; border-radius: 12px; font-size: 24px; background: var(--panel-2); }
.emoji-pick button.on { outline: 2px solid var(--accent); }
.switch { width: 46px; height: 28px; border-radius: 14px; background: var(--line); position: relative; flex-shrink: 0; transition: background .15s; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 2px rgba(0, 0, 0, .3); }
.switch.on { background: var(--accent); }
.switch.on::after { transform: translateX(18px); }

.toast {
  position: fixed; left: 50%; bottom: calc(96px + var(--safe-bottom)); transform: translateX(-50%); z-index: 60;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 14px; font-size: 14px;
  max-width: calc(100% - 32px); text-align: center; box-shadow: 0 4px 16px rgba(0, 0, 0, .25);
}
