:root {
  --ink: #172019;
  --muted: #68736b;
  --line: #d8dfd9;
  --panel: #ffffff;
  --surface: #f3f6f3;
  --accent: #245c43;
  --accent-dark: #19452f;
  --danger: #9c342c;
  --blue: #285f85;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--surface);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.app {
  width: min(1400px, calc(100% - 16px));
  margin: 0 auto;
  padding: 10px 0 22px;
}

.header,
.section-head,
.session-actions,
.title-row {
  display: flex;
  align-items: center;
}

.header {
  justify-content: space-between;
  margin-bottom: 8px;
}

.title-row,
.session-actions {
  gap: 8px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 1.42rem;
}

h2 {
  font-size: 0.94rem;
}

.version {
  color: var(--muted);
  font-size: 0.66rem;
  font-weight: 800;
}

.session-email {
  max-width: 260px;
  overflow: hidden;
  color: var(--muted);
  font-size: 0.66rem;
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.auth-card,
.toolbar,
.inventory-panel,
.sales-panel,
.invoice-card {
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--panel);
  box-shadow: 0 5px 14px rgba(23, 32, 25, 0.05);
}

.auth-card {
  max-width: 390px;
  margin: 60px auto;
  padding: 16px;
}

.auth-card form {
  display: grid;
  gap: 9px;
}

label span,
.sale-total span {
  display: block;
  margin-bottom: 3px;
  color: var(--muted);
  font-size: 0.62rem;
  font-weight: 850;
  text-transform: uppercase;
}

input,
select {
  width: 100%;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfcfb;
  color: var(--ink);
  padding: 5px 8px;
}

input:focus,
select:focus {
  border-color: var(--accent);
  outline: 3px solid rgba(36, 92, 67, 0.15);
}

button {
  min-height: 32px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: white;
  color: var(--ink);
  padding: 5px 10px;
  font-weight: 750;
}

button:hover {
  border-color: #aeb9b0;
}

.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: white;
}

.primary:hover {
  border-color: var(--accent-dark);
  background: var(--accent-dark);
}

.danger-button {
  border-color: var(--danger);
  background: var(--danger);
  color: white;
}

.save-changes-button {
  border-color: var(--accent);
  background: white;
  color: var(--accent);
}

.pdf-button {
  border-color: #9fcde4;
  background: #cfeeff;
  color: #111;
}

.pdf-button:hover {
  border-color: #285f85;
}

.toolbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 8px;
  align-items: stretch;
  border: 0;
  background: transparent;
  box-shadow: none;
  padding: 0;
  margin-bottom: 8px;
}

#appContent {
  display: flex;
  flex-direction: column;
}

.app-tabs {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px;
  margin-bottom: 8px;
}

