:root {
  --bg: #f4f7f8;
  --ink: #17242b;
  --muted: #65747c;
  --line: #dce5e8;
  --panel: #ffffff;
  --cyan: #10b7c8;
  --cyan-dark: #07899a;
  --green: #2f8f6f;
  --amber: #b57515;
  --shadow: 0 16px 40px rgba(23, 36, 43, 0.08);
}

* {
  box-sizing: border-box;
}

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

body.locked .app-shell {
  display: none;
}

.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: #0f181d;
  padding: 24px;
}

.login-screen.hidden {
  display: none;
}

.login-card {
  width: min(420px, 100%);
  display: grid;
  gap: 16px;
  background: #ffffff;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.25);
}

.login-card img {
  width: 100%;
  background: #0f181d;
  border-radius: 8px;
  padding: 16px;
}

.login-card h1 {
  font-size: 28px;
}

.login-card p {
  min-height: 20px;
  margin: 0;
  color: #b33327;
  font-weight: 700;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: 252px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  background: #0f181d;
  color: #eef7f8;
  padding: 24px 18px;
  display: flex;
  flex-direction: column;
  gap: 28px;
}

.brand {
  min-height: 76px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  padding-bottom: 20px;
}

.brand img {
  width: 100%;
  height: auto;
}

.nav {
  display: grid;
  gap: 8px;
}

.nav a {
  color: #c8d6da;
  text-decoration: none;
  padding: 11px 12px;
  border-radius: 6px;
  font-weight: 700;
}

.nav a.active,
.nav a:hover {
  background: rgba(16, 183, 200, 0.18);
  color: #ffffff;
}

.metric {
  margin-top: auto;
  padding: 16px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 8px;
  display: grid;
  gap: 8px;
}

.metric span {
  color: #a7bdc4;
  font-size: 13px;
  font-weight: 700;
}

.metric strong {
  font-size: 34px;
}

.logout-button {
  background: transparent;
  color: #eef7f8;
  border-color: rgba(255, 255, 255, 0.18);
}

main {
  min-width: 0;
  padding: 28px;
}

.page {
  display: none !important;
}

.page.active {
  display: block !important;
}

.workspace.page.active {
  display: grid !important;
}

.company-page.page.active {
  display: grid !important;
}

.settings-page.page.active {
  display: grid !important;
}

.settings-page {
  gap: 18px;
}

.db-status {
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
  text-transform: uppercase;
}

.db-status.online {
  color: var(--green);
}

.db-status.offline {
  color: #b33327;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 20px;
  margin-bottom: 22px;
}

.topbar p {
  margin: 0 0 5px;
  color: var(--cyan-dark);
  font-weight: 800;
  text-transform: uppercase;
  font-size: 12px;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: clamp(28px, 4vw, 42px);
  line-height: 1.05;
}

h2 {
  font-size: 16px;
}

.top-actions,
.preview-actions,
.mini-actions,
.section-head {
  display: flex;
  align-items: center;
  gap: 10px;
}

.section-head {
  justify-content: space-between;
  margin-bottom: 16px;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 330px;
  gap: 18px;
  align-items: start;
}

form {
  display: grid;
  gap: 18px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 18px;
}

.document-control {
  display: grid;
  grid-template-columns: minmax(260px, 420px) 1fr;
  gap: 16px;
}

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}

.segmented button {
  min-height: 48px;
  border: 0;
  background: #f7fbfc;
  color: var(--ink);
  font-weight: 800;
}

.segmented button.active {
  background: var(--cyan);
  color: #06262b;
}

.status-strip {
  display: grid;
  grid-template-columns: 1fr 140px 1fr;
  gap: 12px;
}

.grid {
  display: grid;
  gap: 18px;
}

.grid > *,
.form-grid > *,
.workspace > * {
  min-width: 0;
}

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

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

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 13px;
  margin-top: 16px;
}

.form-grid.single {
  grid-template-columns: 1fr;
}

label,
.preview-row,
.preview-total {
  display: grid;
  gap: 7px;
}

label span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfdfd;
  color: var(--ink);
  min-height: 42px;
  padding: 10px 11px;
  outline: none;
}

