:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #1c2128;
  --muted: #687280;
  --border: #e1e4e8;
  --primary: #334155;
  --primary-text: #ffffff;
  --danger: #b3261e;
  --holiday: #c62828;
  --warn-bg: #fff4dc;
  --warn-text: #6b4a00;
  --info-bg: #eaf0f6;
  --radius: 12px;
  --tabbar: 62px;

  /* kolory typów dni */
  --k-urlop: #2e7d5b;
  --k-zadanie: #b4581a;
  --k-ho: #2a6fb0;
  --k-opieka: #7e4fae;
  --k-sila: #7a6640;
  --k-l4: #c0354f;
  --k-wolne: #7d848c;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418;
    --surface: #1a1e24;
    --surface-2: #232830;
    --text: #e8eaed;
    --muted: #9aa2ad;
    --border: #2d333b;
    --primary: #cbd5e1;
    --primary-text: #0f172a;
    --danger: #f2867d;
    --holiday: #ff8a80;
    --warn-bg: #3a2f14;
    --warn-text: #f3d48a;
    --info-bg: #1d2633;

    --k-urlop: #3c9a70;
    --k-zadanie: #c86b2a;
    --k-ho: #3b84c8;
    --k-opieka: #9568c4;
    --k-sila: #8f7a52;
    --k-l4: #d24b64;
    --k-wolne: #6b727a;
    color-scheme: dark;
  }
}

.k-urlop { --k: var(--k-urlop); }
.k-zadanie { --k: var(--k-zadanie); }
.k-ho { --k: var(--k-ho); }
.k-opieka { --k: var(--k-opieka); }
.k-sila { --k: var(--k-sila); }
.k-l4 { --k: var(--k-l4); }
.k-wolne { --k: var(--k-wolne); }

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
body.sheet-open { overflow: hidden; }

a { color: var(--primary); text-decoration: none; }
h1 { font-size: 1.4rem; line-height: 1.2; margin: .25rem 0 .75rem; }
h2 { font-size: 1.05rem; margin: 0; }
p { margin: .35rem 0; }

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 12px 16px calc(var(--tabbar) + 24px + env(safe-area-inset-bottom));
}

/* ---------------------------------------------------------------- pasek górny */

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.1rem; color: var(--text); letter-spacing: .01em; }

.sync {
  border-radius: 999px; padding: .2rem .7rem; font-size: .85rem; min-height: 32px;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
}
.sync-ok { color: var(--k-urlop); }
.sync-busy { animation: pulse 1s infinite alternate; }
.sync-warn { background: var(--warn-bg); color: var(--warn-text); }
.sync small { font-weight: 700; }
@keyframes pulse { to { opacity: .45; } }

.banner {
  background: var(--warn-bg); color: var(--warn-text);
  padding: .6rem 16px; text-align: center; font-size: .9rem;
}
.banner a { color: inherit; text-decoration: underline; }

/* ---------------------------------------------------------------- dolne menu */

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: space-around;
  height: calc(var(--tabbar) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--border);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: .72rem; color: var(--muted); gap: 2px;
}
.tabbar a.active { color: var(--text); font-weight: 600; }
.tab-icon { font-size: 1.3rem; line-height: 1; }

/* ---------------------------------------------------------------- przyciski i formularze */

button, .button {
  font: inherit; font-size: .95rem;
  display: inline-flex; align-items: center; justify-content: center; gap: .35rem;
  min-height: 44px; padding: .45rem 1rem;
  border-radius: var(--radius); border: 1px solid var(--border);
  background: var(--surface); color: var(--text); cursor: pointer;
}
button:disabled { opacity: .55; cursor: default; }
.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); font-weight: 600; }
.danger { color: var(--danger); }
.big { width: 100%; min-height: 54px; font-size: 1.05rem; }
button.icon, .icon { min-height: 40px; padding: .2rem .6rem; border: none; background: none; color: var(--muted); }
button.link, .link {
  border: none; background: none; padding: .2rem 0; min-height: 0; color: var(--text);
  font-size: .9rem; text-decoration: underline; text-underline-offset: 3px;
}

input, select, textarea {
  font: inherit; width: 100%;
  padding: .6rem .7rem; min-height: 44px;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
}
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--primary); outline-offset: 1px;
}
input[type="checkbox"] { width: 20px; height: 20px; min-height: 0; flex: none; }

.field { display: block; margin: .65rem 0; flex: 1; min-width: 0; }
.field-label { display: block; font-size: .82rem; color: var(--muted); margin-bottom: .2rem; }
.field-hint { display: block; font-size: .78rem; color: var(--muted); margin-top: .2rem; }
.field-row { display: flex; gap: .75rem; }

