:root {
  --page-bg: #edf2f7;
  --panel-bg: #ffffff;
  --panel-border: #dfe7ef;
  --soft-border: #e5e7eb;
  --muted-text: #526071;
  --strong-text: #0f172a;
  --primary: #1d4ed8;
  --primary-soft: #dbeafe;
  --success: #0f766e;
  --danger: #b91c1c;
  --shadow: rgba(15, 23, 42, 0.08);
  --paper-bg: #ffffff;
  --paper-text: #1f2937;
  --paper-border: #d7dfe8;
  --table-header-bg: #f8fafc;
  --company-accent: #1d4ed8;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  color-scheme: light;
}

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--strong-text);
  background: var(--page-bg);
  line-height: 1.5;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

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

button {
  cursor: pointer;
}

.app-root {
  min-height: 100vh;
  padding: 24px;
}

.app-shell {
  width: min(1600px, 100%);
  margin: 0 auto;
  display: grid;
  gap: 20px;
}

.main-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  flex-wrap: wrap;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  padding: 16px 18px;
  box-shadow: 0 8px 20px var(--shadow);
}

.brand-block {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand {
  font-weight: 800;
  color: var(--strong-text);
  letter-spacing: 0.02em;
}

.app-status-pill {
  display: inline-flex;
  align-items: center;
  background: var(--primary-soft);
  color: var(--primary);
  border: 1px solid rgba(29, 78, 216, 0.12);
  border-radius: 999px;
  padding: 4px 8px;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.main-nav {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.account-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 8px;
}

.account-name {
  color: var(--muted-text);
  font-size: 0.82rem;
  font-weight: 600;
}

.auth-shell {
  display: grid;
  min-height: calc(100vh - 48px);
  place-items: center;
}

.auth-card {
  width: min(440px, 100%);
  padding: 28px;
}

.auth-card h1 {
  margin: 0 0 8px;
  font-size: 1.4rem;
}

.auth-card p {
  color: var(--muted-text);
}

.auth-form {
  display: grid;
  gap: 16px;
  margin-top: 20px;
}

.auth-error {
  color: var(--danger);
  font-weight: 600;
}

.profile-list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.profile-list-item > div:first-child {
  display: grid;
  gap: 4px;
}

.profile-list-item small {
  color: var(--muted-text);
}

.default-profile-badge {
  display: inline-flex;
  width: fit-content;
  border-radius: 999px;
  padding: 2px 8px;
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 0.7rem;
  font-weight: 700;
}

.profile-list-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.nav-button {
  border: 1px solid transparent;
  background: #f8fafc;
  color: var(--strong-text);
  padding: 8px 12px;
  border-radius: 10px;
  font-weight: 600;
}

.nav-button.active {
  background: var(--primary-soft);
  color: var(--primary);
  border-color: rgba(29, 78, 216, 0.1);
}

.global-notice {
  background: #ecfdf5;
  color: #065f46;
  border: 1px solid #bbf7d0;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.87rem;
  font-weight: 600;
}

.app-content {
  display: grid;
}

.app-footer {
  display: flex;
  justify-content: center;
  padding: 10px 12px 0;
  color: var(--muted-text);
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.page-card {
  overflow: hidden;
}

.page-body {
  padding: 18px 22px 22px;
}

.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
  margin-bottom: 18px;
}

.stat-card {
  border: 1px solid var(--soft-border);
  border-radius: 12px;
  background: #f8fafc;
  padding: 18px 16px;
  display: grid;
  gap: 8px;
}

.stat-card span {
  color: var(--muted-text);
  font-size: 0.8rem;
}

.stat-card strong {
  font-size: 1.8rem;
}

.dashboard-section {
  display: grid;
  gap: 10px;
}

.dashboard-section h3 {
  margin: 0;
}

.recent-invoice {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  width: 100%;
  text-align: left;
  border: 1px solid var(--soft-border);
  border-radius: 10px;
  padding: 10px 12px;
  background: #fff;
}

.empty-state {
  margin: 0;
  color: var(--muted-text);
}

.summary-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 8px;
}

.summary-list li {
  border: 1px solid var(--soft-border);
  background: #f8fafc;
  border-radius: 10px;
  padding: 10px 12px;
}

.invoice-list-toolbar {
  margin-bottom: 14px;
}

.search-field {
  max-width: 420px;
}

.invoice-table-wrap {
  overflow-x: auto;
}

.invoice-list-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 760px;
}

