:root {
  --bg: #1B1D21;
  --panel: #17181B;
  --field: #2C2F35;
  --border: #383B42;
  --text: #EDEBE6;
  --text-dim: #9B9A94;
  --text-faint: #6E6D68;
  --accent: #D98E3F;
  --accent-dark: #3A2712;
  --accent-text: #F3C88F;
  --success: #5FA776;
  --success-bg: #20301F;
  --danger: #D2645A;
  --danger-bg: #33201D;
  --progress: #5B8FC4;
  --progress-bg: #1E2A36;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 14px;
}

#app { display: flex; height: 100vh; overflow: hidden; }

/* sidebar */
.sidebar {
  width: 196px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 20px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex-shrink: 0;
}
.sidebar-title { padding: 0 10px 12px; font-size: 15px; font-weight: 600; }
.sidebar-status { padding: 0 10px 16px; font-size: 11.5px; color: var(--text-faint); }
.sidebar-status.online { color: var(--success); }
.sidebar-status.offline { color: var(--danger); }
.nav-btn {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: 6px; border: none;
  background: transparent; color: var(--text-dim);
  font-size: 13.5px; cursor: pointer; text-align: left;
}
.nav-btn.active { background: var(--field); color: var(--text); font-weight: 500; }
.nav-btn svg { flex-shrink: 0; }

/* main columns */
.main { flex: 1; display: flex; min-width: 0; }
.list-col { width: 360px; border-right: 1px solid var(--border); display: flex; flex-direction: column; flex-shrink: 0; }
.detail-col { flex: 1; min-width: 0; display: flex; flex-direction: column; overflow-y: auto; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
}
.topbar-title { font-size: 15px; font-weight: 500; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }

.search-wrap { position: relative; }
.search-wrap input {
  background: var(--field); border: 1px solid var(--border); border-radius: 6px;
  color: var(--text); font-size: 13px; padding: 6px 10px 6px 28px; width: 150px; outline: none;
}
.search-wrap svg { position: absolute; left: 8px; top: 8px; color: var(--text-faint); }

.btn {
  border: 1px solid var(--border); background: transparent; color: var(--text);
  border-radius: 6px; padding: 7px 12px; font-size: 13px; cursor: pointer;
}
.btn-primary { background: var(--accent); color: var(--accent-dark); border: none; font-weight: 500; }
.btn-danger-outline { border-color: var(--border); color: var(--danger); }
.btn-icon { display: inline-flex; align-items: center; gap: 6px; }

.list { flex: 1; overflow-y: auto; }
.list-row { padding: 12px 20px; border-bottom: 1px solid var(--border); cursor: pointer; }
.list-row:hover { background: rgba(255,255,255,0.02); }
.list-row.active { background: var(--field); }
.list-row-top { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 4px; }
.list-row-title { font-size: 13.5px; margin-bottom: 2px; }
.list-row-sub { font-size: 12px; color: var(--text-dim); }
.mono { font-family: ui-monospace, "SFMono-Regular", Menlo, monospace; }

.badge { font-size: 11px; padding: 2px 8px; border-radius: 20px; font-weight: 500; }
.badge-nowe { background: var(--danger-bg); color: var(--danger); }
.badge-w_trakcie { background: var(--progress-bg); color: var(--progress); }
.badge-zakonczone { background: var(--success-bg); color: var(--success); }

.status-pill {
  font-size: 12px; padding: 5px 12px; border-radius: 20px; cursor: pointer; font-weight: 500;
  border: 1px solid var(--border); background: transparent; color: var(--text-dim);
}
.status-pill.active-nowe { border-color: var(--danger); background: var(--danger-bg); color: var(--danger); }
.status-pill.active-w_trakcie { border-color: var(--progress); background: var(--progress-bg); color: var(--progress); }
.status-pill.active-zakonczone { border-color: var(--success); background: var(--success-bg); color: var(--success); }

.empty-state { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--text-faint); font-size: 13px; }

.form-wrap { padding: 20px 24px; }
.form-header { display: flex; align-items: center; gap: 8px; margin-bottom: 18px; }
.back-btn { background: none; border: none; color: var(--text-dim); cursor: pointer; display: flex; }
.form-title { font-size: 15px; font-weight: 500; }

