:root {
  --bg: #f8f3e8;
  --surface: #fffdf8;
  --surface-warm: #efe5d1;
  --ink: #243746;
  --ink-muted: #526473;
  --blue: #1e5f8a;
  --blue-dark: #17496b;
  --gold: #f4bf2a;
  --gold-soft: #fdf3d0;
  --terracotta: #b57409;
  --green: #1e7a4f;
  --green-soft: #e3f5ec;
  --red: #b8332a;
  --red-soft: #fdecea;
  --border: #e2d5bc;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(30, 95, 138, 0.08);
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 16px;
  line-height: 1.55;
}

.hidden {
  display: none !important;
}

h1, h2, h3 {
  font-family: 'Fraunces', Georgia, serif;
  margin: 0;
  color: var(--ink);
}

/* ---------- Buttons & Inputs ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 16px;
  border-radius: 10px;
  border: 1px solid transparent;
  font-family: inherit;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s, opacity 0.15s;
}
.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}
.btn-primary {
  background: var(--blue);
  color: #fff;
}
.btn-primary:hover:not(:disabled) {
  background: var(--blue-dark);
}
.btn-secondary {
  background: var(--surface);
  color: var(--ink);
  border-color: var(--border);
}
.btn-secondary:hover:not(:disabled) {
  background: var(--surface-warm);
}
.btn-gold {
  background: var(--gold);
  color: var(--ink);
}
.btn-gold:hover:not(:disabled) {
  background: #e3ad18;
}
.btn-danger {
  background: var(--red-soft);
  color: var(--red);
  border-color: #f3c4c0;
}
.btn-danger:hover:not(:disabled) {
  background: #fadad6;
}
.btn-full {
  width: 100%;
}
.btn-sm {
  padding: 7px 12px;
  font-size: 13.5px;
  border-radius: 8px;
}
.btn-chip {
  background: var(--gold-soft);
  color: var(--ink);
  border: 1px solid #ebd284;
  border-radius: 999px;
  padding: 4px 11px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.btn-chip:hover {
  background: var(--gold);
}
.icon-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 38px;
  height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  cursor: pointer;
  color: var(--ink);
}
.icon-btn:hover {
  background: var(--surface-warm);
}
.link-btn {
  background: none;
  border: none;
  color: var(--blue);
  font-weight: 600;
  cursor: pointer;
  font-size: 14px;
}

/* ---------- Login Screen ---------- */
.login-screen {
  min-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: radial-gradient(circle at top right, #fdf3d0, var(--bg) 55%);
}
.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 32px;
  box-shadow: var(--shadow);
}
.brand-badge {
  display: inline-block;
  background: var(--gold-soft);
  color: var(--terracotta);
  border: 1px solid #ebd284;
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 700;
  margin-bottom: 12px;
}
.login-card h1 {
  font-size: 28px;
  margin-bottom: 8px;
}
.login-sub {
  color: var(--ink-muted);
  margin: 0 0 22px;
  font-size: 15px;
}
.field-label {
  display: block;
  font-weight: 600;
  font-size: 14px;
  margin-bottom: 8px;
}
.user-picker {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 18px;
}
.user-option {
  padding: 12px;
  border-radius: 12px;
  border: 2px solid var(--border);
  background: var(--bg);
  font-family: inherit;
  font-size: 16px;
  font-weight: 600;
  color: var(--ink);
  cursor: pointer;
  transition: all 0.15s;
}
.user-option.selected {
  border-color: var(--blue);
  background: #eaf3f9;
  color: var(--blue-dark);
}
#pin-input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #fff;
  font-size: 16px;
  font-family: inherit;
  margin-bottom: 14px;
}
#pin-input:focus {
  outline: 2px solid var(--blue);
  border-color: transparent;
}
.error-text {
  color: var(--red);
  background: var(--red-soft);
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 14px;
  margin-bottom: 14px;
}

/* ---------- App Shell ---------- */
.app-shell {
  display: flex;
  height: 100%;
  overflow: hidden;
}

.sidebar {
  width: 290px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 20;
}
.sidebar-header {
  padding: 18px 16px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
}
.logo-dot {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  background: var(--blue);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 18px;
}
.brand-title {
  font-family: 'Fraunces', Georgia, serif;
  font-weight: 700;
  font-size: 18px;
  line-height: 1.1;
}
.brand-sub {
  font-size: 12.5px;
  color: var(--ink-muted);
}

