/* Eightlegged Chat operator app. Phone first (one-handed, big targets); two panes from 900px up. */

:root {
  --navy: #1a2848;
  --navy-2: #24365f;
  --bg: #f3f5f9;
  --surface: #ffffff;
  --surface-2: #eef1f6;
  --text: #17203a;
  --muted: #5d6781;
  --line: #dde2ec;
  --accent: #2f6fed;
  --accent-ink: #ffffff;
  --ok: #138a52;
  --ok-soft: #e3f5ec;
  --warn: #b25e00;
  --warn-soft: #fff1df;
  --danger: #c22f3a;
  --danger-soft: #fde8ea;
  --bubble-me: #1a2848;
  --bubble-me-ink: #ffffff;
  --bubble-them: #ffffff;
  --radius: 14px;
  --tap: 48px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1526;
    --surface: #18203a;
    --surface-2: #212b49;
    --text: #e8ecf5;
    --muted: #9aa5c0;
    --line: #2c3758;
    --accent: #6d9bff;
    --accent-ink: #0b1020;
    --ok: #4cc98b;
    --ok-soft: #173a2b;
    --warn: #f0a24a;
    --warn-soft: #3a2a14;
    --danger: #ff7680;
    --danger-soft: #3e1b22;
    --bubble-me: #3a5694;
    --bubble-them: #212b49;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
[hidden] { display: none !important; }
a { color: var(--accent); }
h1, h2, h3 { margin: 0; line-height: 1.2; }
button, input, select, textarea { font: inherit; color: inherit; }
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
svg { width: 24px; height: 24px; flex: none; }

.app { height: 100%; display: flex; flex-direction: column; }

/* ---------- banner (connection, errors) */
.banner {
  position: fixed; left: 12px; right: 12px; top: calc(8px + var(--safe-top)); z-index: 60;
  margin: 0 auto; max-width: 520px; padding: 12px 16px; border-radius: 12px;
  background: var(--navy); color: #fff; font-size: 15px; box-shadow: 0 6px 24px rgba(0, 0, 0, .25);
}
.banner.is-error { background: var(--danger); }

/* ---------- buttons and fields */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 0 18px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); font-weight: 600; cursor: pointer; text-decoration: none; color: var(--text);
}
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn-danger { background: var(--danger-soft); border-color: transparent; color: var(--danger); }
.btn-quiet { background: transparent; border-color: transparent; color: var(--accent); }
.btn-block { width: 100%; }
.btn-big { min-height: 56px; font-size: 18px; border-radius: 14px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 12px; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center; width: var(--tap); height: var(--tap);
  border: 0; border-radius: 50%; background: transparent; color: inherit; cursor: pointer; flex: none;
}
.icon-btn:active { background: rgba(127, 127, 127, .18); }
.stack { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 15px; }
.field input, .field textarea, .field select, .site-filter select {
  width: 100%; min-height: var(--tap); padding: 10px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); font-weight: 400; font-size: 16px;
}
.field textarea { resize: vertical; }
input:focus-visible, textarea:focus-visible, select:focus-visible, button:focus-visible, a:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 55%, transparent); outline-offset: 1px;
}
.alert { margin: 0 0 12px; padding: 10px 14px; border-radius: 10px; background: var(--danger-soft); color: var(--danger); }
.note { margin: 10px 0; padding: 12px 14px; border-radius: 10px; background: var(--warn-soft); font-size: 15px; }
.chip {
  display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 13px; font-weight: 700;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.chip-waiting { background: var(--warn-soft); color: var(--warn); }
.chip-active { background: var(--ok-soft); color: var(--ok); }
.chip-missed { background: var(--danger-soft); color: var(--danger); }
.tag { display: inline-block; margin-left: 6px; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 700; vertical-align: 1px; }
.tag-unchecked { background: var(--warn-soft); color: var(--warn); }
.tag-verified { background: var(--ok-soft); color: var(--ok); }

/* ---------- login */
.login { flex: 1; display: flex; align-items: center; justify-content: center; padding: calc(24px + var(--safe-top)) 16px calc(24px + var(--safe-bottom)); }
.login-card { width: 100%; max-width: 400px; background: var(--surface); border-radius: 20px; padding: 28px 22px; box-shadow: 0 10px 40px rgba(16, 24, 48, .12); }
.login-logo { display: block; border-radius: 16px; margin-bottom: 14px; }
.login-card h1 { font-size: 24px; margin-bottom: 4px; }
.login-card .muted { margin: 0 0 18px; }
.login-card .small { margin: 16px 0 0; }

/* ---------- layout */
.main { flex: 1; display: flex; min-height: 0; }
.pane { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.pane-list { flex: 1; }
.pane-detail { flex: 1; display: none; background: var(--bg); }
.main.show-detail .pane-list { display: none; }
.main.show-detail .pane-detail { display: flex; }
.detail-empty { flex: 1; display: none; flex-direction: column; align-items: center; justify-content: center; gap: 8px; color: var(--muted); }
.detail-empty img { border-radius: 18px; opacity: .85; }
@media (min-width: 900px) {
  .pane-list { flex: 0 0 380px; border-right: 1px solid var(--line); }
  .pane-detail, .main.show-detail .pane-list { display: flex; }
  .detail-empty { display: flex; }
  .pane-detail .back { display: none; }
}

.topbar {
  display: flex; align-items: center; gap: 6px; min-height: 60px;
  padding: calc(6px + var(--safe-top)) 8px 6px 16px; background: var(--navy); color: #fff; flex: none;
}
.topbar .back { margin-left: -8px; }
.topbar-title { font-size: 20px; font-weight: 700; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-who { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar-who .topbar-title { flex: none; font-size: 18px; }
.topbar-sub { font-size: 13px; opacity: .8; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .chip { background: rgba(255, 255, 255, .14); color: #fff; }
.conn-dot { width: 10px; height: 10px; border-radius: 50%; background: #f0a24a; margin-right: 4px; }
.conn-dot.is-up { background: #4cc98b; }
.conn-dot.is-down { background: #ff7680; }

/* ---------- list pane */
.list-scroll { flex: 1; overflow-y: auto; padding: 14px 14px calc(24px + var(--safe-bottom)); -webkit-overflow-scrolling: touch; }
.avail {
  display: flex; align-items: center; gap: 16px; width: 100%; min-height: 76px; padding: 14px 18px;
  border: 2px solid var(--line); border-radius: 18px; background: var(--surface); text-align: left; cursor: pointer;
}
.avail-knob { position: relative; flex: none; width: 64px; height: 36px; border-radius: 999px; background: #b9c1d3; transition: background .2s; }
.avail-knob::after {
  content: ""; position: absolute; top: 4px; left: 4px; width: 28px; height: 28px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, .25); transition: transform .2s;
}
.avail[aria-pressed="true"] { border-color: var(--ok); background: var(--ok-soft); }
.avail[aria-pressed="true"] .avail-knob { background: var(--ok); }
.avail[aria-pressed="true"] .avail-knob::after { transform: translateX(28px); }
.avail-text { display: flex; flex-direction: column; }
.avail-text strong { font-size: 19px; }
.avail-hint { font-size: 14px; color: var(--muted); }
.avail:disabled { opacity: .6; }
.site-filter { display: block; margin-top: 12px; }

.list-head { display: flex; align-items: baseline; gap: 8px; margin: 22px 4px 8px; font-size: 14px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.list-head .count { min-width: 22px; padding: 1px 7px; border-radius: 999px; background: var(--surface-2); text-align: center; }
.list-head.is-hot .count { background: var(--warn); color: #fff; }
.list-empty { margin: 0 4px; padding: 14px 16px; border-radius: 14px; border: 1px dashed var(--line); color: var(--muted); font-size: 15px; }
.cards { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.cardrow { display: flex; align-items: stretch; border-radius: 16px; background: var(--surface); border: 2px solid transparent; overflow: hidden; }
.cardrow.is-current { border-color: var(--accent); }
.cardrow.is-waiting { border-color: color-mix(in srgb, var(--warn) 45%, transparent); }
.cardlink { flex: 1; min-width: 0; display: flex; gap: 12px; align-items: flex-start; padding: 14px 16px; color: var(--text); text-decoration: none; }
.cardlink:active { background: var(--surface-2); }
.card-main { flex: 1; min-width: 0; }
.card-top { display: flex; align-items: baseline; gap: 8px; }
.card-name { font-weight: 700; font-size: 17px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-time { font-size: 14px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.card-time.is-long { color: var(--danger); font-weight: 700; }
.card-site { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-last { margin-top: 4px; font-size: 15px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-accept { align-self: center; min-height: 48px; margin-right: 12px; padding: 0 18px; }

/* ---------- conversation */
.conv, .settings { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.thread { flex: 1; overflow-y: auto; padding: 14px 12px 8px; -webkit-overflow-scrolling: touch; }
.start-card { background: var(--surface); border-radius: 16px; padding: 14px 16px; margin-bottom: 14px; font-size: 15px; border: 1px solid var(--line); }
.start-card dl { margin: 0; display: grid; grid-template-columns: max-content 1fr; gap: 6px 12px; }
.start-card dt { color: var(--muted); }
.start-card dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.start-card details { margin-top: 10px; }
.start-card summary { cursor: pointer; color: var(--accent); min-height: 32px; display: flex; align-items: center; }
.pages { list-style: none; margin: 6px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.pages li { display: flex; gap: 8px; }
.pages .when { color: var(--muted); font-size: 13px; flex: none; font-variant-numeric: tabular-nums; }
.pages .page { min-width: 0; overflow-wrap: anywhere; }
.ctx { margin-top: 8px; padding: 8px 10px; border-radius: 10px; background: var(--surface-2); }

.messages { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.msg { display: flex; flex-direction: column; max-width: 84%; }
.msg-visitor { align-self: flex-start; }
.msg-operator { align-self: flex-end; align-items: flex-end; }
.msg-system { align-self: center; max-width: 92%; text-align: center; font-size: 13px; color: var(--muted); margin: 6px 0; }
.bubble { padding: 9px 13px; border-radius: 18px; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 16px; }
.msg-visitor .bubble { background: var(--bubble-them); border-bottom-left-radius: 6px; border: 1px solid var(--line); }
.msg-operator .bubble { background: var(--bubble-me); color: var(--bubble-me-ink); border-bottom-right-radius: 6px; }
.msg-operator .bubble a { color: inherit; }
.bubble img { display: block; max-width: 100%; max-height: 260px; border-radius: 12px; margin: 2px 0 4px; cursor: zoom-in; background: var(--surface-2); min-width: 80px; min-height: 60px; }
.msg-meta { font-size: 12px; color: var(--muted); margin: 2px 6px 4px; }
.msg.is-pending .bubble { opacity: .6; }
.msg.is-failed .bubble { outline: 2px solid var(--danger); }
.msg-retry { border: 0; background: none; color: var(--danger); font-size: 13px; font-weight: 700; padding: 4px 6px; cursor: pointer; }
.typing { margin: 8px 6px 0; font-size: 14px; color: var(--muted); font-style: italic; }

.conv-foot { flex: none; background: var(--surface); border-top: 1px solid var(--line); padding: 10px 10px calc(10px + var(--safe-bottom)); }
.foot-note { margin: 0 4px 10px; font-size: 15px; color: var(--muted); }
.foot-actions { display: flex; gap: 10px; }
.foot-actions .btn { flex: 1; }
.composer { display: flex; align-items: flex-end; gap: 4px; }
.composer textarea {
  flex: 1; min-height: var(--tap); max-height: 160px; padding: 12px 14px; border-radius: 22px;
  border: 1px solid var(--line); background: var(--bg); resize: none; font-size: 16px; line-height: 1.35;
}
.composer .send { background: var(--accent); color: var(--accent-ink); }
.composer .send:disabled { opacity: .45; }
.composer .icon-btn { color: var(--muted); }
.composer .send.icon-btn { color: var(--accent-ink); }
.slash { list-style: none; margin: 0 0 8px; padding: 6px; max-height: 40vh; overflow-y: auto; border-radius: 14px; background: var(--surface-2); }

/* ---------- saved replies (picker and settings) */
.reply-pick {
  display: block; width: 100%; text-align: left; min-height: var(--tap); padding: 10px 12px; border: 0; border-radius: 10px;
  background: transparent; cursor: pointer;
}
.reply-pick:hover, .reply-pick:focus-visible, .reply-pick.is-active { background: var(--surface); }
.reply-pick .sc { font-weight: 700; color: var(--accent); margin-right: 6px; }
.reply-pick .bd { display: block; font-size: 14px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.replies-admin { list-style: none; margin: 8px 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.replies-admin .reply-pick { background: var(--surface-2); }
.reply-form { margin-top: 12px; }

/* ---------- settings */
.settings-scroll { flex: 1; overflow-y: auto; padding: 14px 14px calc(24px + var(--safe-bottom)); }
.card { background: var(--surface); border-radius: 16px; padding: 16px; margin-bottom: 14px; }
.card h3 { font-size: 17px; margin-bottom: 8px; }
.card p { margin: 6px 0; }
.plain-list { margin: 6px 0 0; padding-left: 20px; color: var(--muted); }
.devices { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.devices li { padding: 10px 12px; border-radius: 12px; background: var(--surface-2); font-size: 14px; }
.devices strong { display: block; font-size: 15px; }
.devices .bad { color: var(--danger); }
.devices .good { color: var(--ok); }
.switch-row { display: flex; align-items: center; gap: 12px; min-height: var(--tap); }
.switch-row input { width: 24px; height: 24px; flex: none; }

/* ---------- bottom sheet */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(8, 12, 24, .45); z-index: 40; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50; max-height: 85vh; overflow-y: auto;
  background: var(--surface); border-radius: 20px 20px 0 0; padding: 8px 16px calc(16px + var(--safe-bottom));
  box-shadow: 0 -8px 30px rgba(0, 0, 0, .2);
}
@media (min-width: 640px) { .sheet { left: 50%; right: auto; width: 480px; transform: translateX(-50%); bottom: 24px; border-radius: 20px; } }
.sheet-grip { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 12px; }
.sheet-title { font-size: 18px; margin: 0 4px 12px; }
.sheet-body { display: flex; flex-direction: column; gap: 8px; }
.sheet-body .btn { justify-content: flex-start; }
.sheet-body .btn small { margin-left: auto; font-weight: 400; color: var(--muted); }
.sheet-search { min-height: var(--tap); padding: 10px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); width: 100%; font-size: 16px; }

/* ---------- photo viewer */
.photo-view { position: fixed; inset: 0; z-index: 70; background: rgba(0, 0, 0, .92); display: flex; align-items: center; justify-content: center; }
.photo-view img { max-width: 100%; max-height: 100%; object-fit: contain; }
.photo-view .icon-btn { position: absolute; top: calc(10px + var(--safe-top)); right: 10px; color: #fff; background: rgba(255, 255, 255, .12); }