.invoice-list-table th,
.invoice-list-table td {
  padding: 12px 10px;
  border-bottom: 1px solid var(--soft-border);
  text-align: left;
  vertical-align: middle;
}

.invoice-list-table th {
  background: #f8fafc;
  color: var(--muted-text);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.invoice-list-table tbody tr:hover {
  background: #f8fafc;
}

.table-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.empty-table {
  text-align: center;
  color: var(--muted-text);
  padding: 22px 12px;
}

.editor-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 16px 22px 0;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(420px, 1.1fr) minmax(420px, 0.9fr);
  gap: 24px;
  width: min(1600px, 100%);
  margin: 0 auto;
  align-items: start;
}

.panel {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  box-shadow: 0 12px 30px var(--shadow);
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 22px;
  border-bottom: 1px solid var(--soft-border);
}

.panel-header h2 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
}

.primary-small,
.ghost-small,
.secondary-button,
.add-item-button,
.remove-item-button {
  border: 0;
  border-radius: 10px;
  font-weight: 600;
  transition: transform 0.15s ease, opacity 0.15s ease;
}

.primary-small,
.add-item-button {
  background: var(--primary-soft);
  color: var(--primary);
  padding: 9px 12px;
}

.ghost-small,
.secondary-button {
  background: #f8fafc;
  color: var(--strong-text);
  border: 1px solid var(--soft-border);
  padding: 8px 10px;
}

.ghost-small.danger,
.remove-item-button {
  background: #fee2e2;
  color: var(--danger);
  border: 1px solid #fecaca;
}

.primary-small:hover,
.ghost-small:hover,
.secondary-button:hover,
.add-item-button:hover,
.remove-item-button:hover {
  transform: translateY(-1px);
}

.editor-panel {
  padding: 0 0 16px;
}

.preview-panel {
  padding: 0 0 16px;
}

.editor-section {
  padding: 18px 22px 0;
}

.editor-section h3,
.quick-profile-header h4 {
  margin: 0 0 12px;
  font-size: 0.96rem;
  font-weight: 700;
  color: var(--strong-text);
}

.profile-section {
  padding-top: 16px;
}

.profile-pickers {
  display: grid;
  gap: 12px;
}

.profile-row {
  display: grid;
  grid-template-columns: minmax(0, 1.5fr) auto;
  gap: 12px;
  align-items: end;
}

.profile-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.quick-profile-panel {
  margin: 18px 22px 0;
  padding: 16px;
  border: 1px solid var(--soft-border);
  border-radius: 12px;
  background: #fafbfd;
}

.quick-profile-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  margin-top: 16px;
}

.checkbox-inline {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
  color: var(--muted-text);
}

.form-grid {
  display: grid;
  gap: 12px;
}

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

.field {
  display: flex;
  flex-direction: column;
  gap: 7px;
  font-size: 0.83rem;
  color: var(--muted-text);
}

.field span {
  font-weight: 600;
}

.field input,
.field textarea,
.field select {
  width: 100%;
  border: 1px solid var(--soft-border);
  border-radius: 10px;
  padding: 10px 12px;
  background: #fff;
  color: var(--strong-text);
}

.field textarea {
  resize: vertical;
  min-height: 84px;
}

.field input:focus,
.field textarea:focus,
.field select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(29, 78, 216, 0.1);
}

.full-width {
  grid-column: 1 / -1;
}

.checkbox-field {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  border: 1px solid var(--soft-border);
  border-radius: 10px;
  padding: 10px 12px;
  background: #f8fafc;
}

.checkbox-field input {
  width: auto;
  accent-color: var(--primary);
}

.logo-preview {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 110px;
  border: 1px solid var(--soft-border);
  border-radius: 12px;
  background: #f8fafc;
  overflow: hidden;
  margin-top: 8px;
}

.logo-preview img {
  display: block;
  max-width: 140px;
  max-height: 90px;
  object-fit: contain;
}

.section-heading-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.items-list {
  display: grid;
  gap: 12px;
}

