/* Three themes, picked in the header: light (milky), dark (black), blue (dark blue).
   With nothing picked yet, follow the computer: light or black. */
:root {
  --bg: #f7f2e7;
  --panel: #fffcf5;
  --raised: #ffffff;
  --text: #2b261d;
  --muted: #7d7362;
  --line: #e6dcc8;
  --accent: #1f7a4d;
  --accent-text: #ffffff;
  --warn-bg: #fbecc8;
  --warn-text: #6e4a00;
  --chip: #efe7d6;
  --shadow: 0 10px 40px rgb(60 45 20 / .14);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #000000;
    --panel: #0b0b0b;
    --raised: #141414;
    --text: #ededed;
    --muted: #8c8c8c;
    --line: #232323;
    --accent: #3fb37a;
    --accent-text: #04140b;
    --warn-bg: #2a1f07;
    --warn-text: #f3cf8a;
    --chip: #1a1a1a;
    --shadow: 0 10px 40px rgb(0 0 0 / .6);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #000000;
  --panel: #0b0b0b;
  --raised: #141414;
  --text: #ededed;
  --muted: #8c8c8c;
  --line: #232323;
  --accent: #3fb37a;
  --accent-text: #04140b;
  --warn-bg: #2a1f07;
  --warn-text: #f3cf8a;
  --chip: #1a1a1a;
  --shadow: 0 10px 40px rgb(0 0 0 / .6);
  color-scheme: dark;
}
:root[data-theme="blue"] {
  --bg: #0b1324;
  --panel: #111b31;
  --raised: #17233e;
  --text: #e5ebf5;
  --muted: #8d9bb5;
  --line: #22304f;
  --accent: #3fb37a;
  --accent-text: #04140b;
  --warn-bg: #2b2410;
  --warn-text: #f3cf8a;
  --chip: #1b2846;
  --shadow: 0 10px 40px rgb(2 6 18 / .6);
  color-scheme: dark;
}

/* theme picker */
.theme-picker { display: inline-flex; gap: 2px; padding: 3px; border-radius: 8px; background: var(--chip); margin-left: 6px; }
.theme-picker button { display: inline-flex; align-items: center; gap: 6px; padding: 5px 10px; border-radius: 6px; font-size: 13px; color: var(--muted); }
.theme-picker button.on { background: var(--raised); color: var(--text); box-shadow: 0 1px 3px rgb(0 0 0 / .15); font-weight: 400; }
.swatch { width: 11px; height: 11px; border-radius: 50%; border: 1px solid rgb(128 128 128 / .45); }
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.45 system-ui, "Segoe UI", sans-serif; }
header { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; padding: 12px 20px; background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 2; }
.brand { font-weight: 700; font-size: 18px; }
.brand span { color: var(--muted); font-weight: 400; font-size: 14px; }
nav { display: flex; gap: 4px; flex-wrap: wrap; }
nav button { background: none; border: 0; padding: 8px 12px; border-radius: 6px; color: var(--muted); cursor: pointer; font: inherit; }
nav button.active { background: var(--chip); color: var(--text); font-weight: 600; }
main { padding: 20px; max-width: 1400px; margin: 0 auto; }
h2 { margin: 0; font-size: 18px; }
h2 small { color: var(--muted); font-weight: 400; font-size: 14px; }
h3 { margin: 28px 0 8px; font-size: 15px; }
.muted { color: var(--muted); }

.banner { background: var(--warn-bg); color: var(--warn-text); padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; margin-bottom: 20px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 10px 14px; }
.stat b { display: block; font-size: 22px; font-variant-numeric: tabular-nums; }
.stat span { color: var(--muted); font-size: 12px; }