.status-bar {
  display: flex;
  gap: 8px;
  padding: 0 16px 14px;
  border-bottom: 1px solid var(--border);
}
.status-pill {
  flex: 1;
  text-align: center;
  font-size: 12px;
  font-weight: 600;
  padding: 4px 8px;
  border-radius: 999px;
  background: var(--surface-warm);
  color: var(--ink-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.status-pill.ok {
  background: var(--green-soft);
  color: var(--green);
}
.status-pill.warn {
  background: var(--gold-soft);
  color: var(--terracotta);
}

.sidebar-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 8px;
  font-size: 12.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--ink-muted);
}

.thread-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 10px 12px;
}
.thread-item {
  width: 100%;
  text-align: left;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 4px;
  cursor: pointer;
  font-family: inherit;
  color: var(--ink);
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.thread-item:hover {
  background: var(--bg);
}
.thread-item.active {
  background: #eaf3f9;
  border-color: #c2dced;
}
.thread-item-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
}
.thread-title {
  font-weight: 600;
  font-size: 14.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.pending-dot {
  background: var(--gold);
  color: var(--ink);
  font-size: 11px;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 999px;
  flex-shrink: 0;
}
.thread-preview {
  font-size: 12.5px;
  color: var(--ink-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-footer {
  padding: 12px 14px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.user-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 2px 0;
}
.user-badge {
  font-size: 14px;
  font-weight: 600;
}

/* ---------- Chat Main ---------- */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  min-width: 0;
}
.chat-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.chat-header-left {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}
.chat-header h2 {
  font-size: 20px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-subtitle {
  font-size: 13px;
  color: var(--ink-muted);
}
.presence-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 3px;
}
.presence-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-muted);
  background: var(--bg);
  border: 1px solid var(--border);
  padding: 2px 9px;
  border-radius: 999px;
}
.presence-chip.online {
  color: var(--green);
  background: var(--green-soft);
  border-color: #b7e4cc;
}
.presence-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #b5b0a4;
}
.presence-chip.online .presence-dot {
  background: #1ea861;
  box-shadow: 0 0 0 2px rgba(30, 168, 97, 0.2);
}

.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.msg-row {
  display: flex;
  flex-direction: column;
  max-width: 780px;
  width: 100%;
}
.msg-row.mine {
  align-self: flex-end;
  align-items: flex-end;
}
.msg-row.other, .msg-row.assistant {
  align-self: flex-start;
  align-items: flex-start;
}
.msg-row.system {
  align-self: center;
  align-items: center;
  max-width: 640px;
}

.msg-meta {
  font-size: 12.5px;
  color: var(--ink-muted);
  margin-bottom: 4px;
  padding: 0 4px;
  font-weight: 600;
}
.msg-bubble {
  padding: 12px 16px;
  border-radius: 16px;
  max-width: 100%;
  word-wrap: break-word;
  box-shadow: 0 2px 8px rgba(36, 55, 70, 0.04);
}
.msg-row.mine .msg-bubble {
  background: var(--blue);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.msg-row.other .msg-bubble {
  background: var(--gold-soft);
  color: var(--ink);
  border: 1px solid #ebd284;
  border-bottom-left-radius: 4px;
}
.msg-row.assistant .msg-bubble {
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.msg-row.system .msg-bubble {
  background: var(--surface-warm);
  color: var(--ink);
  font-size: 14px;
  padding: 8px 14px;
  border-radius: 999px;
}
.msg-bubble a {
  color: inherit;
  text-decoration: underline;
}
.msg-row.assistant .msg-bubble a {
  color: var(--blue);
}
.msg-bubble p {
  margin: 0 0 8px;
}
.msg-bubble p:last-child {
  margin-bottom: 0;
}
.msg-bubble ul {
  margin: 6px 0;
  padding-left: 20px;
}
.msg-bubble code {
  background: rgba(0, 0, 0, 0.07);
  padding: 2px 5px;
  border-radius: 5px;
  font-size: 0.9em;
}
.msg-row.mine .msg-bubble code {
  background: rgba(255, 255, 255, 0.2);
}
.md-table-wrap {
  overflow-x: auto;
  margin: 8px 0;
}
.md-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
.md-table th, .md-table td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}
.md-table th {
  background: var(--bg);
  font-weight: 700;
}

/* Attachments in message */
.msg-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}
.att-img {
  max-width: 240px;
  max-height: 180px;
  border-radius: 10px;
  object-fit: cover;
  border: 1px solid rgba(0, 0, 0, 0.1);
}
.att-file {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.85);
  color: var(--ink);
  font-size: 13px;
  font-weight: 600;
  border: 1px solid var(--border);
  text-decoration: none;
}

