/* =========================================================
   TOKENS
   ========================================================= */
:root {
  --bg:          #0d1117;
  --bg-card:     #161b22;
  --bg-hover:    #1c2130;
  --bg-input:    #0d1117;
  --border:      #30363d;
  --border-focus:#388bfd;
  --text:        #e6edf3;
  --text-muted:  #7d8590;
  --text-dim:    #484f58;
  --accent:      #58a6ff;
  --accent-dim:  #1f4e8f;
  --green:       #3fb950;
  --green-bg:    #0d2b14;
  --red:         #f85149;
  --red-bg:      #2d0f0f;
  --yellow:      #d29922;
  --yellow-bg:   #2b1f06;
  --radius:      6px;
  --shadow:      0 1px 3px rgba(0,0,0,.4);
  --font:        -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono:   'SFMono-Regular', Consolas, 'Liberation Mono', monospace;
}

/* =========================================================
   RESET / BASE
   ========================================================= */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 14px; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select, textarea { font: inherit; }
:focus-visible { outline: 2px solid var(--border-focus); outline-offset: 2px; }

/* =========================================================
   LAYOUT
   ========================================================= */
#app { display: flex; min-height: 100vh; }

/* Sidebar */
#sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 0;
}
#sidebar-logo {
  padding: 18px 20px 16px;
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text);
  border-bottom: 1px solid var(--border);
  letter-spacing: -.3px;
  display: flex; align-items: center; justify-content: center; gap: 8px;
}
#sidebar-logo span { color: var(--accent); }
#sidebar nav { flex: 1; padding: 8px 0; display: flex; flex-direction: column; }
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 20px;
  color: var(--text-muted);
  cursor: pointer;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  font-size: .9rem;
  transition: background .1s, color .1s;
  border-radius: 0;
}
.nav-item:hover { background: var(--bg-hover); color: var(--text); }
.nav-item.active { background: var(--accent-dim); color: var(--text); }
.nav-item .icon { font-size: 1rem; width: 18px; text-align: center; }
.nav-badge { margin-left: auto; background: var(--accent); color: #fff; border-radius: 10px; font-size: .68rem; font-weight: 700; padding: 1px 7px; }
/* Main */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
#topbar {
  height: 52px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 24px;
  gap: 12px;
  background: var(--bg);
}
#topbar-title { font-size: 1rem; font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#content { flex: 1; padding: 24px; overflow-y: auto; }

/* =========================================================
   LOGIN
   ========================================================= */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--bg);
}
.login-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 40px 36px;
  width: 340px;
}
.auth-tabs { display:flex; gap:4px; margin-bottom:18px; border-bottom:1px solid var(--border); padding-bottom:10px; }
.auth-tab { flex:1; background:none; border:none; color:var(--text-muted); padding:6px 0; cursor:pointer; font-size:.88rem; border-radius:4px; transition:color .15s; }
.auth-tab:hover { color:var(--text); }
.auth-tab.active { color:var(--accent); font-weight:600; }
.login-card h1 {
  font-size: 1.4rem;
  font-weight: 700;
  margin-bottom: 6px;
  color: var(--text);
}
.login-card h1 span { color: var(--accent); }
.login-card p {
  color: var(--text-muted);
  font-size: .85rem;
  margin-bottom: 28px;
}

/* =========================================================
   FORMS
   ========================================================= */
.form-group { margin-bottom: 14px; }
.form-group label {
  display: block;
  font-size: .8rem;
  color: var(--text-muted);
  margin-bottom: 5px;
  font-weight: 500;
}
.form-control {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius);
  padding: 7px 10px;
  font-size: .9rem;
  transition: border-color .15s;
}
.form-control:focus { border-color: var(--border-focus); }
.form-control::placeholder { color: var(--text-dim); }
.form-control:disabled { opacity: .55; cursor: not-allowed; background: var(--bg-card); }
select.form-control { cursor: pointer; }
textarea.form-control { resize: vertical; min-height: 70px; }

