*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --gray-100: #f4f4f4;
  --gray-200: #e8e8e8;
  --gray-300: #ccc;
  --gray-600: #555;
  --gray-800: #222;
  --primary: #1a1a1a;
  --accent: #2c5282;
  --danger: #c53030;
  --radius: 8px;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: 'Libre Franklin', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: #eef1f5;
  color: var(--gray-800);
  line-height: 1.5;
  min-height: 100vh;
}

.hidden,
[hidden] {
  display: none !important;
}

/* ── Login ── */
.login-card {
  max-width: 420px;
  margin: 4rem auto;
  padding: 2.5rem 2rem;
  background: #fff;
  border-radius: 12px;
  box-shadow: var(--shadow);
  text-align: center;
}

.login-logo {
  margin-bottom: 1.5rem;
}

.brand-logo {
  display: block;
  max-width: 220px;
  width: 100%;
  height: auto;
  margin: 0 auto;
}

.brand-logo-invoice {
  max-width: 180px;
}

.login-card h1 {
  font-size: 1.35rem;
  font-weight: 700;
  margin-bottom: 0.5rem;
}

.login-subtitle {
  color: var(--gray-600);
  font-size: 0.9rem;
  margin-bottom: 1.5rem;
}

#login-form label {
  display: block;
  text-align: left;
  font-weight: 600;
  font-size: 0.85rem;
  margin-bottom: 0.35rem;
}

#login-form input {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 2px solid var(--gray-200);
  border-radius: var(--radius);
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  margin-bottom: 0.5rem;
}

#login-form input:focus {
  outline: none;
  border-color: var(--accent);
}

.error-msg {
  color: var(--danger);
  font-size: 0.85rem;
  text-align: left;
  margin-bottom: 0.75rem;
}

.login-hint {
  margin-top: 1.25rem;
  font-size: 0.8rem;
  color: var(--gray-600);
}

.form-footer-note {
  margin-top: 1.5rem;
  padding-top: 1rem;
  text-align: center;
  font-size: 0.75rem;
  color: var(--gray-600);
  line-height: 1.4;
}

.form-footer-note a {
  color: var(--gray-600);
  text-decoration: none;
}

.form-footer-note a:hover {
  text-decoration: underline;
}

.login-card .form-footer-note {
  margin-top: 1rem;
  padding-top: 0;
  border-top: none;
}


/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.6rem 1.2rem;
  border: none;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  white-space: nowrap;
}

.btn:active {
  transform: scale(0.98);
}

.btn-primary {
  background: var(--primary);
  color: #fff;
  width: 100%;
}

.btn-primary:hover {
  background: #333;
}

.btn-secondary {
  background: #fff;
  color: var(--primary);
  border: 1.5px solid var(--gray-300);
}

.btn-secondary:hover {
  background: var(--gray-100);
}

.btn-ghost {
  background: transparent;
  color: var(--gray-600);
  border: 1.5px solid var(--gray-300);
}

.btn-ghost:hover {
  background: var(--gray-100);
}

.btn-remove {
  background: none;
  border: none;
  color: var(--danger);
  font-size: 1.25rem;
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  line-height: 1;
}

/* ── Toolbar ── */
.toolbar {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  background: #fff;
  border-bottom: 1px solid var(--gray-200);
  box-shadow: var(--shadow);
}

.toolbar-right {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.driver-badge {
  font-weight: 700;
  font-size: 0.9rem;
  padding: 0.35rem 0.75rem;
  background: var(--gray-100);
  border-radius: 20px;
}

/* ── Form ── */
.form-container {
  max-width: 960px;
  margin: 1rem auto;
  padding: 0 1rem 2rem;
}

.form-section {
  background: #fff;
  border-radius: 12px;
  padding: 1.25rem;
  margin-bottom: 1rem;
  box-shadow: var(--shadow);
}

.form-section h2 {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 1rem;
  padding-bottom: 0.5rem;
  border-bottom: 2px solid var(--gray-100);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
}

.field-full {
  grid-column: 1 / -1;
}

.field label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--gray-600);
  margin-bottom: 0.3rem;
}

.field-hint {
  display: block;
  font-size: 0.7rem;
  color: var(--gray-600);
  margin-top: 0.2rem;
}

.field input,
.field select {
  width: 100%;
  padding: 0.55rem 0.75rem;
  border: 1.5px solid var(--gray-200);
  border-radius: 6px;
  font-family: inherit;
  font-size: 0.95rem;
}

.field input:focus,
.field select:focus {
  outline: none;
  border-color: var(--accent);
}

.field input[readonly] {
  background: var(--gray-100);
}

.address-display {
  padding: 0.75rem;
  background: var(--gray-100);
  border-radius: 6px;
  font-size: 0.9rem;
  min-height: 3rem;
}

.total-display {
  font-size: 1.25rem;
  font-weight: 700;
  padding: 0.5rem 0;
}

.total-final {
  font-size: 1.5rem;
  color: var(--accent);
}

.totals-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 1rem;
}

.finish-actions {
  margin-top: 1.25rem;
  padding-top: 1rem;
  border-top: 1px solid var(--gray-200);
  text-align: center;
}

