/* Stocktake Widget v2 — restyled */

:root {
    --bg: #f7f8fa;
    --surface: #ffffff;
    --border: #e3e6eb;
    --text: #1a2233;
    --text-muted: #6b7280;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --danger: #dc2626;
    --danger-bg: #fef2f2;
    --success: #16a34a;
    --row-hover: #f4f6fa;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 2px 8px rgba(0, 0, 0, 0.04);
}

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 14px;
    line-height: 1.4;
}

header {
    display: flex;
    align-items: center;
    gap: 24px;
    min-height: 88px; /* hold height even when draft PO controls appear/disappear */
    padding: 12px 24px 12px 168px; /* extra left padding to clear the overhanging logo */
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow);
    position: sticky;
    top: 0;
    z-index: 50;
}

.brand {
    position: absolute;
    left: 40px;
    top: 10px;
    display: block;
    line-height: 0;
    z-index: 60;
}

.brand img {
    height: 90px;
    width: auto;
    display: block;
    filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.12));
}

.controls {
    display: flex;
    gap: 16px;
    flex: 1;
    align-items: center;
}

.control {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 12px;
    color: var(--text-muted);
}

.control input {
    height: 32px;
    padding: 0 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface);
    color: var(--text);
    font-size: 14px;
    min-width: 180px;
}

.control input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.mode-tabs {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 3px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: #f3f4f6;
}

.mode-tab {
    height: 30px;
    padding: 0 12px;
    border: 1px solid transparent;
    border-radius: 6px;
    background: transparent;
    color: var(--text-muted);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
}

.mode-tab:hover {
    color: var(--text);
}

.mode-tab.active {
    background: var(--surface);
    border-color: var(--border);
    color: var(--text);
    box-shadow: var(--shadow);
}

.actions {
    display: flex;
    gap: 8px;
}


/* Per-tab toolbar — sits between the page header and the table. Currently
 * only the Add Purchase Order tab uses it (for PO number + Due Date inputs).
 * Left padding matches the page header so contents align with the navbar. */
.tab-toolbar {
    display: flex;
    align-items: flex-end;
    gap: 16px;
    padding: 12px 24px 12px 168px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

/* Button system — one base + colour variants + size variants.
 * Shape is consistent across the widget; colour denotes intent (primary
 * action / neutral / success). Size opts apply where space is tight. */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    height: 32px;
    padding: 0 16px;
    border: 1px solid transparent;
    border-radius: 6px;
    background: transparent;
    color: var(--text);
    font-size: 14px;
    font-weight: 500;
    line-height: 1;
    text-transform: none;
    letter-spacing: 0;
    cursor: pointer;
    transition: background 0.1s, border-color 0.1s, color 0.1s, box-shadow 0.1s;
}

.btn:disabled {
    cursor: not-allowed;
    opacity: 0.5;
}

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

.btn-primary:hover:not(:disabled) {
    background: var(--primary-hover);
    border-color: var(--primary-hover);
}

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

.btn-secondary:hover:not(:disabled) {
    border-color: var(--primary);
    color: var(--primary);
}

.btn-success {
    background: var(--surface);
    border-color: var(--success);
    color: var(--success);
}

.btn-success:hover:not(:disabled) {
    background: var(--success);
    color: #fff;
}

.btn-sm {
    height: 26px;
    padding: 0 10px;
    font-size: 12px;
}

.btn-block {
    width: 100%;
}

main {
    /* Flush against the page header — no gap means no strip for content to
     * peek through as it scrolls. */
    margin-top: 0;
    padding: 0 24px 24px;
}

.loading {
    padding: 32px;
    text-align: center;
    color: var(--text-muted);
}

/* Table */

#stocktake-table {
    width: 100% !important;
    border-collapse: separate !important;
    border-spacing: 0;
    /* No background / border / border-radius / overflow: the table reads as
     * part of the page. overflow:hidden also breaks position:sticky on thead. */
}


/* Use .dataTables_wrapper so the rule also catches the cloned scroll-head thead,
 * which DataTables renders in a separate <table> without the original id. */
.dataTables_wrapper thead th {
    background: #f9fafb;
    border-bottom: 1px solid var(--border);
    padding: 12px 12px !important;
    text-align: left;
    vertical-align: bottom;
    font-weight: 600;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
    /* Pin thead flush below page header (header is sticky at top:0, ~88px tall).
     * Logo overhangs into the thead's top-left corner; the Product Name column
     * header is blank because the logo would conceal it anyway. */
    position: sticky;
    top: 88px;
    z-index: 10;
}

