:root {
  --bg: #f4f5f7;
  --card: #fff;
  --text: #14171f;
  --muted: #667085;
  --faint: #98a2b3;
  --border: #e4e7ec;
  --primary: #0b6e63;
  --primary-soft: #e3f2ef;
  --side: #111418;
  --side-text: #c9ced6;
  --danger: #c8322b;
  --danger-soft: #fdecea;
  --warn: #b54708;
  --warn-soft: #fef0c7;
  --wa: #1fa855;
  --radius: 12px;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: Inter, system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.45;
}
[hidden] { display: none !important; }
a { color: var(--primary); }
h1 { font-size: 24px; margin: 0; font-weight: 800; letter-spacing: -0.01em; }
h2 { font-size: 15px; margin: 0 0 12px; font-weight: 700; }
code { background: #eef0f3; padding: 1px 5px; border-radius: 4px; font-size: 12px; }
.muted { color: var(--muted); font-weight: 400; }
.mt { margin-top: 22px; }
.error { color: var(--danger); min-height: 18px; margin: 6px 0; }

/* Brand */
.brand { display: flex; align-items: center; gap: 10px; font-size: 16px; }
.logo {
  background: #d9393e; color: #fff; font-weight: 800; font-size: 11px; letter-spacing: .5px;
  width: 30px; height: 36px; border-radius: 5px; display: inline-flex; align-items: flex-end;
  justify-content: center; padding-bottom: 5px;
}

.brand-logo { width: 36px; height: 36px; border-radius: 9px; flex: 0 0 auto; }
.brand-name { display: flex; flex-direction: column; line-height: 1.15; font-weight: 700; font-size: 14px; white-space: nowrap; }
.brand-name small { font-size: 11px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; opacity: .6; margin-top: 2px; }
.login-card .brand-logo { width: 44px; height: 44px; border-radius: 11px; }

/* Buttons + inputs */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 0 14px; border-radius: 9px; border: 1px solid var(--border);
  background: var(--card); color: var(--text); font: inherit; font-weight: 600; cursor: pointer;
  text-decoration: none; white-space: nowrap;
}
.btn:hover { background: #f7f8fa; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.light { color: var(--side-text); }
.btn.light:hover { background: rgba(255,255,255,.06); }
.btn.small { min-height: 30px; padding: 0 10px; font-size: 13px; }
.btn.danger-ghost { color: var(--danger); border-color: transparent; background: transparent; }
.btn.danger-ghost:hover { background: var(--danger-soft); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
input, select, textarea {
  font: inherit; color: var(--text); background: #fafbfc; border: 1px solid var(--border);
  border-radius: 9px; padding: 8px 11px; min-height: 38px; width: 100%;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(11,110,99,.25); border-color: var(--primary); }
input[type=checkbox] { width: auto; min-height: 0; accent-color: var(--primary); }
label { display: block; font-weight: 600; margin: 14px 0 6px; }
label.check { display: flex; align-items: center; gap: 8px; font-weight: 500; }
label.check.spaced { margin-top: 42px; }
small { display: block; color: var(--muted); margin-top: 5px; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { background: var(--card); padding: 32px; border-radius: 16px; width: min(380px, 100%); box-shadow: 0 10px 40px rgba(0,0,0,.08); }
.login-card .brand { margin-bottom: 18px; }
.login-card .btn { margin-top: 8px; }

/* Shell */
.shell { display: grid; grid-template-columns: 236px 1fr; min-height: 100vh; }
.sidebar { background: var(--side); color: #fff; padding: 20px 14px; display: flex; flex-direction: column; gap: 22px; position: sticky; top: 0; height: 100vh; }
.sidebar .brand { padding: 0 8px; }
#nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
#nav a {
  color: var(--side-text); text-decoration: none; padding: 10px 12px; border-radius: 10px; font-weight: 500;
  display: flex; align-items: center; justify-content: space-between; gap: 6px;
}
#nav a:hover { background: rgba(255,255,255,.06); color: #fff; }
#nav a.active { background: rgba(255,255,255,.12); color: #fff; font-weight: 600; }
.content { padding: 28px 32px 60px; min-width: 0; }
.page-head { margin-bottom: 18px; }
.page-head p { margin: 4px 0 0; }

/* Cards / KPIs */
.card { background: var(--card); border: 1px solid var(--border); border-radius: 14px; padding: 18px; }
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; margin-bottom: 14px; }
.kpi { background: var(--card); border: 1px solid var(--border); border-radius: 14px; padding: 16px; }
.kpi .value { font-size: 28px; font-weight: 800; letter-spacing: -0.02em; }
.kpi .label { color: var(--muted); font-weight: 500; }
.grid2 { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr); gap: 14px; }
.grid2 > * { min-width: 0; }

.bars { display: flex; align-items: flex-end; gap: 6px; height: 220px; padding-top: 10px; overflow: hidden; }
.bar { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; gap: 4px; min-width: 0; }
.bar .fill { width: 100%; background: var(--primary); border-radius: 5px 5px 2px 2px; min-height: 2px; }
.bar .n { font-size: 11px; color: var(--muted); }
.bar .d { font-size: 10px; color: var(--faint); white-space: nowrap; }
.toplist { margin: 0; padding: 0; list-style: none; }
.toplist li { display: flex; justify-content: space-between; gap: 10px; padding: 8px 0; border-top: 1px solid var(--border); }
.toplist li:first-child { border-top: 0; }
.toplist .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.toplist .count { font-weight: 700; color: var(--primary); }

/* Tables */
.filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; align-items: center; }
.filters input[type=search] { flex: 1 1 240px; width: auto; }
.filters select { width: auto; }
.filters label { display: flex; align-items: center; gap: 6px; margin: 0; font-weight: 500; color: var(--muted); }
.filters input[type=date] { width: auto; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
td { padding: 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; }
td .sub { color: var(--muted); font-size: 12.5px; }
td a[href^="https://wa.me"] { white-space: nowrap; }
td.empty { text-align: center; color: var(--muted); padding: 32px; }
.pager { display: flex; justify-content: space-between; align-items: center; margin-top: 12px; color: var(--muted); }
.pager .btns { display: flex; gap: 6px; }
.pill { display: inline-block; font-size: 11.5px; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: #eef0f3; color: var(--muted); white-space: nowrap; }
.pill.ok { background: var(--primary-soft); color: var(--primary); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.bad { background: var(--danger-soft); color: var(--danger); }
.pill.wa { background: #e5f6ec; color: #13843f; }
.row-actions { display: flex; gap: 4px; justify-content: flex-end; flex-wrap: wrap; }
.link-btn { background: none; border: 0; padding: 0; color: var(--primary); font: inherit; font-weight: 600; cursor: pointer; text-align: left; }

/* Company PDFs */
.dropzone {
  display: flex; flex-direction: column; align-items: center; gap: 4px; margin: 0 0 14px;
  border: 2px dashed #c6ccd5; border-radius: 14px; padding: 26px; background: var(--card); cursor: pointer;
  font-weight: 400; color: var(--muted); text-align: center;
}
.dropzone strong { color: var(--text); font-size: 16px; }
.dropzone.over { border-color: var(--primary); background: var(--primary-soft); }
#uploads { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.upload { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px; display: flex; justify-content: space-between; gap: 12px; }
.upload .state { color: var(--muted); white-space: nowrap; }
.upload.done .state { color: var(--primary); font-weight: 600; }
.upload.failed .state { color: var(--danger); font-weight: 600; white-space: normal; }
.pdf-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 18px; }
.pdf-card { background: var(--card); border: 1px solid var(--border); border-radius: 14px; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.pdf-card.inactive { opacity: .55; }
.cover { aspect-ratio: 1 / 1.414; border-radius: 6px; background: #f2f3f5 center / cover no-repeat; border: 1px solid var(--border); display: grid; place-items: center; overflow: hidden; }
.cover img { width: 100%; height: 100%; object-fit: cover; }
.pdf-card .title { font-weight: 700; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.pdf-card .desc { color: var(--muted); font-size: 12.5px; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.pdf-card .meta { display: flex; flex-wrap: wrap; gap: 4px; }
.pdf-card .btns { display: flex; gap: 6px; margin-top: auto; }
.pdf-card .btns .btn { flex: 1; }
.empty-box { background: var(--card); border: 1px solid var(--border); border-radius: 14px; padding: 40px; text-align: center; color: var(--muted); grid-column: 1 / -1; }

/* Settings form */
.form { max-width: 760px; }
.form label:first-child { margin-top: 0; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.row2 + .row2 { margin-top: 14px; }
.actions { display: flex; align-items: center; gap: 10px; margin-top: 20px; }
.spacer { flex: 1; }
.preview { margin-top: 14px; background: #e9e1d6; border-radius: 12px; padding: 14px; }
.bubble { margin-top: 6px; background: #d9fdd3; border-radius: 10px; padding: 10px 12px; white-space: pre-wrap; max-width: 420px; box-shadow: 0 1px 1px rgba(0,0,0,.08); }

/* Zones / account types */
.mt-grid { margin-top: 14px; }
.group-list { display: flex; flex-direction: column; gap: 10px; }
.group-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 4px 12px; align-items: center; }
.group-row .gname { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.group-row .gnums { color: var(--muted); font-size: 13px; white-space: nowrap; }
.group-row .gbar { grid-column: 1 / -1; height: 6px; background: #eef0f3; border-radius: 99px; overflow: hidden; }
.group-row .gbar span { display: block; height: 100%; background: var(--primary); border-radius: 99px; }
.group-row.none .gname { color: var(--muted); font-weight: 500; }
.form-section { font-size: 15px; margin: 26px 0 2px; }
fieldset.audience { border: 0; padding: 0; margin: 12px 0 0; min-width: 0; }
fieldset.audience legend { font-weight: 600; padding: 0; margin-bottom: 6px; }
.chip-grid { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-flex; align-items: center; gap: 6px; margin: 0; padding: 6px 11px; border: 1px solid var(--border); border-radius: 999px; font-weight: 500; font-size: 13px; cursor: pointer; background: #fafbfc; user-select: none; }
.chip:has(input:checked) { background: var(--primary-soft); border-color: #bfe3da; color: var(--primary); font-weight: 600; }
.chip input { margin: 0; }
.page-head.with-action { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; }
.col-list { margin: 6px 0 4px; padding-left: 20px; color: var(--muted); }
.col-list li { margin: 2px 0; }
.col-list strong { color: var(--text); }
#bulk-preview { margin-top: 8px; }
.bulk-summary { margin-top: 12px; padding: 10px 12px; border-radius: 10px; background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.bulk-errors { margin: 8px 0 0; padding-left: 20px; max-height: 180px; overflow: auto; color: var(--danger); font-size: 13px; }
.type-list { display: flex; flex-direction: column; gap: 8px; margin: 10px 0; }
.type-row { display: flex; gap: 8px; }
.type-row input { flex: 1; }
select.inline { min-height: 32px; padding: 4px 8px; font-size: 13px; width: auto; max-width: 170px; }
select.inline.set { background: var(--primary-soft); border-color: #bfe3da; color: var(--primary); font-weight: 600; }
.pill.zone { background: #e8eefc; color: #3451b2; }
.pill.type { background: #fff3d6; color: #9a5b00; }
.pills { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }

/* Android app download card (dashboard) */
.app-card { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 14px; }
.app-card h2 { margin-bottom: 4px; }
.app-card-text { min-width: 0; }
.app-card-text a { font-weight: 600; word-break: break-all; }
.app-card-btns { display: flex; gap: 8px; flex-wrap: wrap; }
@media (max-width: 760px) { .app-card { flex-direction: column; align-items: stretch; } .app-card-btns .btn { flex: 1; } }

/* Dialog */
dialog { border: 0; border-radius: 16px; padding: 24px; width: min(520px, calc(100% - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(17,20,24,.45); }

.toast { position: fixed; right: 20px; bottom: 20px; background: #1f2430; color: #fff; padding: 12px 16px; border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.2); max-width: 360px; z-index: 10; }
.toast.error { background: var(--danger); }

/* ---------- Tablet (portrait + small laptops): sidebar becomes a top bar ---------- */
@media (max-width: 1024px) {
  .shell { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto 1fr; }
  .sidebar {
    position: sticky; top: 0; z-index: 5; height: auto;
    display: grid; grid-template-columns: 1fr auto; align-items: center;
    padding: 10px 16px 0; gap: 8px 12px;
  }
  .sidebar .brand { padding: 0; }
  #logout { grid-row: 1; grid-column: 2; min-height: 34px; padding: 0 10px; }
  /* One scrollable row of tabs instead of wrapping onto several lines. */
  #nav {
    grid-column: 1 / -1; flex-direction: row; flex-wrap: nowrap; gap: 4px;
    overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch;
    margin: 0 -16px; padding: 4px 16px 10px; scroll-padding: 0 16px;
  }
  #nav::-webkit-scrollbar { display: none; }
  #nav a { flex: 0 0 auto; white-space: nowrap; padding: 8px 14px; border-radius: 999px; gap: 6px; }
  .content { padding: 22px 20px 48px; }
  .grid2 { grid-template-columns: minmax(0, 1fr); }
  .kpis { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
  .filters input[type=search] { flex-basis: 100%; }
}

/* ---------- Phone ---------- */
@media (max-width: 760px) {
  body { font-size: 14.5px; }
  h1 { font-size: 21px; }
  .content { padding: 16px 14px 40px; }
  .page-head { margin-bottom: 14px; }
  .card { padding: 14px; border-radius: 12px; }
  .kpis { grid-template-columns: 1fr 1fr; gap: 8px; }
  .kpi { padding: 12px; border-radius: 12px; }
  .kpi .value { font-size: 22px; }
  .kpi .label { font-size: 12.5px; line-height: 1.3; }
  .row2 { grid-template-columns: 1fr; gap: 0; }
  .page-head.with-action { flex-direction: column; }
  select.inline { max-width: 100%; }
  .row2 > div + div { margin-top: 14px; }
  label.check.spaced { margin-top: 14px; }

  /* Filters: search on its own line, the rest two per line. */
  .filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  .filters input[type=search] { grid-column: 1 / -1; width: 100%; }
  .filters select, .filters .btn, .filters label { width: 100%; min-width: 0; }
  .filters label { flex-direction: column; align-items: stretch; gap: 2px; font-size: 12px; }
  .filters input[type=date] { width: 100%; min-width: 0; }

  /* Chart: fewer date labels so they do not overlap. */
  .bars { height: 170px; gap: 3px; }
  .bar .d { font-size: 10px; }
  .bar .d .m { display: none; } /* day number only on phones */
  .bar .n { font-size: 10px; }


  /* Company PDFs: two per row. */
  .pdf-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }
  .pdf-card { padding: 10px; }
  .dropzone { padding: 20px 14px; }
  .upload { flex-direction: column; gap: 2px; }

  .actions { flex-wrap: wrap; }
  dialog { padding: 18px; width: calc(100% - 24px); max-height: calc(100% - 24px); }
  .toast { left: 14px; right: 14px; bottom: 14px; max-width: none; }
  .login-card { padding: 24px 20px; }
}

/* ---------- Tables become cards on phones and portrait tablets ---------- */
@media (max-width: 900px) {
  /* Tables turn into cards: one card per row, "Label  value" lines. */
  .table-wrap { overflow: visible; }
  table, tbody, tr, td { display: block; width: 100%; }
  thead { display: none; }
  tr { border: 1px solid var(--border); border-radius: 12px; padding: 4px 12px; margin-bottom: 10px; background: var(--card); }
  /* Label on the left; the value (and its grey second line) stacked on the right. */
  td {
    display: grid; grid-template-columns: auto minmax(0, 1fr); column-gap: 14px; justify-items: end;
    align-items: start; padding: 8px 0; border-bottom: 1px solid #f0f1f4; text-align: right;
    overflow-wrap: anywhere;
  }
  td > * { grid-column: 2; max-width: 100%; }
  td[data-label=""] { display: block; }
  tr:last-child td { border-bottom: 1px solid #f0f1f4; }
  td:last-child, tr:last-child td:last-child { border-bottom: 0; }
  td::before {
    content: attr(data-label); grid-column: 1; grid-row: 1 / span 4; justify-self: start; text-align: left;
    color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .03em;
    padding-top: 2px;
  }
  td:not([data-label])::before, td[data-label=""]::before { display: none; }
  td.num { text-align: right; }
  td.empty { display: block; text-align: center; border: 0; }
  td .sub { text-align: right; }
  .row-actions { justify-content: flex-end; width: 100%; }
  .pager { flex-wrap: wrap; gap: 8px; }
  td a[href^="https://wa.me"] { white-space: nowrap; }
}
/* Portrait tablets: two cards side by side. */
@media (min-width: 600px) and (max-width: 900px) {
  tbody { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; align-items: start; }
  tr { margin-bottom: 0; }
  tr:has(td.empty) { grid-column: 1 / -1; }
}

@media (max-width: 360px) {
  .kpis { grid-template-columns: 1fr; }
  .filters { grid-template-columns: 1fr; }
}
