:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-2: #475569;
  --text-3: #94a3b8;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #eff4ff;
  --success: #16a34a;
  --success-soft: #e8f7ee;
  --warning: #b45309;
  --warning-soft: #fff6e6;
  --danger: #dc2626;
  --danger-soft: #fdecec;
  --bar-sent: #2563eb;
  --bar-skip: #cbd5e1;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.05), 0 1px 3px rgba(15, 23, 42, 0.05);
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --surface: #131c2e;
    --surface-2: #0f1727;
    --border: #233047;
    --border-strong: #31415e;
    --text: #e2e8f0;
    --text-2: #a3b1c6;
    --text-3: #64748b;
    --primary: #3b82f6;
    --primary-hover: #60a5fa;
    --primary-soft: #172540;
    --success: #22c55e;
    --success-soft: #11291c;
    --warning: #f59e0b;
    --warning-soft: #2b2110;
    --danger: #f87171;
    --danger-soft: #2c1517;
    --bar-sent: #3b82f6;
    --bar-skip: #31415e;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
code { font-family: ui-monospace, Consolas, monospace; }

.logo {
  width: 34px; height: 34px; border-radius: 9px; flex: none;
  background: url(logo.png) center / cover no-repeat;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-name { font-weight: 700; letter-spacing: -0.01em; white-space: nowrap; }
.brand-sub { font-size: 12px; color: var(--text-2); }

/* ---------- Logowanie ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  padding: 28px 24px; box-shadow: 0 12px 32px rgba(15, 23, 42, 0.08);
}
.login-brand { display: flex; align-items: center; gap: 10px; margin-bottom: 18px; }

/* ---------- Pasek górny ---------- */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5; }
.topbar-inner { max-width: 1200px; margin: 0 auto; padding: 12px 16px; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.topbar-actions { display: flex; gap: 6px; }

/* ---------- Zakładki ---------- */
.tabs { max-width: 1200px; margin: 0 auto; padding: 0 16px; display: flex; gap: 4px; }
.tab {
  border: 0; background: none; cursor: pointer;
  padding: 10px 16px; margin-bottom: -1px;
  font: 600 14px/1 inherit; font-family: inherit; color: var(--text-2);
  border-bottom: 2px solid transparent;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }
.tab-panel[hidden] { display: none; }

.container { max-width: 1200px; margin: 0 auto; padding: 20px 16px 48px; }

/* ---------- Przegląd (okresy: wysłane + zarobek razem) ---------- */
.overview { display: grid; grid-template-columns: repeat(4, 1fr) 0.85fr; gap: 12px; margin-bottom: 18px; }
.ov-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 13px 16px; box-shadow: var(--shadow); min-width: 0; }
.ov-period { font-size: 12.5px; font-weight: 600; color: var(--text-2); }
.ov-sent { margin-top: 6px; display: flex; align-items: baseline; gap: 6px; }
.ov-sent b { font-size: 27px; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.ov-sent span { font-size: 12px; color: var(--text-3); }
.ov-earned { margin-top: 6px; font-size: 15px; font-weight: 700; color: var(--success); font-variant-numeric: tabular-nums; }
.ov-sub { margin-top: 6px; font-size: 11.5px; color: var(--text-3); }
.ov-accent { background: var(--primary); border-color: var(--primary); color: #fff; }
.ov-accent .ov-period { color: rgba(255, 255, 255, 0.85); }
.ov-accent .ov-sent span { color: rgba(255, 255, 255, 0.8); }
.ov-accent .ov-earned { color: #fff; }
.ov-va { background: var(--surface-2); }
@media (max-width: 1000px) { .overview { grid-template-columns: repeat(2, 1fr); } .ov-va { grid-column: span 2; } }
@media (max-width: 560px) { .overview { grid-template-columns: 1fr 1fr; } .ov-va { grid-column: span 2; } }

/* ---------- Karty ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 16px; overflow: hidden; }
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding: 14px 16px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.card-head h2 { font-size: 15px; margin: 0; }
.card-sub { margin: 3px 0 0; font-size: 12px; color: var(--text-3); }
.card-tools { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.card-tools input[type="search"] { width: 240px; }

/* ---------- Wykres ---------- */
.chart { padding: 16px; height: 220px; }
.chart-small { height: 130px; padding: 0; }
.chart svg { width: 100%; height: 100%; display: block; overflow: visible; }
.chart .bar-sent { fill: var(--bar-sent); }
.chart .bar-skip { fill: var(--bar-skip); }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart text { fill: var(--text-3); font-size: 11px; font-family: inherit; }
.chart .bar-group:hover rect { opacity: 0.8; }

/* ---------- Tabela ---------- */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 9px 12px; text-align: left; white-space: nowrap; border-bottom: 1px solid var(--border); font-size: 13px; }
.table td code { font-size: 12px; }
/* Lista VA zmieszczona bez poziomego przewijania — zwarte odstępy i mniejszy klucz */
.table-compact th, .table-compact td { padding: 8px 9px; font-size: 12.5px; }
.table-compact td code { font-size: 11.5px; letter-spacing: -0.01em; }
.table th { font-size: 12px; font-weight: 600; color: var(--text-2); background: var(--surface-2); }
.table td.num, .table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.table tbody tr { cursor: pointer; }
.table tbody tr:hover { background: var(--surface-2); }
.table tbody tr:last-child td { border-bottom: none; }
.owner { font-weight: 600; }
.owner small { display: block; font-weight: 400; color: var(--text-3); font-size: 12px; }
.muted { color: var(--text-3); }
.empty { padding: 32px 16px; text-align: center; color: var(--text-3); }

.pill { display: inline-flex; align-items: center; gap: 6px; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.pill::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.pill-active { background: var(--success-soft); color: var(--success); }
.pill-blocked { background: var(--danger-soft); color: var(--danger); }
.pill-expired { background: var(--warning-soft); color: var(--warning); }

/* ---------- Formularze ---------- */
.field { display: block; margin-top: 12px; }
.field > span { display: block; font-size: 12.5px; font-weight: 600; color: var(--text-2); margin-bottom: 4px; }
input, textarea, select {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border-strong); border-radius: 8px;
  background: var(--surface); color: var(--text); font: inherit;
}
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 18%, transparent); }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
@media (max-width: 560px) { .grid-3 { grid-template-columns: 1fr; } }
.help { font-size: 12.5px; color: var(--text-3); margin: 8px 0 0; }
.form-error { margin-top: 12px; padding: 8px 10px; border-radius: 8px; background: var(--danger-soft); color: var(--danger); font-size: 13px; }

