:root {
  --bg: #f1f5fb;
  --surface: #ffffff;
  --text: #15263f;
  --muted: #58677d;
  --accent: #0b3a78;
  --accent-strong: #072a59;
  --accent-pop: #ff4d0e;
  --bubble-user: #0b3a78;
  --bubble-jacob: #eaf1fb;
  --radius: 14px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
  font-size: clamp(17px, 1.45vw, 20px);
  color: var(--text);
  background: radial-gradient(circle at 15% 10%, #ffffff 0%, #edf4ff 50%, #dde8f8 100%);
}

.app-shell {
  height: 100dvh;
  max-width: 1120px;
  margin: 0 auto;
  display: grid;
  grid-template-rows: auto 1fr auto;
  overflow: hidden;
}

.topbar {
  padding: 18px 20px;
  border-bottom: 1px solid #c4d3ea;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);
  position: sticky;
  top: 0;
  z-index: 3;
}

.topbar-title-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.brand-lockup {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-logo {
  width: 58px;
  height: 58px;
  flex: 0 0 auto;
}

.brand-text {
  display: grid;
  gap: 1px;
}

.brand-name {
  margin: 0;
  color: var(--accent);
  font-size: 0.83rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.topbar h1 {
  margin: 0;
  font-size: clamp(1.35rem, 2vw, 2rem);
  color: var(--accent);
}

.chat-label {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.status {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.messages {
  padding: 20px;
  overflow-y: auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.bubble {
  max-width: min(88%, 900px);
  border-radius: var(--radius);
  padding: 13px 15px;
  line-height: 1.4;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.06);
}

.bubble h1,
.bubble h2,
.bubble h3,
.bubble h4,
.bubble h5,
.bubble h6,
.bubble p {
  margin: 0;
}

.bubble h1,
.bubble h2,
.bubble h3,
.bubble h4,
.bubble h5,
.bubble h6 {
  margin-top: 10px;
  margin-bottom: 6px;
  line-height: 1.25;
}

.bubble p + p,
.bubble p + ul,
.bubble p + ol,
.bubble p + pre,
.bubble p + table,
.bubble ul + p,
.bubble ol + p,
.bubble pre + p,
.bubble blockquote + p,
.bubble table + p,
.bubble blockquote + table,
.bubble table + blockquote {
  margin-top: 10px;
}

.bubble ul,
.bubble ol {
  margin: 6px 0 0;
  padding-left: 22px;
}

.bubble li + li {
  margin-top: 4px;
}

.bubble pre {
  margin: 10px 0 0;
  background: rgba(11, 58, 120, 0.09);
  border: 1px solid #c7d8f1;
  border-radius: 10px;
  padding: 10px 12px;
  overflow-x: auto;
}

.bubble code {
  font-family: Consolas, "Courier New", monospace;
  font-size: 0.92em;
}

.bubble :not(pre) > code {
  background: rgba(11, 58, 120, 0.12);
  border-radius: 6px;
  padding: 0.08em 0.4em;
}

.bubble blockquote {
  margin: 10px 0 0;
  padding: 0 0 0 10px;
  border-left: 3px solid #9fb7da;
  color: #374b6a;
}

.bubble a {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.bubble hr {
  border: 0;
  border-top: 1px solid #c7d8f1;
  margin: 12px 0;
}

.bubble img {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  margin-top: 10px;
}

.bubble input[type="checkbox"] {
  pointer-events: none;
  margin-right: 8px;
}

.bubble table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 10px;
  font-size: 0.95em;
  background: rgba(255, 255, 255, 0.55);
}

.bubble th,
.bubble td {
  border: 1px solid #c7d8f1;
  padding: 6px 8px;
  vertical-align: top;
  text-align: left;
}

.bubble th {
  background: rgba(11, 58, 120, 0.12);
  font-weight: 700;
}

.user {
  align-self: flex-end;
  color: #fff;
  background: var(--bubble-user);
}

.jacob {
  align-self: flex-start;
  background: var(--bubble-jacob);
}

.system {
  align-self: center;
  color: var(--muted);
  background: transparent;
  box-shadow: none;
  font-size: 0.88rem;
}

.composer {
  display: flex;
  gap: 12px;
  align-items: flex-end;
  padding: 16px 20px;
  border-top: 1px solid #c4d3ea;
  background: rgba(255, 255, 255, 0.95);
  position: sticky;
  bottom: 0;
  z-index: 3;
}

textarea {
  resize: none;
  border: 1px solid #b8c9e4;
  border-radius: 10px;
  padding: 12px;
  font: inherit;
  min-height: 56px;
  width: 100%;
}

#messageInput {
  flex: 1;
}

.composer-controls {
  display: grid;
  grid-template-columns: auto;
  gap: 8px;
}

.composer-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

button {
  border: none;
  border-radius: 10px;
  min-height: 44px;
  padding: 0 18px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

button:active {
  background: var(--accent-strong);
}

#sendBtn {
  min-width: 140px;
}

.icon-btn {
  min-width: 48px;
  padding: 0;
  font-size: 1.1rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.settings-form {
  display: grid;
  gap: 12px;
  min-width: min(92vw, 420px);
}

.settings-form label {
  display: grid;
  gap: 6px;
  font-size: 0.95rem;
}

.settings-form input {
  border: 1px solid #b8c9e4;
  border-radius: 8px;
  padding: 10px;
  font: inherit;
}

.last-sync {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.settings-form menu {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0;
  padding: 0;
}

#exportChatsBtn {
  background: #2f4f84;
}

.chats-form {
  display: grid;
  gap: 12px;
  min-width: min(92vw, 760px);
}

.chats-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.chat-list {
  display: grid;
  gap: 10px;
  max-height: 56dvh;
  overflow-y: auto;
}

.chat-item {
  border: 1px solid #c9d8ee;
  background: #fff;
  color: var(--text);
  border-radius: 10px;
  padding: 10px;
}

.chat-item.active {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.chat-item-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.chat-item-main {
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  text-align: left;
  padding: 0;
  min-height: 0;
}

.chat-item-title {
  font-size: 1.05rem;
  font-weight: 700;
  line-height: 1.2;
}

.chat-item-meta {
  margin-top: 4px;
  font-size: 0.84rem;
  color: var(--muted);
}

.chat-item-preview {
  margin-top: 4px;
  font-size: 0.9rem;
  color: #364640;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-item-actions {
  display: flex;
  gap: 8px;
}

.chat-item-action {
  min-height: 36px;
  padding: 0 12px;
  border-radius: 8px;
  border: 1px solid #b8c9e4;
  background: #edf4ff;
  color: var(--text);
}

.chat-item-action.delete {
  border-color: #f2b4a0;
  color: #9c2b00;
}

.visually-hidden {
  position: absolute;
  left: -9999px;
}

@media (max-width: 840px) {
  .app-shell {
    max-width: 100%;
  }

  .composer {
    flex-direction: column;
    align-items: stretch;
  }

  .composer-controls {
    grid-template-columns: 1fr;
  }

  #sendBtn {
    width: 100%;
  }

  .composer-actions {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 520px) {
  body {
    font-size: 17px;
  }

  .topbar {
    padding: 14px 14px;
  }

  .topbar-title-row {
    align-items: center;
  }

  .brand-logo {
    width: 46px;
    height: 46px;
  }

  .brand-name {
    font-size: 0.75rem;
  }

  .messages {
    padding: 14px;
  }

  .composer {
    padding: 12px 14px;
  }

  .chat-item-row {
    align-items: flex-start;
    flex-direction: column;
  }

  .chat-item-actions {
    width: 100%;
  }

  .chat-item-action {
    flex: 1;
  }
}