.item-row {
  display: grid;
  grid-template-columns: minmax(220px, 2fr) minmax(90px, 0.7fr) minmax(110px, 0.9fr) minmax(80px, 0.6fr) minmax(110px, 0.9fr) minmax(110px, 0.9fr) minmax(110px, 0.9fr) auto;
  gap: 10px;
  padding: 10px;
  border: 1px solid var(--soft-border);
  border-radius: 12px;
  background: #fafbfd;
  align-items: start;
}

.item-field {
  min-width: 0;
}

.item-field input,
.item-field select,
.item-note-input {
  width: 100%;
  border: 1px solid var(--soft-border);
  border-radius: 9px;
  padding: 9px 10px;
  background: #fff;
}

.item-note-input {
  margin-top: 8px;
  min-height: 60px;
  resize: vertical;
}

.item-final-price,
.item-amount {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  min-height: 42px;
  padding: 9px 10px;
  border-radius: 9px;
  background: #eef3ff;
  color: var(--strong-text);
  font-weight: 700;
}

.preview-panel {
  position: sticky;
  top: 24px;
}

.preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.print-button {
  white-space: nowrap;
}

.invoice-paper {
  width: min(100%, 820px);
  min-height: 1160px;
  margin: 18px auto 0;
  background: var(--paper-bg);
  border: 1px solid var(--paper-border);
  border-radius: 8px;
  box-shadow: 0 8px 18px rgba(15, 23, 42, 0.04);
  color: var(--paper-text);
  padding: 36px 42px 30px;
}

.template-classic {
  background: #f6f2ec;
  color: #1f2937;
}

.template-classic .invoice-table th {
  background: #342d2a;
  color: #ffffff;
}

.template-classic .summary-box {
  background: #f3efe9;
}

.template-modern {
  background: #fcfdff;
}

.template-modern .invoice-header {
  border-bottom: 2px solid var(--company-accent);
}

.template-modern .summary-box {
  background: linear-gradient(180deg, rgba(29, 78, 216, 0.04), rgba(29, 78, 216, 0.08));
}

.template-minimal {
  background: #ffffff;
}

.template-minimal .invoice-block,
.template-minimal .notes-block,
.template-minimal .bank-details,
.template-minimal .summary-box {
  border: 1px solid rgba(15, 23, 42, 0.08);
  background: #ffffff;
}

.invoice-header {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  padding-bottom: 20px;
  border-bottom: 2px solid rgba(15, 23, 42, 0.1);
}

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

.company-logo {
  width: 66px;
  height: 66px;
  max-width: 100%;
  object-fit: contain;
  border: 1px solid var(--paper-border);
  border-radius: 10px;
  background: #fff;
}

.company-name {
  font-size: clamp(1.2rem, 2vw, 1.8rem);
  font-weight: 800;
  margin-bottom: 4px;
}

.company-address {
  color: var(--muted-text);
  font-size: 0.8rem;
}

.invoice-heading-block {
  text-align: right;
}

.invoice-heading-block h3 {
  margin: 0 0 8px;
  font-weight: 800;
  font-size: clamp(1.3rem, 2vw, 2rem);
  letter-spacing: 0.08em;
}

.meta-line {
  font-size: 0.8rem;
  margin-top: 6px;
}

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

.invoice-block {
  border: 1px solid var(--paper-border);
  border-radius: 10px;
  padding: 14px 16px;
  background: #fafbfd;
  min-height: 180px;
}

.invoice-block h4 {
  margin: 0 0 10px;
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted-text);
}

.invoice-block div {
  font-size: 0.82rem;
  line-height: 1.7;
}

.preview-item-description {
  font-weight: 700;
}

.preview-item-note {
  margin-top: 4px;
  color: var(--muted-text);
  font-size: 0.72rem;
  line-height: 1.5;
  white-space: pre-wrap;
}

.items-section {
  padding-top: 6px;
}

.invoice-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
}

.invoice-table th,
.invoice-table td {
  border: 1px solid var(--paper-border);
  padding: 10px 9px;
  text-align: left;
  vertical-align: top;
}

.invoice-table th {
  background: var(--table-header-bg);
  font-weight: 700;
}

.template-corporate-blue .invoice-table th {
  background: rgba(29, 78, 216, 0.08);
}

