/* Common styles for all pages */
:root {
  color-scheme: light dark;
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-alt: #f5f7fa;
  --text: #111827;
  --muted: #6b7280;
  --border: #d1d5db;
  --hover: #eaf4ff;
  --sticky-separator: #d1d5db;
  --dirty-bg: #fff7ed;
  --dirty-ring: #f59e0b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141a;
    --surface: #161d24;
    --surface-alt: #1d2630;
    --text: #e5e7eb;
    --muted: #9aa4b2;
    --border: #334155;
    --hover: #243447;
    --sticky-separator: #475569;
    --dirty-bg: #3a2d1b;
    --dirty-ring: #d97706;
    --primary: #3b82f6;
    --primary-hover: #60a5fa;
  }
}

body {
  font-family: system-ui, Arial, sans-serif;
  margin: 2rem;
  background: var(--bg);
  color: var(--text);
}

label {
  display:block;
  font-weight: 600;
  padding: .3rem;
}

input[type="text"] {
  width:100%;
  padding:0.4rem;
  margin-bottom:0.7rem;
}

h1 {
  font-size: 1.2rem;
  margin: 0;
}

.app-menu {
  margin-bottom: .9rem;
}

.app-menu ul {
  list-style: none;
  display: flex;
  gap: .5rem;
  flex-wrap: wrap;
  margin: 0;
  padding: 0;
}

.app-menu .menu-row + .menu-row {
  margin-top: .5rem;
}

.app-menu a {
  display: inline-block;
  padding: .3rem .55rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  text-decoration: none;
  color: var(--text);
  background: var(--surface);
}

.app-menu a:hover {
  background: var(--surface-alt);
}

.app-menu a[aria-current="page"] {
  background: var(--hover);
  font-weight: 600;
}

.page-actions {
  display: flex;
  align-items: center;
  gap: .5rem;
  justify-content: flex-end;
  flex-wrap: wrap;
  margin: .75rem 0;
}

.current-rates-actions {
  justify-content: space-between;
}

.current-rates-filter-controls {
  display: flex;
  align-items: center;
  gap: .5rem;
  flex-wrap: wrap;
}

.current-rates-filter-controls select {
  min-width: 90px;
}

#currentRatesBeforeMonthSelect {
  min-width: 120px;
}

.vendor-filter-select {
  min-width: 240px;
}

.brand-create-form {
  align-items: flex-end;
  justify-content: flex-start;
}

.brand-create-form label {
  padding-left: 0;
}

.brand-create-form input[type="text"],
.brand-create-form select {
  box-sizing: border-box;
  width: auto;
  min-width: 220px;
  margin-bottom: 0;
}

.brands-table {
  min-width: 760px;
}

.brands-table td {
  white-space: nowrap;
}

.brands-table td:nth-child(2) {
  min-width: 220px;
}

.row-actions form {
  margin: 0;
}

.month-filter-select {
  min-width: 130px;
}

.product-list-actions form {
  margin: 0;
}

.flash-message {
  margin: .75rem 0;
  padding: .55rem .7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-alt);
}

.error-message {
  border-color: #b91c1c;
  color: #b91c1c;
}

.product-form {
  margin-top: .75rem;
}

.product-form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: .75rem 1rem;
  align-items: start;
}

.product-form-field label {
  padding-left: 0;
}

.product-form-field input,
.product-form-field select,
.product-form-field textarea {
  box-sizing: border-box;
  min-width: 0;
  margin-bottom: 0;
  width: 100%;
}

.product-form-field input[type="checkbox"] {
  width: auto;
  min-width: 0;
  margin: .45rem 0 0;
  accent-color: var(--primary);
}

.product-form-field textarea {
  resize: vertical;
}

.product-form-actions {
  display: flex;
  gap: .5rem;
  margin-top: 1rem;
}

.product-form-actions form {
  margin: 0;
}

.primary-action {
  background: var(--primary);
  border-color: var(--primary);
  color: #ffffff;
}

.primary-action:hover {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}

.primary-action:disabled {
  cursor: progress;
  opacity: .7;
}