/* =========================================================
   BUTTONS
   ========================================================= */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: var(--radius);
  border: none;
  cursor: pointer;
  font-size: .85rem;
  font-weight: 500;
  transition: opacity .15s, background .15s;
  white-space: nowrap;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary   { background: var(--accent);   color: #fff; }
.btn-primary:hover:not(:disabled)   { background: #4396ff; }
.btn-success   { background: var(--green);    color: #0d2b14; }
.btn-success:hover:not(:disabled)   { background: #56d364; }
.btn-danger    { background: var(--red);      color: #fff; }
.btn-danger:hover:not(:disabled)    { background: #ff6b6b; }
.btn-secondary { background: var(--bg-hover); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover:not(:disabled) { background: var(--border); }
.btn-ghost     { background: transparent; color: var(--text-muted); border: 1px solid var(--border); }
.btn-ghost:hover:not(:disabled)     { color: var(--text); border-color: var(--text-muted); }
.btn-sm { padding: 4px 10px; font-size: .78rem; }
.btn-icon { padding: 5px 8px; }

/* =========================================================
   CARDS
   ========================================================= */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 20px;
}
.card-title {
  font-size: .95rem;
  font-weight: 600;
  margin-bottom: 16px;
  color: var(--text);
}

/* =========================================================
   TABLE
   ========================================================= */
.table-wrap { overflow-x: auto; }
table.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .85rem;
}
table.data-table th {
  text-align: left;
  padding: 8px 12px;
  border-bottom: 2px solid var(--border);
  color: var(--text-muted);
  font-weight: 600;
  white-space: nowrap;
  background: var(--bg);
}
table.data-table td {
  padding: 9px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
table.data-table tbody tr:hover { background: var(--bg-hover); }
table.data-table .cell-num {
  font-family: var(--font-mono);
  font-size: .8rem;
  color: var(--accent);
  font-weight: 600;
}
table.data-table .cell-num:hover {
  text-decoration: underline;
}

table.data-table .cell-amount { text-align: right; font-family: var(--font-mono); }

/* =========================================================
   BADGES
   ========================================================= */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 20px;
  font-size: .75rem;
  font-weight: 600;
  white-space: nowrap;
}
.badge-green  { background: var(--green-bg);  color: var(--green);  }
.badge-red    { background: var(--red-bg);    color: var(--red);    }
.badge-yellow { background: var(--yellow-bg); color: var(--yellow); }
.badge-blue   { background: var(--accent-dim); color: var(--accent); }
.badge-gray   { background: #21262d; color: var(--text-muted); }

/* =========================================================
   FILTERS BAR
   ========================================================= */
.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-end;
  margin-bottom: 18px;
}
.filters .form-group { margin-bottom: 0; }
.filters label { font-size: .75rem; }
.filters .form-control { font-size: .82rem; padding: 6px 8px; }
.filters select.form-control { min-width: 120px; }
.filter-search { min-width: 200px; }

/* =========================================================
   PAGINATION
   ========================================================= */
.pagination {
  display: flex;
  gap: 6px;
  align-items: center;
  margin-top: 16px;
  justify-content: flex-end;
}
.pagination button {
  padding: 4px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-muted);
  cursor: pointer;
  font-size: .82rem;
}
.pagination button.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.pagination button:hover:not(.active):not(:disabled) { color: var(--text); }
.pagination button:disabled { opacity: .3; }
.pagination .pages-info { color: var(--text-dim); font-size: .8rem; }

/* =========================================================
   MODAL
   ========================================================= */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.65);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 28px;
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 8px 32px rgba(0,0,0,.6);
}
.modal h2 { font-size: 1.05rem; font-weight: 700; margin-bottom: 20px; }
.modal-footer { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }

/* =========================================================
   DETAIL PANEL (invoice)
   ========================================================= */
.detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px 24px;
  font-size: .85rem;
}
.detail-row { display: flex; flex-direction: column; gap: 2px; }
.detail-label { color: var(--text-muted); font-size: .75rem; font-weight: 500; }
.detail-value { color: var(--text); word-break: break-all; }
.detail-value.mono { font-family: var(--font-mono); font-size: .82rem; }
.detail-full { grid-column: 1 / -1; }

/* =========================================================
   TOAST
   ========================================================= */
#toasts {
  position: fixed;
  bottom: 20px; right: 20px;
  display: flex; flex-direction: column; gap: 8px;
  z-index: 1000;
}
.toast {
  padding: 10px 18px;
  border-radius: var(--radius);
  font-size: .85rem;
  font-weight: 500;
  box-shadow: var(--shadow);
  animation: slideUp .2s ease;
  max-width: 320px;
}
.toast-success { background: var(--green-bg); color: var(--green); border: 1px solid var(--green); }
.toast-error   { background: var(--red-bg);   color: var(--red);   border: 1px solid var(--red); }
.toast-info    { background: var(--accent-dim); color: var(--accent); border: 1px solid var(--accent); }
@keyframes slideUp { from { opacity:0; transform:translateY(10px); } to { opacity:1; transform:translateY(0); } }