.btn-finish {
  width: 100%;
  max-width: 320px;
  padding: 0.85rem 1.5rem;
  font-size: 1rem;
}

.finished-badge {
  margin-top: 0.75rem;
  font-size: 0.85rem;
  color: #2f6b2f;
  font-weight: 600;
}

.form-locked {
  opacity: 0.92;
}

.form-locked input:not([readonly]),
.form-locked select,
.form-locked textarea {
  background: var(--gray-100);
  pointer-events: none;
}

.form-locked .btn-remove {
  display: none;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* ── Line Items ── */
.line-items-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.line-items-header,
.line-item-card {
  display: grid;
  grid-template-columns: 88px minmax(140px, 2fr) 64px 56px 88px 80px 36px;
  gap: 0.5rem;
  align-items: center;
  min-width: 640px;
}

.line-items-header {
  padding: 0.5rem;
  background: var(--gray-100);
  font-weight: 600;
  font-size: 0.85rem;
  border-radius: 6px 6px 0 0;
}

.line-items-list {
  border: 1px solid var(--gray-200);
  border-top: none;
  border-radius: 0 0 6px 6px;
}

.line-item-card {
  padding: 0.5rem;
  border-bottom: 1px solid var(--gray-200);
}

.line-item-card:last-child {
  border-bottom: none;
}

.line-field label {
  display: none;
}

.line-field input,
.line-field select {
  width: 100%;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--gray-200);
  border-radius: 4px;
  font-family: inherit;
  font-size: 0.85rem;
}

.line-field-desc .desc-input[readonly] {
  background: var(--gray-100);
  color: var(--gray-800);
}

.item-search-wrap {
  position: relative;
}

.item-search-results {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 50;
  margin: 0.25rem 0 0;
  padding: 0;
  list-style: none;
  background: #fff;
  border: 1px solid var(--gray-200);
  border-radius: 6px;
  box-shadow: var(--shadow);
  max-height: 200px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.item-search-results li {
  padding: 0.65rem 0.75rem;
  font-size: 0.85rem;
  border-bottom: 1px solid var(--gray-100);
  cursor: pointer;
}

.item-search-results li:last-child {
  border-bottom: none;
}

.item-search-results li:active,
.item-search-results li:hover {
  background: var(--gray-100);
}

.item-search-results li strong {
  color: var(--accent);
  margin-right: 0.35rem;
}

.shop-result-addr {
  display: block;
  font-size: 0.75rem;
  color: var(--gray-600);
  margin-top: 0.15rem;
}

.line-field-amount .amount-cell {
  display: block;
  font-weight: 600;
  text-align: right;
  padding: 0.4rem 0;
}

.line-item-card .btn-remove {
  justify-self: end;
}

.btn-add-mobile {
  margin-top: 0.75rem;
  width: 100%;
}

.returns-section h2 {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.5rem;
}

.section-hint {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--gray-600);
}

.return-row {
  background: #fff8f0;
}

.returns-credit-display {
  color: #c05621;
}

.inv-returns-block {
  margin-bottom: 0.75rem;
}

.inv-returns-title {
  font-weight: 800;
  font-size: 11px;
  letter-spacing: 0.06em;
  margin: 0.5rem 0 0.25rem;
}

.inv-returns-table th {
  background: #f0e6dc;
}

@media (max-width: 640px) {
  .line-items-wrapper {
    overflow: visible;
  }

  .line-items-header {
    display: none;
  }

  .line-items-list {
    border: none;
    min-width: unset;
  }

  .line-item-card {
    display: block;
    min-width: unset;
    background: var(--gray-100);
    border-radius: 8px;
    padding: 0.75rem;
    margin-bottom: 0.75rem;
    border: none;
    position: relative;
  }

  .return-row {
    background: #fff8f0;
  }

  .line-field {
    margin-bottom: 0.6rem;
  }

  .line-field:last-of-type {
    margin-bottom: 0;
  }

  .line-field label {
    display: block;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--gray-600);
    margin-bottom: 0.25rem;
  }

  .line-field input,
  .line-field select {
    font-size: 16px;
    padding: 0.55rem 0.65rem;
  }

  .line-field-amount .amount-cell {
    text-align: left;
    font-size: 1rem;
    padding: 0.25rem 0;
  }

  .line-item-card .btn-remove {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    background: none;
    border: none;
    color: var(--danger);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0.25rem 0.5rem;
    line-height: 1;
  }

  .toolbar {
    flex-direction: column;
    align-items: stretch;
  }

  .toolbar-right {
    justify-content: stretch;
  }

  .toolbar-right .btn {
    flex: 1;
    min-width: 0;
  }

  #btn-add-item {
    display: none;
  }
}

@media (min-width: 641px) {
  #btn-add-item-bottom,
  .btn-add-mobile {
    display: none;
  }
}

/* ── Printable Invoice ── */
.invoice-print {
  max-width: 900px;
  margin: 1rem auto 2rem;
  padding: 1.5rem;
  background: #fff;
  font-size: 11px;
  line-height: 1.4;
}

