* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  color: #1a1a1a;
  background: #f5f5f5;
}

.topbar {
  background: #1e3a5f;
  color: #fff;
  padding: 0.75rem 1rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.brand {
  color: #fff;
  text-decoration: none;
  font-weight: 600;
  font-size: 1.1rem;
}

.brand-logos {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.brand-logo {
  height: 30px;
  width: auto;
  max-width: 110px;
  object-fit: contain;
  background: #fff;
  padding: 3px 6px;
  border-radius: 4px;
}

.user-area {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 1rem;
}

.current-user {
  font-size: 0.9rem;
  opacity: 0.9;
}

.account-link {
  color: #fff;
  font-size: 0.85rem;
  text-decoration: underline;
  text-underline-offset: 2px;
  opacity: 0.9;
}

.account-link:hover {
  opacity: 1;
}

.logout-btn {
  background: transparent;
  border: 1px solid rgba(255,255,255,0.4);
  color: #fff;
  padding: 0.3rem 0.7rem;
  border-radius: 4px;
  font-size: 0.85rem;
  cursor: pointer;
}

.logout-btn:hover {
  background: rgba(255,255,255,0.15);
}

.content {
  max-width: 900px;
  margin: 0 auto;
  padding: 1rem;
}

h1 {
  margin: 0 0 1rem;
  font-size: 1.5rem;
}

h2 {
  margin: 1.5rem 0 0.5rem;
  font-size: 1.1rem;
}

.filters {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 1rem;
  margin-bottom: 1rem;
  padding: 0.75rem;
  background: #fff;
  border-radius: 6px;
}

.filters label {
  font-size: 0.9rem;
}

.filters select {
  padding: 0.35rem 0.5rem;
  font-size: 0.9rem;
  border: 1px solid #ccc;
  border-radius: 4px;
}

.filters button {
  padding: 0.4rem 0.75rem;
  font-size: 0.9rem;
  background: #1e3a5f;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.order-cards {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.order-card-wrap {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
}

.order-card-wrap .order-card {
  flex: 1;
  min-width: 0;
}

.order-card-edit {
  flex-shrink: 0;
  font-size: 0.9rem;
  padding: 0.35rem 0.5rem;
  color: #1e3a5f;
  text-decoration: none;
  border: 1px solid #1e3a5f;
  border-radius: 4px;
}

.order-card-edit:hover {
  background: #1e3a5f;
  color: #fff;
}

.order-card {
  display: block;
  padding: 1rem;
  background: #fff;
  border-radius: 6px;
  text-decoration: none;
  color: inherit;
  border: 1px solid #e0e0e0;
  transition: border-color 0.15s;
}

.order-card:hover {
  border-color: #1e3a5f;
}

.order-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}

.oon {
  font-weight: 600;
  font-size: 1.05rem;
}

.status {
  font-size: 0.8rem;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  text-transform: uppercase;
}

.status-quote { background: #fde9c8; }
.status-draft { background: #e8e8e8; }
.status-pending { background: #fff3cd; }
.status-issued { background: #d4edda; }
.status-voidrequested { background: #ffeaa7; }
.status-void { background: #f8d7da; }
.status-closed { background: #d1ecf1; }

.order-card-body p {
  margin: 0.25rem 0;
  font-size: 0.9rem;
}

.order-card-body .purpose {
  color: #555;
}

.empty {
  color: #666;
  font-style: italic;
}

.detail-header {
  margin-bottom: 1rem;
}

.back {
  display: inline-block;
  margin-bottom: 0.5rem;
  color: #1e3a5f;
  text-decoration: none;
  font-size: 0.9rem;
}

.back:hover {
  text-decoration: underline;
}

.detail-header h1 {
  display: inline;
  margin-right: 0.5rem;
}

.order-meta {
  padding: 1rem;
  background: #fff;
  border-radius: 6px;
  margin-bottom: 1rem;
}

.order-meta p {
  margin: 0.35rem 0;
}

.line-items, .attachments, .audit-logs, .email-logs {
  padding: 1rem;
  background: #fff;
  border-radius: 6px;
  margin-bottom: 1rem;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

th, td {
  padding: 0.5rem;
  text-align: left;
  border-bottom: 1px solid #eee;
}

th {
  font-weight: 600;
}

.log-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.log-list li {
  padding: 0.35rem 0;
  border-bottom: 1px solid #eee;
  font-size: 0.9rem;
}

.error-message {
  font-size: 1.1rem;
  color: #c00;
}

.btn-primary {
  display: inline-block;
  padding: 0.5rem 1rem;
  background: #1e3a5f;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
  text-decoration: none;
}

.btn-primary:hover {
  background: #2a4a7a;
}

.btn-new-order {
  margin-bottom: 1rem;
}

.btn-cancel {
  display: inline-block;
  margin-left: 0.75rem;
  color: #666;
  text-decoration: none;
  font-size: 0.95rem;
}

.btn-cancel:hover {
  text-decoration: underline;
}

.form-errors {
  color: #c00;
  background: #fee;
  padding: 0.75rem 1rem;
  border-radius: 6px;
  margin-bottom: 1rem;
  list-style: disc inside;
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.35rem;
  font-weight: 500;
  font-size: 0.95rem;
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 0.5rem;
  font-size: 1rem;
  border: 1px solid #ccc;
  border-radius: 4px;
}

.line-items-section {
  margin: 1.5rem 0;
  padding: 1rem;
  background: #fff;
  border-radius: 6px;
}

.line-item-row {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr 1fr auto;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 0.5rem;
}

.line-item-row input,
.line-item-row select {
  padding: 0.4rem;
  font-size: 0.95rem;
  border: 1px solid #ccc;
  border-radius: 4px;
}

.btn-remove-line {
  padding: 0.35rem 0.5rem;
  font-size: 0.85rem;
  background: #dc3545;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.totals-summary {
  padding: 1rem;
  background: #f8f9fa;
  border-radius: 6px;
  margin: 1rem 0;
}

.totals-summary p {
  margin: 0.35rem 0;
}

.form-actions {
  margin-top: 1rem;
}

.send-error,
.ui-error {
  color: #c00;
  background: #fee;
  padding: 0.75rem;
  border-radius: 6px;
  margin-bottom: 1rem;
}

.ui-message {
  color: #155724;
  background: #d4edda;
  padding: 0.75rem;
  border-radius: 6px;
  margin-bottom: 1rem;
}

.action-area,
.action-buttons {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1rem;
}

.action-inline {
  display: inline-block;
}

.void-request-form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-width: 400px;
}

.void-request-form textarea {
  padding: 0.5rem;
  font-size: 1rem;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-family: inherit;
}

.upload-attachments-form {
  margin-bottom: 1rem;
  padding: 0.75rem 0;
}

.upload-attachments-form form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-width: 400px;
}

.upload-attachments-form input[type="file"] {
  padding: 0.35rem 0;
  font-size: 0.95rem;
}

.form-hint {
  margin: 0;
  font-size: 0.85rem;
  color: #666;
}

.attachment-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.attachment-item {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.5rem 1rem;
  padding: 0.5rem 0;
  border-bottom: 1px solid #eee;
  font-size: 0.9rem;
}

.attachment-filename {
  font-weight: 500;
}

.attachment-meta {
  color: #555;
}

.attachment-date {
  color: #666;
  font-size: 0.85rem;
}

.attachment-badge {
  font-size: 0.75rem;
  padding: 0.15rem 0.4rem;
  background: #e8e8e8;
  border-radius: 4px;
}

.btn-secondary {
  display: inline-block;
  padding: 0.5rem 1rem;
  background: #1e3a5f;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
}

.btn-secondary:hover {
  background: #2a4a7a;
}

.btn-danger {
  display: inline-block;
  padding: 0.5rem 1rem;
  background: #dc3545;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
}

.btn-danger:hover {
  background: #c82333;
}

.info-text {
  margin: 0.5rem 0 0;
  font-size: 0.95rem;
  opacity: 0.9;
}

.status-panel {
  padding: 0.75rem 1rem;
  border-radius: 6px;
  margin-bottom: 1rem;
}

.status-panel.status-issued {
  background: #d4edda;
  border: 1px solid #c3e6cb;
}

.status-panel.status-pending {
  background: #fff3cd;
  border: 1px solid #ffeaa7;
}

.status-panel.status-draft {
  background: #e8e8e8;
}

.status-panel.status-quote {
  background: #fef3c7;
  border: 1px solid #fde68a;
}

.po-title.quote-title {
  color: #b45309;
}

.quote-note {
  color: #92400e;
  font-size: 0.9rem;
  margin: 0.25rem 0 0.75rem;
}

.status-panel.status-voidrequested {
  background: #ffeaa7;
  border: 1px solid #f1c40f;
}

.status-panel.status-void {
  background: #f8d7da;
}

.send-form {
  margin-bottom: 1rem;
}

/* ----- Official Order form (document-style) ----- */
.form-header {
  margin-bottom: 0.75rem;
}

.doc-controls {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 0.5rem 1rem;
  margin-bottom: 0.75rem;
  padding: 0.5rem 0.75rem;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 4px;
  overflow-x: auto;
}

.doc-control-field {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  flex: 0 1 auto;
  min-width: 0;
}

.doc-controls label {
  font-size: 0.9rem;
  font-weight: 500;
  white-space: nowrap;
}

.doc-controls select {
  padding: 0.4rem 0.6rem;
  font-size: 0.95rem;
  border: 1px solid #ccc;
  border-radius: 4px;
  width: 240px;
  min-width: 0;
  max-width: 240px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.po-document {
  background: #fff;
  border: 1px solid #ccc;
  border-radius: 8px;
  padding: 1.5rem;
  margin-bottom: 1.5rem;
  width: 100%;
  max-width: none;
}

.po-header-row {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  margin-bottom: 1.25rem;
}

.po-title {
  text-align: left;
  font-size: 1.5rem;
  letter-spacing: 0.08em;
  margin: 0;
  color: #1a1a1a;
}

.po-order-number {
  font-size: 1.2rem;
  font-weight: 600;
  margin: 4px 0 10px 0;
}

.po-order-number-pending {
  color: #b6beca !important;
  font-weight: 400 !important;
  font-size: 1rem;
  opacity: 1;
}

.po-order-number-label {
  color: #1a1a1a;
  font-weight: 600;
}

.po-order-number-sep {
  display: inline-block;
  width: 0.25rem;
}

.po-order-date {
  font-size: 0.9rem;
  margin: -4px 0 8px 0;
}

.po-header-status {
  justify-self: center;
  font-size: 0.95rem;
  font-weight: 600;
}

.po-logo-wrap {
  justify-self: end;
  flex-shrink: 0;
}

.po-logo {
  max-width: 170px;
  max-height: 85px;
  object-fit: contain;
}

.po-header-grid {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: start;
  margin-top: 1rem;
  margin-bottom: 1.25rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid #ddd;
}

.po-supplier {
  justify-self: start;
  text-align: left;
  font-size: 0.95rem;
  line-height: 1.5;
}

.po-supplier strong {
  display: block;
  margin-bottom: 0.35rem;
}

.po-supplier input {
  width: 100%;
  margin-bottom: 0.35rem;
  padding: 0.35rem 0.4rem;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 0.9rem;
}

.po-header-spacer {
  min-width: 0;
}

.po-company {
  justify-self: end;
  text-align: right;
  font-size: 0.95rem;
  line-height: 1.5;
}

.po-company strong {
  display: block;
  margin-bottom: 0.35rem;
}

.po-contact-strip {
  margin-top: 6px;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: nowrap;
}

.po-contact-strip > div {
  flex: 1 1 0;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.po-meta-row {
  display: flex;
  gap: 40px;
  font-size: 0.9rem;
  margin-top: 4px;
}

.po-meta-row:first-of-type {
  margin-top: 0;
}

.po-meta-row:last-of-type {
  margin-bottom: 1rem;
}

.po-section {
  margin-bottom: 1.25rem;
}

.po-purpose-section {
  margin-top: 1rem;
  margin-bottom: 1rem;
}

.po-section label {
  display: block;
  margin-bottom: 0.35rem;
  font-weight: 500;
  font-size: 0.95rem;
}

.po-section-title {
  font-size: 1rem;
  margin: 0 0 0.5rem;
  font-weight: 600;
}

.po-section textarea,
.po-section input[type="text"] {
  width: 100%;
  padding: 0.5rem;
  font-size: 1rem;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-family: inherit;
}

.po-table-wrap {
  overflow-x: auto;
  margin-bottom: 0.5rem;
}

.po-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  min-width: 560px;
  table-layout: fixed;
}

.po-table thead {
  background: #f2f2f2;
  font-weight: 600;
}

.po-table th,
.po-table td {
  padding: 0.5rem;
  border: 1px solid #ccc;
  vertical-align: middle;
}

.line-items-table .line-item-row {
  display: table-row;
}

.line-items-table th {
  white-space: nowrap;
  padding-top: 0.35rem;
  padding-bottom: 0.35rem;
}

.line-items-table th:nth-child(1),
.line-items-table td:nth-child(1) { width: 72px; }

.line-items-table th:nth-child(2),
.line-items-table td:nth-child(2) { width: auto; }

.line-items-table th:nth-child(3),
.line-items-table td:nth-child(3) { width: 120px; }

.line-items-table th:nth-child(4),
.line-items-table td:nth-child(4) { width: 90px; }

.line-items-table th:nth-child(5),
.line-items-table td:nth-child(5) { width: 100px; }

.charges-table th:nth-child(1),
.charges-table td:nth-child(1) { width: 90px; }

.charges-table th:nth-child(2),
.charges-table td:nth-child(2) { width: auto; }

.charges-table th:nth-child(3),
.charges-table td:nth-child(3) { width: 120px; }

.charges-table th:nth-child(4),
.charges-table td:nth-child(4) { width: 90px; }

.charges-table th:nth-child(5),
.charges-table td:nth-child(5) { width: 100px; }

.po-table .input-qty {
  width: 100%;
  min-width: 0;
}

.po-table .input-unit-price,
.po-table .input-desc {
  width: 100%;
  min-width: 0;
  padding: 0.35rem 0.3rem;
  border: 1px solid #ccc;
  border-radius: 4px;
  margin: 0;
  box-sizing: border-box;
}

.line-items-table .input-desc {
  height: 2rem;
  min-height: 2rem;
  line-height: 1.2;
  resize: none;
  white-space: nowrap;
  overflow: hidden;
  vertical-align: middle;
  display: block;
}

.line-items-table .input-unit-price,
.line-items-table .line-vat-input,
.line-items-table .line-total-display {
  width: 100%;
  min-width: 0;
  box-sizing: border-box;
}

.line-items-table .line-vat-input {
  padding: 0.35rem 0.3rem;
  border: 1px solid #ccc;
  border-radius: 4px;
  height: 2rem;
  min-height: 2rem;
  margin: 0;
  display: block;
}

.line-items-table .input-qty,
.line-items-table .input-unit-price {
  height: 2rem;
  min-height: 2rem;
  margin: 0;
  display: block;
}

.line-items-table .input-qty {
  padding: 0.35rem 0.25rem;
}

.line-items-table .input-qty::-webkit-outer-spin-button,
.line-items-table .input-qty::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.line-items-table .input-qty[type="number"] {
  -moz-appearance: textfield;
}

.charges-table .charge-type,
.charges-table .charge-desc,
.charges-table .charge-amount {
  width: 100%;
  min-width: 0;
  height: 2rem;
  min-height: 2rem;
  margin: 0;
  padding: 0.35rem 0.3rem;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}

.charges-table .charge-display-box {
  display: block;
  width: 100%;
  height: 2rem;
  min-height: 2rem;
  margin: 0;
  padding: 0.35rem 0.3rem;
  border: 1px solid #ccc;
  border-radius: 4px;
  text-align: right;
  box-sizing: border-box;
}

.line-items-table .line-total-box {
  display: block;
  padding: 0.35rem 0.3rem;
  border: 1px solid #ccc;
  border-radius: 4px;
  height: 2rem;
  min-height: 2rem;
  text-align: right;
}

.line-items-table .input-vat-rate {
  width: 100%;
}

.po-table .line-vat-display,
.po-table .line-total-display,
.po-table .charge-vat-display,
.po-table .charge-total-display {
  white-space: nowrap;
  font-weight: 500;
}

.btn-add-line {
  margin-top: 0.5rem;
  min-height: 44px;
  padding: 0.6rem 1.25rem;
  font-size: 1rem;
  font-weight: 400;
  background: #1e3a5f;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.po-totals {
  text-align: right;
  margin-top: 20px;
  padding-top: 1rem;
  border-top: 1px solid #ddd;
}

.po-totals-line {
  margin: 0.25rem 0;
  font-size: 1rem;
}

.po-totals-line span {
  display: inline-block;
  min-width: 5rem;
  text-align: right;
}

.po-totals-divider {
  border: none;
  border-top: 1px solid #ccc;
  margin: 0.5rem 0;
}

.po-totals-grand {
  margin: 0.75rem 0 0;
  font-weight: 700;
  font-size: 1.1rem;
}

.po-totals-grand span {
  font-size: 1.2rem;
  display: inline-block;
  min-width: 5.5rem;
  text-align: right;
}

.charges-hidden {
  display: none;
}

.charges-toggle {
  display: inline-block;
  margin-top: 20px;
  padding: 0.6rem 1.25rem;
  font-size: 1rem;
  background: #1e3a5f;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.form-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
  margin-top: 1rem;
}

.form-actions #toggle-edit-rows {
  margin-left: auto;
}

.form-actions .btn-primary,
.form-actions .btn-secondary,
.form-actions .btn-cancel {
  min-height: 44px;
  padding: 0.6rem 1.25rem;
  font-size: 1rem;
  font-weight: 400;
}

.form-actions .btn-cancel {
  background: #1e3a5f;
  color: #fff;
  border-radius: 4px;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}

.form-actions .btn-cancel:hover {
  background: #2a4a7a;
  text-decoration: none;
}

@media (max-width: 600px) {
  .doc-controls {
    flex-wrap: wrap;
    overflow-x: visible;
  }
  .doc-control-field {
    flex: 1 1 100%;
  }
  .doc-controls select {
    width: 100%;
    max-width: none;
  }
  .po-header-grid {
    grid-template-columns: 1fr;
  }
  .po-header-spacer {
    display: none;
  }
  .po-meta-row {
    flex-wrap: wrap;
    gap: 0.75rem;
  }
  .po-contact-strip {
    margin-top: 6px;
    display: block;
  }
  .po-contact-strip > div {
    display: block;
    width: 100%;
    white-space: normal;
    overflow: visible;
    text-overflow: clip;
    margin-bottom: 0.25rem;
  }
  .po-table-wrap {
    margin-left: -0.5rem;
    margin-right: -0.5rem;
  }
  .po-table {
    min-width: 480px;
  }
  .form-actions {
    flex-direction: column;
    align-items: stretch;
  }
  .form-actions #toggle-edit-rows {
    margin-left: 0;
  }
  .form-actions .btn-primary,
  .form-actions .btn-secondary,
  .form-actions .btn-cancel {
    width: 100%;
    display: block;
    text-align: center;
    min-height: 48px;
  }
  .form-actions .btn-cancel {
    margin-left: 0;
  }
  .btn-add-line {
    display: block;
    width: 100%;
    padding: 0.6rem 1.25rem;
    font-size: 1rem;
    font-weight: 400;
    min-height: 48px;
  }
}

@media (min-width: 600px) {
  .order-cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  }
}