.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
button, .button { font: inherit; padding: 7px 12px; border-radius: 6px; border: 1px solid var(--line); background: var(--panel); color: var(--text); cursor: pointer; text-decoration: none; display: inline-block; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button:disabled { opacity: .5; cursor: default; }
input, select, textarea { font: inherit; padding: 7px 9px; border-radius: 6px; border: 1px solid var(--line); background: var(--bg); color: var(--text); width: 100%; }
.actions input, .actions select { width: auto; }
textarea { resize: vertical; font-family: inherit; }

.table-wrap { overflow-x: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; }
table { border-collapse: collapse; width: 100%; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
td .sub { color: var(--muted); font-size: 12px; }
td.contact a { display: inline-block; margin: 0 6px 4px 0; padding: 3px 8px; border-radius: 5px; background: var(--chip); color: var(--text); text-decoration: none; font-size: 12px; white-space: nowrap; }
td.contact a.wa { background: #25d36622; }
td select { width: auto; }
td textarea { min-width: 180px; font-size: 12px; padding: 4px 6px; }
.tag { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 4px; background: var(--chip); color: var(--muted); }
.empty { padding: 30px; text-align: center; color: var(--muted); }
.status-won { color: var(--accent); font-weight: 600; }

form fieldset { border: 1px solid var(--line); border-radius: 8px; background: var(--panel); padding: 14px 16px; margin: 0 0 16px; min-width: 0; }
legend { font-weight: 600; padding: 0 4px; }
legend.sub { margin-top: 16px; padding: 0; }
label, .label { display: block; margin: 8px 0; color: var(--muted); font-size: 12px; }
label input, label textarea { margin-top: 4px; color: var(--text); font-size: 14px; }
label.inline { display: flex; align-items: center; gap: 6px; font-size: 14px; color: var(--text); }
label.inline input { width: auto; margin: 0; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); gap: 16px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: end; }
.row > * { flex: 1 1 120px; }
.row > button { flex: 0 0 auto; }
.checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 4px 12px; }
.checks label { display: flex; gap: 6px; align-items: center; margin: 2px 0; color: var(--text); font-size: 13px; }
.checks input { width: auto; margin: 0; }
.save-bar { display: flex; gap: 12px; align-items: center; }

#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; max-width: 90vw; z-index: 10; }

@media (max-width: 600px) {
  main { padding: 16px; }
  .grid { grid-template-columns: 1fr; }
}

/* AI message column + editor */
button.small { padding: 3px 8px; font-size: 12px; margin-top: 4px; }
td.msg { min-width: 200px; max-width: 280px; }
dialog { border: 1px solid var(--line); border-radius: 10px; background: var(--panel); color: var(--text); width: min(640px, calc(100vw - 32px)); padding: 18px; }
dialog::backdrop { background: rgb(0 0 0 / .45); }
dialog h3 { margin: 0 0 4px; }

/* chat */
#chat-open { position: fixed; right: 20px; bottom: calc(20px + env(safe-area-inset-bottom)); z-index: 5; padding: 11px 20px; border-radius: 8px; border: 0; background: var(--accent); color: var(--accent-text); font-weight: 600; box-shadow: var(--shadow); }
#chat-open:hover { filter: brightness(1.05); }
#chat { position: fixed; top: 0; right: 0; bottom: 0; z-index: 6; width: min(460px, 100vw); display: flex; flex-direction: column; background: var(--bg); border-left: 1px solid var(--line); box-shadow: var(--shadow); }
#chat[hidden] { display: none; }

.chat-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px; padding-top: calc(14px + env(safe-area-inset-top)); background: var(--panel); border-bottom: 1px solid var(--line); }
.chat-title { display: flex; flex-direction: column; min-width: 0; }
.chat-title b { font-size: 16px; }
.chat-sub { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-tools { display: flex; align-items: center; gap: 4px; }
.text-btn { border: 1px solid var(--line); background: var(--panel); color: var(--text); border-radius: 999px; padding: 6px 12px; font-size: 13px; }
.icon-btn { width: 36px; height: 36px; padding: 0; display: grid; place-items: center; border: 0; border-radius: 10px; background: transparent; color: var(--muted); }
.icon-btn:hover, .text-btn:hover { background: var(--chip); color: var(--text); }

#chat-log { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 20px 16px 8px; display: flex; flex-direction: column; gap: 20px; }
.msg-row { display: flex; flex-direction: column; }
.msg-row.me { align-items: flex-end; }
.msg-me { max-width: 85%; padding: 10px 14px; border-radius: 18px 18px 6px 18px; background: var(--accent); color: var(--accent-text); line-height: 1.5; overflow-wrap: anywhere; }
.msg-ai { max-width: 100%; line-height: 1.65; overflow-wrap: anywhere; }
.msg-ai p { margin: 0 0 10px; }
.msg-ai p:last-child, .msg-ai ul:last-child, .msg-ai ol:last-child { margin-bottom: 0; }
.msg-ai ul, .msg-ai ol { margin: 0 0 10px; padding-left: 22px; }
.msg-ai li { margin: 3px 0; }
.msg-ai code { background: var(--chip); padding: 1px 5px; border-radius: 4px; font-size: 13px; }
.msg-ai.error { color: var(--muted); }
.msg-actions { margin-top: 6px; display: flex; gap: 6px; }
.msg-actions button { padding: 3px 10px; font-size: 12px; border-radius: 999px; border: 1px solid var(--line); background: transparent; color: var(--muted); }
.msg-actions button:hover { color: var(--text); background: var(--chip); }
.typing { display: inline-flex; gap: 5px; padding: 6px 0; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite ease-in-out; }
.typing span:nth-child(2) { animation-delay: .15s; }
.typing span:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

.chat-empty { margin: auto 0; padding: 8px 4px; }
.chat-empty h4 { margin: 0 0 6px; color: var(--text); font-size: 18px; }
.chat-empty p { margin: 0 0 16px; color: var(--muted); font-size: 14px; }
.suggestions { display: flex; flex-direction: column; gap: 8px; }
.suggestions button { padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel); color: var(--text); font-size: 14px; text-align: left; line-height: 1.4; }
.suggestions button:hover { border-color: var(--accent); }