.tab-button {
  min-height: 30px;
  border-color: #cfd8d1;
  background: #f8faf8;
  color: var(--muted);
  overflow: hidden;
  padding: 4px 6px;
  font-size: clamp(0.58rem, 2.8vw, 0.72rem);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tab-button.active {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.tab-panel {
  display: grid;
  gap: 8px;
}

.workspace {
  display: contents;
}

.invoice-card {
  display: grid;
  gap: 6px;
  min-width: 0;
  padding: 8px;
}

.invoice-top {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 1px;
  overflow: hidden;
  border: 1px solid var(--line);
}

.invoice-top div {
  min-width: 0;
  background: #fafcf9;
  padding: 3px 5px;
}

.invoice-top label {
  min-width: 0;
  background: #fafcf9;
  padding: 3px 5px;
}

.invoice-top .wide-field {
  grid-column: span 2;
}

.invoice-card span {
  display: block;
  margin-bottom: 2px;
  color: var(--muted);
  font-size: 0.55rem;
  font-weight: 850;
  text-transform: uppercase;
}

.invoice-card strong {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.78rem;
}

.invoice-card input {
  min-height: 27px;
  padding: 3px 6px;
  font-size: 0.76rem;
}

.invoice-top input {
  min-height: 22px;
  padding: 2px 5px;
  font-size: 0.7rem;
}

.invoice-top span {
  margin-bottom: 1px;
  font-size: 0.5rem;
}

.invoice-item {
  min-height: 28px;
  overflow: hidden;
  border: 1px solid var(--line);
  background: #fbfcfb;
  color: var(--muted);
  padding: 5px 7px;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.72rem;
}

.invoice-lines {
  display: grid;
  gap: 4px;
}

.invoice-line {
  display: grid;
  grid-template-columns: minmax(150px, 1.2fr) 74px 86px 82px 72px;
  gap: 5px;
  align-items: center;
  border: 1px solid var(--line);
  background: #fff;
  padding: 5px;
}

.invoice-line:nth-child(even) {
  background: #f8faf8;
}

.invoice-line-product {
  min-width: 0;
}

.invoice-line-product strong,
.invoice-line-product span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.invoice-line-product strong {
  font-size: 0.74rem;
}

.invoice-line-product span {
  color: var(--muted);
  font-size: 0.62rem;
}

.invoice-line-total {
  overflow: hidden;
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.74rem;
}

.invoice-line-remove {
  min-height: 27px;
  border-color: #e1b7b3;
  color: var(--danger);
  padding: 2px 5px;
  font-size: 0.64rem;
}

.invoice-actions {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) 90px 180px;
  gap: 8px;
  align-items: end;
}

.invoice-edit-actions {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.invoice-edit-actions button {
  width: 100%;
}

.invoice-grand-total {
  min-width: 0;
  border: 1px solid var(--line);
  background: #fafcf9;
  padding: 5px 7px;
}

.invoice-grand-total strong {
  font-size: 1rem;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.inventory-panel,
.sales-panel {
  padding: 9px;
}

.section-head {
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 7px;
}

.section-head > span {
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 750;
}

.inventory-list {
  display: grid;
  gap: 4px;
}

.inventory-columns,
.inventory-row {
  display: grid;
  grid-template-columns: minmax(145px, 1.35fr) 64px minmax(120px, 1fr) 92px 88px 66px;
  gap: 6px;
}

.inventory-columns {
  padding: 0 6px 4px;
  color: var(--muted);
  font-size: 0.58rem;
  font-weight: 850;
  text-transform: uppercase;
}

.inventory-columns > span {
  align-self: center;
}

.sort-button {
  display: flex;
  min-height: 22px;
  align-items: center;
  gap: 4px;
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 0;
  font-size: inherit;
  font-weight: inherit;
  text-align: left;
  text-transform: uppercase;
}

.sort-button span {
  color: var(--accent);
  font-size: 0.52rem;
}

.sort-button.active {
  color: var(--ink);
}

.sort-button:hover {
  border: 0;
  color: var(--accent-dark);
}

.table-sort-button {
  display: flex;
  width: 100%;
  min-height: 18px;
  align-items: center;
  gap: 3px;
  border: 0;
  background: transparent;
  color: inherit;
  padding: 0;
  font-size: inherit;
  font-weight: inherit;
  text-align: left;
  text-transform: uppercase;
}

.table-sort-button span {
  color: var(--accent);
  font-size: 0.5rem;
}

.table-sort-button.active {
  color: var(--ink);
}

.table-sort-button:hover {
  border: 0;
  color: var(--accent-dark);
}

.inventory-row {
  align-items: center;
  min-height: 43px;
  border: 1px solid var(--line);
  background: #fff;
  padding: 5px 6px;
}

.inventory-row:nth-child(even) {
  background: #f7f9f7;
}

.inventory-row strong,
.inventory-row span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.inventory-row strong {
  font-size: 0.78rem;
}

.inventory-row span {
  color: var(--muted);
  font-size: 0.7rem;
}

.inventory-row .quantity {
  color: var(--ink);
  font-weight: 800;
}

.sell-button {
  min-height: 29px;
  padding: 3px 7px;
  font-size: 0.7rem;
}

.danger-button {
  border-color: var(--danger);
  background: var(--danger);
  color: white;
}

.modal-open {
  overflow: hidden;
}

.sale-overlay {
  position: fixed;
  z-index: 100;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-y: auto;
  background: rgba(23, 32, 25, 0.5);
  padding: 12px;
}

.sale-overlay[hidden] {
  display: none;
}

.sale-card {
  display: grid;
  width: min(620px, 100%);
  max-height: calc(100vh - 24px);
  gap: 9px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--panel);
  box-shadow: 0 18px 48px rgba(23, 32, 25, 0.24);
  padding: 11px;
}

.invoice-edit-card {
  width: min(980px, 100%);
}

.close-button {
  width: 30px;
  min-height: 30px;
  padding: 0;
  font-size: 1.25rem;
  line-height: 1;
}

.sale-quantity-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.payment-grid {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) minmax(120px, 1fr);
  gap: 6px;
}

.invoice-edit-summary {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
}

.invoice-edit-summary div {
  border: 1px solid var(--line);
  background: #f8fbf8;
  padding: 6px 7px;
}

.invoice-edit-summary strong {
  display: block;
  font-size: 0.82rem;
}

.invoice-edit-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 6px;
}

.invoice-edit-grid .wide-field {
  grid-column: span 2;
}

.edit-table-wrap {
  overflow-x: auto;
}