select {
  appearance: none;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--cyan-dark) 50%),
    linear-gradient(135deg, var(--cyan-dark) 50%, transparent 50%);
  background-position:
    calc(100% - 18px) 18px,
    calc(100% - 12px) 18px;
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
  padding-right: 36px;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--cyan);
  box-shadow: 0 0 0 3px rgba(16, 183, 200, 0.14);
}

textarea {
  min-height: 82px;
  resize: vertical;
}

.wide {
  grid-column: 1 / -1;
}

.primary,
.secondary,
.ghost,
.icon {
  min-height: 42px;
  border-radius: 6px;
  border: 1px solid transparent;
  padding: 0 15px;
  font-weight: 800;
}

.primary {
  background: var(--cyan);
  color: #06262b;
}

.secondary {
  background: var(--green);
  color: #ffffff;
}

.ghost {
  background: #ffffff;
  border-color: var(--line);
  color: var(--ink);
}

.compact {
  min-height: 36px;
}

.items-table,
.stored-table {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}

.items-header,
.item-row {
  display: grid;
  grid-template-columns: minmax(220px, 2fr) minmax(130px, 1fr) 86px 110px 115px 42px;
  gap: 8px;
  align-items: center;
  padding: 10px;
}

.items-header,
.stored-header {
  background: #edf5f6;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.item-row + .item-row,
.stored-row + .stored-row {
  border-top: 1px solid var(--line);
}

.item-total {
  text-align: right;
  white-space: nowrap;
}

.icon {
  padding: 0;
  background: #fff2f0;
  color: #b33327;
  border-color: #ffd2ca;
  font-size: 22px;
  line-height: 1;
}

.preview {
  position: sticky;
  top: 28px;
}

.preview-head {
  display: flex;
  align-items: center;
  gap: 14px;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--line);
}

.preview-head img {
  width: 140px;
  height: auto;
  background: #0f181d;
  border-radius: 6px;
  padding: 8px;
}

.preview-head span {
  display: block;
  color: var(--muted);
  font-weight: 800;
  font-size: 12px;
}

.preview-head strong {
  display: block;
  margin-top: 5px;
}

.preview-row {
  grid-template-columns: 1fr auto;
  padding: 14px 0;
  border-bottom: 1px solid var(--line);
}

.preview-total {
  grid-template-columns: 1fr auto;
  align-items: end;
  padding: 18px 0;
}

.preview-total span {
  font-weight: 800;
}

.preview-total strong {
  font-size: 30px;
  color: var(--cyan-dark);
  text-align: right;
}

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

.stored-panel {
  margin-top: 0;
}

.print-preview {
  position: relative;
  display: grid;
  gap: 18px;
  background: #ffffff;
  overflow: hidden;
  width: 100%;
  max-width: none;
  min-width: 0;
  margin: 0 auto;
}

