:root {
  --accent: #ee6b0a;
  --accent-dark: #d85b00;
  --accent-soft: rgba(238, 107, 10, .1);
  --accent-ring: rgba(238, 107, 10, .2);
  --text: #142330;
  --muted: #64717d;
  --muted-2: #94a3b0;
  --bg: #fafbf9;
  --panel: #fff;
  --line: #e2e6e9;
  --line-soft: #eff0f2;
  --green: #16a34a;
  --green-soft: #ecfdf5;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --shadow-sm: 0 1px 2px rgba(20, 35, 48, .05);
  --shadow-md: 0 8px 24px rgba(20, 35, 48, .1);
  --brand-bg: #f8fafc;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

button, input, textarea { font: inherit; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 9px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s ease, border-color .15s ease, filter .15s ease, transform .1s ease;
  background: #fff;
  color: var(--text);
  box-shadow: 0 1px 2px rgba(20, 35, 48, .03);
}
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 4px; }
.btn-primary { 
  background: var(--accent); 
  color: #fff; 
  box-shadow: 0 4px 14px rgba(238, 107, 10, .2);
  border-color: var(--accent);
}
.btn-primary:hover { filter: brightness(.95); }
.btn-primary:disabled { opacity: .6; cursor: progress; box-shadow: none; }
.btn-block { width: 100%; padding: 12px 16px; font-size: 14px; }
.btn-outline { 
  background: #fff; 
  border-color: var(--line); 
  color: var(--text);
}
.btn-outline:hover { border-color: #cbd5e1; background: #f8fafc; }

.icon-btn {
  appearance: none;
  border: 0;
  background: #f1f5f9;
  color: var(--text);
  width: 34px;
  height: 34px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background .15s ease, color .15s ease;
}
.icon-btn:hover { background: #e2e8f0; color: var(--text); }
.icon-btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 4px; }
.icon-btn svg { width: 18px; height: 18px; display: block; }
.icon-btn.ghost { background: transparent; }
.icon-btn.ghost:hover { background: #f1f5f9; }

/* ---------- Login ---------- */
.login-view {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 28px 18px;
  overflow-y: auto;
  background: #fff;
}

.login-card {
  width: min(410px, 100%);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 32px 30px 26px;
  box-shadow: 0 30px 70px rgba(20, 35, 48, .04), 0 8px 24px rgba(20, 35, 48, .02);
}

.login-logo {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), var(--accent-dark));
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 22px;
  font-weight: 800;
  box-shadow: 0 8px 24px rgba(238, 107, 10, .25);
  margin-bottom: 20px;
}

.login-card h1 { margin: 0 0 7px; font-size: 22px; letter-spacing: -.8px; line-height: 1.25; }
.login-sub { margin: 0 0 22px; font-size: 13px; line-height: 1.6; color: var(--muted); }
.login-card label { 
  display: block; 
  font-size: 12px; 
  font-weight: 550;
  color: #35434f; 
  margin-bottom: 6px; 
}
.login-card input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--text);
  padding: 12px 14px;
  font-size: 15px;
  outline: none;
  margin-bottom: 16px;
  transition: border-color .15s ease, box-shadow .15s ease;
}
.login-card input::placeholder { color: #929ca4; }
.login-card input:focus { 
  border-color: var(--accent); 
  box-shadow: 0 0 0 4px var(--accent-ring); 
}
.form-error {
  margin: 0 0 16px;
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--danger);
  background: var(--danger-soft);
  border: 1px solid #fecaca;
  border-radius: 8px;
  padding: 10px 12px;
}
.login-link {
  display: block;
  text-align: center;
  margin-top: 18px;
  font-size: 12px;
  color: var(--muted);
  text-decoration: none;
}
.login-link:hover { color: var(--accent); }

/* ---------- App shell ---------- */
.app-view { display: flex; height: 100vh; height: 100dvh; background: var(--bg); }

