:root {
  --bg: #f4f6f9;
  --bg-elev: #ffffff;
  --bg-soft: #eef1f6;
  --line: #d8dee8;
  --text: #1a2332;
  --muted: #5c6b7f;
  --brand: #9a7b1a;
  --brand-2: #b8921f;
  --ok: #1a7f4b;
  --warn: #b8860b;
  --err: #c23b44;
  --info: #2a6fad;
  --radius: 12px;
  --font: "DM Sans", system-ui, -apple-system, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, monospace;
  --shadow: 0 1px 2px rgba(26, 35, 50, 0.04), 0 8px 24px rgba(26, 35, 50, 0.06);
  --max: 1200px;
  --sidebar: #ffffff;
  --input-bg: #ffffff;
}
* { box-sizing: border-box; }
html { color-scheme: light; }
body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--brand-2); text-decoration: none; }
a:hover { color: #8a6d12; text-decoration: underline; }
h1,h2,h3 { letter-spacing: -0.02em; margin: 0 0 .5rem; color: var(--text); }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; }
p { color: var(--muted); margin: 0 0 1rem; }

.app-shell { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }
@media (max-width: 900px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar {
    position: sticky; top: 0; z-index: 20;
    padding: 0.75rem 0.85rem;
  }
  .main { padding: 1rem 0.9rem 2.5rem; }
  .topbar { flex-direction: column; align-items: stretch; gap: 0.55rem; }
  .topbar .btn-row { width: 100%; }
  .topbar .btn-row .btn { flex: 1; }
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
  .row-2 { grid-template-columns: 1fr; }
  input, select, textarea { font-size: 16px; min-height: 44px; }
  .btn { min-height: 44px; }
  .nav a { min-height: 44px; padding: 0.7rem 0.75rem; }
  .mobile-nav-toggle { min-height: 44px; min-width: 72px; }
  .table-wrap { -webkit-overflow-scrolling: touch; margin: 0 -0.25rem; }
  .line-items-editor .item-row { grid-template-columns: 1fr 1fr; }
  .chat-log { max-height: 50vh; }
  .chat-form { flex-direction: column; align-items: stretch; }
  .chat-form .btn { width: 100%; }
}

.sidebar {
  background: var(--sidebar);
  border-right: 1px solid var(--line);
  padding: 1.1rem .85rem;
  box-shadow: 1px 0 0 rgba(26, 35, 50, 0.02);
}
.brand {
  display: flex; align-items: center; gap: .7rem;
  padding: .35rem .5rem 1.1rem;
  border-bottom: 1px solid var(--line);
  margin-bottom: .85rem;
  text-decoration: none; color: var(--text);
}
.brand-logo-img {
  height: 64px; width: auto; max-width: 220px;
  object-fit: contain; display: block;
}
.brand-text { min-width: 0; }
.brand strong { display: block; font-size: .9rem; line-height: 1.2; }
.brand span { display: block; color: var(--muted); font-size: .72rem; }
@media (max-width: 900px) {
  .brand-logo-img { height: 36px; }
}

.nav a {
  display: flex; align-items: center; gap: .55rem;
  padding: .55rem .7rem; border-radius: 9px;
  color: var(--muted); text-decoration: none; margin-bottom: 2px;
  font-weight: 500;
}
.nav a:hover { background: var(--bg-soft); color: var(--text); text-decoration: none; }
.nav a.active {
  background: #f7f1de;
  color: var(--text);
  border: 1px solid #e8d9a8;
}

.main { padding: 1.25rem 1.4rem 3rem; max-width: 1280px; }
.topbar {
  display: flex; flex-wrap: wrap; gap: .75rem; align-items: center;
  justify-content: space-between; margin-bottom: 1.15rem;
}
.topbar .meta { color: var(--muted); font-size: .85rem; }

