:root {
    --red: #c8202f;
    --red-dark: #a11726;
    --black: #111111;
    --gray-50: #f7f7f8;
    --gray-100: #eef0f2;
    --gray-300: #d7dadf;
    --gray-600: #6b7280;
    --radius: 8px;

    --bg: var(--gray-50);
    --surface: #ffffff;
    --text: #111111;
    --text-muted: var(--gray-600);
    --border: var(--gray-300);
    --hover-bg: var(--gray-100);
}

:root[data-theme="dark"] {
    --bg: #15171b;
    --surface: #1f2126;
    --text: #e8e9eb;
    --text-muted: #9aa0aa;
    --border: #34383f;
    --hover-bg: #2a2d33;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
}

/* Topbar */
.topbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}
.topbar-inner {
    max-width: 1320px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    gap: 24px;
    padding: 10px 20px;
}
.brand-logo { height: 36px; display: block; }
.main-nav { display: flex; gap: 4px; flex: 1; }
.main-nav a {
    color: var(--text);
    text-decoration: none;
    padding: 8px 14px;
    border-radius: var(--radius);
    font-weight: 600;
    font-size: 14px;
}
.main-nav a:hover { background: var(--hover-bg); }
.main-nav a.active { background: var(--red); color: #fff; }
.user-box { display: flex; align-items: center; gap: 12px; font-size: 14px; }
.user-name { font-weight: 600; color: var(--text-muted); }
.session-timer {
    font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
    font-size: 12px;
    color: var(--text-muted);
    background: var(--hover-bg);
    padding: 3px 8px;
    border-radius: 999px;
}
.session-timer-warning { color: #a3231f; background: #fdeaea; }
.logout-link { color: var(--red); text-decoration: none; font-weight: 600; }
.logout-link:hover { text-decoration: underline; }

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 32px;
    height: 32px;
    padding: 0 8px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    border-radius: var(--radius);
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
    line-height: 1;
}
.icon-btn:hover { background: var(--hover-bg); }
.top-actions { display: flex; align-items: center; gap: 8px; }
.nav-toggle { display: none; margin-left: auto; }
.topbar-menu { display: contents; }
.top-actions-fixed {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 10;
}

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    z-index: 200;
}
.modal-overlay[hidden] { display: none; }
.modal-box {
    background: var(--surface);
    color: var(--text);
    border-radius: 12px;
    padding: 28px;
    max-width: 380px;
    width: 100%;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    text-align: center;
}
.modal-box p { margin: 0 0 20px; font-size: 15px; line-height: 1.5; }
.modal-actions { display: flex; gap: 8px; justify-content: center; }
.modal-actions .btn { flex: 1; }

.barcode-sheet-hidden { display: none !important; }

.flash {
    max-width: 1320px;
    margin: 16px auto 0;
    padding: 12px 16px;
    border-radius: var(--radius);
    font-size: 14px;
}
.flash-info { background: #e7f1ff; color: #1a4d8f; }
.flash-success { background: #e6f7ed; color: #14733a; }
.flash-error { background: #fdeaea; color: #a3231f; }

.page {
    max-width: 1320px;
    margin: 0 auto;
    padding: 24px 20px 60px;
}

/* Login */
.login-wrap {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(180deg, var(--surface) 0%, var(--bg) 100%);
    padding: 20px;
}
.login-card {
    width: 100%;
    max-width: 360px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 32px;
    text-align: center;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.06);
}
.brand-text {
    font-size: 40px;
    font-weight: 900;
    letter-spacing: -0.01em;
    color: var(--text);
    margin-bottom: 28px;
    line-height: 1;
}
.brand-text span { color: var(--red); }
.brand-text-sm { font-size: 22px; margin-bottom: 0; display: inline-block; }
.brand { text-decoration: none; display: flex; align-items: center; }
.login-card h1 { font-size: 18px; margin: 0 0 20px; }

.field { text-align: left; margin-bottom: 16px; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; color: var(--text-muted); }
.field input[type="text"],
.field input[type="password"],
.field input[type="number"],
.field select,
.field textarea {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 14px;
    font-family: inherit;
    background: var(--surface);
    color: var(--text);
}
.field textarea { resize: vertical; }

.vessel-autocomplete-wrap { position: relative; }
.vessel-suggestions {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    z-index: 10;
    margin-top: 4px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
    max-height: 220px;
    overflow-y: auto;
}
.vessel-suggestions[hidden] { display: none; }
.vessel-suggestion-item {
    display: block;
    width: 100%;
    text-align: left;
    padding: 10px 12px;
    border: none;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 14px;
    font-family: inherit;
}
.vessel-suggestion-item:last-child { border-bottom: none; }
.vessel-suggestion-item:hover,
.vessel-suggestion-item:active { background: var(--hover-bg); }

.input-prefix-group { display: flex; align-items: stretch; }
.input-prefix-group .input-prefix {
    display: flex;
    align-items: center;
    padding: 10px 4px 10px 12px;
    border: 1px solid var(--border);
    border-right: none;
    border-radius: var(--radius) 0 0 var(--radius);
    background: var(--hover-bg);
    color: var(--text);
    font-weight: 700;
    font-size: 14px;
}
.field .input-prefix-group input[type="text"] {
    width: auto;
    flex: 1;
    border-radius: 0 var(--radius) var(--radius) 0;
}

.field-checkbox { margin-bottom: 16px; }
.checkbox-label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text);
    cursor: pointer;
}
.checkbox-label input { width: 16px; height: 16px; flex-shrink: 0; }

