*, *::before, *::after { box-sizing: border-box; }

:root {
  --bg: #f0f4f8;
  --surface: #ffffff;
  --surface2: #e8f0fe;
  --border: #d0daea;
  --text: #1a2332;
  --muted: #6b7a99;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --danger: #dc2626;
  --sidebar-w: 200px;
}

body { margin: 0; background: var(--bg); color: var(--text); font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; font-size: 14px; }

/* Login */
.login-wrap { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-box { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 32px; width: 340px; }
.login-box h1 { margin: 0 0 8px; font-size: 18px; }
.login-box p { margin: 0 0 24px; color: var(--muted); font-size: 13px; }
.login-box .error { color: var(--danger); font-size: 12px; margin-top: 8px; }
.login-box .auth-ok { color: #16a34a; font-size: 12px; margin-top: 8px; }
.auth-link { display: block; margin-top: 14px; font-size: 12px; color: var(--accent); text-decoration: none; text-align: center; }
.auth-link:hover { text-decoration: underline; }
.auth-stepno { font-size: 11px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--muted); margin: -2px 0 14px; }
.btn-block { width: 100%; padding: 10px 16px; }
.btn-secondary { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--surface2); }
.or-divider { display: flex; align-items: center; gap: 10px; margin: 14px 0; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.6px; }
.or-divider::before, .or-divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }
.code-input { font-size: 20px !important; letter-spacing: 8px; text-align: center; font-variant-numeric: tabular-nums; }

/* ── Settings ─────────────────────────────────────────────────────────────── */
.back-link { color: var(--accent); text-decoration: none; font-size: 14px; font-weight: 500; }
.back-link:hover { text-decoration: underline; }
.settings-section { max-width: 640px; margin-bottom: 28px; }
.settings-heading { margin: 0 0 8px 4px; font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.6px; color: var(--muted); }
.settings-intro { margin: 0 0 10px 4px; font-size: 13px; color: var(--muted); line-height: 1.45; }
.settings-card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.auth-card { max-width: 420px; padding: 24px; }
.auth-card p { margin: 0 0 18px; color: var(--muted); font-size: 13px; line-height: 1.45; }
.auth-card .hint { margin: 18px 0 0; font-size: 12px; }
.auth-card .error, .auth-card .auth-ok { font-size: 12px; margin-top: 8px; }
.auth-card .error { color: var(--danger); }
.auth-card .auth-ok { color: #16a34a; }

.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 14px 16px; }
.setting-row + .setting-row, .list-row + .list-row, .link-row + .link-row { border-top: 1px solid var(--border); }
.setting-text { min-width: 0; }
.setting-label { display: block; font-size: 14px; font-weight: 500; color: var(--text); }
.setting-desc { display: block; margin-top: 3px; font-size: 12px; color: var(--muted); line-height: 1.4; }
.setting-control { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.unit-field { display: flex; align-items: center; background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding-right: 10px; }
.unit-field:focus-within { border-color: var(--accent); }
.unit-field input { width: 64px; border: none; background: transparent; color: var(--text); padding: 7px 4px 7px 10px; font-size: 14px; font-variant-numeric: tabular-nums; outline: none; }
.unit-field span { font-size: 12px; color: var(--muted); }
.btn-small { padding: 7px 12px; font-size: 12px; }
.btn:disabled { opacity: 0.45; cursor: default; }

.list-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; }
.list-row-text { min-width: 0; }
.list-row-text .setting-label, .list-row-text .setting-desc { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-empty { padding: 16px; font-size: 13px; color: var(--muted); }
.icon-btn { flex-shrink: 0; width: 32px; height: 32px; border-radius: 8px; border: 1px solid transparent; background: transparent; color: var(--muted); cursor: pointer; font-size: 13px; }
.icon-btn:hover { background: #fef2f2; border-color: #fecaca; color: var(--danger); }
.add-form { display: grid; grid-template-columns: 1fr 1.4fr auto; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); background: #f8fafc; }
.add-form input { min-width: 0; background: var(--surface); border: 1px solid var(--border); color: var(--text); padding: 8px 10px; border-radius: 6px; font-size: 13px; }
.add-form input:focus { outline: none; border-color: var(--accent); }

.link-row { display: flex; align-items: center; gap: 12px; width: 100%; padding: 14px 16px; background: none; border: none; text-align: left; font: inherit; color: inherit; text-decoration: none; cursor: pointer; }
.link-row:hover { background: #f8fafc; }
.link-row-icon { font-size: 18px; width: 24px; text-align: center; flex-shrink: 0; }
.link-row-text { flex: 1; min-width: 0; }
.chevron { font-size: 22px; line-height: 1; color: var(--muted); }
.link-row-danger .setting-label { color: var(--danger); }

/* Layout */
.layout { display: flex; min-height: 100vh; }
.sidebar { width: var(--sidebar-w); background: var(--surface); border-right: 1px solid var(--border); padding: 20px 12px; display: flex; flex-direction: column; gap: 4px; flex-shrink: 0; }
.sidebar-title { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; padding: 8px 10px 4px; }
.nav-item { padding: 9px 12px; border-radius: 6px; cursor: pointer; color: var(--muted); display: flex; align-items: center; gap: 8px; transition: background 0.15s; }
.nav-item:hover { background: var(--surface2); color: var(--text); }
.nav-item.active { background: var(--surface2); color: var(--text); font-weight: 500; }
.main { flex: 1; padding: 32px; overflow-y: auto; }

/* Page */
.page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px; }
.page-header h2 { margin: 0; font-size: 18px; }

/* Table */
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; padding: 8px 12px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted); border-bottom: 1px solid var(--border); }
.table td { padding: 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tr:last-child td { border-bottom: none; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 4px; font-size: 11px; background: var(--surface2); color: var(--muted); }

/* Mobile tab status pills (connection + SMS credits) — larger and bolder
   than the generic .badge above so the two share equal visual weight. */
.status-pill { display: inline-flex; align-items: center; gap: 8px; padding: 10px 20px; border-radius: 999px; font-size: 16px; font-weight: 600; }

/* Nav LED — live WhatsApp connection status visible from every tab, not
   just Mobile. .nav-icon-wrap is a tight inline-block around just the icon
   glyph (not the icon's padded/background box), so the dot pins to the
   glyph itself rather than floating at the edge of a wider container. */
.nav-icon-wrap { position: relative; display: inline-block; }
.led-dot { position: absolute; top: -3px; right: -7px; width: 8px; height: 8px; border-radius: 50%; border: 1.5px solid var(--surface); transition: background 0.3s; }

/* Buttons */
.btn { padding: 8px 16px; border-radius: 6px; border: none; cursor: pointer; font-size: 13px; font-weight: 500; transition: background 0.15s; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); padding: 5px 10px; font-size: 12px; }
.btn-danger:hover { background: var(--danger); color: #fff; }

/* Forms */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted); margin-bottom: 6px; }
.form-group input, .form-group select { width: 100%; background: var(--bg); border: 1px solid var(--border); color: var(--text); padding: 8px 10px; border-radius: 6px; font-size: 13px; }
.form-group input:focus, .form-group select:focus { outline: none; border-color: var(--accent); }