/* ---------- Przyciski ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 8px; border: 1px solid transparent;
  font: 600 13.5px/1.2 inherit; font-family: inherit; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn-block { width: 100%; margin-top: 16px; padding: 10px; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.btn-secondary:hover { background: var(--surface-2); }
.btn-ghost { background: transparent; color: var(--text-2); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-danger-ghost { background: transparent; color: var(--danger); border-color: transparent; }
.btn-danger-ghost:hover { background: var(--danger-soft); }
.btn:disabled { opacity: 0.5; cursor: default; }
.link { background: none; border: 0; padding: 0; color: var(--primary); font: inherit; font-size: 12.5px; cursor: pointer; }
.icon-btn { width: 32px; height: 32px; border-radius: 8px; border: 0; background: transparent; color: var(--text-2); font-size: 22px; line-height: 1; cursor: pointer; }
.icon-btn:hover { background: var(--surface-2); }

/* ---------- Okna dialogowe ---------- */
.dialog {
  width: min(560px, calc(100vw - 32px)); padding: 22px 22px 18px;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 16px;
  box-shadow: 0 24px 64px rgba(15, 23, 42, 0.25);
}
.dialog-narrow { width: min(440px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(15, 23, 42, 0.45); }
.dialog h3 { margin: 0 0 4px; font-size: 17px; }
.dialog p { color: var(--text-2); margin: 6px 0 0; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.key-box { display: flex; gap: 8px; align-items: center; margin-top: 14px; padding: 10px 12px; background: var(--surface-2); border: 1px dashed var(--border-strong); border-radius: 10px; }
.key-box code { flex: 1; font-size: 17px; font-weight: 700; letter-spacing: 0.04em; }

/* ---------- Panel boczny ---------- */
.drawer-backdrop { position: fixed; inset: 0; background: rgba(15, 23, 42, 0.35); z-index: 20; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(560px, 100vw); z-index: 21;
  background: var(--surface); border-left: 1px solid var(--border); display: flex; flex-direction: column;
  box-shadow: -12px 0 32px rgba(15, 23, 42, 0.15);
}
.drawer-head { display: flex; justify-content: space-between; gap: 12px; padding: 16px 18px; border-bottom: 1px solid var(--border); }
.drawer-head h3 { margin: 0; font-size: 17px; }
.key-inline { display: flex; gap: 8px; align-items: baseline; margin-top: 2px; }
.key-inline code { font-size: 13px; color: var(--text-2); }
.drawer-body { padding: 14px 18px 32px; overflow-y: auto; }
.drawer-body h4 { font-size: 13px; margin: 20px 0 8px; color: var(--text-2); text-transform: uppercase; letter-spacing: 0.04em; }
.drawer-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.drawer-actions .btn { padding: 6px 11px; font-size: 13px; }

.mini-kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 14px; }
.mini-kpis div { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; font-size: 12px; color: var(--text-2); }
.mini-kpis span { display: block; font-size: 20px; font-weight: 700; color: var(--text); font-variant-numeric: tabular-nums; }

.meta { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 14px 0 0; font-size: 13px; }
.meta dt { color: var(--text-3); }
.meta dd { margin: 0; }

.devices, .events { list-style: none; margin: 0; padding: 0; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.devices li, .events li { padding: 8px 12px; font-size: 13px; display: flex; justify-content: space-between; gap: 10px; align-items: center; }
.devices li + li, .events li + li { border-top: 1px solid var(--border); }
.devices small, .events small { display: block; color: var(--text-3); font-size: 12px; }
.events .ev-title { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.events .ev-type { font-weight: 700; width: 16px; flex: none; text-align: center; }
.events .ev-sent .ev-type { color: var(--success); }
.events .ev-skipped .ev-type { color: var(--warning); }
.events .ev-main { flex: 1; min-width: 0; }
.events time { color: var(--text-3); font-size: 12px; flex: none; font-variant-numeric: tabular-nums; }
.list-empty { padding: 12px; color: var(--text-3); font-size: 13px; justify-content: center !important; }

/* ---------- Powiadomienie ---------- */
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 50;
  background: #0f172a; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 13.5px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}
.toast.error { background: var(--danger); }

/* ---------- Statystyki licencji ---------- */
.stats-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-top: 20px; }
.stats-head h4 { margin: 0 !important; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button {
  border: 0; background: var(--surface); color: var(--text-2); padding: 5px 10px;
  font: 600 12px/1.2 inherit; font-family: inherit; cursor: pointer;
}
.seg button + button { border-left: 1px solid var(--border); }
.seg button:hover { background: var(--surface-2); }
.seg button.active { background: var(--primary); color: #fff; }

.stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 10px 0 12px; }
.stat-grid > div { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; min-width: 0; }
.stat-grid b { display: block; font-size: 18px; font-variant-numeric: tabular-nums; color: var(--text); }
.stat-grid span { font-size: 11.5px; color: var(--text-3); }
.stat-grid .up { color: var(--success); }
.stat-grid .down { color: var(--danger); }
@media (max-width: 480px) { .stat-grid { grid-template-columns: repeat(2, 1fr); } }

.chart-legend { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-3); margin-top: 4px; }
.chart-legend .lg { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }
.chart-legend .lg + .lg, .chart-legend .lg-skip { margin-left: 8px; }
.lg-sent { background: var(--bar-sent); }
.lg-skip { background: var(--bar-skip); }

.drawer-body h5 { font-size: 12px; margin: 16px 0 6px; color: var(--text-2); }
.hours { display: grid; grid-template-columns: repeat(24, 1fr); gap: 2px; align-items: end; height: 70px; }
.hours div { display: flex; flex-direction: column; justify-content: flex-end; height: 100%; }
.hours i { display: block; background: var(--bar-sent); border-radius: 2px 2px 0 0; min-height: 1px; }
.hours i.zero { background: var(--border); }
.hours small { font-size: 9.5px; color: var(--text-3); text-align: center; height: 13px; }

.breakdowns { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 16px; }
@media (max-width: 480px) { .breakdowns { grid-template-columns: 1fr; } }
.bars { list-style: none; margin: 0; padding: 0; }
.bars li { position: relative; display: flex; justify-content: space-between; gap: 8px; padding: 4px 8px; font-size: 12.5px; border-radius: 6px; overflow: hidden; }
.bars li + li { margin-top: 3px; }
.bars li::before { content: ""; position: absolute; inset: 0 auto 0 0; width: var(--w, 0%); background: var(--primary-soft); z-index: 0; }
.bars-skip li::before { background: var(--warning-soft); }
.bars li span, .bars li b { position: relative; z-index: 1; }
.bars li span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bars li b { font-variant-numeric: tabular-nums; flex: none; }
.bars .list-empty { color: var(--text-3); padding: 4px 0; }
.bars .list-empty::before { display: none; }

/* ---------- Zarobek ---------- */
.kpis-money { grid-template-columns: repeat(4, 1fr); }
@media (max-width: 900px) { .kpis-money { grid-template-columns: repeat(2, 1fr); } }
.kpi-money { background: var(--success); border-color: var(--success); color: #fff; }
.kpi-money .kpi-label { color: rgba(255, 255, 255, 0.85); }
.mini-kpis .mini-money { background: var(--success-soft); border-color: transparent; }
.mini-kpis .mini-money span { color: var(--success); }
.money { color: var(--success); font-weight: 600; }
.chart .bar-money { fill: var(--success); }
.lg-money { background: var(--success); }
.stat-grid .money-card { background: var(--success-soft); border-color: transparent; }
.stat-grid .money-card b { color: var(--success); }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-items: end; }
@media (max-width: 560px) { .grid-2 { grid-template-columns: 1fr; } }
.check { display: flex; gap: 8px; align-items: flex-start; font-size: 12.5px; color: var(--text-2); margin-top: 12px; padding-bottom: 6px; cursor: pointer; }
.check input { width: auto; flex: none; margin: 2px 0 0; padding: 0; }
.check span { flex: 1; text-align: left; }

.stats-head { flex-wrap: wrap; }
.stats-head h4 { flex: 1; }

/* ---------- VA: wyniki okresowe i rozliczenia ---------- */
.table-static tbody tr { cursor: default; }
.table-static tbody tr:hover { background: transparent; }
.table tfoot td { font-weight: 700; border-top: 2px solid var(--border); border-bottom: none; }
.cell-money { display: block; font-size: 11.5px; color: var(--success); font-weight: 600; }
.period-nav { display: inline-flex; align-items: center; gap: 4px; font-size: 13px; font-weight: 600; white-space: nowrap; }
.period-nav span { min-width: 150px; text-align: center; }
.period-nav .icon-btn:disabled { opacity: 0.35; cursor: default; }

.va-periods { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 14px; }
.va-periods > div { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; min-width: 0; }
.va-periods .p-label { font-size: 11.5px; color: var(--text-3); }
.va-periods .p-count { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.va-periods .p-money { font-size: 13px; font-weight: 700; color: var(--success); font-variant-numeric: tabular-nums; }
.va-periods .p-today { background: var(--success-soft); border-color: transparent; }
@media (max-width: 480px) { .va-periods { grid-template-columns: repeat(2, 1fr); } }
.va-total { margin-top: 8px; font-size: 12.5px; color: var(--text-2); }

.report-wrap { max-height: 320px; overflow-y: auto; border: 1px solid var(--border); border-radius: 10px; margin-top: 8px; }
.report th { position: sticky; top: 0; background: var(--surface); z-index: 1; }
.report th, .report td { padding: 7px 10px; font-size: 12.5px; }
.report tr.zero td { color: var(--text-3); }
.report tr.current td:first-child { font-weight: 700; }

/* ---------- Ustawienia wtyczki narzucone VA ---------- */
.dialog-wide { width: min(720px, calc(100vw - 32px)); }
.settings-tools { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; margin-top: 12px; }
.settings-tools select { width: auto; min-width: 240px; }
.settings-fields { max-height: min(60vh, 560px); overflow-y: auto; margin-top: 10px; padding-right: 4px; }
.settings-fields h4 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-3); margin: 16px 0 4px; }
.set-row { display: grid; grid-template-columns: 92px 1fr; gap: 10px; align-items: end; }
.set-row .field { margin-top: 8px; }
.set-lock { display: flex; align-items: center; gap: 6px; font-size: 12.5px; font-weight: 600; color: var(--text-2); padding-bottom: 9px; cursor: pointer; }
.set-lock input { width: auto; margin: 0; }
.set-row.locked .set-lock { color: var(--primary); }
.set-row:not(.locked) .field > span { color: var(--text-3); }
.set-row:not(.locked) input, .set-row:not(.locked) select, .set-row:not(.locked) textarea { opacity: 0.55; }
.settings-summary { color: var(--text-2); }

/* ---------- Klucz licencji w szczegółach VA ---------- */
.section-head { display: flex; justify-content: space-between; align-items: center; }
.license-key-box { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; }
.license-key-box code { font-size: 14px; font-weight: 600; letter-spacing: 0.03em; }
.license-key-actions { margin-top: 8px; }
.license-key-box .help { margin: 8px 0 0; }
