:root {
    --bg: #f4f8fb;
    --card: #ffffff;
    --text: #243043;
    --muted: #64748b;
    --primary: #0f766e;
    --primary-dark: #0b5f59;
    --secondary: #e2e8f0;
    --danger: #b91c1c;
    --border: #dbe4ef;
    --bg-grad-1: #eef4fb;
    --bg-grad-2: #f7fbff;
    --bg-grad-3: #e9f8f6;
    --input-bg: #ffffff;
    --input-border: #cad5e3;
    --table-head: #f8fafc;
    --soft-surface: #f8fbff;
    --soft-border: #cbd5e1;
    --checker-1: #f8fafc;
    --checker-2: #eef2f7;
    --stroke: #111827;
    --link: #111827;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
    color: var(--text);
    background: linear-gradient(120deg, var(--bg-grad-1) 0%, var(--bg-grad-2) 45%, var(--bg-grad-3) 100%);
    min-height: 100vh;
    overflow-x: hidden;
}

.auth-body {
    display: grid;
    place-items: center;
    padding: 20px;
}

.auth-card,
.form-card,
.table-card,
.search-card,
.highlight-card,
.stat-card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 14px;
    box-shadow: 0 10px 25px rgba(30, 41, 59, 0.07);
}

.auth-card {
    width: 100%;
    max-width: 420px;
    padding: 28px;
}

.auth-card h1,
.auth-card .subtitle,
.auth-card .alert {
    text-align: center;
}

.auth-logo {
    width: 58px;
    height: 58px;
    object-fit: contain;
    display: block;
    margin: 0 auto 10px;
}

.subtitle {
    color: var(--muted);
    margin-top: -8px;
}

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 24px;
    border-bottom: 1px solid var(--border);
    background: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(5px);
}

.topbar h1 {
    margin: 0;
    font-size: clamp(1.1rem, 2.4vw, 1.5rem);
    line-height: 1.2;
}