.invoice-table td:nth-child(2),
.invoice-table td:nth-child(3),
.invoice-table td:nth-child(4),
.invoice-table td:nth-child(5),
.invoice-table td:nth-child(6) {
  text-align: right;
}

.summary-box {
  width: min(320px, 100%);
  margin-left: auto;
  margin-top: 18px;
  border: 1px solid var(--paper-border);
  border-radius: 10px;
  background: #fafbfd;
  overflow: hidden;
}

.summary-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--paper-border);
  font-size: 0.84rem;
}

.summary-row:last-child {
  border-bottom: 0;
}

.total-row {
  font-weight: 700;
  background: rgba(29, 78, 216, 0.08);
}

.bank-details {
  margin-top: 18px;
  border: 1px solid var(--paper-border);
  border-radius: 10px;
  background: #fafbfd;
  padding: 12px 14px;
}

.bank-details h4 {
  margin: 0 0 10px;
  font-size: 0.76rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted-text);
}

.bank-details div {
  font-size: 0.8rem;
  line-height: 1.7;
}

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

.notes-block {
  border: 1px solid var(--paper-border);
  border-radius: 10px;
  padding: 12px 14px;
  background: #fafbfd;
}

.notes-block h4 {
  margin: 0 0 8px;
  font-size: 0.76rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted-text);
}

.notes-block p {
  margin: 0;
  font-size: 0.8rem;
  line-height: 1.6;
  white-space: pre-wrap;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

@page {
  size: A4 portrait;
  margin: 12mm;
}

@media print {
  html,
  body {
    background: #ffffff;
    margin: 0;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  body {
    visibility: visible;
  }

  body.is-printing #app,
  body.is-printing #app * {
    visibility: visible;
  }

  body.is-printing .main-header,
  body.is-printing .main-nav,
  body.is-printing .global-notice,
  body.is-printing .editor-shell,
  body.is-printing .preview-panel > .panel-header,
  body.is-printing .preview-header {
    display: none !important;
  }

  body.is-printing .app-content {
    display: block;
    width: 100%;
  }

  body.is-printing .workspace {
    display: block;
    width: 100%;
  }

  body.is-printing #invoice-preview,
  body.is-printing .invoice-print-target,
  body.is-printing .preview-panel {
    display: block !important;
    visibility: visible !important;
    position: static !important;
    width: 100% !important;
    max-width: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
    border: 0 !important;
    box-shadow: none !important;
    overflow: visible !important;
  }

  body.is-printing .invoice-paper {
    width: 100%;
    max-width: 210mm;
    min-height: auto;
    margin: 0 auto;
    border: 0;
    box-shadow: none;
    border-radius: 0;
    padding: 0;
    page-break-inside: avoid;
    break-inside: avoid;
  }

  body.is-printing .invoice-paper * {
    visibility: visible;
  }
}

@media (max-width: 980px) {
  body {
    overflow-x: hidden;
  }

  .app-root {
    padding: 12px;
  }

  .app-shell,
  .app-content,
  .workspace,
  .preview-panel,
  #invoice-preview {
    width: calc(100vw - 24px);
    max-width: calc(100vw - 24px);
    min-width: 0;
  }

  .invoice-paper {
    width: calc(100vw - 40px);
    max-width: calc(100vw - 40px);
    min-width: 0;
  }

  .workspace {
    grid-template-columns: 1fr;
  }

  .preview-panel {
    position: static;
  }
}

@media (max-width: 640px) {
  .app-root {
    padding: 12px;
  }

  .app-shell {
    width: 100%;
    max-width: 100%;
  }

  .main-header {
    padding: 12px 14px;
  }

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

  .panel-header,
  .editor-section,
  .quick-profile-panel,
  .page-body {
    padding-left: 14px;
    padding-right: 14px;
  }

  .two-columns,
  .invoice-columns,
  .invoice-notes {
    grid-template-columns: 1fr;
  }

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

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

  .invoice-paper {
    padding: 20px 16px 18px;
    overflow: hidden;
  }

  .invoice-table {
    table-layout: fixed;
    width: 100%;
    font-size: 0.72rem;
  }

  .invoice-table th,
  .invoice-table td {
    padding: 8px 6px;
    white-space: normal;
    word-break: break-word;
    overflow-wrap: anywhere;
  }

  .summary-box {
    width: 100%;
  }
}
