:root{
  --bg1:#030712;--bg2:#0f172a;--panel:rgba(12,18,32,.88);--line:rgba(148,163,184,.18);
  --text:#f8fafc;--muted:#94a3b8;--cyan:#22d3ee;--blue:#3b82f6;--violet:#8b5cf6;
  --green:#22c55e;--yellow:#facc15;--red:#ef4444;--sat:rgba(21,39,79,.86);
  --sun:rgba(88,20,30,.84);--holiday:rgba(8,78,92,.88);--boxbg:#081226
}
*{box-sizing:border-box}
html,body{margin:0;min-height:100%}
body{
  color:var(--text);
  font-family:Inter,-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;
  background:
    radial-gradient(circle at 12% 18%,rgba(34,211,238,.14) 0,rgba(34,211,238,0) 26%),
    radial-gradient(circle at 88% 10%,rgba(139,92,246,.14) 0,rgba(139,92,246,0) 24%),
    linear-gradient(160deg,var(--bg1) 0%,var(--bg2) 55%,#111827 100%);
  overflow-x:hidden;
}
.shell{max-width:100vw;margin:0 auto;padding:14px 8px}
.glass,.card{
  background:var(--panel);border:1px solid var(--line);border-radius:22px;
  backdrop-filter:blur(14px) saturate(140%);-webkit-backdrop-filter:blur(14px) saturate(140%);
  box-shadow:0 18px 60px rgba(0,0,0,.35)
}
.nav{display:flex;justify-content:space-between;align-items:center;gap:12px;flex-wrap:wrap;padding:14px 16px;margin-bottom:14px}
.brand{display:flex;align-items:center;gap:12px}.brand-mark{width:42px;height:42px;border-radius:14px;display:grid;place-items:center;font-weight:900;background:linear-gradient(135deg,#22d3ee,#8b5cf6);color:#04111f}
.brand-title{font-weight:900;font-size:18px}.brand-sub{color:var(--muted);font-size:12px}.nav-links,.actions{display:flex;gap:8px;flex-wrap:wrap;align-items:center}
.btn,select,input,textarea{border-radius:12px;border:1px solid var(--line);color:var(--text);background:rgba(8,12,22,.94);padding:10px 12px;font-size:14px}
.btn{cursor:pointer;font-weight:800;background:linear-gradient(135deg,rgba(34,211,238,.18),rgba(59,130,246,.18));transition:.16s ease}
.btn:hover:not(:disabled){background:linear-gradient(135deg,rgba(59,130,246,.34),rgba(37,99,235,.34));border-color:rgba(96,165,250,.42);transform:translateY(-1px);box-shadow:0 10px 22px rgba(37,99,235,.18)}
.btn.primary{color:#04111f;background:linear-gradient(135deg,#22d3ee,#3b82f6)}
.btn.ghost,.btn.secondary{background:rgba(255,255,255,.04);color:var(--text)}
.btn.warn{background:linear-gradient(135deg,#facc15,#f59e0b);color:#111827}.btn.success{background:linear-gradient(135deg,#4ade80,#22c55e);color:#052e16}.btn.danger{background:linear-gradient(135deg,#fb7185,#ef4444);color:#fff}
.btn:disabled,input:disabled,select:disabled,textarea:disabled{opacity:.55;cursor:not-allowed}
.panel{padding:18px;margin-bottom:14px}.section-head{display:flex;justify-content:space-between;align-items:end;gap:12px;flex-wrap:wrap;margin-bottom:14px}.section-head h2,.section-head h3{margin:0}.section-head p,.muted{margin:4px 0 0;color:var(--muted);font-size:13px}
.fields{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:12px}.field,.stat-box,.modal-stat{padding:14px;border-radius:16px;background:rgba(255,255,255,.03);border:1px solid var(--line)}.field label{display:block;margin-bottom:8px;font-weight:800;color:#cbd5e1}
.pill,.status-badge,.checkmark-badge{display:inline-flex;align-items:center;gap:8px;padding:8px 12px;border-radius:999px;font-size:13px;font-weight:800;white-space:nowrap}.pill{background:rgba(255,255,255,.05);border:1px solid rgba(255,255,255,.08);color:#dbeafe}.role-badge{background:linear-gradient(135deg,#22d3ee,#8b5cf6);color:#fff;border-color:rgba(255,255,255,.16);box-shadow:0 10px 24px rgba(59,130,246,.18)}
.status-strip{display:flex;gap:10px;flex-wrap:wrap;align-items:center;margin-top:14px;padding:12px 14px;border-radius:16px;background:rgba(255,255,255,.03);border:1px solid var(--line)}
.flash{padding:12px 14px;border-radius:14px;margin-bottom:12px;border:1px solid transparent}.flash.ok{background:rgba(34,197,94,.12);border-color:rgba(34,197,94,.26)}.flash.err{background:rgba(251,113,133,.12);border-color:rgba(251,113,133,.3)}
.login-wrap{min-height:100vh;display:grid;place-items:center;padding:18px}.login-box{width:min(540px,100%);padding:28px;border-radius:28px}.login-hero{display:grid;gap:18px;text-align:center}.login-logo-wrap{display:flex;justify-content:center;align-items:center;margin-bottom:4px}.login-logo{width:84px;height:84px;border-radius:26px;display:grid;place-items:center;background:linear-gradient(135deg,rgba(34,211,238,.28),rgba(59,130,246,.22),rgba(139,92,246,.24));border:1px solid rgba(255,255,255,.14);box-shadow:0 18px 50px rgba(37,99,235,.18),inset 0 1px 0 rgba(255,255,255,.12)}.login-logo-mark{font-size:30px;font-weight:900;letter-spacing:.02em;color:#eef6ff;text-shadow:0 2px 16px rgba(3,7,18,.35)}.login-title-xl{margin:0;font-size:34px;font-weight:900;letter-spacing:-.03em}.login-subtitle{margin:0;color:var(--muted);font-size:14px;line-height:1.5}.login-form-shell{padding:26px;border-radius:28px;background:linear-gradient(180deg,rgba(255,255,255,.05),rgba(255,255,255,.025));border:1px solid rgba(255,255,255,.08);box-shadow:inset 0 1px 0 rgba(255,255,255,.05)}.login-field label{display:block;margin-bottom:10px;font-weight:900;color:#dbeafe;text-align:left;font-size:18px}.login-form-shell input{width:100%;min-height:50px;border-radius:16px;border:1px solid rgba(255,255,255,.10);background:linear-gradient(180deg,rgba(2,6,23,.88),rgba(2,6,23,.96));box-shadow:inset 0 1px 0 rgba(255,255,255,.04),0 10px 26px rgba(2,6,23,.24);font-size:18px;padding:12px 16px;color:#f8fafc}.login-form-shell .btn.primary{min-height:52px;font-size:20px;border-radius:18px;font-weight:900;letter-spacing:-.02em;box-shadow:0 12px 24px rgba(59,130,246,.18);padding:10px 22px;margin-top:6px}
.summary-cards{display:grid;grid-template-columns:repeat(auto-fit,minmax(260px,1fr));gap:14px;margin-bottom:16px}.panel-tile{transition:.18s ease;cursor:pointer}.panel-tile:hover{transform:translateY(-2px) scale(1.01);border-color:rgba(96,165,250,.3);box-shadow:0 16px 36px rgba(37,99,235,.16);background:linear-gradient(135deg,rgba(59,130,246,.08),rgba(139,92,246,.08))}
.table-wrap{overflow:auto}table{width:100%;border-collapse:separate;border-spacing:0;font-size:13px}th,td{padding:8px 6px;text-align:center;border-bottom:1px solid rgba(255,255,255,.07);border-right:1px solid rgba(255,255,255,.04);white-space:nowrap}th{background:rgba(8,12,22,.96);color:#bae6fd;font-weight:900}td:first-child,th:first-child{text-align:left;background:rgba(8,12,22,.97);font-weight:800;position:sticky;left:0;z-index:1}
.day-head-sat{color:#dbeafe;background:rgba(30,64,175,.42)!important}.day-head-sun{color:#fee2e2;background:rgba(127,29,29,.46)!important}.day-head-holiday{color:#ecfeff;background:rgba(8,145,178,.44)!important}
.sheet-wrap{margin-top:18px;border-radius:20px;border:1px solid var(--line);background:linear-gradient(180deg,rgba(255,255,255,.035),rgba(255,255,255,.02));padding:12px;overflow-x:hidden;max-width:100vw}.sheet-toolbar{display:flex;justify-content:space-between;align-items:center;gap:10px;flex-wrap:wrap;margin-bottom:12px}.sheet-toolbar-right{display:flex;align-items:center;gap:10px;flex-wrap:wrap}.sheet-brush-pill{display:inline-flex;align-items:center;gap:8px;padding:8px 10px;border-radius:14px;background:rgba(255,255,255,.05);border:1px solid rgba(255,255,255,.10);font-weight:800;color:#e2e8f0}.sheet-brush-buttons{display:flex;gap:8px;flex-wrap:wrap;align-items:center}.sheet-brush-btn{padding:6px 10px;border-radius:12px;border:1px solid rgba(255,255,255,.10);background:rgba(3,7,18,.96);color:var(--text);font-size:15px;font-weight:800;cursor:pointer}.sheet-brush-btn.active{background:linear-gradient(135deg,rgba(34,197,94,.36),rgba(21,128,61,.86));border-color:rgba(134,239,172,.38)}
.sheet-table{width:100%;max-width:calc(100vw - 20px);table-layout:fixed;border-collapse:separate;border-spacing:0}.sheet-table th,.sheet-table td{padding:1px;text-align:center;border-right:1px solid rgba(255,255,255,.05);border-bottom:1px solid rgba(255,255,255,.08);min-width:0;background:rgba(8,12,22,.82)}.sheet-table th{font-size:11px;font-weight:900;color:#dbeafe;background:rgba(8,12,22,.98);position:relative}.sheet-cell.saturday{background:var(--sat)}.sheet-cell.sunday{background:var(--sun)}.sheet-cell.holiday{background:var(--holiday)}.sheet-cell.filled .sheet-button{background:linear-gradient(180deg,#22c55e,#166534);color:#ecfdf5}.sheet-button{width:100%;min-height:90px;border-radius:10px;border:1px solid rgba(255,255,255,.10);background:rgba(3,7,18,.96);color:var(--text);font-size:9.5px;font-weight:900;cursor:pointer;text-align:center;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;padding:1px;line-height:1}.sheet-button.empty-value{color:#cbd5e1}.sheet-cell.saturday .sheet-button{background:#dbeafe;color:#111827}.sheet-cell.sunday .sheet-button{background:#fee2e2;color:#111827}.sheet-cell.holiday .sheet-button{background:#d1fae5;color:#111827}
.comment-box{margin-top:14px;padding:14px;border-radius:16px;background:rgba(255,255,255,.03);border:1px solid var(--line)}.comment-box label{display:block;margin-bottom:8px;font-weight:800;color:#cbd5e1}.comment-textarea{width:100%;min-height:40px;max-height:72px;resize:vertical;border-radius:14px;border:1px solid rgba(255,255,255,.10);background:rgba(3,7,18,.96);color:var(--text);padding:8px 12px;font-size:14px;line-height:1.25;outline:none}
.top-stats{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:12px;margin-bottom:16px}.stat-box strong{display:block;font-size:24px;margin-top:4px}.warning-box{margin:14px 0 16px;padding:14px 16px;border-radius:16px;background:rgba(250,204,21,.14);border:1px solid rgba(250,204,21,.34);color:#fde68a;font-weight:700}.danger-banner{margin:14px 0 16px;padding:14px 16px;border-radius:16px;background:rgba(239,68,68,.14);border:1px solid rgba(239,68,68,.34);color:#fecaca;font-weight:700}
.time-input{width:84px;text-align:center;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;background:var(--boxbg)}.sum-box{min-width:64px;padding:10px 8px;border-radius:10px;border:1px solid var(--line);background:var(--boxbg);font-weight:700;display:inline-block}.comment-input{width:220px}.col-blok{background:rgba(59,130,246,.08)}.col-poradnia{background:rgba(192,132,252,.18)}.col-oit{background:rgba(16,185,129,.08)}.day-total-col{background:rgba(255,255,255,.04)}.date-cell{min-width:150px;text-align:left;padding-left:14px;font-weight:700}.date-cell .muted{display:block;font-weight:500}
.hour-brush-bar{display:flex;gap:8px;flex-wrap:wrap;margin:10px 0 12px}.hour-brush{padding:8px 10px;border-radius:12px;border:1px solid rgba(255,255,255,.10);background:rgba(3,7,18,.96);color:var(--text);font-weight:800;cursor:pointer}.hour-brush.active{background:linear-gradient(135deg,rgba(34,197,94,.36),rgba(21,128,61,.86));border-color:rgba(134,239,172,.38)}
@media(max-width:1280px){.top-stats{grid-template-columns:repeat(2,minmax(0,1fr))}}@media(max-width:900px){.fields{grid-template-columns:1fr}.sheet-brush-btn{font-size:13px}}@media(max-width:760px){.top-stats{grid-template-columns:1fr}.mobile-hours table{min-width:760px}}

.user-modal-backdrop{position:fixed;inset:0;background:rgba(2,6,23,.82);backdrop-filter:blur(8px);display:grid;place-items:center;padding:16px;z-index:9999}
.user-modal{width:min(560px,100%);padding:22px;border-radius:24px;background:rgba(12,18,32,.98);border:1px solid rgba(148,163,184,.22);box-shadow:0 24px 80px rgba(0,0,0,.5)}
.user-modal h3{margin:0 0 6px}.user-modal .modal-grid{display:grid;gap:12px;margin-top:16px}.user-modal label{display:block;font-weight:900;color:#cbd5e1;margin-bottom:6px}.user-modal input,.user-modal select{width:100%}.modal-actions{display:flex;justify-content:flex-end;gap:10px;flex-wrap:wrap;margin-top:16px}


.admin-summary-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(240px,1fr));gap:12px;margin:12px 0 16px}
.admin-summary-box{padding:14px;border-radius:16px;background:rgba(255,255,255,.035);border:1px solid var(--line)}
.admin-summary-box strong{display:block;font-size:26px;margin-top:4px}
.admin-missing-list{display:flex;gap:8px;flex-wrap:wrap;margin-top:10px}
.admin-missing-list span{padding:7px 10px;border-radius:999px;background:rgba(250,204,21,.12);border:1px solid rgba(250,204,21,.28);color:#fde68a;font-weight:800;font-size:12px}
.admin-editor-banner{margin:0 0 14px;padding:14px 16px;border-radius:18px;background:linear-gradient(135deg,rgba(34,211,238,.12),rgba(139,92,246,.12));border:1px solid rgba(96,165,250,.28);display:flex;justify-content:space-between;gap:10px;align-items:center;flex-wrap:wrap}
.admin-user-select{min-width:260px}
.admin-mini-status{display:inline-flex;padding:6px 9px;border-radius:999px;font-weight:900;font-size:12px}
.admin-mini-status.ok{background:rgba(34,197,94,.16);color:#bbf7d0;border:1px solid rgba(34,197,94,.3)}
.admin-mini-status.no{background:rgba(250,204,21,.12);color:#fde68a;border:1px solid rgba(250,204,21,.28)}
.admin-mini-status.bad{background:rgba(239,68,68,.14);color:#fecaca;border:1px solid rgba(239,68,68,.28)}
.admin-detail-data{max-width:420px;white-space:normal;word-break:break-word;text-align:left!important}
.admin-actions-inline{display:flex;gap:6px;flex-wrap:wrap;justify-content:flex-end}
.admin-actions-inline .btn{padding:6px 9px;font-size:12px;border-radius:10px}


.admin-missing-list span.ok-pill{background:rgba(34,197,94,.14);border:1px solid rgba(34,197,94,.30);color:#bbf7d0}


.confirmed-banner-force{
  border:1px solid rgba(80,220,120,.55)!important;
  background:rgba(40,120,60,.18)!important;
  color:#b9ffcb!important;
}

.dezy-banner{
  margin:14px 0 16px;
  padding:14px 16px;
  border-radius:16px;
  font-weight:800;
  line-height:1.35;
}
.dezy-banner-yellow{
  background:rgba(250,204,21,.14)!important;
  border:1px solid rgba(250,204,21,.34)!important;
  color:#fde68a!important;
}
.dezy-banner-red{
  background:rgba(239,68,68,.14)!important;
  border:1px solid rgba(239,68,68,.34)!important;
  color:#fecaca!important;
}
.dezy-banner-green{
  background:rgba(34,197,94,.14)!important;
  border:1px solid rgba(34,197,94,.34)!important;
  color:#bbf7d0!important;
}



/* ===== iAPM login visual upgrade v2: EKG + syringe ===== */
.login-wrap{
  min-height:100vh;
  position:relative;
  overflow:hidden;
  display:grid;
  place-items:center;
  padding:24px;
  font-family:Inter,-apple-system,BlinkMacSystemFont,"SF Pro Display","Segoe UI",sans-serif;
  background:
    radial-gradient(circle at 18% 32%, rgba(34,211,238,.18), transparent 34%),
    radial-gradient(circle at 78% 72%, rgba(124,58,237,.34), transparent 38%),
    radial-gradient(circle at 52% 44%, rgba(59,130,246,.13), transparent 34%),
    linear-gradient(135deg,#020617 0%,#07111f 42%,#090b1f 100%);
}

.login-wrap::before{
  content:"";
  position:absolute;
  inset:0;
  opacity:.32;
  background-image:
    linear-gradient(rgba(125,211,252,.06) 1px, transparent 1px),
    linear-gradient(90deg, rgba(125,211,252,.06) 1px, transparent 1px);
  background-size:76px 76px;
  mask-image:radial-gradient(circle at 50% 48%, black 0%, transparent 74%);
  pointer-events:none;
}

.login-wrap::after{
  content:"";
  position:absolute;
  inset:-20%;
  background:
    radial-gradient(circle at 14% 20%, rgba(103,232,249,.15), transparent 8%),
    radial-gradient(circle at 84% 28%, rgba(96,165,250,.14), transparent 9%),
    radial-gradient(circle at 77% 77%, rgba(168,85,247,.18), transparent 12%);
  filter:blur(18px);
  animation:iAPMFloatGlow 10s ease-in-out infinite alternate;
  pointer-events:none;
}

@keyframes iAPMFloatGlow{
  from{transform:translate3d(-12px,-8px,0) scale(1)}
  to{transform:translate3d(16px,10px,0) scale(1.05)}
}

.login-bg-svg{
  position:absolute;
  inset:0;
  width:100%;
  height:100%;
  z-index:1;
  pointer-events:none;
  opacity:.78;
}

.login-ekg-path{
  fill:none;
  stroke:rgba(125,211,252,.54);
  stroke-width:3.2;
  stroke-linecap:round;
  stroke-linejoin:round;
  filter:drop-shadow(0 0 12px rgba(34,211,238,.38));
  stroke-dasharray:1220;
  stroke-dashoffset:1220;
  animation:iAPMEkgDraw 4.6s ease-in-out infinite;
}

.login-ekg-path.dim{
  stroke:rgba(148,163,184,.18);
  stroke-width:2;
  stroke-dasharray:none;
  stroke-dashoffset:0;
  animation:none;
}

@keyframes iAPMEkgDraw{
  0%{stroke-dashoffset:1220;opacity:.12}
  18%{opacity:.92}
  55%{stroke-dashoffset:0;opacity:.76}
  78%{stroke-dashoffset:-1220;opacity:.18}
  100%{stroke-dashoffset:-1220;opacity:.08}
}

.login-syringe{
  transform-origin:center;
  animation:iAPMSyringeFloat 7s ease-in-out infinite alternate;
  opacity:.50;
  filter:drop-shadow(0 0 18px rgba(96,165,250,.28));
}

@keyframes iAPMSyringeFloat{
  from{transform:translate(0,0) rotate(-9deg)}
  to{transform:translate(20px,-14px) rotate(-5deg)}
}

.login-orb{
  position:absolute;
  width:120px;
  height:120px;
  border-radius:50%;
  border:1px solid rgba(125,211,252,.30);
  box-shadow:0 0 32px rgba(34,211,238,.10), inset 0 0 24px rgba(59,130,246,.08);
  pointer-events:none;
  z-index:1;
}

.login-orb.one{left:7%;top:18%;width:96px;height:96px;animation:iAPMOrb 13s linear infinite}
.login-orb.two{right:11%;top:25%;width:116px;height:116px;animation:iAPMOrb 17s linear infinite reverse}
.login-orb.three{right:8%;bottom:16%;width:84px;height:84px;animation:iAPMOrb 14s linear infinite}

@keyframes iAPMOrb{
  from{transform:rotate(0deg) translateY(0)}
  to{transform:rotate(360deg) translateY(8px)}
}

.login-box{
  position:relative;
  z-index:2;
  width:min(500px,100%);
  padding:30px;
  border-radius:32px;
  background:
    linear-gradient(180deg,rgba(15,23,42,.78),rgba(15,23,42,.52)),
    radial-gradient(circle at 50% 0%,rgba(56,189,248,.22),transparent 58%);
  border:1px solid rgba(148,163,184,.22);
  box-shadow:
    0 0 0 1px rgba(255,255,255,.04) inset,
    0 26px 90px rgba(0,0,0,.52),
    0 0 54px rgba(56,189,248,.18);
  backdrop-filter:blur(22px);
}

.login-logo{
  width:76px;
  height:76px;
  border-radius:24px;
  background:
    radial-gradient(circle at 30% 25%,rgba(255,255,255,.40),transparent 24%),
    linear-gradient(135deg,#67e8f9,#3b82f6 52%,#8b5cf6);
  box-shadow:0 18px 44px rgba(59,130,246,.28),0 0 30px rgba(34,211,238,.18);
}

.login-title-xl{
  font-size:34px;
  letter-spacing:-.04em;
  font-weight:900;
  color:#f8fafc;
  text-shadow:0 8px 32px rgba(56,189,248,.16);
}

.login-subtitle{
  margin-top:-6px;
  font-size:15px;
  color:#c7d2fe;
  font-weight:700;
}

.login-form-shell{
  margin-top:18px;
  padding:24px;
  border-radius:26px;
  background:rgba(15,23,42,.62);
  border:1px solid rgba(148,163,184,.16);
}

.login-form-shell input{
  min-height:52px;
  border-radius:16px;
  border:1px solid rgba(148,163,184,.18);
  background:rgba(2,6,23,.82);
  color:#f8fafc;
  box-shadow:inset 0 1px 0 rgba(255,255,255,.05);
}

.login-form-shell label{
  font-weight:850;
  color:#f8fafc;
  letter-spacing:-.02em;
}

.login-form-shell .btn.primary{
  min-height:48px;
  min-width:132px;
  padding:10px 22px;
  border-radius:999px;
  font-size:16px;
  margin-top:10px;
  background:linear-gradient(135deg,#67e8f9,#60a5fa);
  color:#082f49;
  box-shadow:0 14px 34px rgba(56,189,248,.26);
}



/* === HOURS UI PLUS START === */

if(typeof escAttr === 'undefined'){
  var escAttr = function(s){ return String(s ?? '').replaceAll('&','&amp;').replaceAll('"','&quot;').replaceAll('<','&lt;').replaceAll('>','&gt;'); };
}

function hoursStatusText(row){
  if(row?.verified) return 'zgodne';
  if(row?.submitted) return 'potwierdzone';
  return 'edycja';
}

function hoursBigBanner(h){
  if(h?.verified){
    return `<div class="hours-banner-ok">
      <h2>Twój grafik został oznaczony jako zgodny</h2>
      <p>Sekretarka / administrator potwierdził zgodność godzin z wewnętrznym grafikiem szpitala.</p>
    </div>`;
  }
  if(h?.objection){
    return `<div class="danger-banner">
      <div class="danger-banner-title">Administrator zgłosił zastrzeżenie</div>
      <div class="danger-comment">${esc(h.objection)}</div>
    </div>`;
  }
  return '';
}

function openHoursConfirmModal(){
  if(state.hours && state.hours.submitted) return;
  refreshHourTotals();
  state.hoursConfirmModalOpen = true;
  render();
}

function closeHoursConfirmModal(){
  state.hoursConfirmModalOpen = false;
  render();
}

async function finalConfirmHours(){
  await saveHours();
  const d = await apiPost('/hours_submit.php', {month:getSelectedMonth()});
  state.hours = d.data;
  state.hoursConfirmModalOpen = false;
  await go('hours');
}

function hoursConfirmModal(){
  if(!state.hoursConfirmModalOpen) return '';
  const data = collectHours();
  const t = calcHoursTotals(data).totals;
  return `<div class="hours-modal-backdrop">
    <div class="hours-modal-card glass">
      <h2>Ostateczne potwierdzenie godzin</h2>
      <p class="muted">Sprawdź podsumowanie przed zablokowaniem formularza.</p>
      <div class="hours-modal-grid">
        <div class="hours-modal-stat"><span>BLOK</span><strong>${t.blok} h</strong></div>
        <div class="hours-modal-stat"><span>PORADNIA</span><strong>${t.poradnia} h</strong></div>
        <div class="hours-modal-stat"><span>OIT</span><strong>${t.oit} h</strong></div>
        <div class="hours-modal-stat"><span>SUMA</span><strong>${t.all} h</strong></div>
      </div>
      <div class="hours-banner-warn">Po tej operacji wpisy zostaną zablokowane.</div>
      <div class="modal-actions">
        <button class="btn secondary" onclick="closeHoursConfirmModal()">Wróć</button>
        <button class="btn warn" onclick="finalConfirmHours()">Ostatecznie potwierdź godziny</button>
      </div>
    </div>
  </div>`;
}

function hoursView(){
  const month = getSelectedMonth();
  const h = state.hours || {data:{},submitted:false,verified:false,objection:''};
  const data = normalizeHoursData(h.data || {});
  const s = calcHoursTotals(data);
  const locked = h.submitted ? 'disabled' : '';
  const monthLabel = CONFIG?.months?.find(x=>x.value===month)?.label || month;

  return `${nav()}
  ${hoursBigBanner(h)}
  <div class="top-stats">
    <div class="stat-box"><div class="muted">Suma miesiąca</div><strong id="monthTotalLive">${s.totals.all} h</strong></div>
    <div class="stat-box"><div class="muted">BLOK</div><strong id="total_blok">${s.totals.blok} h</strong></div>
    <div class="stat-box"><div class="muted">PORADNIA</div><strong id="total_poradnia">${s.totals.poradnia} h</strong></div>
    <div class="stat-box"><div class="muted">OIT</div><strong id="total_oit">${s.totals.oit} h</strong></div>
  </div>

  <section class="panel glass">
    <div class="section-head">
      <div>
        <h2>Rozliczenie godzin - ${esc(monthLabel)}</h2>
        ${
  h.verified
    ? `<div class="hours-verified-banner">
        <div class="hours-verified-banner-title">
          Potwierdzone i zgodne
        </div>
        <div class="hours-verified-banner-sub">
          Administrator potwierdził zgodność Twojego rozliczenia godzin
          z grafikiem szpitalnym.
        </div>
      </div>`
    : `<p>${
        h.submitted
          ? '✅ Lekarz potwierdził'
          : 'Autosave aktywny co 0,5 s'
      }</p>`
}
      </div>
      <div>
        <select onchange="state.selectedMonth=this.value;go('hours')">
          ${(CONFIG?.months||[]).map(m=>`<option value="${m.value}" ${m.value===month?'selected':''}>${m.label}</option>`).join('')}
        </select>
      </div>
    </div>

    <div class="table-wrap mobile-hours">
      <table>
        <thead>
          <tr>
            <th rowspan="2">Data</th>
            <th colspan="3" class="col-blok">BLOK</th>
            <th colspan="3" class="col-poradnia">PORADNIA</th>
            <th colspan="3" class="col-oit">OIT</th>
            <th rowspan="2" class="day-total-col">Łącznie</th>
            <th rowspan="2">Komentarz</th>
          </tr>
          <tr>
            <th class="col-blok">Od godz.</th><th class="col-blok">Do godz.</th><th class="col-blok">Suma</th>
            <th class="col-poradnia">Od godz.</th><th class="col-poradnia">Do godz.</th><th class="col-poradnia">Suma</th>
            <th class="col-oit">Od godz.</th><th class="col-oit">Do godz.</th><th class="col-oit">Suma</th>
          </tr>
        </thead>
        <tbody>
          ${hourDays(month).map(day=>{
            const k = String(day.getDate());
            const dd = data[k];
            const r = s.rows[k] || {blok:0,poradnia:0,oit:0,total:0};
            return `<tr>
              <td class="date-cell">${day.toLocaleDateString('pl-PL')}<span class="muted">${day.toLocaleDateString('pl-PL',{weekday:'long'})}</span></td>
              ${['blok','poradnia','oit'].map(loc=>`
                <td class="hours-col-${loc}"><input data-hours-input class="time-input" id="h_${k}_${loc}_from" value="${esc(dd[loc].from||'')}" ${locked}></td>
                <td class="hours-col-${loc}"><input data-hours-input class="time-input" id="h_${k}_${loc}_to" value="${esc(dd[loc].to||'')}" ${locked}></td>
                <td class="hours-col-${loc}"><span class="sum-box" id="sum_${k}_${loc}">${r[loc]}</span></td>
              `).join('')}
              <td class="day-total-col"><span class="sum-box ${r.total>24?'over-limit':''}" id="sum_${k}_total">${r.total} h</span></td>
              <td><input data-hours-input class="comment-input" id="h_${k}_comment" value="${esc(dd.comment||'')}" ${locked}></td>
            </tr>`;
          }).join('')}
        </tbody>
      </table>
    </div>

    ${(()=>{
      const hs = dezyHoursSummary(data);

      return `
      <div class="hours-summary-box">
        <div class="hours-summary-title">
          Suma 7+7,583+D+N+24
        </div>

        <div class="hours-summary-grid">

          <div class="hours-summary-pill">
            <div class="hours-summary-pill-label">7h</div>
            <div class="hours-summary-pill-value">${hs.c7}</div>
          </div>

          <div class="hours-summary-pill">
            <div class="hours-summary-pill-label">7,583h</div>
            <div class="hours-summary-pill-value">${hs.c7583}</div>
          </div>

          <div class="hours-summary-pill">
            <div class="hours-summary-pill-label">D = 12h</div>
            <div class="hours-summary-pill-value">${hs.cD}</div>
          </div>

          <div class="hours-summary-pill">
            <div class="hours-summary-pill-label">N = 12h</div>
            <div class="hours-summary-pill-value">${hs.cN}</div>
          </div>

          <div class="hours-summary-pill">
            <div class="hours-summary-pill-label">24h</div>
            <div class="hours-summary-pill-value">${hs.c24}</div>
          </div>

        </div>

        <div class="hours-summary-total">
          <div class="hours-summary-total-label">
            Łączna suma godzin
          </div>

          <div class="hours-summary-total-value">
            ${hs.total} h
          </div>
        </div>
      </div>
      `;
    })()}

    <div class="status-strip">
      ${h.submitted
        ? `<button class="btn ghost" disabled>${h.verified ? 'Potwierdzone i zgodne' : 'Potwierdzone przez lekarza'}</button>`
        : `<button class="btn primary" onclick="saveHours()">Zapisz</button><button class="btn warn" onclick="openHoursConfirmModal()">Potwierdzam rozliczenie godzin</button>`
      }
    </div>
  </section>
  ${hoursConfirmModal()}`;
}

async function exportAdminHoursXlsx(){
  if(typeof ExcelJS === 'undefined'){ alert('Biblioteka XLSX nie załadowała się.'); return; }

  const month = getSelectedMonth();
  const rows = (state.adminHours || []).filter(r => {
    const t = String(r.employment_type || r.employmentType || '').toLowerCase();

    return (
      t === 'kontrakt' ||
      t === 'wojskowy_kontrakt' ||
      t === 'wojskowy kontrakt'
    );
  });
  const wb = new ExcelJS.Workbook();
  const ws = wb.addWorksheet('Podsumowanie');

  ws.addRow(['Zbiorcze godziny', month]);
  ws.addRow(['Lekarz','Jednostka','Suma','BLOK','PORADNIA','OIT','Status']);
  ws.getRow(2).font = {bold:true};

  rows.forEach(r=>{
    const data = normalizeHoursData(r.data || {});
    const t = calcHoursTotals(data).totals;
    ws.addRow([r.display_name || r.username || r.user_id, r.unit_code || '', t.all, t.blok, t.poradnia, t.oit, hoursStatusText(r)]);
  });

  rows.forEach(r=>{
    const name = String(r.display_name || r.username || r.user_id).slice(0,28);
    const sheet = wb.addWorksheet(name);
    const data = normalizeHoursData(r.data || {});
    const sums = calcHoursTotals(data);
    sheet.addRow([r.display_name || r.username || r.user_id, month]);
    sheet.addRow(['Data','BLOK od','BLOK do','BLOK h','PORADNIA od','PORADNIA do','PORADNIA h','OIT od','OIT do','OIT h','Łącznie','Komentarz']);
    sheet.getRow(2).font = {bold:true};

    hourDays(month).forEach(day=>{
      const k = String(day.getDate());
      const d = data[k] || emptyHourDay();
      const s = sums.rows[k] || {blok:0,poradnia:0,oit:0,total:0};
      sheet.addRow([
        day.toLocaleDateString('pl-PL'),
        d.blok.from || '', d.blok.to || '', s.blok,
        d.poradnia.from || '', d.poradnia.to || '', s.poradnia,
        d.oit.from || '', d.oit.to || '', s.oit,
        s.total,
        d.comment || ''
      ]);
    });
  });

  const buffer = await wb.xlsx.writeBuffer();
  const blob = new Blob([buffer],{type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
  const url = URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = `godziny_${month}.xlsx`;
  a.click();
  URL.revokeObjectURL(url);
}

function adminHoursView(){
  const month = getSelectedMonth();
  const rows = (state.adminHours || []).filter(r => {
  const t = String(
    r.employment_type ||
    r.employmentType ||
    ''
  ).toLowerCase().trim();

  return (
    t === 'kontrakt' ||
    t === 'wojskowy kontrakt' ||
    t === 'wojskowy_kontrakt'
  );
});
  const monthLabel = CONFIG?.months?.find(x=>x.value===month)?.label || month;

  const summary = rows.map(r=>{
    const data = normalizeHoursData(r.data || {});
    const totals = calcHoursTotals(data).totals;
    return {name:r.display_name || r.username || ('user #' + r.user_id), total:totals.all, status:hoursStatusText(r)};
  });

  return `${nav()}
  <section class="panel glass">
    <div class="section-head">
      <div>
        <h2>Zbiorcze godziny - ${esc(monthLabel)}</h2>
        <p>Admin widzi swoją lokalizację. Superuser widzi wszystkich.</p>
      </div>
      <div class="actions">
        <select onchange="state.selectedMonth=this.value;go('adminHours')">
          ${(CONFIG?.months||[]).map(m=>`<option value="${m.value}" ${m.value===month?'selected':''}>${m.label}</option>`).join('')}
        </select>
        <button class="btn primary" onclick="exportAdminHoursXlsx()">Eksport XLSX</button>
      </div>
    </div>

    <div class="hours-admin-main">
      <div>
        <div class="table-wrap rounded-table-wrap">
          <table>
            <thead>
              <tr>
                <th>Lekarz</th>
                <th>Suma godzin</th>
                <th>Status</th>
                <th>Akcje administratora</th>
              </tr>
            </thead>
            <tbody>
              ${rows.map(r=>{
                const data = normalizeHoursData(r.data || {});
                const totals = calcHoursTotals(data).totals;
                const name = r.display_name || r.username || ('user #' + r.user_id);
                const status = r.verified ? 'Potwierdzone i zgodne' : r.submitted ? 'Potwierdzone' : 'Do edycji';
                return `<tr>
                  <td><strong>${esc(name)}</strong></td>
                  <td><strong>${totals.all} h</strong></td>
                  <td><span class="pill">${esc(status)}</span></td>
                  <td>
                    <button class="btn secondary" onclick="reopenHours(${Number(r.user_id)},'${escAttr(month)}')">Wycofaj</button>
                    <button class="btn success" onclick="verifyHours(${Number(r.user_id)},'${escAttr(month)}')">${r.verified ? 'Cofnij zgodność' : 'Zgodność h'}</button>
                    <button class="btn danger" onclick="objectionHours(${Number(r.user_id)},'${escAttr(month)}')">Kwestionuj</button>
                  </td>
                </tr>
                <tr>
                  <td colspan="4">
                    <details>
                      <summary>${esc(name)} - ${totals.all} h • ${esc(status)}</summary>
                      <div class="table-wrap">
                        <table>
                          <thead>
                            <tr>
                              <th>Data</th>
                              <th class="col-blok">BLOK</th><th class="col-blok">h</th>
                              <th class="col-poradnia">PORADNIA</th><th class="col-poradnia">h</th>
                              <th class="col-oit">OIT</th><th class="col-oit">h</th>
                              <th class="day-total-col">Łącznie</th>
                              <th>Komentarz</th>
                            </tr>
                          </thead>
                          <tbody>
                            ${hourDays(month).map(day=>{
                              const k = String(day.getDate());
                              const d = data[k] || emptyHourDay();
                              const sums = calcHoursTotals(data).rows[k] || {blok:0,poradnia:0,oit:0,total:0};
                              const fmt = loc => d[loc]?.from && d[loc]?.to ? `${esc(d[loc].from)} - ${esc(d[loc].to)}` : '- - -';
                              return `<tr>
                                <td>${day.toLocaleDateString('pl-PL')}</td>
                                <td class="hours-col-blok">${fmt('blok')}</td><td class="hours-col-blok">${sums.blok}</td>
                                <td class="hours-col-poradnia">${fmt('poradnia')}</td><td class="hours-col-poradnia">${sums.poradnia}</td>
                                <td class="hours-col-oit">${fmt('oit')}</td><td class="hours-col-oit">${sums.oit}</td>
                                <td class="day-total-col"><strong>${sums.total}</strong></td>
                                <td>${esc(d.comment||'')}</td>
                              </tr>`;
                            }).join('')}
                          </tbody>
                        </table>
                      </div>
                    </details>
                  </td>
                </tr>`;
              }).join('')}
            </tbody>
          </table>
        </div>
      </div>

      <aside class="panel glass hours-side-summary">
        <h3>Podsumowanie</h3>
        <table>
          <tbody>
            ${summary.map(x=>`<tr><td><strong>${esc(x.name)}</strong></td><td>${x.total} h</td><td>${esc(x.status)}</td></tr>`).join('')}
          </tbody>
        </table>
      </aside>
    </div>
  </section>`;
}

/* === HOURS UI PLUS END === */



.hours-summary-box{
  margin-top:18px;
  border:1px solid rgba(125,211,252,.25);
  background:linear-gradient(135deg,rgba(15,23,42,.92),rgba(30,41,59,.92));
  border-radius:18px;
  padding:18px;
  box-shadow:0 10px 30px rgba(0,0,0,.22)
}
.hours-summary-title{
  font-size:15px;
  color:#cbd5e1;
  margin-bottom:10px;
  font-weight:800
}
.hours-summary-grid{
  display:grid;
  grid-template-columns:repeat(auto-fit,minmax(110px,1fr));
  gap:10px
}
.hours-summary-pill{
  border-radius:14px;
  padding:12px;
  background:rgba(255,255,255,.05);
  border:1px solid rgba(148,163,184,.16)
}
.hours-summary-pill-label{
  font-size:12px;
  color:#94a3b8;
  margin-bottom:6px
}
.hours-summary-pill-value{
  font-size:24px;
  font-weight:900;
  color:#e2e8f0
}
.hours-summary-total{
  margin-top:14px;
  padding-top:14px;
  border-top:1px solid rgba(148,163,184,.18);
  display:flex;
  justify-content:space-between;
  align-items:center;
  gap:12px
}
.hours-summary-total-label{
  color:#cbd5e1;
  font-size:14px;
  font-weight:800
}
.hours-summary-total-value{
  font-size:34px;
  font-weight:1000;
  color:#86efac
}