#chat-form { padding: 10px 12px; padding-bottom: calc(10px + env(safe-area-inset-bottom)); background: var(--bg); }
.composer { display: flex; align-items: flex-end; gap: 8px; padding: 6px 6px 6px 14px; border: 1px solid var(--line); border-radius: 22px; background: var(--panel); }
.composer:focus-within { border-color: var(--accent); }
.composer textarea { flex: 1; border: 0; background: transparent; padding: 8px 0; resize: none; max-height: 160px; line-height: 1.45; outline: none; color: var(--text); }
.composer .send { width: 36px; height: 36px; padding: 0; display: grid; place-items: center; border: 0; border-radius: 50%; background: var(--accent); color: var(--accent-text); flex: none; transition: opacity .15s; }
.composer .send:disabled { opacity: .35; }

/* Phones: the chat fills the whole screen and the page behind it can't scroll */
@media (max-width: 600px) {
  #chat { width: 100vw; height: 100dvh; border-left: 0; }
  body.chat-open { overflow: hidden; }
  #chat-open { right: 16px; }
}

/* Phones zoom in when a text box under 16px gets focus (the "random zooming"); use 16px there,
   and never let anything make the page wider than the screen */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body { overflow-x: hidden; }
@media (max-width: 900px), (pointer: coarse) {
  input, select, textarea, .composer textarea { font-size: 16px; }
}
dialog textarea, dialog input { max-width: 100%; }
@media (max-width: 600px) {
  dialog { width: calc(100vw - 16px); max-height: calc(100dvh - 32px); padding: 14px; }
  header { padding: 10px 12px; }
  nav { gap: 2px; }
  nav button { padding: 7px 9px; }
}

/* sign-in page */
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px 24px; box-shadow: var(--shadow); }
.login-card .brand { font-size: 22px; margin-bottom: 4px; }
.login-card p { margin: 0 0 18px; }
.login-card button { width: 100%; padding: 10px; margin-top: 6px; }
.login-error { color: #c0392b; font-size: 13px; margin: 4px 0 8px !important; }

/* pull down to get more businesses */
html, body { overscroll-behavior-y: contain; }
#pull { height: 0; overflow: hidden; display: grid; place-items: center; color: var(--muted); font-size: 13px; transition: height .15s ease; }
#pull.busy { height: 44px !important; color: var(--accent); }

/* mobile / landline tags */
.ptag { display: inline-block; margin: 0 6px 4px 0; padding: 2px 7px; border-radius: 999px; font-size: 11px; font-weight: 600; }
.ptag.mobile { background: #25d36626; color: #1a8f47; }
.ptag.landline { background: var(--chip); color: var(--muted); }
.ptag.unknown { background: #f0b42926; color: #a0700f; }
:root[data-theme="dark"] .ptag.mobile, :root[data-theme="blue"] .ptag.mobile { color: #4fd28a; }
:root[data-theme="dark"] .ptag.unknown, :root[data-theme="blue"] .ptag.unknown { color: #f0c04a; }
@media (prefers-color-scheme: dark) { :root:not([data-theme]) .ptag.mobile { color: #4fd28a; } :root:not([data-theme]) .ptag.unknown { color: #f0c04a; } }

/* "Try WhatsApp" on landlines: quieter than the main WhatsApp button */
td.contact a.wa-try { background: transparent; border: 1px dashed var(--line); color: var(--muted); }

/* Double-tapping a button or link no longer zooms the page */
a, button, select, label, input, textarea, .tab, nav { touch-action: manipulation; }