.inv-header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: start;
  gap: 1rem;
  margin-bottom: 1rem;
}

.inv-company {
  font-size: 10px;
}

.inv-company strong {
  font-size: 12px;
}

.inv-logo-center {
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}

.inv-meta {
  text-align: right;
}

.inv-title {
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: 0.05em;
  margin-bottom: 0.5rem;
}

.inv-meta-row {
  display: flex;
  justify-content: flex-end;
  gap: 1rem;
  font-size: 11px;
}

.inv-meta-row span:first-child {
  font-weight: 600;
}

.inv-addresses {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0;
  margin-bottom: 0.75rem;
}

.inv-addr-box {
  border: 1px solid var(--gray-800);
}

.inv-addr-label {
  background: var(--gray-200);
  font-weight: 700;
  padding: 0.25rem 0.5rem;
  font-size: 10px;
  border-bottom: 1px solid var(--gray-800);
}

.inv-addr-box > div:last-child {
  padding: 0.4rem 0.5rem;
  min-height: 2.5rem;
  font-size: 10px;
}

.inv-details-table,
.inv-items-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 0.75rem;
  font-size: 10px;
}

.inv-details-table th,
.inv-items-table th {
  background: var(--gray-200);
  border: 1px solid var(--gray-800);
  padding: 0.25rem 0.4rem;
  font-weight: 700;
  text-align: center;
}

.inv-details-table td,
.inv-items-table td {
  border: 1px solid var(--gray-800);
  padding: 0.25rem 0.4rem;
  text-align: center;
}

.inv-items-table .inv-subhead th {
  font-weight: 600;
  font-size: 9px;
}

.inv-items-table td:nth-child(4) {
  text-align: left;
}

.inv-items-table td:nth-child(5),
.inv-items-table td:nth-child(6),
.inv-items-table th:nth-child(5),
.inv-items-table th:nth-child(6) {
  text-align: right;
}

.inv-footer {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 2rem;
  margin-top: 0.5rem;
}

.inv-summary-left {
  border-collapse: collapse;
  font-size: 10px;
}

.inv-summary-left th,
.inv-summary-left td {
  border: 1px solid var(--gray-800);
  padding: 0.25rem 0.75rem;
}

.inv-summary-left th {
  background: var(--gray-200);
  text-align: left;
}

.inv-summary-right {
  min-width: 260px;
}

.inv-total-row {
  display: flex;
  justify-content: space-between;
  padding: 0.2rem 0;
  font-size: 11px;
}

.inv-total-row.inv-grand {
  font-weight: 800;
  font-size: 12px;
  border-top: 2px solid var(--gray-800);
  margin-top: 0.25rem;
  padding-top: 0.35rem;
}

.inv-driver-line {
  margin-top: 1rem;
  font-size: 10px;
  color: var(--gray-600);
  border-top: 1px dashed var(--gray-300);
  padding-top: 0.5rem;
}

.inv-print-footer {
  margin-top: 0.5rem;
  font-size: 9px;
  color: #888;
  text-align: center;
  line-height: 1.4;
}

/* Preview mode */
.preview-bar {
  display: none;
  position: sticky;
  top: 0;
  z-index: 200;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  background: var(--accent);
  color: #fff;
  box-shadow: var(--shadow);
}

body.preview-mode .preview-bar {
  display: flex;
}

.preview-bar-title {
  font-weight: 700;
  font-size: 0.9rem;
}

.preview-bar-actions {
  display: flex;
  gap: 0.5rem;
  flex-shrink: 0;
}

.preview-bar .btn-secondary {
  background: rgba(255, 255, 255, 0.95);
  color: var(--accent);
  border: none;
}

.preview-bar .btn-primary {
  background: #fff;
  color: var(--primary);
}

body.preview-mode .no-print {
  display: none !important;
}

body.preview-mode .form-footer-note {
  display: none !important;
}

body.preview-mode .invoice-print {
  display: block !important;
  box-shadow: var(--shadow);
  border-radius: 4px;
}

@media (max-width: 640px) {
  .preview-bar {
    flex-direction: column;
    align-items: stretch;
  }

  .preview-bar-actions {
    width: 100%;
  }

  .preview-bar-actions .btn {
    flex: 1;
  }
}

/* ── Print ── */
@media print {
  body {
    background: #fff;
  }

  .no-print,
  #login-screen,
  .preview-bar {
    display: none !important;
  }

  #invoice-screen {
    display: block !important;
  }

  .invoice-print {
    display: block !important;
    margin: 0;
    padding: 0;
    max-width: none;
    box-shadow: none;
  }

  body.preview-mode #invoice-screen::before {
    display: none;
  }

  @page {
    margin: 0.5in;
    size: letter;
  }
}

@media (max-width: 640px) {
  .inv-header {
    grid-template-columns: 1fr;
    text-align: center;
  }

  .inv-meta {
    text-align: center;
  }

  .inv-meta-row {
    justify-content: center;
  }

  .inv-addresses {
    grid-template-columns: 1fr;
  }

  .inv-footer {
    flex-direction: column;
  }
}
