:root {
  color-scheme: dark;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 16px;
  --bg: #090a0f;
  --panel: #101116;
  --panel-raised: #17181f;
  --panel-soft: #1c1e27;
  --line: #292b35;
  --line-strong: #3a3d49;
  --text: #f3f2f8;
  --muted: #9698a7;
  --muted-strong: #b9bbc7;
  --accent: #8c7cff;
  --accent-bright: #b6aaff;
  --accent-soft: rgba(140, 124, 255, 0.14);
  --cyan: #68d9e7;
  --danger: #ff7c91;
  --shadow: 0 24px 80px rgba(0, 0, 0, 0.36);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  min-width: 320px;
  overflow: hidden;
  background:
    radial-gradient(circle at 76% -20%, rgba(103, 83, 255, 0.13), transparent 38%),
    var(--bg);
  color: var(--text);
}

button, input, textarea { font: inherit; }
button { color: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }

button:focus-visible, input:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--accent-bright);
  outline-offset: 2px;
}

[hidden] { display: none !important; }

svg {
  width: 1.25rem;
  height: 1.25rem;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.8;
}

.eyebrow {
  margin: 0;
  color: var(--accent-bright);
  font-size: 0.75rem;
  font-weight: 750;
  letter-spacing: 0.16em;
}

.login-view {
  display: grid;
  min-height: 100dvh;
  place-items: center;
  padding: 1.5rem;
  isolation: isolate;
}

.login-view::before {
  position: fixed;
  z-index: -1;
  inset: 0;
  background-image:
    linear-gradient(rgba(255,255,255,.025) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,.025) 1px, transparent 1px);
  background-size: 48px 48px;
  mask-image: radial-gradient(circle at center, black, transparent 70%);
  content: "";
}

.login-card {
  width: min(100%, 27rem);
  padding: 2.6rem;
  border: 1px solid var(--line);
  border-radius: 1.5rem;
  background: linear-gradient(150deg, rgba(27, 28, 37, 0.96), rgba(14, 15, 21, 0.98));
  box-shadow: var(--shadow);
}

.brand-mark {
  position: relative;
  width: 3.25rem;
  height: 3.25rem;
  margin-bottom: 2rem;
  transform: rotate(45deg);
  border: 1px solid rgba(182, 170, 255, 0.7);
  border-radius: 0.7rem;
  box-shadow: inset 0 0 22px rgba(140, 124, 255, 0.14), 0 0 28px rgba(140, 124, 255, 0.12);
}

.brand-mark span {
  position: absolute;
  inset: 0.55rem;
  border: 1px solid rgba(104, 217, 231, 0.75);
  border-radius: 0.3rem;
}

.brand-mark span:last-child {
  inset: 1.12rem -0.35rem;
  border-color: rgba(182, 170, 255, 0.5);
}

.login-card h1 {
  margin: 0.65rem 0 0;
  font-size: clamp(1.8rem, 5vw, 2.35rem);
  line-height: 1.15;
  letter-spacing: -0.035em;
}

.login-copy {
  margin: 0.85rem 0 2rem;
  color: var(--muted-strong);
  line-height: 1.65;
}

.login-form, .modal-card { display: flex; flex-direction: column; }
.login-form { gap: 0.75rem; }
.login-form label, .modal-card > label { color: var(--muted-strong); font-size: 0.875rem; font-weight: 650; }

.key-input-wrap { position: relative; }

input, textarea {
  width: 100%;
  border: 1px solid var(--line-strong);
  border-radius: 0.75rem;
  background: #0d0e13;
  color: var(--text);
}

