/* 9Yards Subscription Tracker - hand-written design system (no framework, no build step).
   Palette follows the validated dataviz reference palette (light + dark, CVD-checked). */

:root {
  color-scheme: light;
  --surface-1: #fcfcfb;
  --surface-2: #ffffff;
  --page: #f9f9f7;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --gridline: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --success-text: #006300;

  --series-blue: #2a78d6;
  --series-green: #008300;
  --series-magenta: #e87ba4;
  --series-yellow: #eda100;
  --series-aqua: #1baf7a;
  --series-orange: #eb6834;
  --series-violet: #4a3aa7;
  --series-red: #e34948;

  --status-good: #0ca30c;
  --status-warning: #fab219;
  --status-serious: #ec835a;
  --status-critical: #d03b3b;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 2px rgba(11, 11, 11, 0.06), 0 1px 1px rgba(11, 11, 11, 0.04);
  --shadow-md: 0 4px 16px rgba(11, 11, 11, 0.08), 0 2px 6px rgba(11, 11, 11, 0.05);
  --shadow-lg: 0 12px 32px rgba(11, 11, 11, 0.14);
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --surface-1: #1a1a19;
    --surface-2: #212120;
    --page: #0d0d0d;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --gridline: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --success-text: #0ca30c;

    --series-blue: #3987e5;
    --series-green: #008300;
    --series-magenta: #d55181;
    --series-yellow: #c98500;
    --series-aqua: #199e70;
    --series-orange: #d95926;
    --series-violet: #9085e9;
    --series-red: #e66767;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--page);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.5;
}
a { color: var(--series-blue); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { margin: 0 0 4px; font-weight: 650; letter-spacing: -0.01em; }
p { margin: 0 0 8px; color: var(--text-secondary); }
button { font-family: inherit; }

/* ---- Layout shell ---- */
.app-shell { min-height: 100%; display: flex; flex-direction: column; }

.topbar {
  display: flex; align-items: center; gap: 24px;
  padding: 0 20px; height: 60px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 40;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 15px; white-space: nowrap; }
.brand .mark {
  width: 30px; height: 30px; border-radius: 8px;
  background: linear-gradient(135deg, var(--series-blue), var(--series-violet));
  display: flex; align-items: center; justify-content: center; color: #fff; font-size: 14px; font-weight: 800;
}
.brand small { display: block; font-weight: 500; color: var(--text-muted); font-size: 11px; }

.main-nav { display: flex; gap: 4px; flex: 1; }
.main-nav a {
  color: var(--text-secondary); font-weight: 600; font-size: 13.5px;
  padding: 8px 14px; border-radius: var(--radius-sm); text-decoration: none;
}
.main-nav a:hover { background: var(--page); color: var(--text-primary); }
.main-nav a.active { color: var(--series-blue); background: color-mix(in srgb, var(--series-blue) 12%, transparent); }

.topbar-right { display: flex; align-items: center; gap: 14px; }
.user-chip { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.user-chip .avatar {
  width: 28px; height: 28px; border-radius: 50%; background: var(--series-aqua);
  color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 12px;
}
.role-badge {
  font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em;
  padding: 2px 7px; border-radius: 20px; color: #fff;
}
.role-badge.admin { background: var(--series-violet); }
.role-badge.team { background: var(--series-blue); }
.role-badge.viewer { background: var(--text-muted); }

.logout-link { color: var(--text-muted); font-size: 12.5px; font-weight: 600; }

.content { flex: 1; padding: 24px 28px 60px; max-width: 1440px; width: 100%; margin: 0 auto; }

.page-header { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-header h1 { font-size: 21px; }
.page-header .subtitle { color: var(--text-muted); font-size: 13px; }

/* ---- Sub nav (country tabs / period tabs) ---- */
.pill-tabs { display: flex; gap: 6px; padding: 4px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; width: fit-content; }
.pill-tabs a, .pill-tabs button {
  border: none; background: transparent; cursor: pointer;
  padding: 7px 16px; border-radius: 999px; font-weight: 600; font-size: 13px; color: var(--text-secondary);
}
.pill-tabs a.active, .pill-tabs button.active { background: var(--series-blue); color: #fff; }

/* ---- Cards / stat tiles ---- */
.grid { display: grid; gap: 16px; }
.grid.cols-4 { grid-template-columns: repeat(4, 1fr); }
.grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
@media (max-width: 1100px) { .grid.cols-4 { grid-template-columns: repeat(2, 1fr); } .grid.cols-3 { grid-template-columns: repeat(2,1fr); } }
@media (max-width: 640px) { .grid.cols-4, .grid.cols-3, .grid.cols-2 { grid-template-columns: 1fr; } }

.card {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: 18px 20px; box-shadow: var(--shadow-sm);
}
.card h3 { font-size: 13px; color: var(--text-muted); font-weight: 650; text-transform: uppercase; letter-spacing: 0.03em; }

.stat-tile .value { font-size: 26px; font-weight: 700; font-variant-numeric: proportional-nums; margin-top: 2px; }
.stat-tile .delta { font-size: 12.5px; font-weight: 600; margin-top: 4px; }
.stat-tile .delta.good { color: var(--success-text); }
.stat-tile .delta.muted { color: var(--text-muted); }
.stat-tile .icon { width: 34px; height: 34px; border-radius: 9px; display: flex; align-items: center; justify-content: center; font-size: 16px; margin-bottom: 10px; }

.chart-card { padding: 18px 20px 8px; }
.chart-card .chart-wrap { position: relative; height: 260px; margin-top: 8px; }

/* ---- Section / table ---- */
.section { margin-bottom: 30px; }
.section-title-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.section-title-row h2 { font-size: 16px; }
.section-title-row .count-badge {
  font-size: 11.5px; font-weight: 700; color: var(--text-muted); background: var(--surface-2);
  border: 1px solid var(--border); padding: 2px 9px; border-radius: 20px; margin-left: 8px;
}

.service-group { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-md); margin-bottom: 14px; overflow: hidden; }
.service-group-header {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 12px 16px; background: var(--page); border-bottom: 1px solid var(--border);
}
.service-group-header .name { font-weight: 700; font-size: 13.5px; }
.service-group-header .meta { font-size: 12px; color: var(--text-muted); }

table.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data-table th {
  text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: 0.03em;
  color: var(--text-muted); font-weight: 650; padding: 9px 14px; border-bottom: 1px solid var(--gridline);
  white-space: nowrap;
}
table.data-table td { padding: 10px 14px; border-bottom: 1px solid var(--gridline); vertical-align: middle; font-variant-numeric: tabular-nums; }
table.data-table tbody tr:last-child td { border-bottom: none; }
table.data-table tbody tr:hover { background: var(--page); }
table.data-table td.num { text-align: right; }

.badge { display: inline-flex; align-items: center; gap: 4px; font-size: 11px; font-weight: 700; padding: 3px 9px; border-radius: 20px; }
.badge.yearly { background: color-mix(in srgb, var(--series-violet) 15%, transparent); color: var(--series-violet); }
.badge.monthly { background: color-mix(in srgb, var(--series-blue) 15%, transparent); color: var(--series-blue); }
.badge.days-critical { background: color-mix(in srgb, var(--status-critical) 16%, transparent); color: var(--status-critical); }
.badge.days-warning { background: color-mix(in srgb, var(--status-warning) 22%, transparent); color: #8a5a00; }
.badge.days-good { background: color-mix(in srgb, var(--status-good) 15%, transparent); color: var(--status-good); }

.pw-cell { display: flex; align-items: center; gap: 8px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.pw-toggle { border: 1px solid var(--border); background: var(--surface-2); border-radius: 6px; width: 26px; height: 26px; cursor: pointer; color: var(--text-secondary); display:flex; align-items:center; justify-content:center; }
.pw-toggle:hover { background: var(--page); }

.row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.icon-btn {
  border: 1px solid var(--border); background: var(--surface-2); border-radius: 7px; width: 28px; height: 28px;
  display: flex; align-items: center; justify-content: center; cursor: pointer; color: var(--text-secondary);
}
.icon-btn:hover { background: var(--page); color: var(--text-primary); }
.icon-btn.danger:hover { background: color-mix(in srgb, var(--status-critical) 12%, transparent); color: var(--status-critical); border-color: var(--status-critical); }

/* ---- Buttons ---- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; justify-content: center;
  padding: 9px 16px; border-radius: var(--radius-sm); font-weight: 650; font-size: 13px;
  border: 1px solid transparent; cursor: pointer; text-decoration: none;
}
.btn-primary { background: var(--series-blue); color: #fff; }
.btn-primary:hover { background: #2569bd; text-decoration:none; }
.btn-secondary { background: var(--surface-2); color: var(--text-primary); border-color: var(--border); }
.btn-secondary:hover { background: var(--page); text-decoration:none; }
.btn-danger { background: var(--status-critical); color: #fff; }
.btn-danger:hover { background: #b33131; text-decoration:none; }
.btn-sm { padding: 6px 12px; font-size: 12.5px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---- Forms ---- */
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12.5px; font-weight: 650; margin-bottom: 5px; color: var(--text-secondary); }
.field .hint { font-size: 11.5px; color: var(--text-muted); margin-top: 4px; }
input[type="text"], input[type="email"], input[type="password"], input[type="number"], input[type="date"], select, textarea {
  width: 100%; padding: 9px 11px; border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--surface-1); color: var(--text-primary); font-size: 13.5px; font-family: inherit;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--series-blue); outline-offset: 0; border-color: var(--series-blue); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 560px) { .field-row { grid-template-columns: 1fr; } }

/* Toggle switch (permission matrix) */
.switch { position: relative; display: inline-block; width: 38px; height: 22px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .track { position: absolute; inset: 0; background: var(--baseline); border-radius: 999px; cursor: pointer; transition: background .15s; }
.switch .track::before { content: ""; position: absolute; width: 16px; height: 16px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: transform .15s; box-shadow: var(--shadow-sm); }
.switch input:checked + .track { background: var(--series-blue); }
.switch input:checked + .track::before { transform: translateX(16px); }
.switch input:disabled + .track { opacity: 0.5; cursor: not-allowed; }

/* ---- Modal ---- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(11,11,11,0.45); display: none;
  align-items: center; justify-content: center; z-index: 100; padding: 20px;
}
.modal-overlay.open { display: flex; }
.modal {
  background: var(--surface-2); border-radius: var(--radius-lg); box-shadow: var(--shadow-lg);
  width: 100%; max-width: 480px; max-height: 90vh; overflow-y: auto; padding: 22px 24px;
}
.modal h2 { font-size: 16px; margin-bottom: 4px; }
.modal .modal-sub { font-size: 12.5px; color: var(--text-muted); margin-bottom: 16px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.modal-close { position: absolute; }

/* ---- Misc ---- */
.empty-state { text-align: center; padding: 60px 20px; color: var(--text-muted); }
.empty-state .icon { font-size: 34px; margin-bottom: 10px; }
.toast-stack { position: fixed; top: 74px; right: 20px; z-index: 200; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--surface-2); border: 1px solid var(--border); box-shadow: var(--shadow-md); border-radius: var(--radius-sm); padding: 10px 16px; font-size: 13px; font-weight: 600; min-width: 220px; }
.toast.success { border-left: 3px solid var(--status-good); }
.toast.error { border-left: 3px solid var(--status-critical); }

.legend-row { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; font-size: 12px; color: var(--text-secondary); margin-top: 10px; }
.legend-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 5px; }

.table-scroll { overflow-x: auto; }
.coming-soon { text-align: center; padding: 90px 20px; }
.coming-soon .icon { font-size: 40px; margin-bottom: 14px; }
.coming-soon h2 { font-size: 20px; margin-bottom: 8px; }

/* Login page */
.login-shell { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: var(--page); padding: 20px; }
.login-card { width: 100%; max-width: 380px; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-lg); box-shadow: var(--shadow-lg); padding: 32px 30px; }
.login-card .brand { justify-content: center; margin-bottom: 6px; font-size: 16px; }
.login-card .tagline { text-align: center; color: var(--text-muted); font-size: 12.5px; margin-bottom: 24px; }
.login-error { background: color-mix(in srgb, var(--status-critical) 12%, transparent); color: var(--status-critical); border: 1px solid color-mix(in srgb, var(--status-critical) 30%, transparent); border-radius: var(--radius-sm); padding: 9px 12px; font-size: 12.5px; font-weight: 600; margin-bottom: 16px; }
.login-card .btn-primary { width: 100%; padding: 11px; margin-top: 6px; }

.matrix-table th, .matrix-table td { text-align: center; }
.matrix-table td:first-child, .matrix-table th:first-child { text-align: left; }
.admin-note { background: var(--page); border: 1px dashed var(--border); border-radius: var(--radius-sm); padding: 10px 14px; font-size: 12.5px; color: var(--text-secondary); }

.text-muted { color: var(--text-muted); }
.mt-0 { margin-top: 0; }