.field { margin-bottom: 14px; }
.field-label { font-size: 12px; color: var(--text-dim); margin-bottom: 6px; }
.field input, .field select, .field textarea {
  width: 100%; background: var(--field); border: 1px solid var(--border); border-radius: 6px;
  color: var(--text); font-size: 13.5px; padding: 8px 10px; outline: none; font-family: inherit;
}
.field textarea { min-height: 60px; resize: vertical; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.counter-row { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
.counter-label { font-size: 11px; color: var(--text-faint); margin-bottom: 4px; }
.link-btn { background: none; border: none; color: var(--accent-text); font-size: 12px; cursor: pointer; padding: 4px 0; }
.link-btn-dim { background: none; border: none; color: var(--text-dim); font-size: 12px; cursor: pointer; padding: 4px 0; }

.item-row { display: flex; gap: 8px; margin-bottom: 8px; }
.item-row input:first-child { flex: 1; }
.item-row input:nth-child(2) { width: 110px; }
.item-remove { background: none; border: none; color: var(--text-faint); cursor: pointer; padding: 0 4px; }

.totals-box { background: var(--field); border-radius: 8px; padding: 12px 14px; margin-bottom: 16px; font-size: 13px; }
.totals-line { display: flex; justify-content: space-between; margin-bottom: 4px; }
.totals-line.final { font-weight: 600; font-size: 14px; }
.totals-line.final span:last-child { color: var(--accent-text); }

.error-text { color: var(--danger); font-size: 12.5px; margin-bottom: 12px; }
.form-actions { display: flex; gap: 10px; }

.detail-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 18px; }
.detail-title { font-size: 17px; font-weight: 500; }
.detail-sub { font-size: 12.5px; color: var(--text-dim); margin-top: 2px; }
.detail-actions { display: flex; gap: 8px; }
.status-row { display: flex; gap: 8px; margin-bottom: 20px; }
.section { margin-bottom: 18px; }
.section-label { font-size: 12px; color: var(--text-dim); margin-bottom: 8px; display: flex; align-items: center; gap: 6px; }
.meta-row { display: flex; gap: 20px; margin-bottom: 22px; font-size: 13px; color: var(--text-dim); flex-wrap: wrap; }

.items-table { border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.items-table-row { display: flex; justify-content: space-between; padding: 9px 14px; font-size: 13px; border-bottom: 1px solid var(--border); }
.items-table-row:last-child { border-bottom: none; }
.items-total-row { display: flex; justify-content: flex-end; gap: 20px; margin-top: 10px; font-size: 13px; }

.history-row { padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.history-row:last-child { border-bottom: none; }
.history-top { display: flex; justify-content: space-between; margin-bottom: 3px; }
.history-meta { font-size: 12px; color: var(--text-faint); }

.overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center; z-index: 50;
}
.overlay-box {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 24px; width: 320px;
}
.overlay-box h3 { margin: 0 0 12px; font-size: 15px; font-weight: 500; }
.overlay-box p { margin: 0 0 14px; font-size: 12.5px; color: var(--text-dim); }


/* MARTON: import CSV, kartoteka towarów, e-mail */
.import-bar { padding: 8px 12px; border-bottom: 1px solid var(--border); }
.item-row { align-items: center; flex-wrap: wrap; }
.item-row .item-product { width: 100%; background: var(--field); border: 1px solid var(--border); border-radius: 6px; color: var(--text); font-size: 12.5px; padding: 7px 9px; }
.email-box { width: min(460px, calc(100vw - 32px)); }
#email-send:disabled { opacity: .65; cursor: wait; }

/* MARTON v3.1: bezpieczne wyszukiwanie klienta w zleceniu */
#customer-picker { display:flex; flex-direction:column; gap:6px; }
#f-customer-filter { min-width: 360px; }
#f-customer { min-height: 190px; }
@media (max-width: 900px) { #f-customer-filter { min-width: 0; } }


/* v3.2 - kompaktowe wyszukiwanie klienta */
.search-picker { position: relative; }
.picker-results {
  position: absolute; z-index: 30; left: 0; right: 0; top: 66px; max-height: 320px; overflow-y: auto;
  background: var(--field); border: 1px solid var(--border); border-radius: 7px; box-shadow: 0 10px 28px rgba(0,0,0,.35);
}
.picker-result {
  display: block; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--border);
  background: transparent; color: var(--text); padding: 9px 11px; cursor: pointer; font-family: inherit;
}
.picker-result:last-child { border-bottom: 0; }
.picker-result:hover { background: rgba(255,255,255,.05); }
.picker-result-name { display: block; font-size: 13px; white-space: normal; overflow-wrap: anywhere; }
.picker-result-meta { display: block; margin-top: 3px; color: var(--text-dim); font-size: 11.5px; }
.picker-empty { padding: 10px 11px; color: var(--text-faint); font-size: 12px; }
#f-device:disabled { opacity: .7; cursor: not-allowed; }

/* v4: wiele urządzeń w jednym zleceniu */
.order-device-card { border:1px solid var(--border); border-radius:8px; padding:12px; margin:8px 0; background:var(--panel); }
.order-device-head { display:flex; gap:8px; align-items:center; margin-bottom:8px; }
.order-device-head select { flex:1; }
.order-device-card textarea { min-height:64px; margin-bottom:8px; }
.order-device-card .od-notes { margin-top:8px; }
.order-device-detail { padding:8px 0; border-bottom:1px solid var(--border); font-size:13.5px; line-height:1.5; }
.order-device-detail:last-child { border-bottom:0; }

/* MARTON v4.3: wyszukiwanie towarów i ilości */
.item-row .product-picker { flex: 1 1 100%; }
.item-row .item-product-search { width: 100%; box-sizing: border-box; }
.item-row .item-name { flex: 1 1 260px; width: auto; }
.item-row .item-qty { width: 90px !important; flex: 0 0 90px; }
.item-row .item-price { width: 130px !important; flex: 0 0 130px; }
.item-row .item-product-results { top: calc(100% + 3px); }