input { height: 3.1rem; padding: 0 0.95rem; }
input::placeholder, textarea::placeholder { color: #6f7180; }
.key-input-wrap input { padding-right: 3.2rem; }

.icon-button {
  display: inline-grid;
  flex: 0 0 auto;
  width: 2.5rem;
  height: 2.5rem;
  padding: 0;
  place-items: center;
  border: 0;
  border-radius: 0.7rem;
  background: transparent;
  color: var(--muted-strong);
  cursor: pointer;
}

.icon-button:hover { background: var(--panel-soft); color: var(--text); }
.icon-button.bordered { border: 1px solid var(--line); background: var(--panel-raised); }
.reveal-button { position: absolute; top: 0.3rem; right: 0.3rem; }

.primary-button, .secondary-button, .danger-button, .new-chat-button {
  min-height: 2.8rem;
  padding: 0.65rem 1rem;
  border-radius: 0.72rem;
  font-weight: 680;
  cursor: pointer;
}

.primary-button {
  border: 1px solid #9b8cff;
  background: linear-gradient(135deg, #7563ec, #8e78f2);
  color: white;
  box-shadow: 0 8px 22px rgba(113, 89, 231, 0.2);
}

.primary-button:hover { filter: brightness(1.08); }
.primary-button:disabled { cursor: wait; filter: grayscale(.4); opacity: .65; }

.secondary-button, .danger-button {
  border: 1px solid var(--line-strong);
  background: var(--panel-raised);
}

.secondary-button:hover { border-color: #5b5e6e; background: var(--panel-soft); }
.danger-button { color: var(--danger); }
.danger-button:hover { border-color: rgba(255, 124, 145, .48); background: rgba(255, 124, 145, .08); }

.form-error { min-height: 1.15rem; margin: 0; color: var(--danger); font-size: 0.82rem; line-height: 1.4; }
.privacy-note { margin: 1.25rem 0 0; color: var(--muted); font-size: 0.78rem; line-height: 1.5; }

.app-view {
  display: grid;
  height: 100dvh;
  grid-template-columns: 21rem minmax(0, 1fr);
}

.sidebar {
  display: flex;
  min-height: 0;
  flex-direction: column;
  border-right: 1px solid var(--line);
  background: rgba(15, 16, 22, 0.94);
  backdrop-filter: blur(18px);
}

.sidebar-header {
  display: flex;
  min-height: 5rem;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.15rem;
}

.mini-brand { display: flex; align-items: center; gap: 0.8rem; }
.mini-brand > div:last-child { display: flex; flex-direction: column; gap: 0.15rem; }
.mini-brand strong { font-size: 0.98rem; letter-spacing: 0.01em; }
.mini-brand span { color: var(--muted); font-size: 0.72rem; }
.brand-mark-small { width: 1.75rem; height: 1.75rem; margin: 0; border-radius: 0.38rem; }
.brand-mark-small span { inset: 0.3rem; }
.brand-mark-small span:last-child { inset: 0.6rem -0.2rem; }

.avatar-button, .profile-avatar, .chat-avatar {
  display: grid;
  place-items: center;
  border: 1px solid rgba(255,255,255,.12);
  background: var(--avatar-color, #6657cf);
  color: white;
  font-weight: 760;
  text-transform: uppercase;
}

.avatar-button { width: 2.35rem; height: 2.35rem; border-radius: 50%; cursor: pointer; }
.avatar-button:hover { box-shadow: 0 0 0 3px var(--accent-soft); }
.avatar-tone-0 { background: #6657cf; }
.avatar-tone-1 { background: #397a91; }
.avatar-tone-2 { background: #8b546e; }
.avatar-tone-3 { background: #4e7f65; }
.avatar-tone-4 { background: #8a653f; }
.avatar-tone-5 { background: #506ca3; }
.avatar-tone-6 { background: #76578b; }
.avatar-tone-7 { background: #7d5d58; }

.sidebar-actions { display: flex; gap: 0.55rem; padding: 0 1rem 0.85rem; }
.new-chat-button {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  border: 1px solid rgba(140, 124, 255, .35);
  background: var(--accent-soft);
  color: var(--accent-bright);
}
.new-chat-button:hover { background: rgba(140, 124, 255, .22); }

.search-box {
  display: flex;
  height: 2.65rem;
  align-items: center;
  gap: 0.5rem;
  margin: 0 1rem;
  padding: 0 0.8rem;
  border: 1px solid var(--line);
  border-radius: 0.72rem;
  background: #0b0c11;
  color: var(--muted);
}
.search-box svg { width: 1.05rem; }
.search-box input { height: auto; padding: 0; border: 0; outline: 0; background: transparent; font-size: 0.9rem; }

.list-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.3rem 1.15rem 0.55rem;
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 670;
  letter-spacing: 0.04em;
}

.connection-state { display: inline-flex; align-items: center; gap: .4rem; letter-spacing: 0; }
.connection-state::before { width: .38rem; height: .38rem; border-radius: 50%; background: #e0b86b; content: ""; }
.connection-state.online::before { background: #65d6a0; box-shadow: 0 0 8px rgba(101, 214, 160, .5); }
.connection-state.offline::before { background: var(--danger); }

.chat-list { min-height: 0; flex: 1; overflow-y: auto; padding: 0 0.55rem 1rem; }
.chat-item {
  display: grid;
  width: 100%;
  grid-template-columns: 2.65rem minmax(0, 1fr) auto;
  gap: 0.7rem;
  align-items: center;
  padding: 0.75rem 0.65rem;
  border: 0;
  border-radius: 0.8rem;
  background: transparent;
  text-align: left;
  cursor: pointer;
}
.chat-item:hover { background: rgba(255,255,255,.035); }
.chat-item.active { background: var(--accent-soft); }
.chat-item .chat-avatar { width: 2.65rem; height: 2.65rem; border-radius: 0.8rem; }
.chat-item[data-type="user"] .chat-avatar { border-radius: 50%; }
.chat-item-copy { min-width: 0; }
.chat-item-line { display: flex; align-items: center; justify-content: space-between; gap: .5rem; }
.chat-item-name { overflow: hidden; font-size: 0.92rem; font-weight: 660; text-overflow: ellipsis; white-space: nowrap; }
.chat-item-meta { display: block; overflow: hidden; margin-top: .24rem; color: var(--muted); font-size: .76rem; text-overflow: ellipsis; white-space: nowrap; }
.chat-item-time { color: #757785; font-size: .7rem; white-space: nowrap; }
.unread-dot { width: .5rem; height: .5rem; border-radius: 50%; background: var(--accent-bright); box-shadow: 0 0 10px rgba(182,170,255,.45); }

.empty-chat-list { padding: 3rem 2rem; text-align: center; }
.empty-chat-list p { margin: 0 0 .35rem; font-weight: 650; }
.empty-chat-list span { color: var(--muted); font-size: .82rem; line-height: 1.5; }

.conversation { position: relative; min-width: 0; min-height: 0; background: rgba(9, 10, 15, .7); }
.conversation-placeholder { display: grid; height: 100%; place-content: center; justify-items: center; padding: 2rem; text-align: center; }
.conversation-placeholder h2 { margin: .6rem 0 .3rem; font-size: 1.55rem; }
.conversation-placeholder > p:last-child { max-width: 29rem; margin: 0; color: var(--muted); line-height: 1.6; }
.plane-illustration { position: relative; width: 8rem; height: 8rem; margin-bottom: 2rem; transform: rotate(45deg); }
.plane-illustration span { position: absolute; border: 1px solid rgba(140,124,255,.35); border-radius: 1rem; inset: 0; }
.plane-illustration span:nth-child(2) { inset: 1.3rem; border-color: rgba(104,217,231,.4); }
.plane-illustration span:nth-child(3) { inset: 2.6rem; border-color: rgba(182,170,255,.7); box-shadow: 0 0 32px rgba(140,124,255,.18); }

.conversation-content { display: grid; height: 100%; grid-template-rows: auto minmax(0, 1fr) auto; }
.conversation-header {
  display: flex;
  min-height: 5rem;
  align-items: center;
  gap: 0.8rem;
  padding: 0 1.5rem;
  border-bottom: 1px solid var(--line);
  background: rgba(13, 14, 19, .84);
  backdrop-filter: blur(18px);
}
.conversation-header > .chat-avatar { width: 2.55rem; height: 2.55rem; border-radius: .75rem; }
.conversation-header[data-type="user"] > .chat-avatar { border-radius: 50%; }
.conversation-title { min-width: 0; flex: 1; }
.conversation-title h2 { overflow: hidden; margin: 0; font-size: 1rem; text-overflow: ellipsis; white-space: nowrap; }
.conversation-title span { display: block; margin-top: .2rem; color: var(--muted); font-size: .75rem; }
.back-button { display: none; }

.message-scroller { overflow-y: auto; overscroll-behavior: contain; padding: 1.2rem max(1.2rem, calc((100% - 54rem) / 2)); }
.message-list { display: flex; min-height: 100%; flex-direction: column; justify-content: flex-end; gap: .32rem; }
.load-older-button { display: block; margin: 0 auto 1.5rem; padding: .5rem .8rem; border: 1px solid var(--line); border-radius: 99px; background: var(--panel); color: var(--muted-strong); cursor: pointer; font-size: .78rem; }
.load-older-button:hover { border-color: var(--line-strong); color: var(--text); }

.message-row { display: flex; flex-direction: column; align-items: flex-start; margin-top: .6rem; }
.message-row.mine { align-items: flex-end; }
.message-author { margin: 0 0 .28rem .72rem; color: var(--muted); font-size: .72rem; }
.message-row.mine .message-author { margin-right: .72rem; }
.message-bubble { max-width: min(75%, 46rem); padding: .66rem .85rem; border: 1px solid var(--line); border-radius: .35rem 1rem 1rem 1rem; background: var(--panel-raised); line-height: 1.5; overflow-wrap: anywhere; white-space: pre-wrap; }
.message-row.mine .message-bubble { border-color: rgba(140,124,255,.28); border-radius: 1rem .35rem 1rem 1rem; background: linear-gradient(140deg, rgba(105,87,212,.85), rgba(87,71,178,.9)); }
.message-bubble a { color: #a9e8f0; text-decoration: underline; text-decoration-color: rgba(169,232,240,.4); text-underline-offset: 2px; }
.message-time { margin: .25rem .7rem 0; color: #6f7180; font-size: .68rem; }
.unknown-message .message-bubble { border-style: dashed; }
.message-type { display: block; margin-bottom: .35rem; color: var(--accent-bright); font-family: ui-monospace, monospace; font-size: .72rem; }
.message-json { margin: 0; font-family: ui-monospace, monospace; font-size: .78rem; white-space: pre-wrap; }
.system-message { align-self: center; margin: .65rem auto; padding: .4rem .75rem; border: 1px solid var(--line); border-radius: 99px; color: var(--muted); font-size: .75rem; text-align: center; }
.date-separator { align-self: center; margin: 1.4rem auto .75rem; color: #777987; font-size: .72rem; }

.composer {
  display: flex;
  gap: .75rem;
  align-items: flex-end;
  padding: 1rem max(1.2rem, calc((100% - 54rem) / 2)) max(1rem, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: rgba(13, 14, 19, .9);
  backdrop-filter: blur(18px);
}
.composer textarea { field-sizing: content; max-height: 10rem; min-height: 2.8rem; resize: none; padding: .72rem .9rem; line-height: 1.35; }
.send-button { display: grid; width: 2.8rem; height: 2.8rem; flex: 0 0 auto; place-items: center; border: 0; border-radius: .8rem; background: var(--accent); color: #fff; cursor: pointer; }
.send-button:hover { background: #9b8cff; }
.send-button:disabled { cursor: wait; opacity: .5; }

.modal { width: min(92vw, 29rem); padding: 0; border: 1px solid var(--line-strong); border-radius: 1.2rem; background: #14151c; color: var(--text); box-shadow: var(--shadow); }
.modal::backdrop { background: rgba(2,3,7,.72); backdrop-filter: blur(5px); }
.modal-card { gap: 1rem; padding: 1.4rem; }
.modal-card header { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; margin-bottom: .3rem; }
.modal-card h2 { margin: .35rem 0 0; font-size: 1.25rem; }
.modal-card header .close-dialog { margin-top: -.25rem; font-size: 1.6rem; }
.modal-card footer { display: flex; justify-content: flex-end; gap: .6rem; margin-top: .2rem; }
.dialog-help { margin: -.25rem 0 0; color: var(--muted); font-size: .8rem; line-height: 1.5; }
.compact-card { gap: 1.15rem; }
.chat-facts { display: grid; gap: .7rem; margin: 0; }
.chat-facts div { display: flex; justify-content: space-between; gap: 1rem; }
.chat-facts dt { color: var(--muted); }
.chat-facts dd { margin: 0; font-family: ui-monospace, monospace; }
.stacked-actions { display: grid; gap: .6rem; }
.account-identity { display: flex; align-items: center; gap: .9rem; }
.profile-avatar { width: 3.2rem; height: 3.2rem; border-radius: 50%; }
.account-identity > div:last-child { display: flex; flex-direction: column; gap: .2rem; }
.account-identity span { color: var(--muted); font-family: ui-monospace, monospace; font-size: .78rem; }
.account-profile { margin: 0; color: var(--muted-strong); font-size: .88rem; line-height: 1.55; white-space: pre-wrap; }

.toast-region { position: fixed; z-index: 20; right: 1rem; bottom: 1rem; display: grid; gap: .6rem; width: min(24rem, calc(100vw - 2rem)); }
.toast { padding: .8rem 1rem; border: 1px solid var(--line-strong); border-radius: .8rem; background: #1a1b23; box-shadow: 0 12px 36px rgba(0,0,0,.35); color: var(--muted-strong); font-size: .85rem; animation: toast-in .18s ease-out; }
.toast.error { border-color: rgba(255,124,145,.4); color: #ffc0ca; }
@keyframes toast-in { from { transform: translateY(.5rem); opacity: 0; } }

@media (max-width: 760px) {
  .login-card { padding: 2rem 1.4rem; }
  .app-view { display: block; }
  .sidebar, .conversation { position: absolute; inset: 0; }
  .sidebar { border-right: 0; }
  .conversation { display: none; }
  body.chat-open .sidebar { display: none; }
  body.chat-open .conversation { display: block; }
  .back-button { display: inline-grid; }
  .conversation-header { padding: 0 .8rem; }
  .conversation-menu { margin-left: -.25rem; }
  .message-scroller { padding: 1rem .8rem; }
  .message-bubble { max-width: 88%; }
  .composer { padding: .8rem .8rem max(.8rem, env(safe-area-inset-bottom)); }
}

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