.sidebar {
  width: 340px;
  flex-shrink: 0;
  background: #fff;
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 20px 12px;
  background: linear-gradient(180deg, #fff 0%, #f8fafc 100%);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-logo {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), var(--accent-dark));
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 18px;
  font-weight: 800;
  flex-shrink: 0;
  box-shadow: 0 6px 16px rgba(238, 107, 10, .25);
}
.brand-text { min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.brand-text strong { font-size: 15px; letter-spacing: -.4px; line-height: 1.2; color: var(--text); }
.brand-text span { display: flex; align-items: center; gap: 8px; font-size: 11px; color: var(--muted); }
.conn-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted-2);
  flex-shrink: 0;
}
.conn-dot.online { 
  background: var(--green); 
  box-shadow: 0 0 0 4px rgba(22, 163, 74, .15);
}
.conn-dot.offline { 
  background: #ef4444; 
  box-shadow: 0 0 0 4px rgba(239, 68, 68, .15); 
}

.sidebar-search {
  position: relative;
  margin: 4px 20px 10px;
}
.sidebar-search svg {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  width: 16px;
  height: 16px;
  color: var(--muted-2);
  pointer-events: none;
}
.sidebar-search input {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 10px;
  padding: 10px 12px 10px 40px;
  font-size: 14px;
  color: var(--text);
  outline: none;
  transition: border-color .15s ease, box-shadow .15s ease;
}
.sidebar-search input::placeholder { color: #9aa8ba; }
.sidebar-search input:focus { 
  border-color: var(--accent); 
  background: #fff; 
  box-shadow: 0 0 0 4px var(--accent-ring); 
}

.tabs {
  display: flex;
  gap: 4px;
  background: #f1f5f9;
  border-radius: 10px;
  padding: 4px;
  margin: 0 20px 10px;
}
.tab {
  flex: 1;
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  border-radius: 8px;
  padding: 8px 6px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s ease, color .15s ease, box-shadow .15s ease;
}
.tab span {
  display: inline-block;
  min-width: 16px;
  padding: 0 4px;
  border-radius: 4px;
  background: rgba(100, 116, 139, .12);
  font-size: 9px;
  line-height: 16px;
  margin-left: 4px;
}
.tab.active { 
  background: #fff; 
  color: var(--text); 
  box-shadow: var(--shadow-sm); 
}
.tab.active span { 
  background: var(--accent); 
  color: #fff;
}
.tab:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.conversation-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 2px 12px 16px;
  scrollbar-width: thin;
  scrollbar-color: #cbd5e1 transparent;
}
.conversation-list::-webkit-scrollbar { width: 6px; }
.conversation-list::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 3px; }

