/* ── TOKENS ── */
:root {
  --green:       #16a34a;
  --green-dark:  #15803d;
  --green-light: #f0fdf4;
  --sidebar-bg:  #0f172a;
  --sidebar-txt: #94a3b8;
  --sidebar-hvr: #1e293b;
  --bg:          #f1f5f9;
  --surface:     #ffffff;
  --border:      #e2e8f0;
  --text:        #0f172a;
  --muted:       #64748b;
  --danger:      #dc2626;
  --tag-bg:      #eff6ff;
  --tag-txt:     #1e40af;
  --shadow-sm:   0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.05);
  --shadow:      0 4px 12px rgba(0,0,0,.08);
  --r:           8px;
  --sidebar-w:   230px;
  --header-h:    54px;
}

/* ── RESET ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  min-height: 100vh;
}

h1, h2, h3 { font-weight: 700; line-height: 1.2; }
p { color: var(--muted); }

/* ── LOGIN TOGGLE ── */
body.locked .app-shell  { display: none; }
body:not(.locked) #loginView { display: none; }

/* ── LOGIN ── */
.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: linear-gradient(145deg, #0f172a 0%, #1e3a5f 100%);
}

.login-card {
  width: min(400px, 100%);
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 32px;
  background: var(--surface);
  border-radius: 12px;
  box-shadow: 0 24px 64px rgba(0,0,0,.35);
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 8px;
}

.login-brand h1 { font-size: 1.5rem; }
.login-brand p  { font-size: 13px; }

.login-error {
  min-height: 18px;
  font-size: 13px;
  font-weight: 600;
  color: var(--danger);
  text-align: center;
}

/* ── LOGO MARK ── */
.logo-mark {
  flex-shrink: 0;
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 10px;
  background: var(--green);
  color: #fff;
  font-weight: 800;
  font-size: 14px;
  letter-spacing: .05em;
}
.logo-mark.sm { width: 30px; height: 30px; border-radius: 7px; font-size: 11px; }

/* ── BUTTONS ── */
button { font: inherit; cursor: pointer; border: none; border-radius: var(--r); }
button:disabled { opacity: .5; cursor: not-allowed; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; font-weight: 600; }

.btn-primary {
  min-height: 38px; padding: 0 18px;
  background: var(--green); color: #fff; font-size: 14px;
  transition: background .15s;
}
.btn-primary:not(:disabled):hover { background: var(--green-dark); }
.btn-full { width: 100%; }

.btn-outline {
  min-height: 38px; padding: 0 16px;
  background: var(--surface); color: var(--text);
  border: 1.5px solid var(--border); font-size: 14px;
}
.btn-outline:hover { border-color: var(--muted); }

.btn-ghost {
  min-height: 34px; padding: 0 12px;
  background: transparent; color: var(--muted); font-size: 13px;
}
.btn-ghost:hover { background: var(--bg); color: var(--text); }

.btn-sm {
  min-height: 32px; padding: 0 12px;
  background: var(--surface); color: var(--text); font-size: 13px;
  border: 1.5px solid var(--border);
}
.btn-sm:hover { border-color: var(--muted); }

