/* Same look as the ERPNext "Devi Xero Theme". */
:root {
  --blue: #13b5ea;
  --blue-mid: #0d9fd4;
  --blue-ink: #0b7ea6;    /* blue for text on white: easier to read */
  --blue-light: #e8f7fd;
  --bg: #f4f6f9;
  --white: #ffffff;
  --border: #e8edf2;
  --border-strong: #d1d9e0;
  --ink: #1a1a2e;
  --muted: #5f6f7e;
  --warning: #966800;
  --warning-bg: #fff8e6;
  --danger: #b91c1c;
  --danger-bg: #ffeaea;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Nirmala UI", "Noto Sans Tamil", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body { margin: 0; font: 400 15px/1.45 var(--font); color: var(--ink); background: var(--bg); }
h1, h2, p { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px rgba(19, 181, 234, .14); }
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--blue); }

.btn { border: 1px solid transparent; border-radius: 6px; padding: 8px 16px; font-size: 14px; font-weight: 500; cursor: pointer; line-height: 1.2; transition: background .15s, color .15s, border-color .15s; }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--blue); color: #fff; font-weight: 600; }
.btn.primary:hover:not(:disabled) { background: var(--blue-mid); }
.btn.subtle { background: var(--white); border-color: var(--border-strong); }
.btn.subtle:hover:not(:disabled) { border-color: var(--blue); color: var(--blue-ink); }
.btn.light { background: var(--blue); color: #fff; font-weight: 600; }
.btn.light:hover { background: var(--blue-mid); }
.btn.ghost-light { background: transparent; color: var(--muted); }
.btn.ghost-light:hover { color: var(--blue-ink); }
.form-error { color: var(--danger); font-weight: 500; font-size: 14px; }

/* Sign in */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; background: var(--bg); }
.login-card { width: min(380px, 100%); background: var(--white); border: 1px solid var(--border); border-radius: 10px; padding: 32px 28px; display: grid; gap: 16px; }
.login-card h1 { font-size: 24px; font-weight: 600; line-height: 1.2; }
.login-card > p:not(.form-error) { color: var(--muted); }
.login label, .dialog label { display: grid; gap: 4px; font-weight: 400; font-size: 13px; color: var(--muted); }
.login input, .dialog input, .dialog select { width: 100%; padding: 9px 12px; border: 1px solid var(--border-strong); border-radius: 6px; background: var(--white); color: var(--ink); font-size: 15px; }
.login .btn { padding: 11px; }

