:root {
  --ink: #0b2748;
  --muted: #5f7b96;
  --line: #e2eef8;
  --paper: #ffffff;
  --brand: linear-gradient(135deg, #2ad4e0 0%, #1a7ef0 100%);
  --ok: #0b7a52;
  --ok-bg: #e8f7ef;
  --warn: #9a6b10;
  --warn-bg: #fff7e8;
  --bad: #b42318;
  --shadow-sm: 0 4px 16px rgba(13, 70, 140, .05);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: Inter, "Segoe UI", system-ui, sans-serif;
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 12px; line-height: 1.4; }
.error { color: var(--bad); min-height: 1.2em; font-size: 13px; }
.ok { color: var(--ok); min-height: 1.2em; font-size: 13px; }

.admin-app {
  min-height: 100vh;
  min-height: 100dvh;
  background:
    radial-gradient(880px 460px at 6% -70px, rgba(42, 212, 224, .34) 0%, transparent 60%),
    radial-gradient(840px 440px at 96% -50px, rgba(26, 126, 240, .26) 0%, transparent 58%),
    linear-gradient(180deg, #e4f4fb 0%, #eef6fb 46%, #f4f9fd 100%);
}
.admin-shell {
  width: min(1360px, 100%);
  margin: 0 auto;
  padding: 12px 16px calc(20px + var(--safe-b));
}
.admin-top {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  background: rgba(255,255,255,.92);
  border: 1px solid rgba(226, 238, 248, .95);
  border-radius: 18px;
  padding: 10px;
  box-shadow: var(--shadow-sm);
  position: sticky;
  top: 8px;
  z-index: 30;
  backdrop-filter: blur(16px);
}
.admin-logo { text-decoration: none; }
.brand-text {
  display: inline-flex; align-items: center; height: 36px; padding: 0 12px;
  border-radius: 11px; color: #fff; background: var(--brand); font-weight: 800; font-size: 14px;
}
.admin-nav {
  display: flex; justify-content: center; gap: 4px;
  overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none;
  padding: 3px; border-radius: 14px; background: #f3f7fb;
}
.admin-nav::-webkit-scrollbar { display: none; }
.admin-nav a {
  flex: none; text-decoration: none; color: var(--muted);
  font-size: 12px; font-weight: 700; padding: 8px 12px; border-radius: 11px; white-space: nowrap;
}
.admin-nav a.on { background: var(--brand); color: #fff; }
.admin-user { display: flex; align-items: center; gap: 6px; justify-content: flex-end; min-width: 0; }
.bo-session {
  display: inline-flex; align-items: center; gap: 6px; max-width: 160px;
  padding: 6px 10px; border-radius: 11px; border: 1px solid #cfeee8;
  background: #f3fbf8; color: #0b7a52; font-size: 12px; font-weight: 650;
  overflow: hidden;
}
.bo-session strong {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.bo-session .bo-dot {
  width: 8px; height: 8px; border-radius: 50%; background: #12b76a; flex: none;
  box-shadow: 0 0 0 3px rgba(18,183,106,.18);
}
.user-chip {
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  border-radius: 11px; padding: 7px 10px; font-size: 12px; font-weight: 700; cursor: pointer;
}

.admin-main { padding-top: 14px; }
.page-head { margin: 0 0 12px; }
.page-eyebrow {
  margin: 0 0 4px; font-size: 11px; font-weight: 750; letter-spacing: .1em;
  text-transform: uppercase; color: #7a95ad;
}
.admin-main h1 { margin: 0; font-size: clamp(20px, 5vw, 28px); letter-spacing: -.03em; }
.page-sub { margin: 6px 0 0; color: var(--muted); font-size: 13px; line-height: 1.45; }
.card {
  background: var(--paper); border: 1px solid #dceaf5; border-radius: 18px;
  box-shadow: 0 10px 28px rgba(13,70,140,.045); padding: 16px; margin: 0 0 12px;
}
.btn {
  border: 0; border-radius: 999px; padding: 10px 14px; font-weight: 750; cursor: pointer;
  color: #fff; background: var(--brand); font: inherit; min-height: 40px;
}
.btn.secondary { background: #fff; color: var(--ink); border: 1px solid var(--line); }
.stack-form { display: grid; gap: 12px; max-width: 760px; }
.stack-form h2 { margin: 10px 0 0; font-size: 15px; }
label { display: grid; gap: 6px; font-size: 13px; font-weight: 650; color: #35587a; }
label.check { display: flex; align-items: flex-start; gap: 10px; font-weight: 650; line-height: 1.35; }
label.check input { width: 18px; height: 18px; margin-top: 1px; flex: none; }
input, textarea, select {
  width: 100%; border: 1px solid var(--line); border-radius: 12px; padding: 11px 12px;
  background: #fff; color: var(--ink); font: inherit; font-size: 15px;
}
.action-row { display: flex; flex-wrap: wrap; gap: 8px; }
pre {
  background: #f3f7fb; border: 1px solid var(--line); border-radius: 12px;
  padding: 12px; overflow: auto; white-space: pre-wrap; word-break: break-all; font-size: 12px;
}

.login-wrap {
  min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 20px;
}
.login-card { width: min(420px, 100%); display: grid; gap: 12px; }

.inbox-layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 12px;
  height: calc(100dvh - 170px);
  min-height: 480px;
}
.conv-side, .chat-side {
  display: flex; flex-direction: column; min-height: 0; min-width: 0;
  padding: 0; overflow: hidden; margin: 0;
}
.filters {
  display: grid; grid-template-columns: 1fr auto; gap: 8px;
  padding: 12px; border-bottom: 1px solid var(--line); flex: none;
}
.conv-list { overflow: auto; -webkit-overflow-scrolling: touch; flex: 1; }
.conv-item {
  width: 100%; text-align: left; background: transparent; border: 0;
  border-bottom: 1px solid var(--line); border-radius: 0; padding: 12px 14px;
  cursor: pointer; font: inherit; color: inherit;
}
.conv-item.active { background: #eaf6ff; }
.conv-item .status {
  display: inline-block; margin-top: 6px; font-size: 11px; font-weight: 750;
  padding: 3px 8px; border-radius: 999px; background: #eef5fb; color: #35587a;
}
.conv-item .status.waiting { background: var(--warn-bg); color: var(--warn); }
.conv-item .status.human { background: var(--ok-bg); color: var(--ok); }
.empty {
  margin: auto; color: var(--muted); padding: 24px; text-align: center; font-size: 14px;
}
.chat-active { display: flex; flex-direction: column; min-height: 0; height: 100%; }
.chat-head {
  display: flex; justify-content: space-between; gap: 10px; align-items: flex-start;
  padding: 12px; border-bottom: 1px solid var(--line); flex: none;
}
.chat-head > div:first-child { min-width: 0; flex: 1; }
.chat-head strong {
  display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chat-head .action-row { flex: none; }
.back-inbox {
  display: none;
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  border-radius: 999px; padding: 8px 12px; font-size: 12px; font-weight: 750; cursor: pointer;
}
.waiting-banner {
  background: var(--warn-bg); color: var(--warn); padding: 8px 12px;
  font-size: 12px; font-weight: 650; flex: none;
}
.msg-list {
  flex: 1; overflow: auto; -webkit-overflow-scrolling: touch;
  padding: 14px; display: flex; flex-direction: column; gap: 10px; background: #f7fbfe;
}
.bubble {
  max-width: min(85%, 420px); padding: 10px 12px; border-radius: 14px; font-size: 13px;
  white-space: pre-wrap; line-height: 1.4; background: #fff; border: 1px solid var(--line);
}
.bubble.visitor { align-self: flex-start; }
.bubble.agent, .bubble.bot { align-self: flex-end; background: #eaf6ff; border-color: #cfe4ff; }
.bubble.system {
  align-self: center; background: transparent; border-style: dashed; color: var(--muted); border-radius: 999px;
}
.bubble .who { font-size: 11px; color: var(--muted); margin-bottom: 4px; font-weight: 700; }
.bubble img { max-width: min(220px, 70vw); border-radius: 10px; display: block; margin-top: 6px; }
.reply-form {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  padding: 10px 12px calc(10px + var(--safe-b));
  border-top: 1px solid var(--line);
  align-items: end;
  flex: none;
  background: #fff;
}
.reply-form textarea {
  min-height: 42px; max-height: 120px; resize: vertical; font-size: 15px;
}
.reply-form .btn { padding: 10px 12px; white-space: nowrap; }

/* ===== Mobile ===== */
@media (max-width: 900px) {
  .admin-shell { padding: 8px 10px calc(12px + var(--safe-b)); }
  .admin-top {
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "logo user"
      "nav nav";
    gap: 8px;
    top: 6px;
    border-radius: 16px;
    padding: 8px;
  }
  .admin-logo { grid-area: logo; }
  .admin-user { grid-area: user; }
  .admin-nav {
    grid-area: nav;
    justify-content: flex-start;
    width: 100%;
  }
  .bo-session { display: none; }
  .page-head { margin-bottom: 10px; }
  .page-sub { display: none; }
  .admin-main { padding-top: 10px; }
  .card { border-radius: 16px; padding: 14px; }

  .inbox-layout {
    grid-template-columns: 1fr;
    height: calc(100dvh - 140px);
    min-height: 0;
    gap: 0;
  }
  .inbox-layout.mobile-chat .conv-side { display: none; }
  .inbox-layout.mobile-list .chat-side { display: none; }
  .conv-side, .chat-side {
    height: 100%;
    border-radius: 16px;
  }
  .back-inbox { display: inline-flex; align-items: center; }
  .chat-head {
    flex-wrap: wrap;
    align-items: center;
  }
  .chat-head .action-row { width: 100%; }
  .chat-head .action-row .btn { flex: 1; text-align: center; }
  .reply-form {
    grid-template-columns: auto 1fr;
    grid-template-areas:
      "photo input"
      "send send";
  }
  .reply-form #agent-photo-btn { grid-area: photo; }
  .reply-form textarea { grid-area: input; }
  .reply-form button[type="submit"] { grid-area: send; width: 100%; }

  .stack-form .action-row .btn { width: 100%; }
  .stack-form .btn[type="submit"] { width: 100%; }
}

@media (max-width: 420px) {
  .brand-text { font-size: 13px; height: 34px; padding: 0 10px; }
  .admin-nav a { padding: 8px 10px; font-size: 12px; }
  .bubble { max-width: 92%; }
}