.btn-xs {
  min-height: 22px; padding: 0 8px; font-size: 11px; font-weight: 600;
  background: transparent; color: var(--sidebar-txt);
  border: 1px solid rgba(255,255,255,.12);
}
.btn-xs:hover { background: var(--sidebar-hvr); color: #fff; border-color: transparent; }

.btn-range {
  flex: 1; min-height: 28px; font-size: 12px; font-weight: 600;
  background: transparent; color: var(--sidebar-txt);
  border: 1px solid rgba(255,255,255,.1);
  transition: all .12s;
}
.btn-range:hover  { background: var(--sidebar-hvr); color: #fff; border-color: transparent; }
.btn-range.active { background: var(--green); color: #fff; border-color: transparent; }

.btn-sidebar-submit {
  width: 100%; min-height: 36px; margin-top: 4px; font-size: 13px; font-weight: 700;
  background: rgba(22,163,74,.18); color: #4ade80;
  border: 1px solid rgba(22,163,74,.3);
  transition: all .15s;
}
.btn-sidebar-submit:hover { background: var(--green); color: #fff; border-color: transparent; }

.btn-icon {
  width: 30px; height: 30px; padding: 0;
  background: transparent; color: var(--muted); font-size: 16px;
  border-radius: 6px;
}
.btn-icon:hover { background: var(--bg); color: var(--text); }

/* ── FORM ELEMENTS ── */
label {
  display: grid; gap: 5px;
  font-size: 11px; font-weight: 700;
  color: var(--muted); text-transform: uppercase; letter-spacing: .04em;
}

input, select, textarea {
  font: inherit; font-size: 14px; width: 100%;
  min-height: 38px; padding: 0 10px;
  border: 1.5px solid var(--border); border-radius: 6px;
  color: var(--text); background: var(--surface); outline: none;
  transition: border-color .15s;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(22,163,74,.12);
}

select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2.5'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 32px;
}

textarea { min-height: 100px; padding: 10px; resize: vertical; }

/* ── APP SHELL ── */
.app-shell {
  display: grid;
  grid-template-rows: var(--header-h) 1fr;
  min-height: 100vh;
}

/* ── HEADER ── */
.app-header {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

.hd-start { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.hd-title { font-size: 15px; font-weight: 700; }
.hd-sep   { width: 1px; height: 18px; background: var(--border); }
.hd-status {
  font-size: 12px; color: var(--muted);
  max-width: 260px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.hd-status.error { color: var(--danger); }

.hd-nav { display: flex; align-items: center; gap: 2px; }

.nav-tab {
  min-height: 32px; padding: 0 14px;
  background: transparent; color: var(--muted); font-size: 13px; font-weight: 600;
  border-radius: 6px; transition: all .12s;
}
.nav-tab:hover { background: var(--bg); color: var(--text); }
.nav-tab[aria-pressed="true"] { background: var(--green-light); color: var(--green-dark); }

#usersNavButton        { display: none; }
#usersNavButton.active { display: inline-flex; }

.hd-end { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }

.user-chip {
  padding: 3px 10px; border-radius: 20px;
  background: var(--bg); color: var(--muted);
  font-size: 12px; font-weight: 700;
}

/* ── APP BODY ── */
.app-body {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 0;
  overflow: hidden;
}

/* ── SIDEBAR ── */
.sidebar {
  background: var(--sidebar-bg);
  border-right: 1px solid rgba(255,255,255,.05);
  overflow-y: auto;
  padding: 18px 14px;
}

#filters { display: flex; flex-direction: column; gap: 22px; }

.sf-block { display: flex; flex-direction: column; gap: 10px; }

.sf-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}

.sf-label {
  font-size: 10px; font-weight: 800; letter-spacing: .08em;
  text-transform: uppercase; color: var(--sidebar-txt);
}

.sf-actions { display: flex; gap: 4px; }

.label-choices {
  display: flex; flex-direction: column; gap: 2px;
  max-height: 260px; overflow-y: auto;
}

.label-choice { position: relative; }

.label-choice input {
  position: absolute; opacity: 0; pointer-events: none;
  width: 1px; min-height: 1px;
}

.label-choice span {
  display: flex; align-items: center;
  min-height: 32px; padding: 0 10px; border-radius: 6px;
  color: var(--sidebar-txt); font-size: 13px; font-weight: 500;
  cursor: pointer; transition: all .1s;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.label-choice span:hover { background: var(--sidebar-hvr); color: #fff; }
.label-choice input:checked + span { background: var(--green); color: #fff; }

.quick-ranges { display: flex; gap: 4px; }

.date-fields { display: flex; flex-direction: column; gap: 8px; }

.sidebar label {
  font-size: 10px; color: var(--sidebar-txt); letter-spacing: .06em;
}

.sidebar input[type="date"] {
  min-height: 34px; font-size: 13px;
  background: rgba(255,255,255,.05);
  border-color: rgba(255,255,255,.1);
  color: #e2e8f0;
}
.sidebar input[type="date"]:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(22,163,74,.2);
  background: rgba(255,255,255,.08);
}

/* ── MAIN CONTENT ── */
.main-content {
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ── CONTACTS VIEW ── */
#contactsView { display: flex; flex-direction: column; gap: 14px; }
#contactsView.hidden { display: none; }

/* ── ACTION BAR ── */
.action-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--r);
  box-shadow: var(--shadow-sm);
  flex-wrap: wrap;
}

.ab-left { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.check-wrap {
  display: flex; align-items: center; cursor: pointer;
  font-size: inherit; text-transform: none; letter-spacing: 0;
  color: inherit;
}
.check-wrap input { width: 15px; min-height: 15px; accent-color: var(--green); }

.count-group { display: flex; align-items: center; gap: 6px; }

.count-badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 10px; border-radius: 20px;
  background: var(--bg); color: var(--muted);
  font-size: 12px; font-weight: 600;
}
.count-badge strong { color: var(--text); font-weight: 700; }
.count-badge.sel { background: var(--green-light); color: var(--green-dark); }
.count-badge.sel strong { color: var(--green-dark); }

.ab-btns { display: flex; gap: 6px; }

/* ── TABLE ── */
.table-wrap {
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--r);
  box-shadow: var(--shadow-sm);
}

table { width: 100%; min-width: 680px; border-collapse: collapse; }

thead th {
  position: sticky; top: 0; z-index: 1;
  padding: 10px 14px;
  background: #f8fafc;
  border-bottom: 1.5px solid var(--border);
  text-align: left; white-space: nowrap;
  font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .05em; color: var(--muted);
}

tbody td {
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle; font-size: 13px;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: #f8fafc; }

.select-col { width: 42px; text-align: center; }
.select-col input { width: 15px; min-height: 15px; cursor: pointer; accent-color: var(--green); }

.contact-name { font-weight: 600; color: var(--text); }

.subtle { color: var(--muted); font-size: 12px; }
.sidebar .subtle { color: var(--sidebar-txt); font-size: 12px; padding: 4px 0; }

.tag {
  display: inline-flex; align-items: center;
  padding: 2px 8px; margin: 1px 3px 1px 0;
  border-radius: 4px;
  background: var(--tag-bg); color: var(--tag-txt);
  font-size: 11px; font-weight: 600;
}

.empty {
  height: 160px; text-align: center; vertical-align: middle;
  color: var(--muted); font-size: 14px;
}

/* ── SENT PANEL ── */
.sent-panel {
  background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--r);
  box-shadow: var(--shadow-sm);
}

.sent-panel-hd {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.sent-panel-hd h2 { font-size: 14px; }
.sent-panel-hd p  { font-size: 12px; margin-top: 2px; }

.contact-search-bar {
  padding: 10px 0 6px;
}
.contact-search-bar input {
  width: 100%; box-sizing: border-box;
  padding: 7px 12px; font-size: 13px;
  border: 1px solid var(--border); border-radius: var(--r);
  background: var(--bg); color: var(--text);
  outline: none;
}
.contact-search-bar input:focus { border-color: var(--accent); }

.sent-panel-search {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}
.sent-panel-search input {
  width: 100%; box-sizing: border-box;
  padding: 6px 10px; font-size: 13px;
  border: 1px solid var(--border); border-radius: var(--r);
  background: var(--bg); color: var(--text);
  outline: none;
}
.sent-panel-search input:focus { border-color: var(--accent); }

.sent-list { display: flex; flex-direction: column; gap: 10px; padding: 14px 16px; }

.sent-item {
  display: grid; gap: 6px;
  padding: 12px 14px;
  border: 1px solid var(--border); border-radius: 6px;
  background: #fafbfc;
}
.sent-item > div:first-child {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
}
.sent-item > div:first-child strong { font-size: 13px; font-weight: 700; }
.sent-item > div:first-child span   { font-size: 12px; color: var(--muted); }
.sent-item p { font-size: 13px; color: var(--text); line-height: 1.5; }
.sent-meta {
  display: flex; justify-content: space-between; gap: 10px;
  font-size: 11px; color: var(--muted); font-weight: 600;
}

/* ── OTHER VIEWS ── */
#usersView, #metricsView { display: none; }
#usersView.active, #metricsView.active {
  display: flex; flex-direction: column; gap: 16px;
}
#contactsView.hidden { display: none; }

.view-hd {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 16px;
}
.view-hd h2 { font-size: 1.2rem; }
.view-hd p  { margin-top: 4px; }

/* ── USERS ── */
.admin-card {
  display: none;
  padding: 20px;
  background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--r);
  box-shadow: var(--shadow-sm);
}
.admin-card.active { display: block; }

.admin-card h3 {
  font-size: 13px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .04em; color: var(--muted);
  margin-bottom: 14px; padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}

.user-form {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px; align-items: end;
}

.form-submit-col { display: flex; align-items: flex-end; }
.form-submit-col .btn { width: 100%; }

.row-actions { display: flex; gap: 6px; }
.row-actions button {
  min-height: 30px; padding: 0 10px; font-size: 12px; font-weight: 600;
  background: var(--surface); border: 1.5px solid var(--border); border-radius: 5px;
}
.row-actions button:hover { border-color: var(--muted); }

/* ── METRICS ── */
.metric-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

.metric-card {
  padding: 18px;
  background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--r);
  box-shadow: var(--shadow-sm);
}
.metric-card span {
  font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .05em; color: var(--muted);
}
.metric-card strong {
  display: block; margin-top: 8px;
  font-size: 28px; font-weight: 700; color: var(--text);
}

.metric-panels {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.metric-panel {
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--r);
  box-shadow: var(--shadow-sm);
}
.metric-panel h3 {
  font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .05em; color: var(--muted); margin-bottom: 12px;
}

.ranking-row, .series-row {
  display: grid; grid-template-columns: 1fr auto; gap: 12px;
  align-items: center; padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.ranking-row:last-child, .series-row:last-child { border-bottom: none; }
.ranking-row > div > strong, .series-row > div > strong { font-size: 13px; font-weight: 600; }
.ranking-row > strong, .series-row > strong { font-size: 13px; }

.bar { height: 4px; margin-top: 6px; border-radius: 2px; background: #e2e8f0; overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--green); border-radius: 2px; }

/* ── MODAL ── */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 100;
  display: grid; place-items: center; padding: 20px;
  background: rgba(15,23,42,.55);
  backdrop-filter: blur(4px);
}
.modal-backdrop[hidden] { display: none; }

.modal {
  width: min(540px, 100%); max-height: 90vh; overflow-y: auto;
  display: flex; flex-direction: column; gap: 16px;
  padding: 24px;
  background: var(--surface);
  border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 20px 60px rgba(0,0,0,.22);
}

.modal-hd {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
}
.modal-hd h2 { font-size: 16px; }
.modal-hd p  { font-size: 13px; margin-top: 3px; }

.template-meta {
  font-size: 12px; font-weight: 600; color: var(--muted);
  padding: 8px 10px; background: var(--bg); border-radius: 5px;
}

.template-params {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px;
}

.template-preview-wrap {
  border: 1.5px solid var(--border); border-radius: 6px; overflow: hidden;
}
.preview-lbl {
  display: block; padding: 6px 12px;
  background: var(--bg); border-bottom: 1px solid var(--border);
  font-size: 10px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .06em; color: var(--muted);
}
.template-preview {
  padding: 12px; max-height: 160px; overflow-y: auto;
  font-size: 13px; line-height: 1.6; white-space: pre-wrap; color: var(--text);
}

.modal-ft {
  display: flex; justify-content: flex-end; gap: 10px;
  padding-top: 8px; border-top: 1px solid var(--border);
}

/* ── UTILITIES ── */
.error { color: var(--danger) !important; }

/* ── RESPONSIVE ── */
@media (max-width: 1100px) {
  .metric-grid { grid-template-columns: repeat(2, 1fr); }
  .user-form   { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 860px) {
  :root { --sidebar-w: 200px; }
  .metric-grid, .metric-panels, .user-form { grid-template-columns: 1fr; }
}

@media (max-width: 680px) {
  .app-body { grid-template-columns: 1fr; }
  .sidebar  { display: none; }
  .hd-nav   { display: none; }
  .main-content { padding: 12px; }
  .action-bar { flex-direction: column; align-items: stretch; }
}