/* Table wrapper styles */
.wrap {
  max-width: 100%;
  max-height: 70vh;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}

/* Table styles */
table {
  border-collapse: collapse;
  min-width: 900px;
  width: 100%;
  table-layout: auto;
}

th, td {
  border: 1px solid var(--border);
  padding: .4rem .6rem;
  text-align: left;
  background: var(--surface);
}

/* Sticky header */
thead th {
  position: sticky;
  top: 0;
  z-index: 3;
  background: var(--surface);
}

/* Right-aligned cells for numeric data */
th.numeric, td.numeric,
td.qty {
  text-align: right;
}

/* Monthly table specific styles */
.monthly-table th,
.monthly-table td {
  text-align: right;
}

.monthly-table th:nth-child(1),
.monthly-table td:nth-child(1),
.monthly-table th:nth-child(2),
.monthly-table td:nth-child(2) {
  text-align: left;
  white-space: nowrap;
}

/* Sticky columns */
.sticky-col-1,
.sticky-col-2 {
  position: sticky;
  z-index: 2;
  background: var(--surface);
}

.sticky-col-1 {
  left: 0;
  box-shadow: 2px 0 0 var(--sticky-separator);
}

.sticky-col-2 {
  box-shadow: 2px 0 0 var(--sticky-separator);
}

thead .sticky-col-1,
thead .sticky-col-2 {
  z-index: 4;
  background: var(--surface);
}

/* Zebra striping */
tbody tr:nth-child(even) td {
  background: var(--surface-alt);
}

tbody tr:nth-child(even) .sticky-col-1,
tbody tr:nth-child(even) .sticky-col-2 {
  background: var(--surface-alt);
}

.product-sheet-wrap {
  max-height: calc(100dvh - 18rem);
}