/* Modal */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.3); display: flex; align-items: center; justify-content: center; z-index: 100; }
.modal { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 24px; width: 420px; }
.modal h3 { margin: 0 0 4px; }
.modal .subtitle { color: var(--muted); font-size: 12px; margin: 0 0 20px; }
.modal-footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }

/* Inline add row */
.add-row { display: flex; gap: 8px; margin-top: 16px; }
.add-row input { flex: 1; background: var(--bg); border: 1px solid var(--border); color: var(--text); padding: 8px 10px; border-radius: 6px; font-size: 13px; }
.add-row input:focus { outline: none; border-color: var(--accent); }

/* Blueprint prompt — collapsed preview + expandable textarea */
.bp-prompt { font-size: 12px; color: var(--muted); font-family: monospace; cursor: pointer; }
.bp-collapsed { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 360px; }
.bp-collapsed:hover { color: var(--accent); }
.bp-full {
  width: 100%;
  min-width: 280px;
  height: 220px;
  background: var(--bg);
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 10px;
  font-size: 12px;
  font-family: monospace;
  color: var(--text);
  resize: vertical;
  outline: none;
  line-height: 1.5;
}

/* Empty state */
.empty { color: var(--muted); text-align: center; padding: 40px; font-size: 13px; }

/* ── Mobile bottom nav (≤640px) ─────────────────────────────────────────── */
@media (max-width: 640px) {
  .sidebar { display: none; }
  .main { padding: 16px 12px 90px; overflow-x: hidden; }
  .modal { width: calc(100vw - 32px); }
  .add-row { flex-wrap: wrap; }
  .add-row input { min-width: 0; }

  /* Settings: stack label above control; allowlist form one field per line */
  .setting-row { flex-direction: column; align-items: stretch; gap: 10px; }
  .setting-control { justify-content: space-between; }
  .add-form { grid-template-columns: 1fr; }
  .auth-card { padding: 20px 16px; }
  .login-box { width: calc(100vw - 32px); max-width: 360px; padding: 24px 20px; }

  /* Scrollable table wrapper */
  .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; width: 100%; }
  .table { min-width: 0; width: 100%; }

  /* Constrain group name cell so Remove button stays visible */
  .table td:first-child { max-width: 45vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .table td:first-child span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  /* Blueprint prompt — don't overflow */
  .bp-collapsed { max-width: 180px; }
  .bp-full { min-width: unset; width: 100%; }

  .bottom-nav {
    display: flex;
    position: fixed;
    bottom: 0; left: 0; right: 0;
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 6px 2px env(safe-area-inset-bottom, 4px);
    gap: 0;
    z-index: 50;
  }
  .bottom-nav-item {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    cursor: pointer;
    padding: 0 1px;
  }
  .bottom-nav-item .bnav-icon {
    border-radius: 10px;
    padding: 3px 8px;
    font-size: 16px;
    transition: background 0.15s;
  }
  .bottom-nav-item.active .bnav-icon { background: var(--surface2); }
  .bottom-nav-item .bnav-label {
    font-size: 9px;
    color: var(--muted);
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
  }
  .bottom-nav-item.active .bnav-label { color: var(--accent); }
}