/* =========================================================
   SYNC LOG
   ========================================================= */
.sync-result {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: .82rem;
}
.sync-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px;
  border-radius: var(--radius);
  background: var(--bg-hover);
}
.step-ok   { color: var(--green); }
.step-warn { color: var(--yellow); }
.step-err  { color: var(--red); }

/* =========================================================
   STATS ROW
   ========================================================= */
.stats-row {
  display: flex;
  gap: 16px;
  margin-bottom: 22px;
  flex-wrap: wrap;
}
.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 20px;
  min-width: 150px;
  flex: 1;
}
.stat-card .stat-value {
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--text);
  line-height: 1.2;
}
.stat-card .stat-label {
  font-size: .75rem;
  color: var(--text-muted);
  margin-top: 2px;
}
.stat-card.accent .stat-value { color: var(--accent); }
.stat-card.green  .stat-value { color: var(--green); }
.stat-card.red    .stat-value { color: var(--red); }

/* =========================================================
   INLINE EDIT
   ========================================================= */
.inline-note {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
}
.inline-note input {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: .82rem;
  width: 100%;
  padding: 2px 4px;
  border-radius: 3px;
  cursor: pointer;
}
.inline-note input:focus {
  background: var(--bg-input);
  border: 1px solid var(--border-focus);
  color: var(--text);
  cursor: text;
  outline: none;
}
.inline-note input::placeholder { color: var(--text-dim); }

/* =========================================================
   CHECKBOX TOGGLE (is_paid)
   ========================================================= */
.toggle-paid {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}
.toggle-paid input[type=checkbox] {
  width: 16px; height: 16px;
  accent-color: var(--green);
  cursor: pointer;
}

/* =========================================================
   LOADING
   ========================================================= */
.spinner {
  width: 16px; height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .6s linear infinite;
  display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-overlay {
  display: flex; align-items: center; justify-content: center;
  padding: 40px;
  color: var(--text-muted);
  gap: 10px;
  font-size: .9rem;
}

/* =========================================================
   EMPTY STATE
   ========================================================= */
.empty-state {
  text-align: center;
  padding: 56px 20px;
  color: var(--text-muted);
}
.empty-state .empty-icon { font-size: 2.5rem; margin-bottom: 14px; }
.empty-state p { font-size: .9rem; }

/* =========================================================
   SCROLLBAR
   ========================================================= */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }

/* =========================================================
   RESPONSIVE
   ========================================================= */
/* =========================================================
   SIDEBAR TOGGLE
   ========================================================= */
#btn-sidebar-toggle {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius);
  font-size: 1.2rem;
  line-height: 1;
  display: flex;
  align-items: center;
  transition: color .15s, background .15s;
}
#btn-sidebar-toggle:hover { color: var(--text); background: var(--bg-hover); }

#sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  z-index: 99;
}
#sidebar-overlay.visible { display: block; }

#sidebar {
  transition: transform .22s ease, width .22s ease;
}
#sidebar.collapsed {
  width: 0;
  overflow: hidden;
  border-right: none;
}

@media (max-width: 768px) {
  #sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    z-index: 100;
    transform: translateX(-100%);
    width: 220px !important;
    overflow: hidden;
  }
  #sidebar.open {
    transform: translateX(0);
  }
  .detail-grid { grid-template-columns: 1fr; }
  .stats-row { gap: 10px; }
}

/* =========================================================
   SIDEBAR (detail panel po prawej)
   ========================================================= */