.topbar-title {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.topbar-logo {
    width: 34px;
    height: 34px;
    object-fit: contain;
    flex: 0 0 auto;
}

.topbar-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

.container {
    max-width: 99%;
    margin: 22px auto;
    padding: 0 16px 30px;
    display: grid;
    gap: 16px;
}

.form {
    display: grid;
    gap: 10px;
}

.password-field {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
    align-items: center;
}

.password-field input {
    min-width: 0;
}

input,
select,
textarea {
    width: 100%;
    border: 1px solid var(--input-border);
    border-radius: 10px;
    padding: 11px 12px;
    font-size: 0.95rem;
    background: var(--input-bg);
    color: var(--text);
}

input:focus,
select:focus,
textarea:focus {
    outline: 2px solid rgba(15, 118, 110, 0.18);
    border-color: var(--primary);
}

.btn {
    border: none;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 10px;
    padding: 10px 14px;
    cursor: pointer;
    font-weight: 600;
}

.btn.primary {
    background: var(--primary);
    color: #fff;
}

.btn.primary:hover {
    background: var(--primary-dark);
}

.btn.secondary {
    background: var(--secondary);
    color: var(--text);
}

.btn.ghost {
    background: var(--input-bg);
    border: 1px solid var(--border);
    color: var(--text);
}

.btn.edit {
    background: #e0f2fe;
    color: #0c4a6e;
}

.btn.edit:hover {
    background: #bae6fd;
}

.btn.danger {
    background: var(--danger);
    color: #fff;
}

.btn.big {
    font-size: 1.05rem;
    padding: 12px 16px;
}

.btn.small {
    font-size: 0.85rem;
    padding: 7px 10px;
}

.highlight-card,
.form-card,
.table-card,
.search-card {
    padding: 16px;
}

.highlight-card.centered {
    text-align: center;
}

.actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

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

table {
    width: 100%;
    border-collapse: collapse;
    min-width: 0;
}

th,
td {
    border-bottom: 1px solid var(--border);
    text-align: left;
    vertical-align: top;
    padding: 10px;
    font-size: 0.92rem;
}

th {
    background: var(--table-head);
}

.row-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.items-preview {
    display: block;
    max-width: 340px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
    gap: 12px;
}

.stat-card {
    padding: 16px;
}

.stat-card h2 {
    margin-top: 0;
    color: var(--muted);
    font-size: 0.92rem;
}

.stat-card p {
    margin: 0;
    font-size: 2rem;
    font-weight: 700;
}

.alert {
    padding: 10px 12px;
    border-radius: 8px;
    font-size: 0.95rem;
}

.alert.success {
    background: #dcfce7;
    color: #166534;
}

.alert.error {
    background: #fee2e2;
    color: #991b1b;
}

.print-wrapper {
    max-width: 740px;
    margin: 20px auto 40px;
    padding: 0 16px;
}

.purchase-sheet {
    background: var(--card);
    border: 1px solid var(--stroke);
    border-radius: 8px;
    width: min(100%, 148mm);
    min-height: 210mm;
    margin: 0 auto;
    padding: 6mm;
    box-shadow: 0 10px 24px rgba(15, 23, 42, 0.08);
    display: flex;
    flex-direction: column;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 11pt;
    line-height: 1.2;
    position: relative;
    overflow: hidden;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
}

.purchase-sheet::before {
    content: "";
    position: absolute;
    inset: 12% 10%;
    background: url("fluir.png") center / contain no-repeat;
    opacity: 0.12;
    pointer-events: none;
    z-index: 0;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
}

.sheet-title {
    margin: 0 0 6px;
    text-align: center;
    font-size: 14pt;
    letter-spacing: 0.02em;
}

.sheet-subtitle {
    margin: 0 0 10px;
    text-align: center;
    letter-spacing: 0.06em;
    font-size: 12pt;
    font-weight: 600;
}

.sheet-content {
    flex: 1 1 auto;
    padding: 4mm;
    position: relative;
    z-index: 1;
}

.sheet-box {
    border: 1px solid var(--stroke);
    border-radius: 4px;
    padding: 8px 10px;
    margin-bottom: 8px;
}

.sheet-meta {
    min-height: 31mm;
}

.sheet-meta-row {
    display: flex;
    gap: 16px;
    align-items: center;
    justify-content: space-between;
}

.sheet-meta-info {
    flex: 1 1 auto;
    min-width: 0;
}

.sheet-meta-qr {
    flex: 0 0 auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    text-align: center;
    font-size: 9pt;
    color: var(--text);
}

.sheet-meta-qr img {
    width: 90px;
    height: 90px;
    border: 1px solid var(--stroke);
    border-radius: 4px;
    padding: 3px;
    background: var(--input-bg);
}

.validation-link {
    color: var(--link);
    text-decoration: underline;
}

.sheet-meta p {
    margin: 5px 0;
}

.sheet-box h3 {
    margin: 0 0 10px;
    font-size: 11pt;
}

.sheet-box ul {
    margin: 0;
    padding-left: 20px;
}

.sheet-box li {
    margin-bottom: 4px;
}

.sheet-box:last-child {
    min-height: 0;
}

.sheet-signatures {
    margin-top: auto;
    display: grid;
    grid-template-columns: 1fr;
    gap: 20px;
    padding-top: 16px;
    position: relative;
    z-index: 1;
}

.sign-line {
    --sig-x: 0px;
    --sig-y: 0px;
    --sig-scale: 1;
    text-align: center;
    font-size: 0.9rem;
    width: 70%;
    margin: 0 auto 50px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0;
}

.sign-name {
    font-size: 0.95rem;
    font-weight: 700;
    color: #1e3a8a;
    font-family: "Lucida Handwriting", "Segoe Script", "Brush Script MT", "Comic Sans MS", cursive;
    letter-spacing: 0.02em;
    transform: translate(var(--sig-x), var(--sig-y)) scale(var(--sig-scale));
    transform-origin: center bottom;
}

.sign-image {
    display: block;
    max-width: 72%;
    max-height: 70px;
    width: auto;
    margin: 0 auto;
    object-fit: contain;
    transform: translate(calc(var(--sig-x)), calc(14px + var(--sig-y))) scale(var(--sig-scale));
    transform-origin: center bottom;
}

.protected-signature {
    pointer-events: none;
    -webkit-user-drag: none;
    user-select: none;
}

.sign-stroke {
    display: block;
    width: 100%;
    border-top: 1px solid #111827;
    margin-top: -14px;
}

.sign-role {
    font-size: 0.86rem;
    color: var(--text);
    margin-top: 3px;
}

.signature-thumb {
    max-width: 160px;
    max-height: 45px;
    display: block;
    margin: 0 auto;
    object-fit: contain;
    background: #fff;
}

.signature-editor {
    border: 1px solid #dbe4ef;
    border-radius: 10px;
    padding: 12px;
    background: var(--soft-surface);
}

.doc-sign-preview {
    border: 1px solid #dbe4ef;
    border-radius: 10px;
    padding: 12px;
    background: var(--soft-surface);
}

.doc-sign-sheet {
    border: 1px solid var(--soft-border);
    border-radius: 8px;
    background: var(--input-bg);
    min-height: 145px;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding: 12px;
}

.doc-sign-line {
    --sig-x: 0px;
    --sig-y: 0px;
    --sig-scale: 1;
    width: 76%;
    text-align: center;
}

.doc-sign-line .sign-image {
    display: none;
    max-height: 52px;
}

.doc-sign-canvas {
    display: block;
    width: min(72%, 420px);
    height: auto;
    margin: 0 auto;
}

.editor-title {
    margin: 0 0 4px;
    font-weight: 700;
}

.editor-help {
    margin: 0 0 10px;
    color: #64748b;
    font-size: 0.9rem;
}

.editor-controls {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 8px 10px;
    align-items: center;
    margin-bottom: 10px;
}

.editor-controls .btn {
    grid-column: 1 / -1;
    justify-self: start;
}

.editor-buttons {
    grid-column: 1 / -1;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.editor-buttons .btn.active {
    background: #0f766e;
    color: #fff;
}

#sig_canvas {
    width: 100%;
    max-width: 100%;
    height: auto;
    min-height: 280px;
    border-radius: 8px;
    background: var(--input-bg);
    border: 1px solid var(--soft-border);
    cursor: move;
}

#sig_canvas.crop-mode {
    cursor: crosshair;
}