.btn {
    display: inline-block;
    background: var(--red);
    color: #fff;
    border: none;
    padding: 10px 18px;
    border-radius: var(--radius);
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
    text-decoration: none;
}
.btn:hover { background: var(--red-dark); }
.btn-secondary { background: var(--hover-bg); color: var(--text); }
.btn-secondary:hover { background: var(--border); }
.btn-danger { background: #a3231f; }
.btn-danger:hover { background: #7f1a17; }
.btn-sm { padding: 6px 10px; font-size: 12px; }

.error-msg { background: #fdeaea; color: #a3231f; padding: 10px 12px; border-radius: var(--radius); font-size: 13px; margin-bottom: 16px; }
.login-card .btn { width: 100%; margin-top: 4px; }

/* Label tool */
.label-tool {
    display: flex;
    gap: 32px;
    align-items: flex-start;
    flex-wrap: wrap;
}
.label-form {
    flex: 0 0 460px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 24px;
}
.label-form h1 { font-size: 18px; margin: 0 0 20px; }
.label-form-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 20px; }
.label-form-head h1 { margin-bottom: 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 16px; }
.card-head h2 { margin-bottom: 0; }

.option-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}
.option-btn { position: relative; display: block; }
.option-btn input { position: absolute; opacity: 0; width: 0; height: 0; }
.option-btn span {
    display: block;
    text-align: center;
    padding: 10px 6px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.02em;
    cursor: pointer;
    user-select: none;
}
.option-btn input:checked + span {
    background: var(--red);
    border-color: var(--red);
    color: #fff;
}
.option-btn input:focus-visible + span {
    outline: 2px solid var(--red-dark);
    outline-offset: 2px;
}
.btn-print { width: 100%; margin-top: 8px; padding: 12px; font-size: 15px; }

.btn-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin-top: 8px;
}
.btn-grid .btn {
    width: 100%;
    margin-top: 0;
}
.btn-grid .hint {
    grid-column: 1 / -1;
    margin-top: 0;
}

.label-preview-wrap { flex: 1 1 420px; min-width: 320px; }
.preview-title { font-size: 14px; color: var(--text-muted); margin: 0 0 12px; text-transform: uppercase; letter-spacing: 0.05em; }

#previewArea {
    display: flex;
    flex-direction: column;
    gap: 24px;
    max-height: calc(100vh - 160px);
    overflow: auto;
    padding: 4px;
}

