@import url('https://fonts.googleapis.com/css2?family=Clash+Display:wght@400;500;600&family=Work+Sans:wght@300;400;500;600&display=swap');

:root {
  --bg-1: #0f0f10;
  --bg-2: #151617;
  --surface: #1d1f21;
  --surface-2: #242729;
  --text: #f5f5f4;
  --muted: #b7b7b0;
  --accent: #f36d00;
  --accent-2: #ff9a3d;
  --border: rgba(255, 255, 255, 0.08);
  --shadow: 0 40px 80px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: 'Work Sans', sans-serif;
  color: var(--text);
  background: radial-gradient(circle at 20% 20%, rgba(243, 109, 0, 0.15), transparent 45%),
    radial-gradient(circle at 80% 0%, rgba(255, 154, 61, 0.2), transparent 40%),
    linear-gradient(160deg, var(--bg-1), var(--bg-2));
  min-height: 100vh;
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}

.backdrop {
  position: fixed;
  inset: 0;
  background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" viewBox="0 0 200 200"><rect width="200" height="200" fill="none"/><circle cx="100" cy="100" r="1" fill="rgba(255,255,255,0.08)"/></svg>') repeat;
  opacity: 0.5;
  pointer-events: none;
}

.brand-mark {
  font-family: 'Clash Display', sans-serif;
  text-transform: uppercase;
  letter-spacing: 0.2em;
  font-size: 0.7rem;
  color: var(--accent-2);
}

h1, h2, h3, h4 {
  font-family: 'Clash Display', sans-serif;
  margin: 0 0 0.5rem 0;
}

h2 {
  font-size: 2.2rem;
}

h3 {
  font-size: 1.4rem;
}

h4 {
  font-size: 1rem;
  margin-top: 1.6rem;
}

p {
  margin: 0.4rem 0 1.2rem 0;
  color: var(--muted);
}

.app {
  position: relative;
  padding: 2.5rem max(4vw, 1.25rem) 4rem;
  padding-left: max(4vw, 1.25rem, env(safe-area-inset-left));
  padding-right: max(4vw, 1.25rem, env(safe-area-inset-right));
  width: 100%;
}

.app-inner {
  width: 100%;
  max-width: 1400px;
  margin: 0 auto;
}

.hidden {
  display: none !important;
}

.hero {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 2rem;
  margin-bottom: 2rem;
}

.hero-actions {
  display: flex;
  gap: 0.8rem;
  flex-wrap: wrap;
}

.layout {
  display: grid;
  grid-template-columns: 1fr 1.2fr;
  gap: 2rem;
}

.layout > * {
  min-width: 0;
}

.panel {
  background: rgba(28, 30, 32, 0.9);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 1.8rem;
  box-shadow: var(--shadow);
  backdrop-filter: blur(12px);
}

.preview-panel {
  padding: 0;
  min-width: 0;
  overflow-x: auto;
  overflow-y: auto;
}

label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 1rem;
}

input {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 12px;
  padding: 0.6rem 0.8rem;
  font-size: 0.95rem;
}

select {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 12px;
  padding: 0.6rem 0.8rem;
  font-size: 0.95rem;
}

input:focus {
  outline: 2px solid rgba(243, 109, 0, 0.5);
  border-color: transparent;
}

select:focus {
  outline: 2px solid rgba(243, 109, 0, 0.5);
  border-color: transparent;
}

.field-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
}

.btn {
  border-radius: 999px;
  border: 1px solid transparent;
  padding: 0.7rem 1.4rem;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
}

.btn.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #111;
  box-shadow: 0 15px 30px rgba(243, 109, 0, 0.3);
}

.btn.primary:hover {
  transform: translateY(-1px);
}

.btn.ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}

.gate {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(10, 10, 10, 0.9);
  z-index: 10;
  padding: 1rem;
}

.gate-card {
  background: rgba(23, 24, 25, 0.95);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 2.5rem;
  width: 100%;
  max-width: 420px;
  text-align: center;
  box-shadow: var(--shadow);
}

.pin-inputs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.6rem;
  margin: 1.2rem 0;
  width: 100%;
}

.pin {
  text-align: center;
  font-size: 1.1rem;
  width: 100%;
  min-width: 0;
  height: 3rem;
}

.error {
  color: #ff6b6b;
  font-size: 0.85rem;
  min-height: 1.2rem;
  margin-top: 0.8rem;
}

.invoice {
  background: #fff;
  color: #111;
  padding: 2rem;
  min-height: 100%;
  font-family: 'Work Sans', sans-serif;
  -webkit-print-color-adjust: exact;
  width: 100%;
  max-width: 100%;
  min-width: 0;
}

