:root {
    --font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    --page-bg: #eef3f8;
    --page-bg-soft: #f8fafc;
    --sidebar-bg: #07111f;
    --sidebar-bg-2: #0d2136;
    --card-bg: #ffffff;
    --card-bg-soft: #f8fafc;
    --text: #0f172a;
    --muted: #64748b;
    --muted-2: #94a3b8;
    --border: #dbe4ef;
    --border-soft: #edf2f7;
    --primary: #0e7490;
    --primary-dark: #155e75;
    --primary-soft: #e6f7fb;
    --danger: #b42318;
    --danger-soft: #fee2e2;
    --danger-soft-hover: #fecaca;
    --success: #027a48;
    --success-soft: #dcfae6;
    --warning: #b54708;
    --warning-soft: #fef0c7;
    --shadow: 0 24px 70px rgba(15, 23, 42, .10);
    --shadow-soft: 0 12px 35px rgba(15, 23, 42, .07);
    --radius: 22px;
    --radius-sm: 14px;
}

* { box-sizing: border-box; }

html { min-height: 100%; }

body {
    margin: 0;
    min-height: 100%;
    font-family: var(--font-family);
    background:
        radial-gradient(circle at top left, rgba(14, 116, 144, .16), transparent 34rem),
        linear-gradient(135deg, var(--page-bg-soft) 0%, var(--page-bg) 48%, #e8eef6 100%);
    color: var(--text);
    font-size: 14px;
}

a { color: inherit; text-decoration: none; }
button, input, select, textarea { font-family: inherit; }

.app {
    min-height: 100vh;
    display: grid;
    grid-template-columns: 290px minmax(0, 1fr);
}

.sidebar {
    min-height: 100vh;
    padding: 26px 20px;
    background:
        linear-gradient(180deg, rgba(255,255,255,.05), transparent 22rem),
        linear-gradient(180deg, var(--sidebar-bg), var(--sidebar-bg-2));
    color: #e5edf6;
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
}

.brand {
    display: flex;
    align-items: center;
    gap: 14px;
    margin-bottom: 32px;
}

.brand-mark {
    width: 50px;
    height: 50px;
    display: grid;
    place-items: center;
    border-radius: 18px;
    background: linear-gradient(135deg, #22d3ee, var(--primary) 55%, #164e63);
    color: #fff;
    font-weight: 900;
    letter-spacing: .04em;
    box-shadow: 0 16px 35px rgba(8, 145, 178, .35);
}

.brand-title, .brand-subtitle { display: block; }
.brand-title { color: #fff; font-size: 18px; font-weight: 850; letter-spacing: -.02em; }
.brand-subtitle { color: #8fa6bd; font-size: 12px; margin-top: 3px; }

.sidebar-section { flex: 1; }
.sidebar-label {
    color: #71869b;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: .14em;
    font-weight: 800;
    padding: 0 12px 10px;
}
.sidebar-nav { display: grid; gap: 8px; }
.sidebar-link {
    display: flex;
    align-items: center;
    gap: 11px;
    min-height: 46px;
    padding: 11px 13px;
    border-radius: 16px;
    color: #cbd5e1;
    font-size: 14px;
    font-weight: 700;
    transition: .16s ease;
}
.sidebar-link:hover { background: rgba(255,255,255,.075); color: #fff; transform: translateX(2px); }
.sidebar-link.active { background: #fff; color: var(--text); box-shadow: 0 16px 36px rgba(0,0,0,.22); }
.sidebar-icon { width: 24px; text-align: center; font-weight: 900; }

.sidebar-footer { border-top: 1px solid rgba(255,255,255,.10); padding-top: 18px; }
.user-mini { display: flex; align-items: center; gap: 11px; }
.avatar {
    width: 42px;
    height: 42px;
    border-radius: 15px;
    display: grid;
    place-items: center;
    background: linear-gradient(135deg, #e0f2fe, #67e8f9);
    color: #164e63;
    font-weight: 900;
}
.avatar.small { width: 36px; height: 36px; border-radius: 13px; font-size: 14px; }
.user-mini-name { color: #fff; font-size: 13px; font-weight: 800; }
.user-mini-role { color: #8fa6bd; font-size: 12px; margin-top: 2px; }
.logout-link { display: inline-flex; margin-top: 14px; color: #8fa6bd; font-size: 12px; font-weight: 700; }
.logout-link:hover { color: #fff; }

.main { min-width: 0; }
.topbar {
    min-height: 96px;
    padding: 22px 34px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 22px;
    background: rgba(248,250,252,.78);
    backdrop-filter: blur(18px);
    border-bottom: 1px solid rgba(219,228,239,.82);
    position: sticky;
    top: 0;
    z-index: 10;
}
.topbar-kicker { color: var(--primary-dark); font-size: 12px; font-weight: 850; text-transform: uppercase; letter-spacing: .12em; }
.topbar h1 { margin: 5px 0 0; font-size: 30px; line-height: 1.1; letter-spacing: -.04em; }
.topbar-user {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 48px;
    padding: 6px 12px 6px 7px;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 18px;
    box-shadow: 0 8px 24px rgba(15,23,42,.04);
}
.topbar-user-name { font-size: 13px; font-weight: 800; }
.topbar-user-role { font-size: 12px; color: var(--muted); }

.topbar-user-info { min-width: 0; }
.topbar-logout {
    margin-left: 10px;
    padding: 8px 11px;
    border-radius: 12px;
    background: var(--primary-soft);
    color: var(--primary-dark);
    font-size: 12px;
    font-weight: 850;
    white-space: nowrap;
}
.topbar-logout:hover { background: #d7f1f7; }
.content { padding: 34px; }

.page-head, .crm-page-head {
    background:
        radial-gradient(circle at 18% 20%, rgba(34,211,238,.16), transparent 23rem),
        linear-gradient(135deg, #fff 0%, #f8fbff 54%, #eef9fc 100%);
    border: 1px solid rgba(219,228,239,.95);
    border-radius: 30px;
    padding: 26px;
    margin-bottom: 22px;
    box-shadow: var(--shadow-soft);
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 24px;
}
.page-head h2, .crm-page-head h2 { margin: 0; font-size: 28px; letter-spacing: -.04em; }
.page-head p, .crm-page-head p { margin: 9px 0 0; max-width: 760px; color: var(--muted); line-height: 1.6; }

.card, .crm-panel {
    background: rgba(255,255,255,.92);
    border: 1px solid rgba(219,228,239,.95);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
}
.card-header, .crm-panel-header {
    padding: 20px 22px;
    border-bottom: 1px solid var(--border-soft);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}
.card-title, .crm-panel-title { margin: 0; font-size: 18px; letter-spacing: -.03em; }
.card-subtitle, .crm-panel-subtitle { margin-top: 4px; color: var(--muted); font-size: 13px; }
.card-body { padding: 22px; }

.table-wrap { overflow-x: auto; }
.table {
    width: 100%;
    min-width: 1050px;
    border-collapse: separate;
    border-spacing: 0;
    background: #fff;
}
.table th {
    background: #f8fafc;
    color: var(--muted);
    font-size: 11px;
    text-align: left;
    text-transform: uppercase;
    letter-spacing: .09em;
    font-weight: 900;
    padding: 13px 18px;
    border-bottom: 1px solid var(--border-soft);
}
.table td {
    padding: 16px 18px;
    border-bottom: 1px solid var(--border-soft);
    vertical-align: middle;
    font-size: 14px;
}
.table tbody tr { transition: .12s ease; }
.table tbody tr:hover td { background: #fbfdff; }
.table tbody tr:last-child td { border-bottom: 0; }

.btn, .popup-btn {
    border: 0;
    border-radius: 12px;
    padding: 10px 16px;
    min-height: 40px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    background: var(--primary);
    color: #fff;
    font-weight: 800;
    cursor: pointer;
    text-decoration: none;
    font-size: 14px;
    line-height: 1;
    transition: .15s ease;
}
.btn:hover, .popup-btn:hover { transform: translateY(-1px); }
.btn-light, .popup-btn-light { background: #fff; border: 1px solid var(--border); color: var(--text); }
.btn-primary, .popup-btn-primary { background: var(--primary); color: #fff; }
.btn-danger, .popup-btn-danger { background: var(--danger-soft); color: var(--danger); }
.btn-danger:hover, .popup-btn-danger:hover { background: var(--danger-soft-hover); }
.actions { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }

.badge {
    display: inline-flex;
    align-items: center;
    min-height: 26px;
    padding: 5px 10px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 850;
    white-space: nowrap;
}
.badge-success { background: var(--success-soft); color: var(--success); }
.badge-warning { background: var(--warning-soft); color: var(--warning); }
.badge-neutral { background: var(--primary-soft); color: var(--primary-dark); }

.form-grid, .crm-form-grid, .crm-data-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.form-field, .crm-form-field, .crm-data-item {
    background: var(--card-bg-soft);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 14px;
}
.form-field-full, .crm-form-full, .crm-data-full { grid-column: 1 / -1; }
.form-field label, .crm-form-field label, .crm-data-label {
    display: block;
    color: var(--muted);
    font-size: 12px;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: .06em;
    margin-bottom: 6px;
}
.form-field input, .form-field select, .form-field textarea,
.crm-form-field input, .crm-form-field select, .crm-form-field textarea {
    width: 100%;
    border: 1px solid #cbd5e1;
    border-radius: 10px;
    padding: 10px 12px;
    font-size: 14px;
    background: #fff;
    color: var(--text);
}
.form-field textarea, .crm-form-field textarea { min-height: 110px; resize: vertical; }
.crm-data-value { color: var(--text); font-weight: 650; word-break: break-word; }
.form-footer {
    padding: 18px 22px;
    background: var(--card-bg-soft);
    border-top: 1px solid var(--border-soft);
    display: flex;
    justify-content: flex-end;
    gap: 10px;
}

.crm-popup-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, .55);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    padding: 30px;
}
.crm-popup-backdrop.is-open { display: flex; }
.crm-popup {
    width: min(920px, 100%);
    max-height: calc(100vh - 60px);
    background: #fff;
    border-radius: 22px;
    box-shadow: 0 30px 90px rgba(15,23,42,.35);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}
.crm-popup-large { width: min(1080px, 100%); }
.crm-popup-header {
    padding: 24px 28px;
    background: linear-gradient(135deg, var(--sidebar-bg), var(--primary-dark));
    color: #fff;
    display: flex;
    justify-content: space-between;
    gap: 20px;
}
.crm-popup-title { font-size: 24px; font-weight: 800; margin: 0; }
.crm-popup-subtitle { opacity: .8; margin-top: 6px; }
.crm-popup-close {
    border: 0;
    background: rgba(255,255,255,.15);
    color: #fff;
    width: 38px;
    height: 38px;
    border-radius: 12px;
    cursor: pointer;
    font-size: 22px;
}
.crm-popup-body { padding: 26px 28px; overflow: auto; }
.crm-popup-footer {
    padding: 18px 28px;
    background: var(--card-bg-soft);
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 10px;
}
.crm-delete-form { margin: 0; }

.company-name { font-weight: 850; }
.muted { color: var(--muted); }
.link { color: var(--primary-dark); font-weight: 800; }
.link:hover { text-decoration: underline; }

.auth-page {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 28px;
}
.auth-card {
    width: min(430px, 100%);
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 26px;
    box-shadow: var(--shadow);
    padding: 28px;
}
.auth-card h1 { margin: 0 0 8px; font-size: 28px; letter-spacing: -.04em; }
.auth-card p { color: var(--muted); margin: 0 0 20px; }
.auth-error { background: var(--danger-soft); color: var(--danger); border-radius: 12px; padding: 12px; font-weight: 700; }
.auth-form { display: grid; gap: 12px; }
.auth-form input { width: 100%; border: 1px solid var(--border); border-radius: 12px; padding: 12px; }
.auth-form button { width: 100%; }

@media (max-width: 1180px) {
    .app { grid-template-columns: 1fr; }
    .sidebar { position: static; min-height: auto; }
    .sidebar-footer { display: none; }
    .sidebar-nav { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}
@media (max-width: 820px) {
    .topbar, .page-head, .crm-page-head { flex-direction: column; align-items: stretch; }
    .topbar-user { justify-content: space-between; width: 100%; }
    .topbar-logout { margin-left: auto; }
    .content { padding: 20px; }
    .sidebar-nav { grid-template-columns: 1fr 1fr; }
    .form-grid, .crm-form-grid, .crm-data-grid { grid-template-columns: 1fr; }
    .form-field-full, .crm-form-full, .crm-data-full { grid-column: auto; }
}

.company-files-box {
    border: 1px solid #e5e7eb;
    border-radius: 16px;
    padding: 16px;
    background: #f8fafc;
}

.company-files-upload {
    display: grid;
    gap: 10px;
    margin: 10px 0 12px;
}

.company-upload-row {
    display: grid;
    grid-template-columns: minmax(180px, 1fr) minmax(220px, 1.2fr);
    gap: 10px;
    align-items: center;
}

.company-files-list {
    display: grid;
    gap: 10px;
    margin-bottom: 14px;
}

.company-file-row {
    border: 1px solid #e5e7eb;
    border-radius: 14px;
    padding: 12px;
    background: #ffffff;
}

.company-file-meta {
    margin-top: 4px;
    color: #64748b;
    font-size: 12px;
    line-height: 1.45;
    word-break: break-all;
}

@media (max-width: 760px) {
    .company-upload-row {
        grid-template-columns: 1fr;
    }
}

.company-file-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
    justify-content: flex-end;
}
.company-file-token {
    border: 0;
    background: transparent;
    color: var(--primary-dark);
    font-weight: 800;
    padding: 0;
    cursor: pointer;
    text-decoration: underline;
    word-break: break-all;
}
.crm-popup-file {
    width: min(1560px, calc(100vw - 24px));
    height: min(920px, calc(100vh - 24px));
    min-width: 720px;
    min-height: 520px;
    max-width: calc(100vw - 24px);
    max-height: calc(100vh - 24px);
    resize: both;
    overflow: auto;
}
.crm-popup-file .crm-popup-header {
    padding: 16px 22px;
    flex: 0 0 auto;
}
.company-file-popup-body {
    padding: 0;
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
    background: #f8fafc;
}
.company-file-popup-body iframe {
    width: 100%;
    height: 100%;
    border: 0;
    display: block;
    background: #fff;
}
@media (max-width: 760px) {
    .crm-popup-file {
        min-width: 0;
        width: calc(100vw - 18px);
        height: calc(100vh - 18px);
        resize: vertical;
    }
}


/* Company file popup: real mouse resize */
.crm-popup-backdrop#companyFilePopup {
    align-items: center;
    justify-content: center;
}
.crm-popup-file {
    position: relative;
    width: min(1280px, calc(100vw - 48px));
    height: min(820px, calc(100vh - 48px));
    min-width: 520px;
    min-height: 360px;
    max-width: calc(100vw - 24px);
    max-height: calc(100vh - 24px);
    resize: none !important;
    overflow: hidden !important;
    display: flex;
    flex-direction: column;
}
.crm-popup-file .crm-popup-header {
    flex: 0 0 auto;
}
.company-file-popup-body {
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
    padding: 0;
    background: #fff;
}
.company-file-popup-body iframe {
    width: 100%;
    height: 100%;
    border: 0;
    display: block;
    background: #fff;
}
.company-file-resize-handle {
    position: absolute;
    right: 0;
    bottom: 0;
    width: 38px;
    height: 38px;
    cursor: nwse-resize;
    z-index: 99999;
    pointer-events: auto;
    touch-action: none;
    border-radius: 10px 0 20px 0;
    background:
        linear-gradient(135deg, transparent 0 48%, rgba(13, 84, 104, .85) 50%, transparent 52%) 9px 18px / 18px 18px no-repeat,
        linear-gradient(135deg, transparent 0 48%, rgba(13, 84, 104, .65) 50%, transparent 52%) 16px 18px / 16px 16px no-repeat,
        rgba(255,255,255,.92);
    box-shadow: -2px -2px 10px rgba(15,23,42,.12);
}
.company-file-resize-handle:hover {
    background-color: #e0f2fe;
}
body.company-file-resizing,
body.company-file-resizing * {
    cursor: nwse-resize !important;
    user-select: none !important;
}
body.company-file-resizing iframe {
    pointer-events: none !important;
}
@media (max-width: 760px) {
    .crm-popup-file {
        width: calc(100vw - 18px) !important;
        height: calc(100vh - 18px) !important;
        min-width: 0;
        min-height: 0;
    }
}


/* FINAL FIX: popup pliku jako okno + zawartość zawsze na 100% okna */
#companyFilePopup.crm-popup-backdrop {
    align-items: initial !important;
    justify-content: initial !important;
    padding: 0 !important;
}
#companyFilePopup.crm-popup-backdrop.is-open {
    display: block !important;
}
#companyFilePopup .crm-popup-file {
    position: fixed !important;
    left: 50% !important;
    top: 50% !important;
    transform: translate(-50%, -50%);
    width: min(1280px, calc(100vw - 80px)) !important;
    height: min(820px, calc(100vh - 80px)) !important;
    min-width: 520px !important;
    min-height: 360px !important;
    max-width: calc(100vw - 16px) !important;
    max-height: calc(100vh - 16px) !important;
    overflow: hidden !important;
    resize: none !important;
    display: flex !important;
    flex-direction: column !important;
}
#companyFilePopup .crm-popup-file .crm-popup-header {
    flex: 0 0 auto !important;
    cursor: move;
    user-select: none;
}
#companyFilePopup .company-file-popup-body {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    width: 100% !important;
    height: auto !important;
    overflow: hidden !important;
    padding: 0 !important;
    background: #fff !important;
}
#companyFilePopup .company-file-popup-body iframe {
    width: 100% !important;
    height: 100% !important;
    min-width: 100% !important;
    min-height: 100% !important;
    border: 0 !important;
    display: block !important;
    background: #fff !important;
}
#companyFilePopup .company-file-resize-handle {
    position: absolute !important;
    right: 0 !important;
    bottom: 0 !important;
    width: 42px !important;
    height: 42px !important;
    cursor: nwse-resize !important;
    z-index: 100000 !important;
    pointer-events: auto !important;
    touch-action: none !important;
}
body.company-file-resizing,
body.company-file-resizing *,
body.company-file-dragging,
body.company-file-dragging * {
    user-select: none !important;
}
body.company-file-resizing iframe,
body.company-file-dragging iframe {
    pointer-events: none !important;
}

/* Scroll wewnętrzny tylko dla popupa dodawania firmy */
#addCompanyPopup .crm-popup > form {
    display: flex;
    flex-direction: column;
    max-height: calc(100vh - 60px);
    min-height: 0;
    margin: 0;
}

#addCompanyPopup .crm-popup-header,
#addCompanyPopup .crm-popup-footer {
    flex: 0 0 auto;
}

#addCompanyPopup .crm-popup-body {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
}

.company-upload-row {
    grid-template-columns: minmax(160px, 1fr) minmax(170px, 1fr) 54px minmax(220px, 1.2fr);
}

.company-upload-row input[type="color"] {
    width: 54px;
    min-width: 54px;
    height: 42px;
    padding: 4px;
    cursor: pointer;
}

.company-file-status-badge {
    display: inline-flex;
    align-items: center;
    margin-left: 8px;
    padding: 4px 9px;
    border: 1px solid currentColor;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 800;
    line-height: 1.2;
    vertical-align: middle;
}

.company-existing-file-status {
    display: grid;
    grid-template-columns: 140px minmax(180px, 1fr) 95px 54px;
    gap: 8px;
    align-items: center;
    margin-top: 10px;
}

.company-existing-file-status label {
    margin: 0;
    color: #64748b;
    font-size: 12px;
    font-weight: 700;
}

.company-existing-file-status input[type="color"] {
    width: 54px;
    min-width: 54px;
    height: 40px;
    padding: 4px;
    cursor: pointer;
}

@media (max-width: 900px) {
    .company-upload-row,
    .company-existing-file-status {
        grid-template-columns: 1fr;
    }

    .company-upload-row input[type="color"],
    .company-existing-file-status input[type="color"] {
        width: 100%;
    }
}