.invoice-edit-table {
  min-width: 760px;
}

.invoice-edit-table td:first-child strong,
.invoice-edit-table td:first-child span {
  display: block;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.invoice-edit-table td:first-child span {
  color: var(--muted);
  font-size: 0.62rem;
}

.invoice-edit-table input {
  width: 100%;
  min-height: 26px;
  padding: 3px 5px;
}

.payment-history {
  display: grid;
  gap: 3px;
  max-height: 82px;
  overflow-y: auto;
  border: 1px solid var(--line);
  background: #fbfcfb;
  padding: 5px;
}

.payment-history div {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  border-bottom: 1px solid #edf1ee;
  padding: 2px 0;
  font-size: 0.7rem;
}

.payment-history div:last-child {
  border-bottom: 0;
}

.selected-lot {
  min-height: 55px;
  border: 1px solid var(--line);
  border-left: 4px solid var(--accent);
  background: #f8fbf8;
  padding: 7px 8px;
}

.selected-lot strong,
.selected-lot span {
  display: block;
}

.selected-lot strong {
  font-size: 0.82rem;
}

.selected-lot span {
  margin-top: 3px;
  color: var(--muted);
  font-size: 0.7rem;
}

small {
  display: block;
  margin-top: 3px;
  color: var(--muted);
  font-size: 0.64rem;
}

.sale-total {
  display: flex;
  justify-content: space-between;
  align-items: end;
  border-top: 1px solid var(--line);
  padding-top: 8px;
}

.sale-total strong {
  font-size: 1.24rem;
}

.sale-button {
  min-height: 42px;
}

.text-button {
  min-height: 24px;
  border: 0;
  color: var(--blue);
  padding: 0 3px;
  font-size: 0.7rem;
}

.status,
.empty {
  color: var(--muted);
  font-size: 0.7rem;
}

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

.status.saved {
  color: var(--accent);
}

.sales-panel {
  margin-top: 8px;
}

.table-search {
  display: grid;
  margin: 6px 0;
}

.table-search input {
  min-height: 28px;
  padding: 3px 7px;
  font-size: 0.72rem;
}

.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  min-width: 760px;
  border-collapse: collapse;
  table-layout: fixed;
}

th,
td {
  border: 1px solid var(--line);
  padding: 3px 5px;
  overflow: hidden;
  text-align: left;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.66rem;
  line-height: 1.15;
}

th {
  background: #eef2ee;
  color: #4e5a51;
  font-size: 0.58rem;
  text-transform: uppercase;
}

tbody tr:nth-child(even) {
  background: #f8faf8;
}

.invoice-action-column,
.invoice-action-cell {
  width: 34px;
  text-align: center;
}

.icon-button {
  width: 24px;
  min-height: 24px;
  border-color: #cfd8d1;
  color: var(--blue);
  padding: 0;
  font-size: 0.72rem;
  line-height: 1;
}

.icon-button:hover {
  border-color: var(--blue);
}

.empty {
  padding: 12px 4px;
  text-align: center;
}

[hidden] {
  display: none !important;
}

@media (max-width: 880px) {
  .toolbar {
    grid-template-columns: 1fr;
  }

  .invoice-top {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .invoice-line {
    grid-template-columns: minmax(150px, 1fr) 70px 80px 78px 68px;
  }

  .invoice-edit-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .inventory-row {
    grid-template-columns: minmax(125px, 1.3fr) 52px minmax(100px, 1fr) 86px 76px 66px;
  }

  .inventory-columns {
    grid-template-columns: minmax(125px, 1.3fr) 52px minmax(100px, 1fr) 86px 76px 66px;
  }
}

@media (max-width: 560px) {
  .app {
    width: min(100% - 10px, 1400px);
  }

  .header {
    align-items: flex-start;
  }

  .inventory-columns,
  .inventory-row {
    grid-template-columns: minmax(105px, 1fr) 42px 74px 60px 66px;
  }

  .inventory-columns .loose,
  .inventory-row .loose {
    display: none;
  }

  .sale-quantity-grid {
    grid-template-columns: 1fr;
  }

  .payment-grid {
    grid-template-columns: 1fr;
  }

  .invoice-top {
    grid-template-columns: 1fr;
  }

  .invoice-line {
    grid-template-columns: 1fr 68px 76px;
  }

  .invoice-edit-summary,
  .invoice-edit-grid {
    grid-template-columns: 1fr 1fr;
  }

  .invoice-line-total {
    text-align: left;
  }

  .invoice-line-remove {
    grid-column: span 3;
  }

  .invoice-actions {
    grid-template-columns: 1fr;
  }
}
