/* ============================================================
   Kamero Links — design system
   Brand: purple #6F4898 (primary), yellow #FFB700 (secondary)
   A single source of truth: tokens → primitives → components.
   ============================================================ */

:root {
  /* Brand */
  --purple: #6f4898;
  --purple-600: #5d3a82;
  --purple-700: #46295f;
  --purple-300: #a98cc7;
  --purple-100: #efe9f5;
  --yellow: #ffb700;
  --yellow-600: #e6a500;

  /* Neutrals (cool, slightly purple-tinted grays) */
  --bg: #f7f6fb;
  --surface: #ffffff;
  --surface-2: #f4f2f8;
  --sidebar: #1e1530;
  --sidebar-2: #2a1f42;
  --border: #e9e4f1;
  --border-strong: #ddd5ea;

  --text: #1c1530;
  --text-2: #4a4360;
  --muted: #7b7491;
  --muted-2: #a9a3bd;

  /* Semantic */
  --success: #189d54;
  --success-bg: #e6f7ee;
  --danger: #d63b3b;
  --danger-bg: #fcebeb;
  --warning: #c98a00;

  /* Spacing scale (4px base) */
  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 20px;
  --s6: 24px; --s8: 32px; --s10: 40px; --s12: 48px;

  /* Radii */
  --r-sm: 8px; --r: 12px; --r-lg: 16px; --r-xl: 22px;

  /* Type scale */
  --fs-xs: 12px; --fs-sm: 13px; --fs-base: 14px; --fs-md: 15px;
  --fs-lg: 18px; --fs-xl: 22px; --fs-2xl: 28px; --fs-3xl: 34px;

  /* Shadows */
  --shadow-xs: 0 1px 2px rgba(28, 21, 48, 0.06);
  --shadow-sm: 0 2px 6px rgba(28, 21, 48, 0.07);
  --shadow: 0 6px 20px rgba(28, 21, 48, 0.09);
  --shadow-lg: 0 18px 48px rgba(28, 21, 48, 0.18);
  --ring: 0 0 0 3px rgba(111, 72, 152, 0.18);

  --sidebar-w: 248px;
  --topbar-h: 64px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html { -webkit-text-size-adjust: 100%; }
body {
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: var(--fs-base);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

a { color: var(--purple); text-decoration: none; }
a:hover { color: var(--purple-600); }

h1, h2, h3 { letter-spacing: -0.02em; line-height: 1.25; }
h1 { font-size: var(--fs-2xl); font-weight: 800; }
h2 { font-size: var(--fs-lg); font-weight: 700; }
h3 { font-size: var(--fs-md); font-weight: 700; }

.muted { color: var(--muted); }
.mono { font-family: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace; font-size: 0.92em; }
.right { text-align: right; }
.row { display: flex; align-items: center; gap: var(--s3); flex-wrap: wrap; }
.spacer { flex: 1; }
.hidden { display: none !important; }
.tabular { font-variant-numeric: tabular-nums; }

/* ---------- Buttons ---------- */
button, .btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
  font-family: inherit; font-size: var(--fs-base); font-weight: 600; line-height: 1;
  padding: 10px 16px; color: #fff; background: var(--purple);
  border: 1px solid transparent; border-radius: var(--r-sm); cursor: pointer;
  transition: background .15s, box-shadow .15s, transform .04s, border-color .15s;
  box-shadow: var(--shadow-xs); white-space: nowrap;
}
button:hover, .btn:hover { background: var(--purple-600); color: #fff; }
button:active { transform: translateY(1px); }
button:disabled { opacity: .5; cursor: not-allowed; }

.btn-secondary { background: var(--yellow); color: #3a2a00; }
.btn-secondary:hover { background: var(--yellow-600); color: #3a2a00; }
.btn-ghost { background: var(--surface); color: var(--text-2); border-color: var(--border-strong); box-shadow: none; }
.btn-ghost:hover { background: var(--surface-2); border-color: var(--purple-300); color: var(--text); }
.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #bd3030; }
.btn-sm { padding: 7px 11px; font-size: var(--fs-sm); }
.btn-icon { padding: 8px; }

/* ---------- Inputs ---------- */
label { display: block; font-size: var(--fs-sm); font-weight: 600; color: var(--text-2); margin-bottom: 6px; }
.field { margin-bottom: var(--s4); }
input[type="text"], input[type="url"], input[type="number"], input[type="password"], input[type="search"], select {
  width: 100%; font-family: inherit; font-size: var(--fs-base); color: var(--text);
  padding: 10px 13px; background: var(--surface);
  border: 1px solid var(--border-strong); border-radius: var(--r-sm); outline: none;
  transition: border-color .15s, box-shadow .15s;
}
input::placeholder { color: var(--muted-2); }
input:focus, select:focus { border-color: var(--purple); box-shadow: var(--ring); }
select { cursor: pointer; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M3 4.5L6 7.5L9 4.5' stroke='%237b7491' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 11px center; padding-right: 30px; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r-lg); padding: var(--s6); box-shadow: var(--shadow-sm);
}
.card-head { display: flex; align-items: center; gap: var(--s3); margin-bottom: var(--s5); }
.card-head h2 { margin: 0; }
.card + .card { margin-top: var(--s5); }
.section-gap { margin-top: var(--s6); }

/* ---------- Badges ---------- */
.badge { display: inline-flex; align-items: center; gap: 5px; padding: 3px 9px; border-radius: 999px; font-size: var(--fs-xs); font-weight: 600; }
.badge-api { background: var(--purple-100); color: var(--purple); }
.badge-web { background: #eee9f3; color: var(--muted); }
.badge-on { background: var(--success-bg); color: var(--success); }
.badge-off { background: var(--danger-bg); color: var(--danger); }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: .8; }

/* ---------- Alerts ---------- */
.alert { padding: 11px 14px; border-radius: var(--r-sm); font-size: var(--fs-base); margin-bottom: var(--s4); }
.alert-error { background: var(--danger-bg); border: 1px solid #f3cccc; color: #a82d2d; }
.alert-success { background: var(--success-bg); border: 1px solid #bfe9d1; color: #11703c; }

/* ============================================================
   App shell — fixed sidebar + scrollable content
   ============================================================ */
.shell { display: flex; min-height: 100vh; }

.sidebar {
  position: fixed; inset: 0 auto 0 0; width: var(--sidebar-w);
  background: linear-gradient(180deg, var(--sidebar), var(--sidebar-2));
  color: #cfc6e0; display: flex; flex-direction: column; padding: var(--s5) var(--s4);
  z-index: 30;
}
.sidebar .brand { display: flex; align-items: center; gap: 11px; font-weight: 700; font-size: var(--fs-lg); color: #fff; padding: 0 var(--s2) var(--s6); }
.sidebar .brand .logo {
  width: 30px; height: 30px; border-radius: 9px; flex-shrink: 0;
  background: linear-gradient(135deg, var(--purple), var(--yellow));
  display: grid; place-items: center; box-shadow: 0 4px 12px rgba(111, 72, 152, .5);
}
.sidebar .brand .logo svg { width: 17px; height: 17px; }
.nav-group { display: flex; flex-direction: column; gap: 2px; }
.nav-label { font-size: 11px; text-transform: uppercase; letter-spacing: .07em; color: #8c81a6; padding: var(--s4) var(--s2) var(--s2); font-weight: 700; }
.nav-item {
  display: flex; align-items: center; gap: var(--s3); padding: 9px var(--s3); border-radius: var(--r-sm);
  color: #cfc6e0; font-size: var(--fs-base); font-weight: 500; cursor: pointer; transition: background .15s, color .15s;
  background: none; border: none; width: 100%; text-align: left; box-shadow: none;
}
.nav-item:hover { background: rgba(255, 255, 255, .07); color: #fff; }
.nav-item.active { background: rgba(111, 72, 152, .45); color: #fff; }
.nav-item svg { width: 18px; height: 18px; flex-shrink: 0; opacity: .9; }
.sidebar .sidebar-foot { margin-top: auto; padding-top: var(--s4); border-top: 1px solid rgba(255,255,255,.08); }

.main { flex: 1; margin-left: var(--sidebar-w); min-width: 0; display: flex; flex-direction: column; }

.topbar {
  height: var(--topbar-h); display: flex; align-items: center; gap: var(--s4);
  padding: 0 var(--s8); background: rgba(255,255,255,.8); backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20;
}
.topbar h1 { font-size: var(--fs-xl); }
.topbar .sub { color: var(--muted); font-size: var(--fs-sm); }

.content { padding: var(--s8); max-width: 1280px; width: 100%; }
.view { display: none; }
.view.active { display: block; animation: fade .2s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* ---------- Stat cards ---------- */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: var(--s4); }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg);
  padding: var(--s5); box-shadow: var(--shadow-sm); position: relative; overflow: hidden;
}
.stat .stat-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--s3); }
.stat .stat-ico { width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center; background: var(--purple-100); color: var(--purple); }
.stat .stat-ico svg { width: 19px; height: 19px; }
.stat .stat-ico.alt { background: #fff5da; color: var(--yellow-600); }
.stat .value { font-size: var(--fs-2xl); font-weight: 800; letter-spacing: -.03em; line-height: 1; }
.stat .label { font-size: var(--fs-sm); color: var(--muted); margin-top: 6px; font-weight: 500; }

/* ---------- Charts ---------- */
.chart-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: var(--s5); }
.chart-host { width: 100%; }
.chart-svg { width: 100%; height: auto; display: block; }
.chart-empty { color: var(--muted); font-size: var(--fs-base); text-align: center; padding: var(--s10) var(--s3); background: var(--surface-2); border-radius: var(--r); }

.chart-legend { display: flex; gap: var(--s5); flex-wrap: wrap; margin-bottom: var(--s2); font-size: var(--fs-sm); color: var(--muted); }
.chart-legend-item { display: inline-flex; align-items: center; gap: 7px; }
.chart-dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex-shrink: 0; }

.donut-wrap { display: flex; align-items: center; gap: var(--s5); flex-wrap: wrap; }
.donut-svg { width: 150px; height: 150px; flex-shrink: 0; }
.donut-legend { flex: 1; min-width: 150px; display: flex; flex-direction: column; gap: 7px; }
.chart-legend-row { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: var(--s2); font-size: var(--fs-sm); }
.chart-legend-label { color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chart-legend-val { color: var(--muted); font-variant-numeric: tabular-nums; }

.bars { display: flex; flex-direction: column; gap: var(--s2); }
.bar-row { display: grid; grid-template-columns: 120px 1fr 48px; align-items: center; gap: var(--s3); font-size: var(--fs-sm); }
.bar-row > span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-2); }
.bar-track { background: var(--surface-2); border-radius: 6px; height: 10px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 6px; background: linear-gradient(90deg, var(--purple), var(--purple-300)); }

/* Segmented control */
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border-strong); border-radius: var(--r-sm); padding: 3px; gap: 2px; }
.seg-btn { background: transparent; color: var(--muted); box-shadow: none; padding: 5px 12px; font-size: var(--fs-sm); font-weight: 600; border-radius: 6px; }
.seg-btn:hover { background: var(--surface); color: var(--purple); }
.seg-btn.active, .seg-btn.active:hover { background: var(--purple); color: #fff; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; border-radius: var(--r); }
table { width: 100%; border-collapse: collapse; font-size: var(--fs-base); }
th, td { text-align: left; padding: 12px 14px; border-bottom: 1px solid var(--border); }
th { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 700; background: var(--surface-2); }
thead tr th:first-child { border-top-left-radius: var(--r); }
thead tr th:last-child { border-top-right-radius: var(--r); }
tbody tr { transition: background .12s; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: none; }
td.actions { white-space: nowrap; text-align: right; }
td.actions button { margin-left: 6px; }

.table-scroll { max-height: 360px; overflow-y: auto; border: 1px solid var(--border); border-radius: var(--r); }
table.compact { font-size: var(--fs-sm); }
table.compact th { position: sticky; top: 0; z-index: 1; }
table.compact th, table.compact td { padding: 9px 12px; }

.toolbar { display: flex; gap: var(--s3); flex-wrap: wrap; margin-bottom: var(--s5); }
.toolbar input, .toolbar select { width: auto; flex: 1; min-width: 150px; }
.search-wrap { position: relative; flex: 2; min-width: 200px; }
.search-wrap svg { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); width: 16px; height: 16px; color: var(--muted-2); pointer-events: none; }
.search-wrap input { width: 100%; padding-left: 36px; }

.pagination { display: flex; gap: var(--s3); align-items: center; justify-content: center; margin-top: var(--s5); }

.loading { display: none; color: var(--muted); font-size: var(--fs-base); padding: var(--s2) 0; }
.loading.show { display: block; }
.empty { text-align: center; color: var(--muted); padding: var(--s10); }

/* ---------- Modals ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(28, 21, 48, .5); display: none; align-items: flex-start; justify-content: center; z-index: 60; padding: var(--s8) var(--s4); backdrop-filter: blur(4px); overflow-y: auto; }
.modal-backdrop.open { display: flex; }
.modal { background: var(--surface); border-radius: var(--r-lg); padding: var(--s6); max-width: 760px; width: 100%; box-shadow: var(--shadow-lg); margin: auto; }
.modal h2 { display: flex; justify-content: space-between; align-items: center; gap: var(--s4); }
.modal .close { background: none; color: var(--muted); padding: 4px 9px; font-size: 22px; box-shadow: none; }
.modal .close:hover { background: var(--surface-2); color: var(--text); }

/* ---------- Login ---------- */
.login-screen {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; padding: var(--s5); z-index: 100;
  background:
    radial-gradient(900px 480px at 100% -8%, rgba(255,183,0,.16), transparent 60%),
    radial-gradient(1100px 620px at -5% -10%, rgba(111,72,152,.28), transparent 55%),
    var(--sidebar);
}
.login-card { width: 100%; max-width: 410px; background: var(--surface); border-radius: var(--r-xl); padding: var(--s8); box-shadow: var(--shadow-lg); }
.login-brand { display: flex; align-items: center; gap: 11px; font-weight: 700; font-size: var(--fs-lg); margin-bottom: var(--s6); }
.login-brand .logo { width: 32px; height: 32px; border-radius: 9px; background: linear-gradient(135deg, var(--purple), var(--yellow)); display: grid; place-items: center; box-shadow: 0 4px 12px rgba(111,72,152,.4); }
.login-brand .logo svg { width: 18px; height: 18px; }

/* ---------- Public home page ---------- */
.home-wrap { min-height: 100vh; display: flex; flex-direction: column; }
.home-header { display: flex; align-items: center; justify-content: space-between; padding: var(--s4) var(--s8); border-bottom: 1px solid var(--border); background: rgba(255,255,255,.8); backdrop-filter: blur(10px); position: sticky; top: 0; z-index: 10; }
.home-header .brand { display: flex; align-items: center; gap: 11px; font-weight: 700; font-size: var(--fs-lg); }
.home-header .brand .logo { width: 30px; height: 30px; border-radius: 9px; background: linear-gradient(135deg, var(--purple), var(--yellow)); display: grid; place-items: center; box-shadow: 0 4px 12px rgba(111,72,152,.4); }
.home-header .brand .logo svg { width: 17px; height: 17px; }
.home-header nav a { color: var(--muted); margin-left: var(--s5); font-weight: 600; font-size: var(--fs-base); }
.home-header nav a:hover { color: var(--purple); }
.home-main { flex: 1; display: flex; align-items: flex-start; justify-content: center; padding: var(--s12) var(--s5); }
.home-card { width: 100%; max-width: 600px; }
.hero { text-align: center; margin-bottom: var(--s8); }
.hero h1 { font-size: var(--fs-3xl); margin-bottom: var(--s3); }
.hero p { color: var(--muted); font-size: var(--fs-md); }

.result-box { margin-top: var(--s5); padding: var(--s5); background: linear-gradient(135deg, var(--purple-100), var(--surface-2)); border: 1px solid var(--border-strong); border-radius: var(--r); }
.result-box .short-link { font-size: var(--fs-lg); font-weight: 700; color: var(--purple); word-break: break-all; }

details summary { color: var(--purple); cursor: pointer; margin-bottom: var(--s4); font-weight: 600; font-size: var(--fs-base); list-style: none; }
details summary::-webkit-details-marker { display: none; }
details summary::before { content: "+ "; font-weight: 700; }
details[open] summary::before { content: "− "; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s3); }

/* ---------- Responsive ---------- */
.menu-toggle { display: none; }
@media (max-width: 860px) {
  .sidebar { transform: translateX(-100%); transition: transform .2s ease; box-shadow: var(--shadow-lg); width: 248px; }
  .sidebar.open { transform: none; }
  .main { margin-left: 0; }
  .menu-toggle { display: inline-flex; }
  .content { padding: var(--s5); }
  .topbar { padding: 0 var(--s5); }
}
@media (max-width: 560px) { .grid-2 { grid-template-columns: 1fr; } }

/* ============================================================
   Country picker + geo-rules (country-based routing)
   ============================================================ */

/* Each routing rule: country picker · destination URL · delete */
.geo-rules { display: flex; flex-direction: column; gap: var(--s3); }
.geo-rule {
  display: grid; grid-template-columns: 220px 1fr auto; gap: var(--s3); align-items: center;
  animation: fade .18s ease;
}
.geo-rule .geo-rule-url { margin: 0; }
.geo-rule .geo-rule-del { flex-shrink: 0; color: var(--muted); }
.geo-rule .geo-rule-del:hover { color: var(--danger); border-color: #f0caca; background: var(--danger-bg); }

.geo-empty { color: var(--muted); font-size: var(--fs-sm); padding: var(--s3) 0; }
.geo-add { margin-top: var(--s3); }
.geo-hint { color: var(--muted); font-size: var(--fs-xs); margin-top: var(--s2); display: flex; align-items: center; gap: 6px; }
.geo-hint svg { width: 13px; height: 13px; flex-shrink: 0; }

/* Country picker combobox */
.cpick { position: relative; }
.cpick-btn {
  width: 100%; justify-content: space-between; gap: var(--s2);
  padding: 9px 12px; font-weight: 500; color: var(--muted);
}
.cpick-btn.has-value { color: var(--text); font-weight: 600; }
.cpick-val { display: inline-flex; align-items: center; gap: 9px; overflow: hidden; }
.cpick-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cpick-flag { font-size: 17px; line-height: 1; flex-shrink: 0; }
.cpick-caret { width: 15px; height: 15px; flex-shrink: 0; transition: transform .15s; opacity: .7; }
.cpick.open .cpick-caret { transform: rotate(180deg); }
.cpick.open .cpick-btn { border-color: var(--purple); box-shadow: var(--ring); }

.cpick-panel {
  display: none; position: absolute; z-index: 70; top: calc(100% + 6px); left: 0; right: 0;
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--r);
  box-shadow: var(--shadow-lg); overflow: hidden; min-width: 240px;
}
.cpick.open .cpick-panel { display: block; }
.cpick-search-wrap { position: relative; padding: var(--s3); border-bottom: 1px solid var(--border); }
.cpick-search-wrap svg { position: absolute; left: 23px; top: 50%; transform: translateY(-50%); width: 15px; height: 15px; color: var(--muted-2); pointer-events: none; }
.cpick-search { width: 100%; padding-left: 34px !important; }
.cpick-list { list-style: none; max-height: 260px; overflow-y: auto; padding: var(--s2); margin: 0; }
.cpick-opt {
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: var(--r-sm); cursor: pointer; font-size: var(--fs-base);
}
.cpick-opt:hover, .cpick-opt.active { background: var(--surface-2); }
.cpick-opt.selected { background: var(--purple-100); color: var(--purple); font-weight: 600; }
.cpick-opt.disabled { opacity: .38; cursor: not-allowed; }
.cpick-opt-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cpick-opt-code { font-size: var(--fs-xs); color: var(--muted-2); font-variant-numeric: tabular-nums; }
.cpick-empty { padding: var(--s4); text-align: center; color: var(--muted); font-size: var(--fs-sm); }

@media (max-width: 560px) {
  .geo-rule { grid-template-columns: 1fr auto; grid-template-areas: "picker del" "url url"; }
  .geo-rule .geo-rule-picker { grid-area: picker; }
  .geo-rule .geo-rule-del { grid-area: del; }
  .geo-rule .geo-rule-url { grid-area: url; }
}

/* Country rules summary in the public result box */
.result-geo { margin-top: var(--s4); padding-top: var(--s4); border-top: 1px solid var(--border-strong); display: flex; flex-direction: column; gap: 6px; }
.geo-chip { font-size: var(--fs-sm); color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.geo-chip .mono { color: var(--purple); }

/* Read-only routing-rules summary in the analytics detail modal */
.geo-rule-summary { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--r); padding: var(--s4); }
.geo-summary-row { display: grid; grid-template-columns: minmax(120px, auto) auto 1fr; align-items: center; gap: var(--s3); font-size: var(--fs-sm); padding: 4px 0; }
.geo-summary-row .mono { color: var(--purple); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