.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: rgba(0, 0, 0, 0.8);
    align-items: center;
    justify-content: center;
}

.modal-content {
    background-color: transparent;
    width: 80%;
    height: 95%;
    border: none;
    padding: 0;
    border-radius: 0;
}

@media (max-width: 780px) {
    .sheet-signatures {
        grid-template-columns: 1fr;
        gap: 20px;
    }
}

@media (max-width: 900px) {
    .btn {
        min-height: 40px;
    }

    .btn.small {
        min-height: 36px;
    }
}

@media (max-width: 768px) {
    .topbar {
        padding: 10px 12px;
        flex-wrap: wrap;
        gap: 10px;
    }

    .topbar-right {
        width: 100%;
        justify-content: space-between;
        flex-wrap: wrap;
    }

    .topbar-logo {
        width: 30px;
        height: 30px;
    }

    .container {
        margin-top: 14px;
        padding: 0 12px 24px;
    }

    .auth-card,
    .form-card,
    .table-card,
    .search-card,
    .highlight-card,
    .stat-card {
        border-radius: 12px;
    }

    .actions {
        gap: 8px;
    }

    .actions .btn {
        width: 100%;
    }

    .row-actions {
        flex-direction: row;
    }

    .row-actions .btn {
        flex: 1 1 140px;
    }

    .editor-controls {
        grid-template-columns: 1fr;
    }

    .editor-controls label {
        font-weight: 600;
    }

    .doc-sign-sheet {
        min-height: 120px;
        padding: 10px;
    }

    .doc-sign-line {
        width: 92%;
    }

    .doc-sign-canvas {
        width: min(92%, 360px);
    }
}