.invoice.pdf {
  width: 8.5in;
  max-width: 8.5in;
}

body.pdf-mode .preview-panel {
  overflow: visible;
}

body.pdf-mode .invoice {
  width: 8.5in;
  max-width: 8.5in;
}

.invoice-top {
  display: flex;
  justify-content: space-between;
  gap: 1.5rem;
  border-bottom: 1px solid #e5e7eb;
  padding-bottom: 1.2rem;
}

.invoice-top > * {
  min-width: 0;
}

.invoice-title {
  font-weight: 600;
  font-size: 1.1rem;
  color: #111;
}

.invoice-meta {
  text-align: right;
  flex: 0 1 220px;
  min-width: 0;
}

.meta-row {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  font-size: 0.9rem;
}

.meta-total {
  margin-top: 0.8rem;
  padding: 0.6rem 0.8rem;
  background: #111;
  color: #fff;
  border-radius: 8px;
  text-align: left;
}

.invoice-bill {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin: 1.2rem 0;
}

.invoice-bill > * {
  min-width: 0;
}

.bill-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.2em;
  color: #666;
  margin-bottom: 0.4rem;
}

.brand-stamp {
  font-family: 'Clash Display', sans-serif;
  color: var(--accent);
  border: 1px solid #111;
  padding: 0.5rem 1rem;
  text-transform: uppercase;
  letter-spacing: 0.15em;
}

.invoice-table {
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  overflow: hidden;
}

.table-header,
.table-row {
  display: grid;
  grid-template-columns: 3fr 1fr 1fr 1fr;
  padding: 0.6rem 0.8rem;
  font-size: 0.85rem;
  gap: 0.6rem;
}

.table-header {
  background: #111;
  color: #fff;
  font-weight: 600;
}

.table-row:nth-child(even) {
  background: #f8fafc;
}

.invoice-summary {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-top: 1.4rem;
}

.summary-table div {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  padding: 0.35rem 0;
  border-bottom: 1px dashed #e5e7eb;
}

.summary-table .summary-total {
  font-weight: 700;
  font-size: 0.95rem;
  border-bottom: none;
}

.summary-notes {
  font-size: 0.85rem;
  color: #444;
}

.from-block,
.bill-block,
.summary-notes,
.summary-table span,
.summary-table strong {
  overflow-wrap: anywhere;
  word-break: break-word;
}

.line-items {
  display: grid;
  gap: 0.8rem;
}

.line-item {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(72px, 0.8fr) minmax(96px, 0.9fr) minmax(110px, 1fr) auto;
  gap: 0.6rem;
  align-items: center;
}

.line-item input,
.line-item select {
  width: 100%;
  min-width: 0;
}

.line-item-remove {
  border: none;
  background: transparent;
  color: #ff6b6b;
  cursor: pointer;
  font-size: 1.1rem;
}

.workspace-panel {
  display: flex;
  flex-direction: column;
}

.tab-bar {
  display: flex;
  gap: 0.6rem;
  margin-bottom: 1rem;
}

.tab-btn {
  box-shadow: none;
  transform: none;
}

.tab-btn.active {
  background: rgba(243, 109, 0, 0.14);
  border-color: rgba(243, 109, 0, 0.55);
  color: #ffd7b0;
}

.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
}

.history-list {
  display: grid;
  gap: 0.8rem;
}

.history-item {
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0.8rem;
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: center;
  background: rgba(255, 255, 255, 0.02);
}

.history-item.pending {
  border-left: 4px solid #ffb347;
}

.history-item.paid {
  border-left: 4px solid #00d38f;
}

.history-item-info {
  min-width: 0;
}

.history-item-info div {
  color: var(--muted);
  font-size: 0.85rem;
  margin-top: 0.3rem;
}

.history-item-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.btn.ghost.danger {
  border-color: rgba(255, 107, 107, 0.45);
  color: #ff9b9b;
}

.history-toggle.pending {
  border-color: rgba(255, 179, 71, 0.45);
  color: #ffd39b;
  background: rgba(255, 179, 71, 0.12);
}

.history-toggle.paid {
  border-color: rgba(0, 211, 143, 0.55);
  color: #9df8d9;
  background: rgba(0, 211, 143, 0.2);
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: grid;
  place-items: center;
  z-index: 50;
  padding: 1rem;
}

.modal-card {
  width: min(760px, 100%);
  max-height: 92vh;
  overflow: auto;
  background: rgba(23, 24, 25, 0.98);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 1rem;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: center;
  margin-bottom: 0.8rem;
}

.company-form {
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0.8rem;
  margin-bottom: 0.8rem;
}