/* Proposal Card */
.proposal-card {
  margin-top: 10px;
  width: 100%;
  max-width: 620px;
  background: var(--surface);
  border: 2px solid var(--blue);
  border-radius: 14px;
  padding: 16px;
  box-shadow: var(--shadow);
}
.proposal-card.approved {
  border-color: var(--green);
}
.proposal-card.rejected {
  border-color: var(--border);
  opacity: 0.8;
}
.proposal-card.failed {
  border-color: var(--red);
}
.proposal-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}
.proposal-badge {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--blue);
  background: #eaf3f9;
  padding: 3px 9px;
  border-radius: 999px;
}
.proposal-status {
  font-size: 12.5px;
  font-weight: 700;
}
.proposal-status.pending { color: var(--terracotta); }
.proposal-status.approved { color: var(--green); }
.proposal-status.rejected { color: var(--ink-muted); }
.proposal-status.failed { color: var(--red); }

.proposal-title {
  font-family: 'Fraunces', Georgia, serif;
  font-size: 19px;
  margin: 4px 0 6px;
}
.proposal-summary {
  font-size: 14.5px;
  color: var(--ink-muted);
  margin: 0 0 12px;
}
.proposal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.proposal-result {
  margin-top: 10px;
  font-size: 14px;
  padding: 8px 12px;
  border-radius: 8px;
  background: var(--green-soft);
  color: var(--green);
}
.proposal-result.error {
  background: var(--red-soft);
  color: var(--red);
}

/* AI Status Bar */
.ai-status-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 20px;
  font-size: 14px;
  color: var(--blue-dark);
  background: #eaf3f9;
  border-top: 1px solid #c2dced;
}
.spinner {
  width: 16px;
  height: 16px;
  border: 2.5px solid #b5d4e8;
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Composer */
.composer-wrap {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 12px 16px;
}
.pending-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}
.pending-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 13px;
  font-weight: 600;
}
.pending-chip button {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--ink-muted);
  font-weight: 700;
}
.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
}
#message-input {
  flex: 1;
  resize: none;
  max-height: 140px;
  padding: 10px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg);
  font-family: inherit;
  font-size: 16px;
  line-height: 1.45;
  color: var(--ink);
}
#message-input:focus {
  outline: 2px solid var(--blue);
  background: #fff;
  border-color: transparent;
}
.ai-toggle {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  font-size: 13.5px;
  font-weight: 700;
  cursor: pointer;
  user-select: none;
  height: 42px;
}
.attach-btn {
  height: 42px;
  width: 42px;
  flex-shrink: 0;
}
.send-btn {
  height: 42px;
  padding: 0 18px;
  flex-shrink: 0;
}

/* Drop Overlay */
.drop-overlay {
  position: absolute;
  inset: 0;
  background: rgba(30, 95, 138, 0.14);
  backdrop-filter: blur(2px);
  display: grid;
  place-items: center;
  z-index: 30;
  pointer-events: none;
}
.drop-box {
  background: var(--surface);
  border: 2px dashed var(--blue);
  padding: 28px 36px;
  border-radius: 18px;
  font-size: 18px;
  font-weight: 700;
  color: var(--blue-dark);
  box-shadow: var(--shadow);
}

/* Modals */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 20px;
}
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(23, 73, 107, 0.45);
}
.modal-card {
  position: relative;
  width: 100%;
  max-width: 780px;
  max-height: 85vh;
  background: var(--surface);
  border-radius: 18px;
  border: 1px solid var(--border);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.modal-eyebrow {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--terracotta);
  letter-spacing: 0.05em;
}
.modal-body {
  padding: 20px;
  overflow-y: auto;
}
.wp-preview-prose img {
  max-width: 100%;
  border-radius: 10px;
  margin: 12px 0;
}
.wp-tabs {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.wp-tab {
  padding: 6px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-weight: 600;
  cursor: pointer;
}
.wp-tab.active {
  background: var(--blue);
  color: #fff;
  border-color: var(--blue);
}
#wp-search {
  margin-left: auto;
  padding: 6px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-family: inherit;
}
.wp-list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  margin-bottom: 8px;
  background: #fff;
}
.wp-item-title {
  font-weight: 700;
}
.wp-item-meta {
  font-size: 12.5px;
  color: var(--ink-muted);
}

/* Mobile Responsive */
.mobile-only {
  display: none;
}
@media (max-width: 780px) {
  .mobile-only {
    display: inline-flex;
  }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 10px 0 30px rgba(0, 0, 0, 0.18);
  }
  .sidebar.open {
    transform: translateX(0);
  }
  .messages-container {
    padding: 14px;
  }
}