.actions { display: flex; flex-wrap: wrap; gap: .5rem; margin: 1rem 0; }
.actions.sticky {
  position: sticky; bottom: calc(var(--tabbar) + env(safe-area-inset-bottom));
  background: var(--bg); padding: .6rem 0; margin-bottom: 0; z-index: 2;
}
.check { display: flex; gap: .6rem; align-items: center; margin: .6rem 0; font-size: .95rem; }

.num-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 0 .75rem; }
.num-grid .field { margin: .4rem 0; }

/* ---------------------------------------------------------------- komunikaty */

.loading, .empty { color: var(--muted); text-align: center; padding: 2rem 1rem; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.hint { color: var(--muted); font-size: .8rem; margin: 0 0 .5rem; text-align: center; }
.error { color: var(--danger); }
.warning, .info { border-radius: var(--radius); padding: .65rem .85rem; margin: .5rem 0; font-size: .9rem; }
.warning { background: var(--warn-bg); color: var(--warn-text); }
.info { background: var(--info-bg); }

.toasts {
  position: fixed; left: 16px; right: 16px; z-index: 40;
  bottom: calc(var(--tabbar) + 12px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; align-items: center; gap: .4rem; pointer-events: none;
}
.toast {
  max-width: 520px; padding: .6rem 1rem; border-radius: var(--radius);
  background: var(--text); color: var(--bg); font-size: .92rem; box-shadow: 0 4px 16px #0003;
}
.toast-error { background: var(--danger); color: #fff; }

.login { max-width: 420px; margin: 20vh auto 0; padding: 0 16px; text-align: center; }
.login .button { margin-top: 1.5rem; }

.section { margin: 1.1rem 0; padding-top: .9rem; border-top: 1px solid var(--border); }
.section-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: .4rem; }

.swatch {
  display: inline-block; width: .8em; height: .8em; border-radius: 3px; flex: none;
  background: var(--k, var(--muted));
}

/* ---------------------------------------------------------------- liczniki */

.counter-pair { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .6rem; }
.counter {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: .7rem .8rem .6rem; border-top: 4px solid var(--k);
}
.counter-title { font-size: .82rem; font-weight: 600; color: var(--muted); }
.counter-left { display: flex; align-items: baseline; gap: .35rem; margin: .1rem 0 .35rem; }
.counter-left b { font-size: 2.3rem; line-height: 1.05; font-variant-numeric: tabular-nums; color: var(--k); }
.counter-left span { font-size: .78rem; color: var(--muted); line-height: 1.15; }
.counter.over .counter-left b, .counter.over .counter-left span { color: var(--danger); }

.bar { display: flex; height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.bar-used { background: var(--k); }
.bar-planned {
  background: repeating-linear-gradient(135deg, var(--k) 0 3px, transparent 3px 6px);
  opacity: .75;
}

.counter-detail { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: .05rem .5rem; margin: .5rem 0 0; font-size: .8rem; }
.counter-detail div { display: contents; }
.counter-detail dt { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.counter-detail dd { margin: 0; font-weight: 600; text-align: right; font-variant-numeric: tabular-nums; }
.counter-detail small { font-weight: 400; color: var(--muted); }

.minis { display: flex; flex-wrap: wrap; gap: .35rem; margin: .6rem 0 .2rem; }
.mini {
  display: inline-flex; align-items: center; gap: .35rem;
  padding: .2rem .6rem; border-radius: 999px; font-size: .8rem;
  background: var(--surface); border: 1px solid var(--border); color: var(--muted);
}
.mini b { color: var(--text); font-variant-numeric: tabular-nums; }
.mini.over b { color: var(--danger); }

/* ---------------------------------------------------------------- kalendarz */

.stepper { display: flex; align-items: center; justify-content: center; gap: .25rem; margin: 1rem 0 .3rem; position: relative; }
.stepper h2 { min-width: 11rem; text-align: center; font-size: 1.1rem; }
.step { font-size: 1.7rem; line-height: 1; min-width: 44px; color: var(--text); }
.today-btn { position: absolute; right: 0; }

.range-banner {
  display: flex; justify-content: space-between; align-items: center; gap: .5rem;
  background: var(--info-bg); border-radius: var(--radius); padding: .45rem .8rem; margin-bottom: .5rem; font-size: .88rem;
}

.cal { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; touch-action: pan-y; }
.cal-wd { text-align: center; font-size: .72rem; color: var(--muted); padding: .2rem 0; font-weight: 600; }
.cal-wd.we { color: var(--holiday); opacity: .75; }

.cal-day {
  position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center;
  aspect-ratio: 1 / 1.05; min-height: 44px; padding: 0; gap: 0;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
  font-variant-numeric: tabular-nums;
}
.cal-day .num { font-size: 1rem; font-weight: 500; line-height: 1.1; }
.cal-day .abbr { font-size: .62rem; font-weight: 700; letter-spacing: .02em; line-height: 1; margin-top: 2px; }
.cal-day.we { background: var(--surface-2); color: var(--muted); border-color: transparent; }
.cal-day.hol { color: var(--holiday); background: var(--surface-2); border-color: transparent; }
.cal-day.hol .num { font-weight: 700; }
.cal-day.past:not(.filled) .num { opacity: .6; }

.cal-day.filled { background: var(--k); border-color: var(--k); color: #fff; }
.cal-day.filled.past { opacity: .78; }
/* wpis w dniu, który się nie liczy (np. urlop w sobotę) */
.cal-day.filled.nc {
  background: repeating-linear-gradient(135deg, var(--k) 0 4px, transparent 4px 8px), var(--surface-2);
  color: var(--text);
}
.cal-day.today { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--text); }
.cal-day.sel { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--primary); }
.cal-day.sel::after {
  content: "od"; position: absolute; top: 2px; left: 4px; font-size: .55rem; font-weight: 700;
}
.note-dot {
  position: absolute; top: 4px; right: 4px; width: 6px; height: 6px; border-radius: 50%;
  background: currentColor; opacity: .85;
}

.legend { display: flex; flex-wrap: wrap; gap: .3rem .8rem; margin: .7rem 0 .2rem; font-size: .74rem; color: var(--muted); }
.legend-item { display: inline-flex; align-items: center; gap: .3rem; }
.legend-item.hol .swatch { background: var(--surface-2); border: 2px solid var(--holiday); }

/* ---------------------------------------------------------------- listy wpisów */

.entries { list-style: none; margin: .8rem 0 0; padding: 0; display: flex; flex-direction: column; gap: .4rem; }
.entry-row {
  width: 100%; justify-content: flex-start; text-align: left; gap: .6rem;
  padding: .5rem .75rem; border-left: 4px solid var(--k);
}
.entry-row.past { opacity: .7; }
.entry-row .swatch { display: none; }
.entry-main { flex: 1; min-width: 0; font-size: .9rem; }
.entry-note { display: block; color: var(--muted); font-size: .8rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry-count { color: var(--muted); font-size: .82rem; white-space: nowrap; }

.summary-table { width: 100%; border-collapse: collapse; font-size: .88rem; margin-top: .5rem; font-variant-numeric: tabular-nums; }
.summary-table th, .summary-table td { padding: .45rem .3rem; border-bottom: 1px solid var(--border); text-align: right; }
.summary-table thead th { font-size: .72rem; color: var(--muted); font-weight: 600; }
.summary-table tbody th { text-align: left; font-weight: 500; }
.summary-table tbody th .swatch { margin-right: .45rem; vertical-align: -1px; }
.summary-table tr.sub th { padding-left: 1.6rem; color: var(--muted); font-size: .82rem; }
.summary-table tr.sub td { color: var(--muted); }
.summary-table tr.over td b { color: var(--danger); }

.holidays { list-style: none; padding: 0; margin: .3rem 0; font-size: .88rem; }
.holidays li { display: flex; gap: .8rem; padding: .3rem 0; border-bottom: 1px dashed var(--border); }
.holidays .h-date { width: 6.2rem; flex: none; color: var(--holiday); font-variant-numeric: tabular-nums; }
.holidays small { color: var(--muted); }
.holidays li.sat small { color: var(--warn-text); font-weight: 600; }
.holidays li.sun { opacity: .65; }

/* ---------------------------------------------------------------- wybór typu i zakresy */

.kinds { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: .4rem; margin: .5rem 0; }
.kind-btn { justify-content: flex-start; gap: .5rem; padding: .4rem .7rem; font-size: .9rem; }
.kind-btn .swatch { width: 14px; height: 14px; border-radius: 4px; }
.kind-btn.active { background: var(--k); border-color: var(--k); color: #fff; font-weight: 600; }
.kind-btn.active .swatch { background: #fff; }

.preview { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .6rem .8rem; margin: .6rem 0; }
.preview-main { display: flex; align-items: center; gap: .45rem; font-size: .98rem; }

/* ---------------------------------------------------------------- panel od dołu */

.sheet-backdrop {
  position: fixed; inset: 0; z-index: 30; background: #0007;
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade .15s ease-out;
}
.sheet {
  width: 100%; max-width: 640px; max-height: 92vh; overflow-y: auto;
  background: var(--bg); border-radius: 18px 18px 0 0;
  padding: .5rem 16px calc(16px + env(safe-area-inset-bottom));
  animation: slide .18s ease-out;
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; padding: .3rem 0 .2rem; }
.sheet-head h2 { font-size: 1.05rem; }
@keyframes fade { from { opacity: 0; } }
@keyframes slide { from { transform: translateY(30px); opacity: .6; } }
@media (prefers-reduced-motion: reduce) {
  .sheet, .sheet-backdrop, .sync-busy { animation: none; }
}

@media (min-width: 640px) {
  .sheet { border-radius: 18px; margin-bottom: 5vh; }
  .sheet-backdrop { align-items: center; }
  .cal-day { aspect-ratio: 1 / .8; }
}