@media (max-width: 640px) {
    .table-wrap {
        overflow: visible;
    }

    .responsive-table {
        border-collapse: separate;
        border-spacing: 0;
    }

    .responsive-table thead {
        display: none;
    }

    .responsive-table tbody,
    .responsive-table tr,
    .responsive-table td {
        display: block;
        width: 100%;
    }

    .responsive-table tr {
        border: 1px solid var(--border);
        border-radius: 10px;
        background: var(--card);
        margin-bottom: 10px;
        padding: 6px 10px;
    }

    .responsive-table td {
        border-bottom: 1px dashed var(--border);
        padding: 8px 0;
        display: grid;
        grid-template-columns: minmax(100px, 42%) 1fr;
        gap: 8px;
        align-items: start;
        word-break: break-word;
    }

    .responsive-table td:last-child {
        border-bottom: none;
    }

    .responsive-table td::before {
        content: attr(data-label);
        font-weight: 700;
        color: var(--muted);
        font-size: 0.82rem;
        padding-top: 2px;
    }

    .responsive-table td.row-actions {
        display: flex;
        gap: 8px;
        align-items: stretch;
        flex-wrap: wrap;
        padding-top: 10px;
    }

    .responsive-table td.row-actions::before {
        content: none;
    }

    .responsive-table td.row-actions .btn {
        flex: 1 1 120px;
    }

    .items-preview {
        max-width: 100%;
        white-space: normal;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    .responsive-table td[colspan] {
        display: block;
        border-bottom: none;
        text-align: center;
        padding: 10px 0;
    }

    .responsive-table td[colspan]::before {
        content: none;
    }

    .purchase-sheet {
        width: 100%;
        min-height: auto;
        padding: 5mm;
    }

    .sheet-content {
        padding: 2mm;
    }

    .sheet-meta-row {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
    }

    .sheet-meta-qr {
        align-self: center;
    }

    .sheet-meta-qr img {
        width: 76px;
        height: 76px;
    }

    .sign-line {
        width: 92%;
    }

    .sign-image {
        max-width: 82%;
    }
}

@media print {
    @page {
        size: A5 portrait;
        margin: 0;
    }

    body {
        background: #fff;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    .no-print {
        display: none !important;
    }

    .print-wrapper {
        margin: 0;
        padding: 0;
    }

    .purchase-sheet {
        border: 1px solid #000;
        box-shadow: none;
        border-radius: 0;
        width: 148mm;
        min-height: 210mm;
        margin: 0;
        padding: 6mm;
    }
}
#sig_canvas.rotate-mode { cursor: ew-resize; }

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0b1220;
        --card: #0f172a;
        --text: #e5edf7;
        --muted: #94a3b8;
        --secondary: #1e293b;
        --border: #334155;
        --bg-grad-1: #0b1220;
        --bg-grad-2: #0f172a;
        --bg-grad-3: #111827;
        --input-bg: #0b1324;
        --input-border: #334155;
        --table-head: #111a2f;
        --soft-surface: #0c162a;
        --soft-border: #334155;
        --checker-1: #101a31;
        --checker-2: #0b1324;
        --stroke: #5b6b80;
        --link: #cbd5e1;
    }

    .topbar {
        background: rgba(15, 23, 42, 0.85);
        border-bottom-color: #334155;
    }

    .auth-card,
    .form-card,
    .table-card,
    .search-card,
    .highlight-card,
    .stat-card {
        box-shadow: 0 10px 24px rgba(2, 6, 23, 0.45);
    }

    .btn.edit {
        background: #1e3a5f;
        color: #bfdbfe;
    }

    .btn.edit:hover {
        background: #24507f;
    }



    .sign-name {
        color: #93c5fd;
    }

    .sign-stroke {
        border-top-color: var(--stroke);
    }

    @media (max-width: 640px) {
        .responsive-table tr {
            background: var(--card);
            border-color: var(--border);
        }

        .responsive-table td {
            border-bottom-color: var(--border);
        }

        .responsive-table td::before {
            color: var(--muted);
        }

        .responsive-table td[colspan] {
            color: var(--muted);
        }
    }
}