@media (min-width: 641px) {
  .bottom-nav { display: none; }
}

/* ── Tool chip editor ───────────────────────────────────────────────────── */
.tool-chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 12px; background: var(--bg); border-radius: 8px; border: 1px solid var(--border); margin-top: 8px; }
.chip { display: inline-flex; align-items: center; gap: 5px; padding: 3px 8px 3px 6px; border-radius: 99px; font-size: 11px; border: 1px solid var(--border); background: var(--surface); color: var(--text); cursor: pointer; user-select: none; transition: background 0.1s, border-color 0.1s; }
.chip.on  { background: #eff6ff; border-color: #93c5fd; color: var(--accent); }
.chip.off { background: #f8fafc; border-color: #e2e8f0; color: #94a3b8; text-decoration: line-through; }
.chip-dot { width: 6px; height: 6px; border-radius: 50%; flex-shrink: 0; }
.chip.on  .chip-dot { background: var(--accent); }
.chip.off .chip-dot { background: #cbd5e1; }
.chip-save-row { display: flex; align-items: center; gap: 8px; margin-top: 10px; }
.chip-save-row .hint { font-size: 11px; color: var(--muted); }

/* ── Expand row (tool editor beneath blueprint row) ─────────────────────── */
.expand-row td { padding: 0 !important; }
.expand-inner { padding: 14px 16px 16px; background: #f8fafc; border-bottom: 1px solid var(--border); }
.expand-inner h4 { margin: 0 0 8px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted); }

/* ── Toggle switch ──────────────────────────────────────────────────────── */
.toggle { position: relative; display: inline-block; width: 36px; height: 20px; flex-shrink: 0; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle .slider { position: absolute; cursor: pointer; inset: 0; background: #cbd5e1; border-radius: 20px; transition: 0.2s; }
.toggle .slider::before { position: absolute; content: ""; height: 14px; width: 14px; left: 3px; bottom: 3px; background: white; border-radius: 50%; transition: 0.2s; }
.toggle input:checked + .slider { background: var(--accent); }
.toggle input:checked + .slider::before { transform: translateX(16px); }

/* ── Group pills (blueprint tags in Tools table) ────────────────────────── */
.group-pills { display: flex; flex-wrap: wrap; gap: 4px; }
.group-pill { font-size: 10px; background: var(--surface2); color: var(--accent); padding: 2px 7px; border-radius: 99px; white-space: nowrap; }

/* ── Sortable table headers ─────────────────────────────────────────────── */
.table th.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
.table th.sortable:hover { color: var(--accent); }
.table th.sort-asc  .sort-icon::after { content: ' ↑'; color: var(--accent); }
.table th.sort-desc .sort-icon::after { content: ' ↓'; color: var(--accent); }
.table th:not(.sort-asc):not(.sort-desc) .sort-icon::after { content: ' ↕'; opacity: 0.3; }

/* ── Tab bar (Tools category filter) ───────────────────────────────────── */
.tab-bar { display: flex; border-bottom: 1px solid var(--border); margin-bottom: 20px; }
.tab { padding: 8px 16px; cursor: pointer; font-size: 13px; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; transition: color 0.15s; }
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 500; }