.a4-page {
    background: #fff;
    color: var(--black);
    width: 210mm;
    height: 297mm;
    max-width: 100%;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.label-half {
    flex: 1 1 50%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 10mm;
    gap: 6mm;
}
.label-half.label-empty { visibility: hidden; }

.label-logo { max-width: 55%; max-height: 30mm; object-fit: contain; }
.label-vessel { font-size: 11mm; font-weight: 800; letter-spacing: 0.02em; text-transform: uppercase; word-break: break-word; }
.label-option {
    font-size: 7mm;
    font-weight: 800;
    color: #fff;
    background: var(--red);
    padding: 3mm 8mm;
    border-radius: 4mm;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
    color-adjust: exact;
}
.label-dc { font-size: 5mm; font-weight: 700; color: var(--black); letter-spacing: 0.02em; }
.label-extra { font-size: 4mm; color: #444; max-width: 80%; white-space: pre-wrap; }
.label-count { font-size: 11mm; font-weight: 800; color: var(--gray-600); margin-top: 2mm; }

.cut-line { margin: 0 10mm; }

/* Shared form helpers */
.req { color: var(--red); }
.hint { font-size: 12px; color: var(--text-muted); margin-top: 6px; }
.field input[type="file"] { width: 100%; font-size: 13px; color: var(--text); }

.dropzone {
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    padding: 16px;
    text-align: center;
    background: var(--hover-bg);
    transition: border-color 0.15s ease, background 0.15s ease;
}
.dropzone.dropzone-active {
    border-color: var(--red);
    background: #fdeaea;
}
:root[data-theme="dark"] .dropzone.dropzone-active { background: rgba(200, 32, 47, 0.15); }
.dropzone input[type="file"] { width: 100%; font-size: 13px; color: var(--text); }
.dropzone-text { margin: 8px 0 0; font-size: 12px; color: var(--text-muted); pointer-events: none; }
.warning-box {
    margin-top: 8px;
    background: #fff4e5;
    color: #8a5a00;
    border: 1px solid #f3ce8f;
    padding: 8px 10px;
    border-radius: var(--radius);
    font-size: 12px;
    line-height: 1.4;
}
.btn[disabled] { background: var(--border); color: var(--text-muted); cursor: not-allowed; }

/* Document preview (paklijst, barcode) */
.doc-preview-area {
    display: flex;
    flex-direction: column;
    max-height: calc(100vh - 160px);
    overflow: auto;
    padding: 4px;
}

.a4-doc {
    background: #fff;
    color: var(--black);
    width: 210mm;
    min-height: 297mm;
    max-width: 100%;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12);
    padding: 15mm;
    flex-shrink: 0;
}

.doc-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 8mm;
    border-bottom: 1.5px solid var(--black);
    padding-bottom: 3mm;
    margin-bottom: 4mm;
}
.doc-header-text {
    font-size: 5.5mm;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    word-break: break-word;
}
.doc-header-option { color: var(--red); margin-left: 2mm; }
.doc-header-summary { font-size: 4mm; font-weight: 600; color: var(--gray-600); margin-top: 2mm; text-transform: none; letter-spacing: normal; }
.doc-barcode-wrap { flex-shrink: 0; }
.doc-barcode-wrap svg { display: block; max-width: 50mm; height: auto; }
#pakBarcode { max-width: 50mm; }

.barcode-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6mm;
}
.barcode-box {
    border: 1px solid var(--gray-300);
    border-radius: 4px;
    padding: 6mm;
    display: flex;
    align-items: center;
    justify-content: center;
}
.barcode-box svg { display: block; max-width: 100%; height: auto; }

.pak-extra-box {
    background: #fff4e5;
    border: 1px solid #f3ce8f;
    border-left: 4px solid #c8202f;
    color: #6b4a00;
    padding: 4mm 6mm;
    margin-bottom: 6mm;
    font-size: 4mm;
    white-space: pre-wrap;
    border-radius: 4px;
}

.pak-table-wrap { margin-bottom: 10mm; }
#pakTable { width: 100%; border-collapse: collapse; font-size: 3.6mm; }
#pakTable thead { display: table-header-group; }
#pakTable th, #pakTable td {
    border: 1px solid var(--gray-300);
    padding: 2mm 3mm;
    text-align: left;
    word-break: break-word;
}
#pakTable th { background: var(--gray-100); font-weight: 700; }
#pakTable .pak-col-nowrap { white-space: nowrap; word-break: normal; }
#pakTable .pak-table-empty { background: #fff; color: var(--gray-600); font-weight: 400; font-style: italic; text-align: center; padding: 10mm; }

.pak-signoff-title { font-weight: 700; font-size: 4.2mm; margin-bottom: 6mm; }
.pak-signoff-lines { display: flex; flex-direction: row; gap: 16mm; }
.pak-signoff-line { flex: 1; font-size: 4mm; display: flex; align-items: flex-end; gap: 4mm; }
.pak-signoff-line span { flex: 1; border-bottom: 1px solid var(--black); height: 6mm; }

@media (max-width: 900px) {
    .label-tool { flex-direction: column; }
    .label-form { flex: 1 1 auto; width: 100%; }
    #previewArea { max-height: none; }
}

@media (max-width: 860px) {
    .topbar-inner { flex-wrap: wrap; padding: 10px 16px; }
    .nav-toggle { display: inline-flex; }
    .topbar-menu {
        display: none;
        flex-basis: 100%;
        flex-direction: column;
        align-items: stretch;
        gap: 4px;
        padding-top: 10px;
    }
    .topbar-menu.open { display: flex; }
    .main-nav { flex-direction: column; gap: 2px; }
    .main-nav a { width: 100%; padding: 10px 12px; }
    .user-box {
        flex-wrap: wrap;
        width: 100%;
        padding-top: 10px;
        margin-top: 6px;
        border-top: 1px solid var(--border);
    }
}

.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }

@media (max-width: 600px) {
    .page { padding: 16px 12px 40px; }
    .card { padding: 16px; }
    .label-form { padding: 16px; }
    .doc-preview-area, #previewArea { max-height: none; }
    .vh-tabs { overflow-x: auto; flex-wrap: nowrap; -webkit-overflow-scrolling: touch; }
    .vh-tabs a { flex-shrink: 0; }
}