.modal-actions {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.company-list {
  display: grid;
  gap: 0.7rem;
}

.report-form {
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0.8rem;
  margin-bottom: 0.8rem;
}

.report-grid {
  display: grid;
  gap: 0.65rem;
}

.report-grid div {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem 0.75rem;
  background: rgba(255, 255, 255, 0.02);
}

.report-grid span {
  color: var(--muted);
}

.report-grid strong {
  color: var(--text);
}

.report-note {
  margin: 0.85rem 0 0;
  font-size: 0.85rem;
}

.company-row {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: flex-start;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0.8rem;
  background: rgba(255, 255, 255, 0.02);
}

.company-row-info div {
  color: var(--muted);
  font-size: 0.85rem;
}

.company-row-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

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

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

  .hero {
    flex-direction: column;
    align-items: flex-start;
  }
}

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

@media print {
  @page {
    size: letter;
    margin: 0.3in;
  }

  html,
  body {
    height: auto !important;
    min-height: 0 !important;
    overflow: visible !important;
  }

  body {
    background: #fff;
    margin: 0;
  }

  .backdrop,
  .hero,
  #invoice-form {
    display: none !important;
  }

  .app {
    padding: 0 !important;
    min-height: 0 !important;
    height: auto !important;
  }

  .app-inner {
    max-width: none !important;
    min-height: 0 !important;
    height: auto !important;
  }

  .layout {
    display: block !important;
    min-height: 0 !important;
    height: auto !important;
  }

  .panel,
  .preview-panel {
    background: transparent !important;
    border: 0 !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    padding: 0 !important;
    overflow: visible !important;
    backdrop-filter: none !important;
    min-height: 0 !important;
    height: auto !important;
    margin: 0 !important;
  }

  #invoice {
    position: static !important;
    width: 100% !important;
    max-width: 100% !important;
    min-height: auto !important;
    max-height: none !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: visible !important;
    page-break-inside: auto !important;
    break-inside: auto !important;
  }

  /* Force desktop invoice layout in print. Safari can apply mobile breakpoints at print width. */
  .invoice-top {
    display: flex !important;
    flex-direction: row !important;
    justify-content: space-between !important;
    align-items: flex-start !important;
  }

  .invoice-meta {
    text-align: right !important;
    min-width: 220px !important;
  }

  .invoice-bill {
    display: flex !important;
    flex-direction: row !important;
    justify-content: space-between !important;
    align-items: flex-start !important;
  }

  .invoice-summary {
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
  }

  .table-header,
  .table-row {
    display: grid !important;
    grid-template-columns: 3fr 1fr 1fr 1fr !important;
    gap: 0.6rem !important;
  }
}

@media (max-width: 720px) {
  .app {
    padding: 1.5rem 5vw 3rem;
    padding-left: max(5vw, 1rem, env(safe-area-inset-left));
    padding-right: max(5vw, 1rem, env(safe-area-inset-right));
  }

  h2 {
    font-size: 1.8rem;
  }

  .hero {
    align-items: center;
    text-align: center;
  }

  .hero-actions {
    justify-content: center;
  }

  .panel {
    padding: 1.3rem;
  }

  .invoice {
    padding: 1.2rem;
  }

  .invoice-top {
    flex-direction: column;
    align-items: flex-start;
  }

  .invoice-meta {
    text-align: left;
    width: 100%;
  }

  .invoice-bill {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.8rem;
  }

  .table-header,
  .table-row {
    grid-template-columns: 2fr 0.8fr 1fr 1fr;
    font-size: 0.8rem;
  }

  .line-item {
    grid-template-columns: 1fr 1fr;
  }

  .line-item input[type='text'] {
    grid-column: 1 / -1;
  }

  .history-item {
    flex-direction: column;
    align-items: flex-start;
  }

  .company-row {
    flex-direction: column;
  }
}

@media (max-width: 480px) {
  .gate-card {
    padding: 1.6rem;
  }

  .hero-actions {
    width: 100%;
  }

  .hero-actions .btn {
    flex: 1 1 100%;
  }

  .pin-inputs {
    gap: 0.35rem;
  }

  .pin {
    font-size: 1.2rem;
    height: 2.7rem;
  }

  .table-header {
    display: none;
  }

  .table-row {
    grid-template-columns: 1fr 1fr;
    grid-template-areas:
      'desc desc'
      'qty unit'
      'total total';
    row-gap: 0.4rem;
    font-size: 0.82rem;
  }

  .table-row span:nth-child(1) {
    grid-area: desc;
    font-weight: 600;
  }

  .table-row span:nth-child(2) {
    grid-area: qty;
  }

  .table-row span:nth-child(3) {
    grid-area: unit;
  }

  .table-row span:nth-child(4) {
    grid-area: total;
    font-weight: 600;
  }
}