#stocktake-table tbody td {
    padding: 8px 12px;
    border-top: 1px solid var(--border);
    vertical-align: middle;
}

#stocktake-table tbody tr:first-child td {
    border-top: none;
}

#stocktake-table tbody tr:hover {
    background: var(--row-hover);
}

#stocktake-table tbody td a {
    color: var(--text);
    text-decoration: none;
}

#stocktake-table tbody td a:hover {
    color: var(--primary);
    text-decoration: underline;
}

#stocktake-table input[type="number"] {
    width: 80px;
    height: 28px;
    padding: 0 8px;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 14px;
    text-align: right;
}

#stocktake-table input.transfer-qty {
    width: 90px; /* accommodate the leading sign */
}

.dataTables_wrapper thead .col-transfer .hint {
    display: block;
    font-weight: 500;
    font-size: 10px;
    text-transform: none;
    letter-spacing: 0;
    color: var(--text-muted);
    margin-top: 2px;
}

/* PO column styles live under .dataTables_wrapper so they apply to the visible
 * scroll-head clone as well as the original hidden thead. */
.dataTables_wrapper .col-po {
    min-width: 160px;
}

.dataTables_wrapper thead .col-po .po-header-row {
    display: flex;
    align-items: center;
    gap: 12px;
}

.dataTables_wrapper thead .col-po .po-heading {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
    text-transform: none;
}

.dataTables_wrapper thead .col-po .po-number {
    display: block;
    color: var(--text);
    font-size: 13px;
    font-weight: 600;
    line-height: 1.2;
}

.dataTables_wrapper thead .col-po .po-due-date {
    display: block;
    color: var(--text-muted);
    font-size: 11px;
    font-weight: 500;
    line-height: 1.2;
    letter-spacing: 0;
    text-transform: none;
}

/* Receive button positioning only — appearance comes from .btn .btn-success .btn-sm .btn-block */

#stocktake-table input[type="number"]:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.12);
}

#stocktake-table input[readonly] {
    background: #f3f4f6;
    color: var(--text-muted);
}

#stocktake-table input.invalid {
    border-color: var(--danger);
    background: var(--danger-bg);
}

.history-toggle {
    border: 0;
    background: transparent;
    color: var(--text);
    cursor: pointer;
    font-size: 20px;
    font-weight: 700;
    padding: 2px 8px;
    border-radius: 4px;
    line-height: 1;
    transition: background 0.1s, color 0.1s;
}

.crm-link,
#stocktake-table tbody td a.crm-link,
.history-table td a.crm-link {
    display: inline-block;
    padding: 1px 6px;
    margin-left: 2px;
    border-radius: 4px;
    color: var(--primary);
    font-size: 14px;
    font-weight: 700;
    line-height: 1;
    text-decoration: none;
    transition: background 0.1s, color 0.1s;
}

.crm-link:hover,
#stocktake-table tbody td a.crm-link:hover,
.history-table td a.crm-link:hover,
.history-toggle:hover {
    background: rgba(37, 99, 235, 0.12);
    color: var(--primary);
    text-decoration: none;
}

tr.history-open {
    background: var(--row-hover);
}

.history-cell {
    padding: 0 12px 12px 36px;
    background: #fafbfc;
    border-top: 1px solid var(--border);
    max-height: 240px;
    overflow-y: auto;
}

.history-table thead th {
    position: sticky;
    top: 0;
    background: #fafbfc;
}

.history-table {
    width: auto;
    border-collapse: collapse;
    font-size: 13px;
}

.history-table th,
.history-table td {
    padding: 4px 12px;
    border-bottom: 1px solid var(--border);
    text-align: left;
}

.history-table th {
    color: var(--text-muted);
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.history-table td.num,
.history-table th.num {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

.history-loading,
.history-empty,
.history-error {
    padding: 4px 0;
    color: var(--text-muted);
    font-size: 13px;
}

.history-error {
    color: var(--danger);
}

.inactive-tag {
    color: #9ca3af;
    font-size: 10px;
    font-style: italic;
    margin-left: 4px;
}

.field-warning {
    display: block;
    color: var(--danger);
    font-size: 11px;
    margin-top: 2px;
}

/* Live preview of stock changes (Transfer / Spot Check) */
.stock-value.dirty {
    text-decoration: line-through;
    color: var(--danger);
    opacity: 0.7;
}

.stock-preview {
    margin-left: 6px;
    color: var(--primary);
    font-weight: 600;
}

/* DataTables overrides */

.dataTables_wrapper .dataTables_filter,
.dataTables_wrapper .dataTables_info,
.dataTables_wrapper .dataTables_paginate {
    display: none;
}
