/* Base styles */
:root {
  --text-color: #111827;
  --muted-color: #6b7280;
  --border-color: #e5e7eb;
  --accent-color: #2563eb;
  --danger-color: #dc2626;
  --bg-color: #ffffff;
  --table-header-bg: #f3f4f6;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg-color);
  color: var(--text-color);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
    Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", Arial,
    "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", sans-serif;
  line-height: 1.5;
  font-size: 14px;
}

.invoice-page {
  max-width: 900px;
  margin: clamp(12px, 4vw, 32px) auto;
  padding: clamp(12px, 4vw, 32px);
  border: 1px solid #bdc0c7;
  border-radius: 12px;
  background: #fff;
}

/* Header */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 24px;
}

.actions {
  position: fixed;
  top: max(8px, env(safe-area-inset-top));
  right: max(8px, env(safe-area-inset-right));
  display: flex;
  gap: 8px;
  z-index: 10;
}

.action {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: #f9fafb;
  color: var(--text-color);
  border: 1px solid #bdc0c7;
  border-radius: 8px;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

.action:hover {
  background: #f3f4f6;
}

.doc-title {
  margin: 0;
  font-size: clamp(20px, 2.2vw + 1rem, 28px);
  letter-spacing: 2px;
}

/* Credit note state */
.is-credit { color: var(--danger-color); }
.is-credit .doc-title { color: var(--danger-color); }
.is-credit .school-address,
.is-credit .school-contact,
.is-credit .meta-label,
.is-credit .items-table thead th,
.is-credit .totals-label,
.is-credit .page-footer {
  color: var(--danger-color) !important;
}

/* Details and metadata */
.party-and-meta {
  display: grid;
  grid-template-columns: 1.2fr 0.8fr;
  gap: clamp(12px, 3vw, 24px);
  margin-bottom: clamp(12px, 3vw, 24px);
}

.school-name {
  margin: 0 0 4px 0;
  font-size: 20px;
}

.school-address {
  margin: 0 0 4px 0;
  color: var(--muted-color);
}

.school-contact {
  margin: 0;
  color: var(--muted-color);
}

.divider {
  margin: 0 8px;
  color: var(--border-color);
}

.invoice-meta {
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 12px;
  background: #fafafa;
  height: fit-content;
}

.meta-row {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
}

.meta-label {
  color: var(--muted-color);
}

.meta-value {
  font-weight: 600;
}

/* Table */
.items-table {
  width: 100%;
  border-collapse: collapse;
  border: 1px solid var(--border-color);
}

/* Make table horizontally scrollable on very narrow screens */
.line-items {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.items-table thead th {
  text-align: left;
  background: var(--table-header-bg);
  padding: 10px 12px;
  font-weight: 600;
  color: var(--muted-color);
  border: 1px solid var(--border-color);
}

.items-table th.num, .items-table td.num {
  text-align: right;
}

.items-table tbody td {
  padding: 12px;
  border: 1px solid var(--border-color);
}

.items-table tbody tr:nth-child(even) {
  background: #fafafa;
}

/* Totals */
.totals {
  width: 100%;
  display: grid;
  grid-template-columns: auto minmax(110px, auto);
  column-gap: 16px;
  row-gap: 8px;
  margin-top: 16px;
  margin-left: auto; /* push block to the right */
  max-width: 360px; /* keep a tidy block width */
}

.totals-row {
  display: contents;
}

.totals-label {
  justify-self: start; /* label left in the right-aligned block */
  color: var(--muted-color);
}

.totals-value {
  justify-self: end; /* number right-aligned */
  text-align: right;
  font-weight: 700;
}

.grand-total .totals-value {
  font-size: 18px;
}

/* Footer */
.page-footer {
  margin-top: 32px;
  padding-top: 16px;
  border-top: 1px solid var(--border-color);
  color: var(--muted-color);
  text-align: center;
}

/* Toasts */
.toast-container {
  position: fixed;
  left: max(12px, env(safe-area-inset-left));
  bottom: max(12px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 1000;
}

.toast {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: start;
  gap: 10px;
  min-width: min(92vw, 360px);
  max-width: min(92vw, 420px);
  padding: 12px 14px;
  background: #ffffff;
  border: 1px solid rgba(0,0,0,0.06);
  border-radius: 12px;
  box-shadow: 0 12px 24px rgba(0,0,0,0.12), 0 2px 8px rgba(0,0,0,0.06);
  color: var(--text-color);
  transform: translateY(8px);
  opacity: 0;
  animation: toast-slide-in 220ms ease-out forwards;
}

.toast.success .toast-icon { color: #16a34a; }
.toast.error .toast-icon { color: #dc2626; }

.toast-icon {
  font-size: 18px;
  line-height: 1;
}

.toast-content {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.toast-title {
  font-weight: 700;
}

.toast-message {
  color: var(--muted-color);
}

.toast-close {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--muted-color);
  cursor: pointer;
  padding: 2px;
}

.toast.fade-out {
  animation: toast-fade-out 300ms ease-in forwards;
}

@keyframes toast-slide-in {
  from { transform: translateY(8px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

@keyframes toast-fade-out {
  from { transform: translateY(0); opacity: 1; }
  to { transform: translateY(4px); opacity: 0; }
}

/* Print styles */
@media print {
  .invoice-page {
    border: none;
    margin: 0;
    padding: 0;
    border-radius: 0;
  }

  .actions {
    display: none !important;
  }

  body {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  .items-table thead th {
    background: var(--table-header-bg) !important;
  }

  @page {
    margin: 16mm;
  }
}

/* Responsive */
@media (max-width: 720px) {
  .invoice-page {
    margin: 0;
    border: none;
    border-radius: 0;
    padding: 12px;
  }

  .party-and-meta {
    grid-template-columns: 1fr;
  }

  .doc-title {
    font-size: 22px;
  }

  /* Hide button labels on tiny screens, keep icons only */
  .action .label { display: none; }

  /* Totals go full-width with right-aligned numbers */
  .totals {
    max-width: none;
    grid-template-columns: 1fr auto;
  }
}

/* Ultra-small devices (e.g., iPhone SE width ~320px, Galaxy Fold folded) */
@media (max-width: 360px) {
  html, body { font-size: 13px; }
  .invoice-page { padding: 10px; }
  .actions { top: 8px; right: 8px; }
}

/* Very large screens: add comfortable max width */
@media (min-width: 1440px) {
  .invoice-page { max-width: 1000px; }
}