/* Layout */
.app { height: 100dvh; display: grid; grid-template-columns: minmax(300px, 360px) 1fr; }
.sidebar { background: var(--white); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.side-head { padding: 12px 16px; display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; border-bottom: 1px solid var(--border); }
.side-head h1 { font-size: 18px; font-weight: 600; line-height: 1.2; }
.side-actions { display: flex; align-items: center; gap: 0; }
.side-actions .btn { font-size: 13px; padding: 6px 7px; }
.side-actions .btn.light { padding: 6px 10px; margin-right: 2px; }
.side-tools { padding: 12px 16px; display: grid; gap: 8px; }
#search, .number-filter { width: 100%; border: 1px solid var(--border); background: var(--bg); color: var(--ink); }
#search { padding: 7px 16px; border-radius: 20px; font-size: 14px; }
#search::placeholder { color: var(--muted); }
.number-filter { padding: 7px 10px; border-radius: 6px; font-size: 14px; }
.filters { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin: 0 -16px -12px; padding: 0 12px; }
.filter { border: 0; border-bottom: 2px solid transparent; background: transparent; color: var(--muted); padding: 7px 8px; cursor: pointer; font-size: 13px; }
.filter:hover { color: var(--blue-ink); }
.filter.on { color: var(--blue-ink); border-bottom-color: var(--blue); font-weight: 600; }

/* Each row has a small meter for its reply window */
.chat-list { list-style: none; margin: 0; padding: 0; flex: 1; overflow-y: auto; min-height: 0; }
.chat-row { position: relative; width: 100%; text-align: left; border: 0; border-left: 3px solid transparent; border-bottom: 1px solid var(--border); background: transparent; color: inherit; cursor: pointer; padding: 10px 16px 10px 25px; display: grid; grid-template-columns: 1fr auto; gap: 1px 10px; transition: background .1s; }
.chat-row:hover { background: var(--bg); }
.chat-row.active { background: var(--blue-light); border-left-color: var(--blue); }
.chat-who { font-size: 14px; font-weight: 500; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.chat-time { font-size: 12px; color: var(--muted); justify-self: end; }
.chat-preview { font-size: 13px; color: var(--muted); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; min-width: 0; }
.chat-row.has-unread .chat-who { font-weight: 700; }
.chat-row.has-unread .chat-preview { color: var(--ink); }
.chat-row.has-unread .chat-time { color: var(--blue-ink); font-weight: 600; }
.unread { justify-self: end; background: var(--blue); color: #fff; border-radius: 10px; min-width: 20px; padding: 0 6px; text-align: center; font-weight: 700; font-size: 12px; line-height: 20px; }
.chat-via { grid-column: 1 / -1; font-size: 12px; color: var(--muted); }
.row-meter { position: absolute; left: 10px; top: 12px; bottom: 12px; width: 4px; border-radius: 2px; background: var(--border); overflow: hidden; display: flex; align-items: flex-end; }
.row-meter > div { width: 100%; background: var(--blue); }
.list-empty { padding: 24px 16px; color: var(--muted); font-size: 14px; }
.alert-bar { margin: 12px 16px; padding: 10px 12px; border-radius: 8px; background: var(--warning-bg); color: var(--warning); display: grid; gap: 8px; font-size: 13px; line-height: 1.35; }
.alert-bar .btn { justify-self: start; background: var(--blue); color: #fff; font-size: 13px; padding: 6px 12px; }
.alert-bar .btn:hover { background: var(--blue-mid); }
.alert-status { padding: 9px 16px; background: var(--warning-bg); color: var(--warning); font-size: 13px; line-height: 1.35; }
.side-foot { padding: 7px 16px; font-size: 13px; color: var(--muted); display: flex; align-items: center; justify-content: space-between; gap: 8px; border-top: 1px solid var(--border); }
.foot-actions { display: flex; gap: 2px; }
.sound-toggle { font-size: 12px; padding: 2px 6px; }
.conn.bad { color: var(--warning); font-weight: 600; }

/* Nothing open */
.thread { min-width: 0; min-height: 0; display: flex; }
.no-chat { margin: auto; padding: 32px 24px; max-width: 480px; text-align: center; }
.no-chat h2 { font-size: 26px; font-weight: 600; line-height: 1.2; margin-bottom: 6px; }
.no-chat p { color: var(--muted); }

/* Conversation */
.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; }
.chat-head { display: flex; align-items: center; gap: 10px; min-height: 52px; padding: 7px 20px; background: var(--white); border-bottom: 1px solid var(--border); }
.icon-btn { display: none; width: 40px; height: 40px; border: 0; background: transparent; font-size: 28px; line-height: 1; cursor: pointer; color: var(--muted); }
.chat-title { flex: 1; min-width: 0; }
.chat-title h2 { font-size: 16px; font-weight: 600; line-height: 1.25; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.chat-number { color: var(--muted); font-size: 12px; text-decoration: none; }
.chat-number:hover { color: var(--blue-ink); }
.chat-title .chat-via { color: var(--blue-ink); font-size: 12px; font-weight: 500; margin-left: 8px; }
.ai-toggle { display: flex; align-items: center; gap: 7px; font-size: 13px; cursor: pointer; white-space: nowrap; }
.ai-toggle input { width: 16px; height: 16px; accent-color: var(--blue); }

.chat-body { flex: 1; min-height: 0; display: flex; }

/* Reply window meter: the level drops over 24 hours. */
.gauge { flex: none; width: 50px; padding: 14px 10px 14px 6px; display: flex; justify-content: flex-end; gap: 6px; background: var(--white); border-right: 1px solid var(--border); }
.gauge-scale { display: flex; flex-direction: column; justify-content: space-between; font-size: 11px; line-height: 1; color: var(--muted); }
.tube { flex: none; width: 8px; border-radius: 4px; overflow: hidden; display: flex; align-items: flex-end; background: var(--border); }
.tube-fill { width: 100%; height: 0; border-radius: 4px; background: var(--blue); transition: height .6s ease-out; }

/* Says what the level means, right above the reply box */
.window { background: var(--blue-light); color: var(--blue-ink); font-size: 13px; padding: 5px 20px; }
.window.low { background: var(--danger-bg); color: var(--danger); font-weight: 600; }
.window.closed { background: var(--warning-bg); color: var(--warning); }

.messages { flex: 1; min-width: 0; min-height: 0; overflow-y: auto; padding: 16px clamp(14px, 3.5vw, 44px); display: block; }
.older { display: block; margin: 0 auto 14px; }
.day { text-align: center; margin: 18px 0 10px; color: var(--muted); font-size: 12px; }
.msg { display: flex; flex-direction: column; margin: 3px 0; max-width: min(620px, 82%); align-items: flex-start; }
.msg.team { margin-left: auto; align-items: flex-end; }
.msg.grouped-start { margin-top: 12px; }
.bubble { padding: 7px 12px; border-radius: 8px; background: var(--white); border: 1px solid var(--border); white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.team .bubble { background: var(--blue-light); border-color: #cdeefa; }
.msg.template .bubble { border-style: dashed; border-color: var(--blue); }
.msg.note .bubble { color: var(--muted); font-style: italic; }
.media-link { display: block; }
.media-img { display: block; max-width: min(320px, 62vw); max-height: 320px; min-height: 80px; border-radius: 6px; background: var(--bg); }
.media-audio { display: block; width: min(300px, 56vw); height: 44px; }
.media-video { display: block; width: min(340px, 62vw); max-height: 320px; border-radius: 6px; background: #000; }
.media-doc { display: block; font-weight: 500; color: var(--blue-ink); padding: 4px 2px; overflow-wrap: anywhere; }
.media-failed { display: block; color: var(--danger); font-size: 13px; }
.media-caption { margin-top: 6px; }
.msg-meta { font-size: 11.5px; color: var(--muted); padding: 2px 3px 0; }
.msg-meta .read { color: var(--blue-ink); font-weight: 700; }
.msg-meta .failed { color: var(--danger); font-weight: 700; }

.composer-error { padding: 6px 20px 0; background: var(--white); }
.composer { display: flex; align-items: flex-end; gap: 8px; padding: 10px 20px calc(10px + env(safe-area-inset-bottom)); background: var(--white); border-top: 1px solid var(--border); }
.composer textarea { flex: 1; min-width: 0; resize: none; border: 1px solid var(--border-strong); border-radius: 6px; padding: 8px 12px; max-height: 150px; background: var(--white); font-size: 15px; }
.composer .btn { padding: 9px 16px; }
.file-chip { display: flex; align-items: center; gap: 10px; padding: 8px 20px; background: var(--white); border-top: 1px solid var(--border); }
.file-name { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 14px; font-weight: 500; color: var(--blue-ink); }
.file-chip .btn { font-size: 13px; padding: 5px 10px; }
.file-chip + .composer { border-top: 0; }
.composer.template-only textarea, .composer.template-only #send, .composer.template-only #attach { display: none; }
.composer.template-only #template-open { flex: 1; background: var(--blue); color: #fff; font-weight: 600; border-color: transparent; }
.composer.template-only #template-open:hover { background: var(--blue-mid); color: #fff; }

/* Settings */
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
.settings { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
.settings-head { display: flex; align-items: center; gap: 10px; min-height: 52px; padding: 7px 20px; background: var(--white); border-bottom: 1px solid var(--border); }
.settings-head h2 { font-size: 18px; font-weight: 500; }
.settings-tabs { flex: 1; display: flex; align-self: stretch; gap: 2px; margin: -7px 0 -7px 12px; }
.settings-tab { border: 0; border-bottom: 2px solid transparent; background: transparent; color: var(--muted); padding: 0 10px; cursor: pointer; font-size: 14px; }
.settings-tab:hover { color: var(--blue-ink); }
.settings-tab.on { color: var(--blue-ink); border-bottom-color: var(--blue); font-weight: 600; }
.settings-pane { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.settings-bar { display: flex; align-items: flex-end; gap: 10px; flex-wrap: wrap; padding: 12px 20px 0; }
.settings-hint { flex: 1; min-width: 200px; color: var(--muted); font-size: 13px; max-width: 72ch; align-self: center; }
.bar-field { flex: 1; min-width: 200px; display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.bar-field select { max-width: 320px; padding: 8px 10px; border: 1px solid var(--border-strong); border-radius: 6px; background: var(--white); color: var(--ink); font-size: 14px; }
.hook-note { margin: 12px 20px 0; padding: 10px 12px; border-radius: 8px; background: var(--blue-light); color: var(--ink); font-size: 13px; overflow-wrap: anywhere; }
.hook-note.bad { background: var(--danger-bg); color: var(--danger); }
.hook-empty { padding: 12px 0; }
.hook-url { display: block; font-size: 12px; color: var(--muted); overflow-wrap: anywhere; max-width: 36ch; }
.hook-actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
.hook-actions .btn { font-size: 13px; padding: 5px 10px; }
.connect-title { margin: 20px 0 4px; font-size: 14px; font-weight: 600; }
.connect-url { margin-top: 6px; font-size: 14px; font-weight: 500; color: var(--blue-ink); overflow-wrap: anywhere; }
#hook-events { display: grid; gap: 4px; }
.tpl-status { font-weight: 500; }
.tpl-status.ok { color: #0d7a47; }
.tpl-status.wait { color: var(--warning); }
.tpl-status.bad { color: var(--danger); }
.tpl-reason { display: block; font-size: 12px; font-weight: 400; color: var(--muted); }
.tpl-message { max-width: 44ch; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 13px; color: var(--muted); }
.tpl-add { justify-self: start; font-size: 13px; padding: 5px 10px; }

/* Template page: form on the left, preview on the right */
.tpl-editor { flex: 1; min-height: 0; overflow-y: auto; display: grid; grid-template-columns: minmax(0, 1fr) 340px; align-items: start; gap: 20px; padding: 16px 20px 24px; }
.tpl-form { display: grid; gap: 12px; min-width: 0; }
.tpl-editor-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.tpl-editor h3 { margin: 0; font-size: 16px; font-weight: 600; }
.tpl-editor h4 { margin: 0; font-size: 13px; font-weight: 600; }
.tpl-section { background: var(--white); border: 1px solid var(--border); border-radius: 8px; padding: 14px; display: grid; gap: 10px; }
.tpl-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.tpl-editor label:not(.sr) { display: grid; gap: 4px; font-size: 13px; color: var(--muted); min-width: 0; }
.tpl-editor input:not([type=file]), .tpl-editor select, .tpl-editor textarea { width: 100%; min-width: 0; padding: 8px 12px; border: 1px solid var(--border-strong); border-radius: 6px; background: var(--white); color: var(--ink); font-size: 14px; }
.tpl-editor textarea { resize: vertical; font-size: 15px; }
.tpl-under { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.tpl-help { flex: 1; min-width: 180px; font-size: 12px; color: var(--muted); }
#tpl-samples, #tpl-buttons { display: grid; gap: 10px; }
#tpl-samples:empty, #tpl-buttons:empty { display: none; }
.tpl-button-row { display: grid; grid-template-columns: 130px minmax(0, 1fr) minmax(0, 1.3fr) auto; gap: 8px; align-items: center; }
.tpl-button-row .btn { grid-column: 4; font-size: 13px; padding: 6px 10px; }
.tpl-actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.tpl-preview { position: sticky; top: 0; display: grid; gap: 8px; }
.tpl-phone { min-height: 200px; padding: 16px 14px; background: #e6e2da; border-radius: 10px; }
.tpl-bubble { background: var(--white); border-radius: 8px 8px 8px 2px; padding: 6px; font-size: 14px; }
.pv-media { display: grid; place-items: center; height: 120px; border-radius: 6px; background: var(--border); color: var(--muted); font-size: 13px; margin-bottom: 6px; overflow: hidden; text-align: center; padding: 8px; overflow-wrap: anywhere; }
.pv-header { font-weight: 700; padding: 2px 6px; overflow-wrap: anywhere; }
.pv-body { padding: 2px 6px; white-space: pre-wrap; overflow-wrap: anywhere; }
.pv-body.empty { color: var(--muted); }
.pv-footer { padding: 2px 6px; font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
.pv-buttons { display: grid; gap: 4px; margin-top: 4px; }
.pv-buttons > div { background: var(--white); border-radius: 6px; padding: 7px; text-align: center; font-size: 14px; color: var(--blue-ink); font-weight: 500; overflow-wrap: anywhere; }
.settings-note { padding: 12px 20px 0; color: var(--muted); font-size: 13px; max-width: 72ch; }
.settings-note.form-error { color: var(--danger); }
.settings-scroll { overflow: auto; padding: 12px 20px 20px; min-height: 0; }
.users { width: 100%; border-collapse: separate; border-spacing: 0; background: var(--white); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.users th, .users td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: top; font-size: 14px; }
.users tbody tr:last-child td { border-bottom: 0; }
.users tbody tr:hover { background: var(--blue-light); }
.users th { font-size: 12px; color: var(--muted); font-weight: 400; background: #fafbfc; }
.users .user-handle { color: var(--muted); }
.users .off { color: var(--danger); font-weight: 500; }
.users td.actions { text-align: right; }
.role { font-size: 12px; font-weight: 500; color: var(--blue-ink); display: block; }
.user-numbers { border: 1px solid var(--border); border-radius: 6px; padding: 8px 12px 10px; margin: 0; display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.user-numbers legend { padding: 0 4px; }
#user-phones { display: grid; gap: 4px; }
#user-phones:empty { display: none; }
.dialog label.check, .user-numbers label.check { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--ink); }
.dialog label.check input { width: 16px; height: 16px; flex: none; accent-color: var(--blue); }
.dialog input:read-only:not([type=checkbox]) { background: var(--bg); color: var(--muted); }

/* Dialogs */
.dialog { border: 1px solid var(--border); border-radius: 10px; padding: 20px; width: min(480px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); color: var(--ink); box-shadow: 0 8px 40px rgba(0, 0, 0, .12); }
.dialog::backdrop { background: rgba(26, 26, 46, .4); }
.dialog form { display: grid; gap: 12px; }
.dialog h2 { font-size: 16px; font-weight: 600; padding-bottom: 12px; border-bottom: 1px solid var(--border); }
.template-body { white-space: pre-wrap; background: var(--bg); border-radius: 6px; padding: 10px 12px; font-size: 14px; overflow-wrap: anywhere; }
.template-body:empty, .template-cost:empty { display: none; }
.template-cost { color: var(--warning); font-size: 13px; }
#template-fields { display: grid; gap: 10px; }
#template-fields:empty { display: none; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

@media (max-width: 1080px) {
  .tpl-editor { grid-template-columns: minmax(0, 1fr); }
  .tpl-preview { position: static; order: -1; }
}

@media (max-width: 760px) {
  body { font-size: 16px; }
  .app { grid-template-columns: 1fr; }
  .sidebar { grid-column: 1; grid-row: 1; border-right: 0; }
  .thread { display: none; grid-column: 1; grid-row: 1; background: var(--bg); }
  .app.chat-open .sidebar { display: none; }
  .app.chat-open .thread { display: flex; }
  .icon-btn { display: block; }
  #search, .number-filter, .composer textarea { font-size: 16px; }
  .chat-head { padding: 4px 12px 4px 4px; }
  .gauge { width: 18px; padding: 10px 5px; }
  .gauge-scale { display: none; }
  .tube { width: 6px; }
  .window { padding: 5px 12px; }
  .messages { padding: 12px 10px; }
  .msg { max-width: 90%; }
  .composer { padding: 8px 12px calc(8px + env(safe-area-inset-bottom)); }
  .composer-error { padding: 6px 12px 0; }
  .file-chip { padding: 8px 12px; }
  .composer .btn { padding: 9px 10px; }
  .settings-head { padding: 4px 12px 4px 4px; }
  .settings-note { padding: 10px 12px 0; }
  .settings-bar { padding: 10px 12px 0; }
  .settings-head h2 { display: none; }
  .settings-tabs { margin-left: 4px; }
  .hook-note { margin: 10px 12px 0; }
  .hook-actions { justify-content: flex-start; }
  .settings-tab { padding: 0 6px; font-size: 13px; }
  .tpl-message { max-width: none; }
  .tpl-editor { grid-template-columns: minmax(0, 1fr); padding: 12px; }
  .tpl-preview { position: static; order: -1; }
  .tpl-row { grid-template-columns: minmax(0, 1fr); }
  .tpl-button-row { grid-template-columns: minmax(0, 1fr); padding-bottom: 8px; border-bottom: 1px solid var(--border); }
  .tpl-button-row .btn { grid-column: 1; justify-self: start; }
  .settings-scroll { padding: 10px 12px 16px; }
  .users thead { display: none; }
  .users tr { display: block; border-bottom: 1px solid var(--border); padding: 8px 12px; }
  .users td { display: block; border: 0; padding: 2px 0; }
  .users td.actions { text-align: left; padding-top: 6px; }
}

@media (prefers-reduced-motion: reduce) { * { transition: none !important; scroll-behavior: auto !important; } }