.print-preview::before {
  content: "";
  position: absolute;
  inset: 0 0 auto;
  height: 8px;
  background: linear-gradient(90deg, #10b7c8, #2f8f6f 55%, #17242b);
}

.print-meta,
.print-scope,
.print-bottom {
  display: grid;
  gap: 16px;
}

.print-header {
  display: flex;
  justify-content: space-between;
  align-items: end;
  gap: 20px;
  border-bottom: 1px solid #c7d7dc;
  padding: 0 0 16px;
}

.print-logo-box {
  background: #111b20;
  border-radius: 8px;
  box-shadow: 0 12px 30px rgba(15, 24, 29, 0.18);
  padding: 18px 24px;
  width: 100%;
  min-height: 108px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 12px;
}

.print-logo-box img {
  display: block;
  width: min(560px, 96%);
  height: auto;
}

.print-title {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 4px;
}

.print-title span {
  color: #087f8f;
  font-size: 34px;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

.print-title strong {
  font-size: 22px;
  color: #17242b;
}

.print-meta {
  grid-template-columns: 1fr 1fr 1.25fr;
}

.print-scope {
  grid-template-columns: 1.5fr 1fr 1fr;
  border: 1px solid #d7e5e8;
  border-radius: 8px;
  padding: 14px;
  background: linear-gradient(180deg, #f7fbfb, #edf6f7);
}

.print-meta span,
.print-scope span,
.print-notes span,
.print-totals span {
  display: block;
  color: var(--muted);
  font-size: 11px;
  font-weight: 900;
  text-transform: uppercase;
}

.print-notes > div,
.print-payments {
  border: 1px solid #e0eaed;
  border-radius: 8px;
  padding: 12px;
  background: #fbfdfd;
  align-content: start;
  justify-items: start;
}

.print-meta strong,
.print-scope strong {
  display: block;
  margin: 4px 0;
}

.print-meta p,
.print-notes p,
.print-payments p {
  margin: 0;
  color: #334249;
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-line;
}

.print-items {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
  overflow: hidden;
  border-radius: 8px;
}

.print-items th,
.print-items td {
  border-bottom: 1px solid #dfe9eb;
  padding: 11px 10px;
  text-align: left;
  vertical-align: top;
}

.print-items th {
  background: #10262d;
  color: #c9f3f7;
  font-size: 11px;
  text-transform: uppercase;
}

.print-items th:nth-child(n+3),
.print-items td:nth-child(n+3) {
  text-align: right;
  white-space: nowrap;
}

.print-bottom {
  grid-template-columns: minmax(0, 1fr) minmax(360px, 0.95fr);
  align-items: start;
}

.print-notes {
  display: grid;
  gap: 12px;
}

.paynow-box {
  width: 100%;
  max-width: 220px;
  aspect-ratio: 1;
  border: 1px dashed #8ea4aa;
  border-radius: 8px;
  background: #f8fbfb;
  display: grid;
  place-items: center;
  overflow: hidden;
  color: var(--muted);
  font-size: 13px;
  font-weight: 900;
  text-align: center;
  transition: border-color 0.2s ease, background 0.2s ease, box-shadow 0.2s ease;
}

.qr-upload-target {
  cursor: pointer;
}

.qr-upload-target:hover,
.qr-upload-target.drag-over,
.qr-upload-target:focus {
  border-color: var(--cyan-dark);
  background: #eefafb;
  box-shadow: 0 0 0 3px rgba(16, 183, 200, 0.14);
  outline: none;
}

.paynow-box img {
  display: none;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.paynow-box.has-qr img {
  display: block;
}

.paynow-box.has-qr div {
  display: none;
}

.print-totals {
  border: 1px solid #d8e5e8;
  border-radius: 8px;
  overflow: hidden;
  background: #ffffff;
  width: 100%;
}

.print-totals div {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}

.print-totals strong {
  font-size: 18px;
}

.print-totals div:last-child {
  border-bottom: 0;
}

.print-totals .grand {
  background: #10262d;
  color: #ffffff;
  font-size: 20px;
}

.print-totals .grand span {
  color: #bceff5;
}

.print-footer {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  border-top: 1px solid #d7e5e8;
  padding-top: 12px;
  color: #596a72;
  font-size: 12px;
  font-weight: 800;
}

.company-page {
  display: grid;
  gap: 18px;
}

.company-hero {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 18px;
  background: #10262d;
  color: #ffffff;
  border-radius: 8px;
  padding: 18px;
  box-shadow: var(--shadow);
}

.company-brand-card {
  display: flex;
  align-items: center;
  gap: 16px;
}

.company-brand-card img {
  width: 220px;
  height: auto;
}

.company-brand-card span {
  display: block;
  color: #a8d9df;
  font-size: 12px;
  font-weight: 900;
  text-transform: uppercase;
}

.company-brand-card strong {
  display: block;
  margin-top: 5px;
  font-size: 28px;
}

.company-actions {
  display: flex;
  gap: 10px;
}

.company-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 18px;
}

.company-card:last-child {
  grid-column: 1 / -1;
}

.company-card {
  min-width: 0;
}

.company-card dl {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin: 16px 0 0;
}

.company-card dt {
  color: var(--muted);
  font-size: 11px;
  font-weight: 900;
  text-transform: uppercase;
}

.company-card dd {
  margin: 4px 0 0;
  min-width: 0;
  overflow-wrap: anywhere;
  font-weight: 750;
}

.company-card .full {
  grid-column: 1 / -1;
}

.company-payment {
  display: grid;
  grid-template-columns: minmax(180px, 260px) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

.company-edit-card .form-grid {
  margin-top: 16px;
}

.company-payment .form-grid {
  margin-top: 0;
}

.stored-panel input {
  max-width: 260px;
}

.stored-header,
.stored-row {
  display: grid;
  grid-template-columns: 110px 150px minmax(160px, 1fr) 140px 120px 120px 94px;
  gap: 10px;
  align-items: center;
  padding: 12px;
}

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

.row-actions {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
}

.row-actions button {
  min-height: 32px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: #ffffff;
  font-weight: 800;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  display: none;
  place-items: center;
  background: rgba(15, 24, 29, 0.55);
  z-index: 20;
  padding: 24px;
}

.modal-backdrop.active {
  display: grid;
}

.choice-modal {
  width: min(460px, 100%);
  background: #ffffff;
  border-radius: 8px;
  padding: 22px;
  box-shadow: var(--shadow);
}

.choice-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 18px;
}

@media (max-width: 1320px) {
  .workspace,
  .document-control,
  .grid.three {
    grid-template-columns: 1fr;
  }

  .preview {
    position: static;
  }

  form {
    min-width: 0;
  }
}

@media (max-width: 860px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    height: auto;
  }

  .brand img {
    max-width: 310px;
  }

  .topbar,
  .grid.two,
  .company-grid,
  .status-strip,
  .form-grid {
    grid-template-columns: 1fr;
  }

  .topbar,
  .top-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .mini-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .company-hero,
  .company-brand-card,
  .company-actions,
  .company-payment {
    align-items: stretch;
    flex-direction: column;
    display: flex;
  }

  .company-brand-card img {
    max-width: 260px;
    width: 100%;
  }

  .items-header {
    display: none;
  }

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

  .item-description,
  .item-total {
    grid-column: 1 / -1;
  }

  .stored-header {
    display: none;
  }

  .stored-row {
    grid-template-columns: 1fr;
  }

  .print-preview {
    min-width: 0;
  }
}

@media print {
  @page {
    size: A4;
    margin: 8mm;
  }

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

  body {
    background: #ffffff;
  }

  .sidebar,
  .login-screen,
  .modal-backdrop,
  .top-actions,
  .company-page,
  .settings-page,
  .document-control,
  .grid,
  .line-items,
  .preview,
  .stored-panel,
  .topbar {
    display: none !important;
  }

  .app-shell,
  .workspace {
    display: block;
  }

  main {
    padding: 0;
  }

  .print-preview {
    display: grid !important;
    width: 194mm;
    min-height: 281mm;
    max-width: none;
    min-width: 0;
    margin: 0;
    box-shadow: none;
    border: 0;
    border-radius: 0;
    padding: 0;
    gap: 8px;
    font-size: 11px;
  }

  .panel {
    box-shadow: none;
  }

  .print-header {
    padding: 0 0 8px;
  }

  .print-logo-box {
    width: 100%;
    min-height: 62px;
    padding: 9px 10px;
    margin-top: 7px;
  }

  .print-logo-box img {
    width: min(460px, 96%);
  }

  .print-title span {
    font-size: 24px;
  }

  .print-meta,
  .print-scope,
  .print-bottom {
    gap: 8px;
  }

  .print-meta {
    grid-template-columns: 1.1fr 1.1fr 0.9fr;
  }

  .print-scope {
    padding: 7px;
  }

  .print-items {
    font-size: 10.5px;
  }

  .print-items th,
  .print-items td {
    padding: 5px 6px;
  }

  .print-bottom {
    grid-template-columns: minmax(0, 1fr) 250px;
  }

  .paynow-box {
    max-width: 82px;
  }

  .print-meta p,
  .print-notes p,
  .print-payments p {
    font-size: 11px;
    line-height: 1.28;
  }

  .print-totals div {
    padding: 5px 8px;
    font-size: 11px;
  }

  .print-notes > div,
  .print-payments {
    padding: 8px;
  }

  .print-footer {
    padding-top: 8px;
    font-size: 10px;
  }
}