#sidebar-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: 480px;
  max-width: 100vw;
  height: 100vh;
  background: var(--bg-card);
  border-left: 1px solid var(--border);
  box-shadow: -4px 0 24px rgba(0,0,0,.35);
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform .25s cubic-bezier(.4,0,.2,1);
  z-index: 400;
  overflow: hidden;
}
#sidebar-panel.open {
  transform: translateX(0);
}
#detail-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 399;
}
#detail-overlay.open {
  display: block;
}
.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.sidebar-header h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
}
.sidebar-close {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 1.3rem;
  line-height: 1;
  padding: 4px 8px;
  border-radius: 4px;
}
.sidebar-close:hover { background: var(--bg-hover); color: var(--text); }
.sidebar-body {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}
.sidebar-footer {
  display: flex;
  gap: 8px;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
@media (max-width: 540px) {
  #sidebar-panel { width: 100vw; }
}

/* =========================================================
   BANNERY SUBSKRYPCJI
   ========================================================= */
.sub-banner {
  width: 100%;
  padding: 10px 20px;
  font-size: .875rem;
  font-weight: 500;
  text-align: center;
  flex-shrink: 0;
  z-index: 10;
}
.sub-banner--expired {
  background: rgba(239,68,68,.15);
  color: #f87171;
  border-bottom: 1px solid rgba(239,68,68,.3);
}
.sub-banner--warning {
  background: rgba(245,158,11,.12);
  color: #fbbf24;
  border-bottom: 1px solid rgba(245,158,11,.25);
}

/* Chip kontekstu firmy obsługiwanej (topbar) */
#company-selector { margin-right: 10px; flex-shrink: 1; min-width: 0; }
.ctx-chip { display: flex; align-items: center; gap: 6px; max-width: 260px; padding: 4px 6px 4px 10px;
  border: 1px solid var(--accent); border-radius: 999px; font-size: .8rem; font-weight: 600; color: var(--accent); }
.ctx-chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ctx-chip-back { background: none; border: none; color: var(--text-muted); cursor: pointer; padding: 0 4px; font-size: .85rem; }
.ctx-chip-back:hover { color: var(--danger, #f87171); }
@media (max-width: 540px) { .ctx-chip { max-width: 140px; } }

/* =========================================================
   MODUŁ ZAMÓWIEŃ (Kontrahenci / Produkty / Zamówienia)
   ========================================================= */

.btn-xs {
  padding: 3px 8px;
  font-size: .76rem;
  border-radius: 4px;
}

.cell-actions {
  white-space: nowrap;
  text-align: right;
}
.cell-actions .btn { margin-left: 4px; }

.subtabs {
  display: flex;
  gap: 4px;
  margin-bottom: 14px;
  border-bottom: 1px solid var(--border);
}
.subtab {
  background: none;
  border: none;
  color: var(--text-muted);
  padding: 8px 14px;
  cursor: pointer;
  font-size: .86rem;
  border-bottom: 2px solid transparent;
  transition: color .15s, border-color .15s;
}
.subtab:hover { color: var(--text); }
.subtab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

/* =========================================================
   POZYCJE UMOWY — karta na pozycję + wyszukiwarka produktu
   (ten sam wzorzec co selektor firmy w topbarze, przeskalowany
   do szerokości formularza zamiast stałej szerokości topbara)
   ========================================================= */
.citem-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  margin-bottom: 10px;
  background: var(--bg);
}
.citem-row { margin-bottom: 8px; }
.citem-row:last-child { margin-bottom: 0; }
.citem-row-top { display: flex; gap: 8px; align-items: center; }
.citem-row-top .psel-wrap { flex: 1; min-width: 0; }
.citem-remove { flex-shrink: 0; }
.citem-row-numbers { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
.citem-field { min-width: 0; }
.citem-field input { min-width: 0; width: 100%; }
.citem-field label { display: block; font-size: .72rem; color: var(--text-muted); margin-bottom: 2px; }

.psel-wrap {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: border-color .15s;
  height: 34px;
}
.psel-wrap:focus-within { border-color: var(--accent); }
.psel-icon { font-size: .85rem; flex-shrink: 0; }
.psel-input {
  background: none;
  border: none;
  outline: none;
  color: var(--text);
  font-size: .82rem;
  width: 100%;
  padding: 0 !important;
}
.psel-input::placeholder { color: var(--text-muted); font-weight: 500; }
.psel-dropdown {
  display: none;
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0,0,0,.35);
  z-index: 500;
  padding: 4px 0;
  max-height: 240px;
  overflow-y: auto;
}
.psel-dropdown.open { display: block; }
.psel-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  text-align: left;
  padding: 8px 14px;
  background: none;
  border: none;
  color: var(--text);
  font-size: .85rem;
  cursor: pointer;
}
.psel-item:hover     { background: var(--bg-hover); }
.psel-item-name      { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.psel-item-price     { font-size: .75rem; color: var(--text-muted); flex-shrink: 0; }
.psel-empty          { padding: 10px 14px; color: var(--text-muted); font-size: .85rem; }

@media (max-width: 380px) {
  .citem-row-numbers { grid-template-columns: 1fr; }
}

/* Wiersze list otwierające ekran pozycji */
.data-table tbody tr.clickable { cursor: pointer; }
.data-table tbody tr.clickable:hover { background: rgba(127,127,127,.08); }


/* =========================================================
   TOPBAR: zespół, ustawienia, profil (od lewej; profil skrajnie po prawej)
   ========================================================= */
#topbar-actions { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
#topbar-tools { display: flex; align-items: center; gap: 2px; flex-shrink: 0; margin-left: 4px; }
.tool-btn {
  position: relative;
  width: 36px; height: 36px;
  display: inline-flex; align-items: center; justify-content: center;
  background: none; border: none; border-radius: var(--radius);
  color: var(--text-muted); cursor: pointer;
  transition: color .15s, background .15s;
}
.tool-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.tool-btn:hover { color: var(--text); background: var(--bg-hover); }
.tool-btn.active { color: var(--accent); background: var(--accent-dim); }
.tool-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.tool-avatar { margin-left: 4px; }
.tool-avatar span {
  width: 28px; height: 28px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--accent-dim); color: var(--text);
  font-size: .8rem; font-weight: 700; text-transform: uppercase;
}
.tool-avatar:hover span, .tool-avatar[aria-expanded="true"] span { background: var(--accent); color: #fff; }

#user-menu-wrap { position: relative; }
#user-menu {
  position: absolute; right: 0; top: calc(100% + 8px); z-index: 300;
  min-width: 220px; max-width: calc(100vw - 24px);
  background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0,0,0,.45); padding: 6px 0;
}
#user-menu[hidden] { display: none; }
.um-head { display: flex; flex-direction: column; gap: 2px; padding: 8px 14px 10px; border-bottom: 1px solid var(--border); margin-bottom: 4px; }
.um-email { color: var(--text); font-size: .85rem; word-break: break-all; }
.um-role { color: var(--text-muted); font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; }
.um-item {
  display: block; width: 100%; text-align: left; padding: 8px 14px;
  background: none; border: none; color: var(--text-muted); font-size: .88rem; cursor: pointer;
}
.um-item:hover { background: var(--bg-hover); color: var(--text); }
.um-logout:hover { color: var(--red); background: var(--red-bg); }