@supports selector(body:has(#productsTable)) {
  html:has(#productsTable),
  html:has(#employeeDataTable),
  html:has(#categoryDataTable) {
    height: 100%;
    overflow: hidden;
  }

  body:has(#productsTable),
  body:has(#employeeDataTable),
  body:has(#categoryDataTable) {
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    height: calc(100dvh - 4rem);
    overflow: hidden;
  }

  body:has(#productsTable) main,
  body:has(#employeeDataTable) main,
  body:has(#categoryDataTable) main {
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
  }

  body:has(#productsTable) .product-sheet-wrap,
  body:has(#employeeDataTable) .product-sheet-wrap,
  body:has(#categoryDataTable) .product-sheet-wrap {
    flex: 1 1 auto;
    min-height: 0;
    max-height: none;
  }
}

.product-sheet-table {
  table-layout: fixed;
  width: auto;
  min-width: max-content;
}

.product-sheet-table th,
.product-sheet-table td {
  min-width: 170px;
  width: 170px;
}

.product-sheet-table th {
  padding: .45rem .55rem;
  white-space: nowrap;
}

.product-sheet-table td {
  padding: 0;
  vertical-align: top;
}

.product-sheet-table .sticky-col {
  position: sticky;
  z-index: 2;
  background: var(--surface);
}

.product-sheet-table thead .sticky-col {
  z-index: 5;
}

.product-sheet-table .sticky-col-name {
  left: 0;
  min-width: 260px;
  width: 260px;
  max-width: 260px;
}

.product-sheet-table .sticky-col-pnk {
  left: 260px;
  min-width: 150px;
  width: 150px;
  max-width: 150px;
}

.product-sheet-table .sticky-col-product-code {
  left: 410px;
  min-width: 180px;
  width: 180px;
  max-width: 180px;
  box-shadow: 2px 0 0 var(--sticky-separator);
}

.employee-sheet-table .sticky-col-employee-name {
  left: 0;
  min-width: 260px;
  width: 260px;
  max-width: 260px;
}

.employee-sheet-table .sticky-col-employee-company {
  left: 260px;
  min-width: 180px;
  width: 180px;
  max-width: 180px;
}

.employee-sheet-table .sticky-col-employee-department {
  left: 440px;
  min-width: 220px;
  width: 220px;
  max-width: 220px;
  box-shadow: 2px 0 0 var(--sticky-separator);
}

.category-sheet-table .sticky-col-category-subcategory-country {
  left: 0;
  min-width: 250px;
  width: 250px;
  max-width: 250px;
}

.category-sheet-table .sticky-col-category-big-category {
  left: 250px;
  min-width: 170px;
  width: 170px;
  max-width: 170px;
}

.category-sheet-table .sticky-col-category-division {
  left: 420px;
  min-width: 180px;
  width: 180px;
  max-width: 180px;
  box-shadow: 2px 0 0 var(--sticky-separator);
}

.product-sheet-table tbody tr:nth-child(even) .sticky-col {
  background: var(--surface-alt);
}

.product-sheet-row.is-dirty .sticky-col,
.employee-sheet-row.is-dirty .sticky-col,
.category-sheet-row.is-dirty .sticky-col,
.product-sheet-table tbody tr:nth-child(even).is-dirty .sticky-col {
  background: var(--dirty-bg);
}

.product-sheet-cell.is-dirty {
  background: var(--dirty-bg);
  box-shadow: inset 0 0 0 2px var(--dirty-ring);
}

.product-sheet-input,
.product-sheet-select,
.product-sheet-textarea {
  box-sizing: border-box;
  width: 100%;
  min-width: 0;
  margin: 0;
  border: 0;
  border-radius: 0;
  padding: .38rem .45rem;
  background: transparent;
  color: var(--text);
  font: inherit;
}

.product-sheet-checkbox {
  display: block;
  width: 1rem;
  height: 1rem;
  margin: .55rem auto;
  accent-color: var(--primary);
}

.product-sheet-select {
  min-width: 0;
}

.product-sheet-select-boolean {
  text-align: center;
}

.product-sheet-textarea {
  min-height: 2.15rem;
  resize: vertical;
}

.product-sheet-input[readonly] {
  color: var(--muted);
}

.product-sheet-input:focus,
.product-sheet-select:focus,
.product-sheet-textarea:focus {
  outline: 2px solid var(--primary);
  outline-offset: -2px;
}

.product-review-dialog {
  width: min(980px, calc(100vw - 2rem));
  max-height: calc(100vh - 2rem);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0;
  background: var(--surface);
  color: var(--text);
}

.product-review-dialog::backdrop {
  background: rgb(17 24 39 / 55%);
}

.product-review-content {
  display: grid;
  gap: .75rem;
  padding: 1rem;
}

.product-review-content h2 {
  margin: 0;
  font-size: 1.05rem;
}

.product-review-changes-wrap {
  max-height: 55vh;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.product-review-table {
  min-width: 760px;
}

.product-review-table td {
  max-width: 280px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  vertical-align: top;
}

.product-review-actions {
  display: flex;
  justify-content: flex-end;
  gap: .5rem;
  flex-wrap: wrap;
}

/* Clickable cells */
td[data-clickable="true"] {
  cursor: pointer;
}

td[data-clickable="true"]:hover {
  background: var(--hover);
}
/* Details page header */
header {
  display: flex;
  align-items: baseline;
  gap: .75rem;
  flex-wrap: wrap;
}

.muted {
  color: var(--muted);
  font-size: .9rem;
}

.toolbar {
  display: flex;
  gap: .5rem;
  margin-left: auto;
}

/* Buttons */
button {
  padding: .35rem .6rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

#log {
  margin-top:1rem;
  padding:0.7rem;
  background:#f3f3f3;
  min-height:2rem;
  white-space:pre-wrap;
}

button:hover {
  background: var(--surface-alt);
}

/* Status messages */
#status {
  margin-top: .5rem;
}

/* Details page specific */
.details-page {
  margin: 1rem;
}

.details-page .wrap {
  margin-top: 1rem;
  max-height: 75vh;
}

.details-page th,
.details-page td {
  padding: .45rem .6rem;
}

/* Chart pages */
.row {
  display: grid;
  gap: 1rem;
  max-width: 900px;
}

select {
  min-width: 320px;
  padding: .3rem;
}

.card {
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  padding: 1rem;
}

canvas {
  width: 100%;
  height: 30vh;
}

/* Code elements */
code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}