.grid { display: grid; gap: 1rem; }
.grid-2 { grid-template-columns: repeat(2, minmax(0,1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0,1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0,1fr)); }
@media (max-width: 1000px) {
  .grid-4 { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .grid-3, .grid-2 { grid-template-columns: 1fr; }
}

.card {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
  box-shadow: var(--shadow);
}
.card h2, .card h3 { margin-bottom: .65rem; }
.stat .label { color: var(--muted); font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; }
.stat .value { font-size: 1.55rem; font-weight: 700; margin-top: .2rem; color: var(--text); }
.stat .value.gold { color: var(--brand-2); }

.btn, button.btn, input[type=submit].btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  border: 1px solid transparent; border-radius: 10px;
  padding: .55rem .95rem; font-weight: 600; font-size: .9rem;
  cursor: pointer; text-decoration: none; font-family: inherit;
  background: linear-gradient(180deg, #e0bc3a, #c9a227);
  color: #1a1405;
}
.btn:hover { filter: brightness(1.03); text-decoration: none; }
.btn.secondary {
  background: var(--bg-elev); color: var(--text); border-color: var(--line);
}
.btn.secondary:hover { background: var(--bg-soft); }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--line); }
.btn.danger { background: #fde8ea; color: #8b1e26; border-color: #f0c0c5; }
.btn.sm { padding: .35rem .65rem; font-size: .8rem; }
.btn-row { display: flex; flex-wrap: wrap; gap: .5rem; }

label { display: block; font-size: .8rem; color: var(--muted); margin-bottom: .28rem; font-weight: 500; }
input, select, textarea {
  width: 100%; padding: .55rem .7rem; border-radius: 9px;
  border: 1px solid var(--line); background: var(--input-bg); color: var(--text);
  font: inherit;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid rgba(201, 162, 39, 0.45); outline-offset: 1px;
  border-color: #c9a227;
}
textarea { min-height: 90px; resize: vertical; }
.field { margin-bottom: .85rem; }
.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
@media (max-width: 700px) { .row-2 { grid-template-columns: 1fr; } }

table { width: 100%; border-collapse: collapse; font-size: .92rem; }
th, td { text-align: left; padding: .55rem .45rem; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
tr:hover td { background: #f8fafc; }
.table-wrap { overflow-x: auto; }

.badge {
  display: inline-block; padding: .15rem .5rem; border-radius: 999px;
  font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .03em;
  background: #e8edf3; color: #3d4d61;
}
.badge.new, .badge.draft { background: #e8edf3; color: #3d4d61; }
.badge.sent, .badge.contacted, .badge.scheduled { background: #e3f0fa; color: #1a5a8a; }
.badge.qualified, .badge.in_progress, .badge.partial { background: #f7f0d8; color: #7a6210; }
.badge.won, .badge.accepted, .badge.paid, .badge.completed { background: #dff3e8; color: #14663c; }
.badge.lost, .badge.declined, .badge.cancelled, .badge.void, .badge.spam { background: #fde8ea; color: #8b1e26; }
.badge.overdue, .badge.on_hold { background: #fff0db; color: #8a5508; }

.flash-wrap { margin-bottom: 1rem; }
.flash {
  padding: .7rem .9rem; border-radius: 10px; margin-bottom: .4rem;
  border: 1px solid var(--line);
}
.flash.ok { background: #e8f7ef; border-color: #b7e4c7; color: #14663c; }
.flash.error { background: #fde8ea; border-color: #f0c0c5; color: #8b1e26; }

.muted { color: var(--muted); }
.mono { font-family: var(--mono); font-size: .85rem; }
.right { text-align: right; }
.split { display: flex; justify-content: space-between; gap: 1rem; align-items: flex-start; flex-wrap: wrap; }
.stack > * + * { margin-top: .75rem; }
.hr { border: 0; border-top: 1px solid var(--line); margin: 1rem 0; }
.filters { display: flex; flex-wrap: wrap; gap: .5rem; align-items: end; margin-bottom: 1rem; }
.filters .field { margin: 0; min-width: 140px; }
.link-box {
  background: var(--bg-soft); border: 1px dashed #b8c2d0; border-radius: 10px;
  padding: .7rem .85rem; word-break: break-all; font-family: var(--mono); font-size: .82rem;
  color: var(--text);
}
.empty { color: var(--muted); padding: 1rem 0; }

/* Auth / public */
.auth-page, .public-page {
  min-height: 100vh; display: grid; place-items: center; padding: 1.5rem;
  background: linear-gradient(180deg, #eef2f7 0%, #f8f9fb 40%, #ffffff 100%);
}
.auth-card, .public-card {
  width: min(480px, 100%);
  background: var(--bg-elev); border: 1px solid var(--line);
  border-radius: 16px; padding: 1.5rem; box-shadow: var(--shadow);
}
.public-card { width: min(720px, 100%); }
.public-card table { margin: 1rem 0; }
.totals { margin-top: 1rem; max-width: 280px; margin-left: auto; }
.totals .line { display: flex; justify-content: space-between; padding: .25rem 0; color: var(--muted); }
.totals .grand {
  font-weight: 800; font-size: 1.15rem; color: var(--brand-2);
  border-top: 1px solid var(--line); margin-top: .4rem; padding-top: .5rem;
}

.line-items-editor .item-row {
  display: grid; grid-template-columns: 2fr .7fr .9fr auto; gap: .45rem; margin-bottom: .45rem;
}
@media (max-width: 700px) {
  .line-items-editor .item-row { grid-template-columns: 1fr 1fr; }
}

.mobile-nav-toggle { display: none; }
@media (max-width: 900px) {
  .mobile-nav-toggle { display: inline-flex; margin-bottom: .5rem; }
  .nav { display: none; }
  .sidebar.open .nav { display: block; }
}

/* Chat (assistant) — light */
.chat-log { flex: 1; overflow-y: auto; max-height: 420px; padding-right: .25rem; margin-bottom: .75rem; }
.chat-bubble {
  padding: .7rem .85rem; border-radius: 12px; margin-bottom: .55rem;
  border: 1px solid var(--line); white-space: pre-wrap; line-height: 1.45;
  color: var(--text);
}
.chat-bubble.user {
  background: #eef6ff; border-color: #c5daf0; margin-left: 1.5rem;
}
.chat-bubble.assistant {
  background: var(--bg-soft); margin-right: 1.5rem;
}
.chat-form { display: flex; gap: .5rem; align-items: flex-end; }
.chat-form textarea { flex: 1; margin: 0; }

/* Spam detector UI */
.spam-banner {
  border-radius: 12px;
  padding: .9rem 1.1rem;
  margin-bottom: 1rem;
  border: 1px solid var(--line);
  font-size: .95rem;
}
.spam-banner.high {
  background: #fde8ea;
  border-color: #e8a0a6;
  color: #7a1520;
}
.spam-banner.medium {
  background: #fff6e0;
  border-color: #e8d090;
  color: #6b4e08;
}
.spam-banner.low {
  background: #e8f7ef;
  border-color: #b7e4c7;
  color: #14663c;
}
.spam-flag {
  color: #b42318;
  font-size: .72rem;
  font-weight: 800;
  letter-spacing: .04em;
  margin-top: .2rem;
}
.badge.spam-high { background: #fde8ea; color: #8b1e26; }
.badge.spam-medium { background: #fff0db; color: #8a5508; }
.badge.spam-low { background: #dff3e8; color: #14663c; }
tr.row-spam td { background: #fff5f5; }
tr.row-spam-med td { background: #fffbf0; }
.badge.spam { background: #fde8ea; color: #8b1e26; }