@media (max-width: 768px) {
  #topbar { padding: 0 10px; gap: 6px; }
}

.pt-color { width: 64px; height: 36px; padding: 2px; border: 1px solid var(--border); border-radius: var(--radius); background: none; cursor: pointer; }
.portal-act-title { margin: 22px 0 8px; font-size: .95rem; }

/* ===== Ekran pozycji: treść zakładki + panel boczny z najnowszymi informacjami (public/entity.js) ===== */
.ent-layout { display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; align-items: start; }
.ent-layout.has-aside { grid-template-columns: minmax(0, 1fr) 320px; }
.ent-main { min-width: 0; }
.ent-aside { display: flex; flex-direction: column; gap: 12px; position: sticky; top: 12px; max-height: calc(100vh - 24px); overflow-y: auto; }
.aside-card { background: var(--bg-card); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; }
.aside-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 4px; }
.aside-title { font-weight: 600; font-size: .88rem; }
.aside-more { font-size: .76rem; color: var(--text-muted); text-decoration: none; white-space: nowrap; }
.aside-more:hover { color: var(--text); text-decoration: underline; }
.aside-row { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; padding: 7px 6px; margin: 0 -6px; border-top: 1px solid var(--border); border-radius: 6px; cursor: pointer; }
.aside-row:hover { background: var(--bg-hover); }
.aside-main { min-width: 0; flex: 1; }
.aside-row-title { font-size: .84rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.aside-row-meta { font-size: .74rem; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.aside-badge { flex: none; }
.aside-empty { font-size: .8rem; color: var(--text-muted); padding: 6px 0 2px; border-top: 1px solid var(--border); }
@media (max-width: 1100px) {
  .ent-layout.has-aside { grid-template-columns: minmax(0, 1fr); }
  .ent-aside { position: static; max-height: none; }
}

.info-note { margin: 0 0 14px; padding: 10px 12px; border: 1px solid var(--accent); border-left-width: 4px; border-radius: var(--radius); background: var(--bg); font-size: .84rem; }
.info-note p { margin: 4px 0 0; color: var(--text-muted); line-height: 1.45; }

/* Komunikaty portalu (0.9.8.09) */
.pm-chip { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: .75rem; font-weight: 600; white-space: nowrap; }
.pm-field { display: inline-block; padding: 1px 7px; border-radius: 6px; background: var(--bg); border: 1px solid var(--border); font-family: var(--font-mono, monospace); font-size: .78rem; }
.pm-steps { margin: 6px 0 0; padding-left: 20px; line-height: 1.5; color: var(--text-muted); } .pm-steps code, .info-note code { font-family: var(--font-mono, monospace); color: var(--accent); }
.pm-inserts { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; margin-bottom: 6px; font-size: .8rem; color: var(--text-muted); }
.pm-insert { cursor: pointer; padding: 1px 7px; border: 1px dashed var(--border); border-radius: 6px; background: none; color: var(--accent); font-family: var(--font-mono, monospace); font-size: .78rem; }
.pm-insert:hover { background: var(--bg-hover); }
.pm-detected { margin: 0 0 12px; padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius); font-size: .84rem; } .pm-detected ul { margin: 6px 0 0; padding-left: 18px; } .pm-detected li { margin: 3px 0; }
.pm-preview { padding: 12px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg); line-height: 1.5; max-height: 220px; overflow: auto; }
.pm-missing { margin-top: 8px; color: var(--warning, #f59e0b); font-size: .82rem; }
.pm-fieldgroup { margin-top: 18px; padding-top: 12px; border-top: 1px solid var(--border); } .pm-fieldgroup-title { font-weight: 600; margin-bottom: 2px; }
.pm-sub { font-size: .72rem; color: var(--text-muted); } .pm-meta { margin-top: 12px; font-size: .84rem; color: var(--text-muted); } .pm-meta div { margin: 3px 0; }

/* Podpowiedzi etykiety szablonu komunikatu (Ustawienia -> Komunikaty portalu) */
.pm-sugg { position: absolute; left: 0; right: 0; top: 100%; margin-top: 2px; z-index: 30; padding: 4px; max-height: 280px; overflow-y: auto;
  background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: 0 6px 18px rgba(0, 0, 0, .18); }
.pm-sugg[hidden] { display: none; }
.pm-sugg-item { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 5px 8px; border-radius: 6px; cursor: pointer; }
.pm-sugg-item:hover, .pm-sugg-item.active { background: var(--bg-hover); }
.pm-sugg-count { font-size: .72rem; color: var(--text-muted); }

/* Umowy: przełącznik Rejestr / Do obsługi, chipy kroków */
.view-switch { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; margin-bottom: 12px; }
.view-switch button { background: none; border: 0; padding: 7px 14px; cursor: pointer; color: var(--text); font: inherit; }
.view-switch button.active { background: var(--accent); color: #fff; }
.step-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.step-chip { border: 1px solid var(--border); background: none; color: var(--text); border-radius: 999px; padding: 3px 12px; font: inherit; font-size: .82rem; cursor: pointer; }
.step-chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
/* Dashboard */
.dash-layout { display: grid; grid-template-columns: minmax(0, 960px) minmax(0, 1fr) 260px; gap: 16px; align-items: start; }
.dash-layout > .dash-shortcuts { grid-column: 3; position: sticky; top: 12px; }
@media (max-width: 1000px) { .dash-layout { grid-template-columns: minmax(0, 1fr); } .dash-layout > .dash-shortcuts { grid-column: 1; order: -1; position: static; } }
.dash-shortcuts { padding: 12px 14px; }
.dash-sc-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.dash-shortcuts h3 { margin: 0; font-size: .9rem; text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); }
.dash-sc-btns { display: flex; flex-direction: column; gap: 8px; }
.dash-sc-btns .btn { justify-content: flex-start; text-align: left; }
.dash-sc-empty { color: var(--text-muted); font-size: .85rem; }
.dash-boxes { display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; }
.dash-box { padding: 0; overflow: hidden; }
.dash-box-head { display: flex; align-items: baseline; gap: 12px; padding: 12px 16px; cursor: pointer; border-bottom: 1px solid var(--border); }
.dash-box-head:hover .dash-title { color: var(--accent); }
.dash-count { font-size: 1.5rem; font-weight: 700; color: var(--accent); line-height: 1; min-width: 1.5ch; }
.dash-count.zero { color: var(--text-muted); }
.dash-title { font-weight: 600; }
.dash-more { margin-left: auto; font-size: .78rem; color: var(--text-muted); }
.dash-row { display: grid; grid-template-columns: 96px minmax(120px, 220px) minmax(0, 1fr) auto; gap: 12px; align-items: baseline; padding: 8px 16px; border-bottom: 1px solid var(--border); cursor: pointer; font-size: .88rem; }
.dash-row:last-child { border-bottom: 0; }
.dash-row:hover, .dash-row:focus { background: var(--bg-hover, rgba(127,127,127,.08)); outline: none; }
.dash-date { color: var(--text-muted); white-space: nowrap; font-size: .8rem; }
.dash-who { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dash-what { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dash-extra { text-align: right; white-space: nowrap; color: var(--text-muted); }
.dash-empty { margin: 0; padding: 12px 16px; color: var(--text-muted); font-size: .85rem; }
.dash-rest { padding: 6px 16px 10px; font-size: .8rem; color: var(--text-muted); cursor: pointer; }
@media (max-width: 700px) { .dash-row { grid-template-columns: 1fr auto; } .dash-date { grid-column: 1; } .dash-who { grid-column: 1; } .dash-what { grid-column: 1 / -1; white-space: normal; } .dash-extra { grid-row: 1; grid-column: 2; } }
.stat-card.yellow .stat-value { color: var(--yellow, #b7791f); }
.pick-row { padding: 7px 10px; border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 3px; cursor: pointer; }
.pick-row:hover { border-color: var(--accent); }

/* Wątek zgłoszenia: maile, odpowiedzi (widoczne w portalu), wpisy klienta i notatki wewnętrzne */
.tk-thread{display:flex;flex-direction:column;gap:10px;max-width:860px}
.tk-item{border:1px solid var(--border);border-radius:var(--radius);padding:10px 14px;background:var(--bg-card, #fff);border-left:4px solid var(--border)}
.tk-item.tk-in{border-left-color:#64748b}
.tk-item.tk-out{border-left-color:#2563eb;background:rgba(37,99,235,.05)}
.tk-item.tk-internal{border-left-color:#d97706;background:rgba(217,119,6,.08);border-style:dashed;border-left-style:solid}
.tk-item.tk-mail{cursor:pointer}
.tk-head{display:flex;gap:10px;align-items:baseline;flex-wrap:wrap;font-size:.8rem;color:var(--text-muted);margin-bottom:4px}
.tk-who{font-weight:700;color:var(--text)}
.tk-when{margin-left:auto;white-space:nowrap}
.tk-subject{font-weight:600;margin-bottom:2px}
.tk-body{white-space:pre-wrap;overflow-wrap:anywhere}
.tk-clip{color:var(--text-muted);font-size:.88rem;max-height:7.5em;overflow:hidden}
.tk-meta{margin-top:6px;font-size:.75rem;color:var(--text-muted)}
.tk-files{margin-top:8px;display:flex;flex-wrap:wrap;gap:6px 14px;font-size:.85rem}
.email-conflict{text-decoration:none}.flash{animation:flashbg 2s ease-out}@keyframes flashbg{from{background:rgba(224,168,58,.3)}to{background:transparent}}