@media (max-width: 480px) {
    .option-grid { grid-template-columns: 1fr; }
    .btn-grid { grid-template-columns: 1fr; }
    .btn-grid .hint { grid-column: 1; }
    .add-user-form { grid-template-columns: 1fr; }
    .card-head, .label-form-head { flex-wrap: wrap; }
    .vh-result-row { flex-direction: column; align-items: flex-start; }
    .vh-result-actions { width: 100%; }
    .vh-result-actions .btn { flex: 1; }
    .track-row-label { flex-basis: 48px; font-size: 13px; }
}

/* Users page */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 24px;
    margin-bottom: 24px;
}
.card h2 { font-size: 16px; margin: 0 0 16px; }
.card-narrow { max-width: 520px; margin-left: auto; margin-right: auto; }

.track-rows { display: flex; flex-direction: column; gap: 10px; }
.track-row { display: flex; align-items: center; gap: 10px; margin: 0; }
.track-row-label { flex: 0 0 56px; font-weight: 700; }
.track-row-input {
    flex: 1;
    min-width: 0;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 14px;
    font-family: inherit;
    background: var(--surface);
    color: var(--text);
}
.track-row .btn { flex-shrink: 0; }
table.users-table { width: 100%; border-collapse: collapse; font-size: 14px; }
table.users-table th, table.users-table td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--hover-bg); }
table.users-table th { color: var(--text-muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.03em; }
.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 700; }
.badge-admin { background: #fdeaea; color: #a3231f; }
.badge-editor { background: #e7f1ff; color: #1a4d8f; }
.badge-search { background: var(--hover-bg); color: var(--text-muted); }
.row-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row-actions form { display: inline-flex; gap: 6px; align-items: center; margin: 0; }
.row-actions select, .row-actions input[type="password"] {
    padding: 6px 8px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 13px;
    background: var(--surface);
    color: var(--text);
}
.add-user-form { display: grid; grid-template-columns: 1fr 1fr 150px auto; gap: 12px; align-items: end; }
.add-user-form .field { margin-bottom: 0; }

@media (max-width: 700px) {
    .add-user-form { grid-template-columns: 1fr; }
}

/* Vesselhub */
.vh-tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 20px;
    border-bottom: 1px solid var(--border);
}
.vh-tabs a {
    display: inline-block;
    padding: 10px 16px;
    text-decoration: none;
    color: var(--text-muted);
    font-weight: 600;
    font-size: 14px;
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
}
.vh-tabs a:hover { color: var(--text); }
.vh-tabs a.active { color: var(--red); border-bottom-color: var(--red); }

.vh-results { display: flex; flex-direction: column; gap: 10px; margin-top: 16px; }
.vh-result-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 16px 20px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
}
.vh-result-vessel { font-size: 22px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.01em; }
.vh-result-office { font-size: 14px; color: var(--text-muted); margin-top: 4px; }
.vh-result-actions { display: flex; gap: 8px; flex-wrap: wrap; flex-shrink: 0; }

.vh-selection-summary { font-weight: 600; margin: 12px 0; }

.vh-email-list { display: flex; flex-direction: column; gap: 10px; margin-top: 8px; }
.vh-office-item {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px 16px;
    cursor: pointer;
}
.vh-office-item:hover { background: var(--hover-bg); }
.vh-office-item.selected {
    border-color: var(--red);
    background: #fdeaea;
}
:root[data-theme="dark"] .vh-office-item.selected { background: rgba(200, 32, 47, 0.15); }
.vh-office-item input { width: 18px; height: 18px; margin-top: 2px; flex-shrink: 0; }
.vh-office-item input:disabled { opacity: 0.4; }
.vh-office-name { font-weight: 700; }
.vh-office-emails-preview { font-size: 13px; color: var(--text-muted); margin-top: 2px; }
.vh-copy-all { grid-column: 1 / -1; }

/* Print */
@page {
    size: A4;
    margin: 0;
}
@media print {
    .no-print { display: none !important; }
    body { background: #fff; color: #111111; }
    .page { max-width: none; padding: 0; margin: 0; }
    .label-tool { display: block; }
    .a4-page, .a4-doc { zoom: 1 !important; }
    #previewArea {
        max-height: none;
        overflow: visible;
        gap: 0;
        padding: 0;
    }
    .a4-page {
        box-shadow: none;
        width: 210mm;
        height: 297mm;
        page-break-after: always;
    }
    .a4-page:last-child { page-break-after: auto; }

    .doc-preview-area {
        max-height: none;
        overflow: visible;
        padding: 0;
    }
    .a4-doc {
        box-shadow: none;
        width: 100%;
        min-height: 0;
        padding: 0;
    }
    .doc-header,
    .pak-signoff { page-break-inside: avoid; }
    .barcode-box { page-break-inside: avoid; }
    #pakTable tr { page-break-inside: avoid; }
    #pakTable thead { display: table-header-group; }
}