.conv-item {
  width: 100%;
  display: flex;
  gap: 10px;
  align-items: flex-start;
  appearance: none;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
  text-align: left;
  transition: background .12s ease, border-color .12s ease;
}
.conv-item:hover { background: #f8fafc; }
.conv-item.active { background: var(--accent-soft); border-color: #ffe1cf; }
.conv-item:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.conv-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: linear-gradient(135deg, #fb923c, #ea580c);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 14px;
  font-weight: 700;
  flex-shrink: 0;
}
.conv-item.is-closed .conv-avatar { background: #cbd5e1; }
.conv-main { flex: 1; min-width: 0; padding-top: 1px; }
.conv-top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.conv-name {
  font-size: 13px;
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conv-time { font-size: 10px; color: var(--muted-2); flex-shrink: 0; }
.conv-bottom { display: flex; align-items: center; gap: 6px; margin-top: 3px; }
.conv-preview {
  flex: 1;
  font-size: 11.5px;
  line-height: 1.35;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conv-item.is-closed .conv-preview { color: var(--muted-2); }
.unread-count {
  flex-shrink: 0;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-size: 9.5px;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
}
.closed-tag {
  flex-shrink: 0;
  font-size: 9px;
  font-weight: 600;
  letter-spacing: .02em;
  color: var(--muted);
  background: #f1f5f9;
  border-radius: 4px;
  padding: 2px 5px;
  text-transform: uppercase;
}

.list-empty {
  padding: 48px 24px;
  text-align: center;
}
.list-empty p { margin: 0 0 8px; font-size: 14px; font-weight: 550; color: var(--text); }
.list-empty span { font-size: 12.5px; line-height: 1.5; color: var(--muted); }

/* ---------- Main / empty ---------- */
.main { flex: 1; min-width: 0; position: relative; display: flex; flex-direction: column; }

.empty-state {
  margin: auto;
  padding: 48px 32px;
  max-width: 400px;
  text-align: center;
}
.empty-icon {
  width: 56px;
  height: 56px;
  border-radius: 16px;
  background: #fff;
  border: 1px solid var(--line);
  color: var(--accent);
  display: grid;
  place-items: center;
  margin: 0 auto 20px;
  box-shadow: var(--shadow-sm);
}
.empty-icon svg { width: 28px; height: 28px; }
.empty-state h2 { margin: 0 0 8px; font-size: 20px; letter-spacing: -.5px; }
.empty-state p { margin: 0 0 16px; font-size: 14px; line-height: 1.6; color: var(--muted); }

/* ---------- Chat ---------- */
.chat-panel {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  background: #f8fafc;
}

.chat-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: var(--accent);
  color: #fff;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
  min-height: 58px;
}
.chat-head .back { display: none; }
.chat-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: linear-gradient(135deg, #fb923c, #ea580c);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 14px;
  font-weight: 700;
  flex-shrink: 0;
}
.chat-info { flex: 1; min-width: 0; }
.chat-info h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-info p {
  margin: 4px 0 0;
  font-size: 11px;
  color: rgba(255, 255, 255, .5);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.status-pill {
  flex-shrink: 0;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .03em;
  text-transform: uppercase;
  color: #15803d;
  background: var(--green-soft);
  border: 1px solid #bbf7d0;
  padding: 3px 10px;
  border-radius: 999px;
}
.status-pill.closed { color: var(--muted); background: #f1f5f9; border-color: var(--line); }

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 24px 28px 16px;
  scrollbar-width: thin;
  scrollbar-color: #cbd5e1 transparent;
}
.messages::-webkit-scrollbar { width: 7px; }
.messages::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }

.day {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 14px 0 18px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--muted-2);
}
.day::before, .day::after { content: ""; height: 1px; background: #e2e8f0; flex: 1; }

.msg { display: flex; align-items: flex-end; gap: 8px; margin-bottom: 13px; }
.msg.mine { justify-content: flex-end; }
.msg-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 11px;
  font-weight: 700;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  margin-bottom: 17px;
}
.bubble-wrap { max-width: 65%; min-width: 0; }
.msg-name { margin: 0 0 3px 5px; font-size: 10.5px; font-weight: 650; color: var(--muted); }
.bubble {
  padding: 10px 14px;
  border-radius: 15px;
  font-size: 13.5px;
  line-height: 1.48;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
}
.theirs .bubble {
  background: #fff;
  border: 1px solid var(--line);
  border-bottom-left-radius: 5px;
  box-shadow: var(--shadow-sm);
}
.mine .bubble {
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 5px;
  box-shadow: 0 4px 14px rgba(238, 107, 10, .18);
}
.bubble a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
.msg-time { display: block; margin-top: 4px; font-size: 9.5px; color: var(--muted-2); }
.mine .msg-time { text-align: right; }
.mine .msg-name { text-align: right; margin: 0 5px 3px 0; }

.msg-system { text-align: center; margin: 14px 0; }
.msg-system span {
  display: inline-block;
  max-width: 80%;
  padding: 6px 12px;
  border-radius: 999px;
  background: #eef2f7;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.typing {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 26px 8px;
  font-size: 11px;
  color: var(--muted);
  flex-shrink: 0;
}
.typing-dots {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 5px 8px;
}
.typing-dots i {
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--muted-2);
  animation: typing 1.15s ease-in-out infinite;
}
.typing-dots i:nth-child(2) { animation-delay: .14s; }
.typing-dots i:nth-child(3) { animation-delay: .28s; }
@keyframes typing {
  0%, 60%, 100% { transform: translateY(0); opacity: .5; }
  30% { transform: translateY(-3px); opacity: 1; }
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 9px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: #fff;
  border-top: 1px solid var(--line);
  flex-shrink: 0;
}
.composer textarea {
  flex: 1;
  min-height: 42px;
  max-height: 130px;
  resize: none;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 12px;
  padding: 11px 13px;
  font-size: 13px;
  line-height: 1.4;
  color: var(--text);
  outline: none;
  transition: border-color .15s ease, box-shadow .15s ease;
  scrollbar-width: thin;
}
.composer textarea::placeholder { color: #9aa8ba; }
.composer textarea:focus { 
  border-color: var(--accent); 
  background: #fff; 
  box-shadow: 0 0 0 4px var(--accent-ring); 
}
.composer textarea:disabled { background: #f8fafc; color: var(--muted-2); cursor: not-allowed; }
.send-btn {
  width: 42px;
  height: 42px;
  flex-shrink: 0;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: opacity .15s ease, filter .15s ease, transform .1s ease;
}
.send-btn svg { width: 20px; height: 20px; margin-left: 2px; }
.send-btn:hover:not(:disabled) { filter: brightness(.94); }
.send-btn:active:not(:disabled) { transform: scale(.96); }
.send-btn:disabled { opacity: .4; cursor: not-allowed; }
.send-btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 4px; }

/* ---------- Details ---------- */
.details {
  position: absolute;
  top: 58px;
  right: 0;
  bottom: 0;
  width: 320px;
  max-width: 100%;
  background: #fff;
  border-left: 1px solid var(--line);
  box-shadow: -18px 0 40px rgba(20, 35, 48, .08);
  transform: translateX(100%);
  visibility: hidden;
  transition: transform .22s ease, visibility .22s;
  z-index: 20;
  display: flex;
  flex-direction: column;
}
.details.open { transform: none; visibility: visible; }
.details-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px 12px;
  border-bottom: 1px solid var(--line-soft);
}
.details-head h3 { margin: 0; font-size: 14px; letter-spacing: -.5px; }
.details-body {
  padding: 12px 20px 24px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.detail { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.detail > span {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .05em;
  text-transform: uppercase;
  color: var(--muted-2);
}
.detail strong, .detail a, .detail code {
  font-size: 13px;
  line-height: 1.5;
  color: #334155;
  font-weight: 550;
  overflow-wrap: anywhere;
  word-break: break-word;
}
.detail a { color: var(--accent); text-decoration: none; }
.detail a:hover { text-decoration: underline; }
.detail code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 11px; color: var(--muted); }
.detail .small { font-size: 12px; color: var(--muted); font-weight: 450; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 32px;
  transform: translate(-50%, 16px);
  background: #0f172a;
  color: #fff;
  font-size: 13px;
  line-height: 1.4;
  padding: 10px 20px;
  border-radius: 8px;
  box-shadow: 0 12px 28px rgba(20, 35, 48, .3);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
  z-index: 100;
  max-width: min(440px, calc(100% - 32px));
  text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .sidebar { width: 320px; }
  .bubble-wrap { max-width: 74%; }
}

@media (max-width: 780px) {
  .app-view:not(.mobile-chat) .main { display: none; }
  .app-view.mobile-chat .sidebar { display: none; }
  .sidebar { width: 100%; border-right: 0; }
  .chat-head { padding: 10px 12px; }
  .chat-head .back { display: grid; }
  .messages { padding: 20px 14px 12px; }
  .bubble-wrap { max-width: 82%; }
  .typing { padding: 0 16px 7px; }
  .composer { padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); }
  .details { width: 100%; max-width: 100%; box-shadow: none; }
  .status-pill { display: none; }
  .empty-state { display: none; }
}

@media (max-width: 520px) {
  .chat-info p { max-width: 38vw; }
  .login-card { padding: 28px 24px 22px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition: none !important;
  }
  .details { transition: none; }
}