/* ============ Tokeny ============ */
:root {
  --bg: #f5f4ef;
  --surface: #ffffff;
  --surface-2: #faf9f6;
  --ink: #1b1b18;
  --ink-2: #44443e;
  --muted: #74736b;
  --line: #e3e1d8;
  --line-strong: #cfccc0;

  --accent: #1d5c47;
  --accent-hover: #164a39;
  --accent-soft: #e6f0ec;
  --accent-ink: #ffffff;

  --ok: #1f7a4d;
  --ok-bg: #e7f4ec;
  --warn: #8a4b00;
  --warn-bg: #fff3dc;
  --danger: #b42318;
  --danger-bg: #fdeceb;
  --info-bg: #eef1f6;

  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgba(27, 27, 24, .05), 0 2px 8px rgba(27, 27, 24, .04);

  --font: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  --fs-xs: .75rem;
  --fs-sm: .8125rem;
  --fs: .9375rem;
  --fs-lg: 1.125rem;
  --fs-xl: 1.5rem;
  --gutter: 24px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 400 var(--fs)/1.5 var(--font);
}
a { color: var(--accent); }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: var(--fs-sm); }
.mono { font-family: var(--mono); font-size: var(--fs-xs); }

/* ============ Góra ============ */
.topbar {
  background: var(--ink);
  color: #f3f2ec;
}
.topbar-inner {
  max-width: 1400px; margin: 0 auto; padding: 0 var(--gutter);
  display: flex; align-items: center; gap: 28px; min-height: 58px;
}
.brand { font-weight: 600; letter-spacing: -.01em; display: flex; align-items: center; gap: 10px; white-space: nowrap; }
.brand-mark {
  width: 26px; height: 26px; border-radius: 7px; background: var(--accent);
  display: grid; place-items: center; font: 600 12px/1 var(--mono); color: #fff;
}
.nav { display: flex; gap: 4px; }
.nav a {
  color: #cfcdc4; text-decoration: none; padding: 8px 12px; border-radius: var(--radius-sm);
  font-weight: 500; font-size: var(--fs-sm);
}
.nav a:hover { color: #fff; background: rgba(255,255,255,.07); }
.nav a.active { color: #fff; background: rgba(255,255,255,.12); }
.status-pills { margin-left: auto; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.logout { display: flex; }
.linkbtn { background: none; border: 0; color: #cfcdc4; font: inherit; font-size: var(--fs-sm); cursor: pointer; padding: 6px; }
.linkbtn:hover { color: #fff; }

.pill {
  display: inline-flex; align-items: center; gap: 7px; padding: 4px 11px; border-radius: 999px;
  font-size: var(--fs-xs); font-weight: 600; letter-spacing: .02em; white-space: nowrap;
}
.pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.pill-off { background: #3a3a35; color: #bdbbb1; }
.pill-test { background: #4a3a14; color: #ffd27a; }
.pill-live { background: #134233; color: #7fe0b4; }
.pill-live::before { animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .35; } }

/* ============ Pasek stanu ============ */
.statusbar { background: var(--surface); border-bottom: 1px solid var(--line); }
.statusbar-inner {
  max-width: 1400px; margin: 0 auto; padding: 10px var(--gutter);
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 24px; font-size: var(--fs-sm); color: var(--ink-2);
}
.statusbar b { color: var(--ink); font-weight: 600; }
.statusbar .spacer { margin-left: auto; }
.status-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; }
.status-dot.ok { background: var(--ok); }
.status-dot.error { background: var(--danger); }
.status-dot.running { background: var(--warn); animation: pulse 1s infinite; }

/* ============ Treść ============ */
.page { max-width: 1400px; margin: 0 auto; padding: 24px var(--gutter) 120px; }
.page-head { display: flex; align-items: end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 18px; }
h1 { font-size: var(--fs-xl); line-height: 1.2; letter-spacing: -.02em; margin: 0; font-weight: 600; }
h2 { font-size: var(--fs-lg); margin: 0 0 4px; font-weight: 600; letter-spacing: -.01em; }
.lead { color: var(--muted); margin: 4px 0 0; font-size: var(--fs-sm); }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card-pad { padding: 20px 22px; }

/* Komunikaty */
.flashes { display: grid; gap: 8px; margin-bottom: 18px; }
.flash { padding: 11px 14px; border-radius: var(--radius-sm); font-size: var(--fs-sm); border: 1px solid transparent; }
.flash-ok { background: var(--ok-bg); color: var(--ok); border-color: #c9e7d5; }
.flash-warn { background: var(--warn-bg); color: var(--warn); border-color: #f3d9a6; font-weight: 500; }
.flash-error { background: var(--danger-bg); color: var(--danger); border-color: #f5c6c2; }

/* Przyciski */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 38px; padding: 0 16px; border-radius: var(--radius-sm);
  font: 500 var(--fs-sm)/1 var(--font); text-decoration: none; cursor: pointer;
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink);
  transition: background .15s, border-color .15s;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); border-color: var(--muted); }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-danger { color: var(--danger); border-color: #eab3ae; }
.btn-sm { height: 32px; padding: 0 12px; font-size: var(--fs-xs); }
.btn:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}

/* Pola */
input[type=text], input[type=password], input[type=number], input[type=search], select, textarea {
  font: inherit; font-size: var(--fs-sm); color: var(--ink);
  height: 38px; padding: 0 11px; border: 1px solid var(--line-strong); border-radius: var(--radius-sm);
  background: var(--surface); width: 100%;
}
textarea { height: auto; padding: 9px 11px; resize: vertical; min-height: 84px; }
select {
  -webkit-appearance: none; appearance: none;
  padding-right: 38px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5l5 5 5-5' fill='none' stroke='%2374736b' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
  background-size: 12px 8px;
  cursor: pointer;
}
input::placeholder, textarea::placeholder { color: #a6a49a; }
label.field { display: grid; gap: 6px; font-size: var(--fs-sm); font-weight: 500; }
.hint { font-size: var(--fs-xs); color: var(--muted); font-weight: 400; }

/* ============ Statystyki ============ */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 18px; }
.stat { padding: 14px 16px; text-decoration: none; color: inherit; display: block; transition: border-color .15s; }
a.stat:hover { border-color: var(--line-strong); }
.stat .k { font-size: var(--fs-xs); color: var(--muted); font-weight: 500; text-transform: uppercase; letter-spacing: .05em; }
.stat .v { font-size: 1.6rem; font-weight: 600; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.stat.warn .v { color: var(--warn); }
.stat.good .v { color: var(--ok); }
.stat.bad .v { color: var(--danger); }

/* ============ Pasek narzędzi ============ */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 12px; }
.toolbar form.search { display: flex; gap: 8px; flex: 1 1 380px; }
.toolbar form.search input { flex: 1; }
.toolbar form.search select { width: auto; }
.import-form { display: flex; gap: 8px; align-items: center; }
.import-form input[type=file] { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.import-form label.btn { cursor: pointer; }

/* ============ Tabela produktów ============ */
.table-wrap { overflow-x: auto; }
table.grid { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); }
table.grid th {
  text-align: left; font-size: var(--fs-xs); font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: .05em; padding: 11px 12px; border-bottom: 1px solid var(--line);
  background: var(--surface-2); white-space: nowrap;
}
table.grid td { padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.grid tr:last-child td { border-bottom: 0; }
table.grid tr.disabled td { opacity: .55; }
table.grid tr.dirty td { background: #fffbea; }
.prod-title { font-weight: 500; color: var(--ink); max-width: 340px; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.prod-meta { font-size: var(--fs-xs); color: var(--muted); margin-top: 2px; }
.price { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.price-input { width: 96px !important; text-align: center; font-variant-numeric: tabular-nums; }
.price-input::placeholder { text-align: center; }
.step-select { width: 168px !important; }
.prod { display: flex; gap: 12px; align-items: center; }
.prod-text { min-width: 0; }
.thumb {
  width: 48px; height: 48px; flex: none; border-radius: var(--radius-sm);
  object-fit: contain; background: #fff; border: 1px solid var(--line);
}
.thumb-empty { display: block; background: var(--surface-2); }
.pos-diff { font-size: var(--fs-xs); margin-top: 3px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.m-summary { display: none; }
.reason { font-size: var(--fs-xs); color: var(--ink-2); max-width: 280px; }
.reason .arrow { font-weight: 600; color: var(--accent); white-space: nowrap; }

.tag {
  display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: var(--fs-xs); font-weight: 600; white-space: nowrap;
}
.tag-ok { background: var(--ok-bg); color: var(--ok); }
.tag-warn { background: var(--warn-bg); color: var(--warn); }
.tag-bad { background: var(--danger-bg); color: var(--danger); }
.tag-muted { background: var(--info-bg); color: var(--ink-2); }
.tag-live { background: var(--accent-soft); color: var(--accent); }

/* Przełącznik */
.switch { position: relative; display: inline-block; width: 38px; height: 22px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch .track {
  position: absolute; inset: 0; background: var(--line-strong); border-radius: 999px; transition: background .15s; cursor: pointer;
}
.switch .track::after {
  content: ""; position: absolute; width: 16px; height: 16px; left: 3px; top: 3px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.2); transition: transform .15s;
}
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::after { transform: translateX(16px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Pasek zapisu */
.savebar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  background: var(--ink); color: #f3f2ec; transform: translateY(110%); transition: transform .2s ease;
}
.savebar.show { transform: none; }
.savebar-inner { max-width: 1400px; margin: 0 auto; padding: 12px var(--gutter); display: flex; align-items: center; gap: 16px; }
.savebar .spacer { margin-left: auto; }
.savebar .btn { border-color: transparent; }

.pager { display: flex; gap: 6px; justify-content: center; margin-top: 16px; flex-wrap: wrap; }
.pager a, .pager span { min-width: 36px; height: 36px; display: grid; place-items: center; border-radius: var(--radius-sm); text-decoration: none; font-size: var(--fs-sm); }
.pager a { border: 1px solid var(--line); background: var(--surface); color: var(--ink); }
.pager span.current { background: var(--ink); color: #fff; }

.empty { padding: 48px 24px; text-align: center; color: var(--muted); }
.empty h2 { color: var(--ink); }

/* ============ Ustawienia ============ */
.settings { display: grid; gap: 16px; max-width: 900px; }
.section-head { margin-bottom: 16px; }
.section-head p { margin: 2px 0 0; color: var(--muted); font-size: var(--fs-sm); }
.row-toggle { display: flex; gap: 14px; align-items: flex-start; padding: 12px 0; border-top: 1px solid var(--line); }
.row-toggle:first-of-type { border-top: 0; padding-top: 0; }
.row-toggle .txt { flex: 1; }
.row-toggle .txt b { display: block; font-weight: 500; font-size: var(--fs-sm); }
.row-toggle .txt span { font-size: var(--fs-xs); color: var(--muted); }

.choice-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.choice-grid.two { grid-template-columns: repeat(2, 1fr); }
.choice { position: relative; display: block; cursor: pointer; }
.choice input { position: absolute; opacity: 0; }
.choice .box {
  display: block; height: 100%; border: 1.5px solid var(--line-strong); border-radius: var(--radius); padding: 14px 16px;
  transition: border-color .15s, background .15s;
}
.choice .box b { display: block; font-size: var(--fs-lg); font-weight: 600; letter-spacing: -.01em; }
.choice .box span { font-size: var(--fs-xs); color: var(--muted); display: block; margin-top: 2px; }
.choice .box.compact b { font-size: var(--fs-sm); }
.choice input:checked + .box { border-color: var(--accent); background: var(--accent-soft); }
.choice input:focus-visible + .box { outline: 2px solid var(--accent); outline-offset: 2px; }
label.field.custom-step { margin-top: 10px; display: none; max-width: 220px; }
label.field.custom-step.show { display: grid; }

.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }
.example {
  margin-top: 14px; padding: 12px 14px; background: var(--surface-2); border: 1px dashed var(--line-strong);
  border-radius: var(--radius-sm); font-size: var(--fs-sm); color: var(--ink-2);
}
.sticky-actions { position: sticky; bottom: 0; padding: 14px 0; background: linear-gradient(transparent, var(--bg) 30%); display: flex; gap: 10px; }
.danger-zone { border-color: #f0d5a8; background: #fffaf0; }

/* ============ Historia ============ */
.tabs { display: flex; gap: 4px; margin-bottom: 12px; flex-wrap: wrap; }
.tabs a { padding: 7px 12px; border-radius: var(--radius-sm); text-decoration: none; color: var(--ink-2); font-size: var(--fs-sm); font-weight: 500; }
.tabs a.active { background: var(--ink); color: #fff; }
.change-arrow { color: var(--muted); padding: 0 4px; }
.down { color: var(--ok); }
.up { color: var(--warn); }
.runs-list { display: grid; }
.run { display: grid; grid-template-columns: 150px 80px 1fr; gap: 12px; padding: 10px 0; border-top: 1px solid var(--line); font-size: var(--fs-sm); }
.run:first-child { border-top: 0; }
.run .msg { white-space: pre-line; color: var(--ink-2); }

/* ============ Logowanie ============ */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; padding: 30px 28px; }
.login-card .brand { color: var(--ink); margin-bottom: 22px; }
.login-card form { display: grid; gap: 14px; }

/* ============ Mobile ============ */
@media (max-width: 1100px) {
  .reason { max-width: 200px; }
}
@media (max-width: 860px) {
  :root { --gutter: 16px; }
  .hide-sm { display: none; }

  /* ----- nagłówek ----- */
  .topbar-inner {
    display: grid; grid-template-columns: 1fr auto; gap: 12px;
    grid-template-areas: "brand logout" "pills pills" "nav nav";
    padding-top: 14px; padding-bottom: 12px;
  }
  .brand { grid-area: brand; }
  .logout { grid-area: logout; }
  .status-pills { grid-area: pills; margin-left: 0; }
  .nav {
    grid-area: nav; display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px;
    padding: 4px; border-radius: var(--radius); background: rgba(255,255,255,.06);
  }
  .nav a { text-align: center; padding: 9px 4px; }

  /* ----- pasek stanu ----- */
  .statusbar-inner { gap: 6px 18px; padding-top: 12px; padding-bottom: 12px; }
  .statusbar .spacer { margin-left: 0; width: 100%; margin-top: 4px; }
  .statusbar .spacer .btn { width: 100%; height: 40px; }

  .stats { grid-template-columns: repeat(2, 1fr); }
  .form-grid, .choice-grid, .choice-grid.two { grid-template-columns: 1fr; }

  /* ----- produkty: każda oferta jako osobna karta ----- */
  .card.table-wrap:has(table.cards) { background: none; border: 0; box-shadow: none; overflow: visible; }
  table.grid.cards thead { display: none; }
  table.grid.cards, table.grid.cards tbody, table.grid.cards td { display: block; width: 100%; }
  table.grid.cards tr {
    position: relative; display: grid; grid-template-columns: 1fr 1fr; gap: 12px;
    grid-template-areas: "title title" "min max" "step step" "reason reason";
    padding: 16px; margin-bottom: 12px;
    background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
  }
  table.grid.cards td { padding: 0; border: 0; }
  table.grid.cards td[data-label]::before {
    content: attr(data-label); display: block; font-size: var(--fs-xs); color: var(--muted); font-weight: 500; margin-bottom: 5px;
  }
  table.grid.cards td.c-on { position: absolute; top: 16px; right: 16px; width: auto; }
  table.grid.cards td.c-on::before { display: none; }
  table.grid.cards td.c-title { grid-area: title; }
  table.grid.cards td.c-price, table.grid.cards td.c-comp, table.grid.cards td.c-pos { display: none; }
  table.grid.cards td.c-min { grid-area: min; }
  table.grid.cards td.c-max { grid-area: max; }
  table.grid.cards td.c-step { grid-area: step; }
  table.grid.cards td.c-reason { grid-area: reason; }

  .prod { padding-right: 52px; align-items: flex-start; }
  .thumb { width: 56px; height: 56px; }
  .prod-title, .reason { max-width: none; }
  .prod-title { -webkit-line-clamp: 3; line-height: 1.35; }
  .prod-meta .ean { display: none; }

  .m-summary {
    display: block; margin-top: 14px; border: 1px solid var(--line); border-radius: var(--radius-sm);
    background: var(--surface-2); overflow: hidden;
  }
  .m-prices { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; padding: 12px; }
  .m-k { display: block; font-size: var(--fs-xs); color: var(--muted); font-weight: 500; }
  .m-v { display: block; font-size: 1.0625rem; font-weight: 600; font-variant-numeric: tabular-nums; margin-top: 1px; }
  .m-sub { display: block; font-size: var(--fs-xs); color: var(--muted); margin-top: 1px; }
  .m-pos {
    display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
    padding: 9px 12px; border-top: 1px solid var(--line); background: var(--info-bg);
  }
  .m-pos .pos-diff { margin-top: 0; font-weight: 600; }
  .m-pos .tag { background: var(--surface); box-shadow: inset 0 0 0 1px currentColor; }
  tr.st-lose .m-pos { background: var(--danger-bg); border-top-color: #f5c6c2; }
  tr.st-win .m-pos { background: var(--ok-bg); border-top-color: #c9e7d5; }
  tr.st-lose .m-pos .up { color: var(--danger); }

  table.grid.cards td.c-reason .reason {
    font-size: var(--fs-xs); color: var(--ink-2); line-height: 1.45;
  }
  table.grid.cards td.c-reason {
    border-top: 1px dashed var(--line); padding-top: 12px;
  }
  .price-input, .step-select { width: 100% !important; height: 42px; font-size: var(--fs); }
  table.grid.cards tr.disabled td { opacity: 1; }
  table.grid.cards tr.disabled { opacity: .6; }
  table.grid.cards tr.dirty { background: #fffbea; border-color: #f0dc9a; }
  table.grid.cards tr.dirty td { background: transparent; }

  .run { grid-template-columns: 1fr auto; }
  .run .msg { grid-column: 1 / -1; }
  .toolbar form.search { flex-wrap: wrap; flex-basis: 100%; }
  .toolbar form.search input { flex: 1 1 100%; }
  .toolbar form.search select { flex: 1; }
  .toolbar > .btn, .import-form, .import-form label.btn { flex: 1; }
}
