:root {
  --primary-color: #113a55;
  --primary-dark: #b39400;
  --secondary-color: #f0f4f8;
  --text-dark: #2c3e50;
  --text-light: #7f8c8d;
  --border-color: #ecf0f1;
  --success-color: #27ae60;
  --danger-color: #e74c3c;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'Cairo', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  direction: rtl;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  min-height: 100vh;
  padding: 20px;
  overflow-x: hidden;
}

.main-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  max-width: 1600px;
  margin: 0 auto;
}

.main-container.single-pane {
  grid-template-columns: 1fr;
  max-width: 900px;
}

.auth-overlay {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(15, 23, 42, 0.48);
  backdrop-filter: blur(3px);
  z-index: 9999;
  min-height: 100dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

body.app-locked .auth-overlay {
  display: flex;
}

body.app-locked {
  overflow: hidden;
}

body.app-locked .main-container {
  filter: blur(4px);
  pointer-events: none;
  user-select: none;
}

.auth-card {
  width: min(420px, 100%);
  background: #ffffff;
  border-radius: 16px;
  border: 1px solid #e6eaf0;
  padding: 24px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.22);
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
}

.auth-title {
  margin: 0 0 8px;
  color: var(--text-dark);
  font-size: 24px;
  font-weight: 800;
}

.auth-subtitle {
  margin: 0 0 18px;
  color: var(--text-light);
  line-height: 1.6;
}

.auth-form {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
}

.auth-input {
  width: 100%;
  border: 2px solid var(--border-color);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 16px;
  font-family: inherit;
  min-height: 48px;
  direction: ltr;
  text-align: left;
}

.auth-input:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(212, 168, 0, 0.14);
}

.auth-submit {
  border: none;
  border-radius: 10px;
  background: var(--primary-color);
  color: #fff;
  padding: 12px 18px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  transition: background-color 0.2s ease, transform 0.2s ease;
  min-height: 48px;
}

.auth-submit:hover {
  background: var(--primary-dark);
  transform: translateY(-1px);
}

.auth-submit:disabled {
  background: #bfc5d1;
  cursor: not-allowed;
  transform: none;
}

.auth-attempts {
  margin: 12px 0 0;
  color: var(--text-light);
  font-size: 13px;
}

.auth-error {
  display: none;
  margin: 8px 0 0;
  color: #c0392b;
  font-weight: 700;
  font-size: 13px;
}

/* Editor Section */
.editor-section {
  display: flex;
  flex-direction: column;
}

.editor-card {
  background: white;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  animation: slideInLeft 0.5s ease;
}

@keyframes slideInLeft {
  from {
    opacity: 0;
    transform: translateX(-30px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes slideInRight {
  from {
    opacity: 0;
    transform: translateX(30px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

.section-title {
  color: var(--text-dark);
  font-size: 18px;
  font-weight: 600;
  margin: 20px 0 15px;
  padding-bottom: 10px;
  border-bottom: 2px solid var(--primary-color);
}

.section-title:first-child {
  margin-top: 0;
}

/* Form Styling */
.form-group {
  margin-bottom: 20px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 15px;
}

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

.form-input {
  display: flex;
  flex-direction: column;
}

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

.form-input label {
  color: var(--text-dark);
  font-weight: 600;
  margin-bottom: 8px;
  font-size: 14px;
}

.form-input input {
  padding: 12px;
  border: 2px solid var(--border-color);
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.3s ease;
  font-family: inherit;
}

.form-input textarea {
  padding: 12px;
  border: 2px solid var(--border-color);
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.3s ease;
  font-family: inherit;
  resize: vertical;
  min-height: 84px;
}

.form-input input:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(212, 168, 0, 0.1);
}

.form-input textarea:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(212, 168, 0, 0.1);
}

.form-input input::placeholder {
  color: var(--text-light);
}

.form-input textarea::placeholder {
  color: var(--text-light);
}

/* Table Styling */
.table-container {
  overflow-x: auto;
  margin: 15px 0;
  border-radius: 8px;
  border: 1px solid var(--border-color);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.edit-table thead {
  background: var(--primary-color);
}

.invoice-table thead {
  background: var(--primary-color);
}

table thead th {
  padding: 12px;
  color: white;
  font-weight: 600;
  text-align: center;
}

table tbody td {
  padding: 10px;
  border-bottom: 1px solid var(--border-color);
  text-align: center;
  background: white;
}

.edit-table tbody input,
.edit-table tbody select {
  width: 100%;
  padding: 8px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  font-size: 13px;
  text-align: center;
  font-family: inherit;
  background: #fff;
}

.edit-table tbody input:focus,
.edit-table tbody select:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px rgba(212, 168, 0, 0.1);
}

.edit-table tbody button {
  background: var(--danger-color);
  color: white;
  border: none;
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-weight: 600;
  transition: all 0.3s ease;
}

.edit-table tbody button:hover {
  background: #c0392b;
  transform: scale(1.05);
}

/* Button Styling */
.btn-add {
  background: var(--success-color);
  color: white;
  border: none;
  padding: 12px 20px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  margin: 15px 0;
  transition: all 0.3s ease;
  width: 100%;
}

.btn-add:hover {
  background: #229954;
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(39, 174, 96, 0.3);
}

.button-group {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 20px;
}

.btn-primary,
.btn-secondary {
  padding: 12px 20px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  transition: all 0.3s ease;
  text-align: center;
}

.btn-primary {
  background: var(--primary-color);
  color: white;
}

.btn-primary:hover {
  background: var(--primary-dark);
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(212, 168, 0, 0.3);
}

.btn-secondary {
  background: #3498db;
  color: white;
}

.btn-secondary:hover {
  background: #2980b9;
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(52, 152, 219, 0.3);
}

/* Preview Section */
.preview-section {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.preview-section.preview-hidden {
  position: fixed;
  top: 0;
  left: -99999px;
  width: 210mm;
  z-index: -1;
}

/* Keep the hidden PDF surface in desktop layout on all devices */
.preview-section.preview-hidden .invoice-preview {
  border-radius: 0;
  padding: 8mm 8mm 6mm;
  font-size: 13px;
  width: 210mm;
  max-width: 100%;
  min-height: 297mm;
  margin: 0;
}

.preview-section.preview-hidden .invoice-header {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0;
  direction: ltr;
}

.preview-section.preview-hidden .company-info {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.preview-section.preview-hidden .company-logo {
  width: 5cm;
  margin-bottom: 0;
}

.preview-section.preview-hidden .invoice-info {
  text-align: right;
  align-items: flex-end;
  gap: 2px;
  min-width: 40mm;
  margin-top: 0;
  direction: rtl;
}

.preview-section.preview-hidden .invoice-divider {
  margin: 10px 0;
}

.preview-section.preview-hidden .footer-approvals {
  grid-template-columns: 1fr 1fr;
  gap: 8mm;
  align-items: start;
  direction: rtl;
  justify-items: end;
}

.preview-section.preview-hidden .footer-approvals.single-approval {
  grid-template-columns: 1fr;
  gap: 0;
}

.preview-section.preview-hidden .footer-title {
  font-size: 16px;
  margin-bottom: 4px;
}

.preview-section.preview-hidden .footer-sign-label {
  font-size: 14px;
  margin-bottom: 2px;
}

.preview-section.preview-hidden .footer-signature {
  font-size: 18px;
  min-height: 18px;
}

.preview-section.preview-hidden .footer-stamp {
  width: calc(190px + 1cm);
  height: calc(190px + 1cm);
  margin-top: -3cm;
  margin-right: 0.8cm;
}

.invoice-preview {
  background: white;
  border-radius: 0;
  padding: 8mm 8mm 6mm;
  box-shadow: none;
  animation: none;
  font-size: 13px;
  width: 210mm;
  max-width: 100%;
  min-height: 297mm;
  margin: 0;
}

.invoice-preview::before {
  content: '';
  display: block;
  height: 2mm;
  background: var(--primary-color);
  margin-bottom: 10mm;
}

.invoice-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0;
  direction: ltr;
}

.company-info {
  flex: 1;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.company-logo {
  width: 5cm;
  max-width: 100%;
  height: auto;
  margin-bottom: 0;
  object-fit: contain;
}

.company-name {
  color: var(--primary-color);
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 2px;
}

.company-slogan {
  color: var(--text-light);
  font-size: 12px;
  font-style: italic;
}

.invoice-info {
  text-align: right;
  display: flex;
  flex-direction: column;
  gap: 2px;
  align-items: flex-end;
  min-width: 40mm;
  direction: rtl;
}

.invoice-info p {
  color: var(--text-dark);
  font-weight: 600;
  width: 100%;
  text-align: right;
  direction: rtl;
}

.invoice-divider {
  height: 2px;
  background: linear-gradient(90deg, var(--primary-color), transparent);
  margin: 10px 0;
}

.client-info {
  margin-bottom: 15px;
}

.client-info .label {
  color: var(--text-dark);
  font-weight: 600;
  font-size: 12px;
}

.client-info .value {
  color: var(--text-dark);
  font-size: 16px;
  font-weight: 700;
  margin-top: 5px;
}

.invoice-table {
  margin: 15px 0;
}

.invoice-table tbody tr {
  background: var(--secondary-color);
}

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

.invoice-table tbody tr:last-child td {
  border-bottom: none;
}

.note-section {
  display: none;
  margin: 12px 0 14px;
  padding: 12px 14px;
  border-radius: 8px;
  border-right: 4px solid var(--primary-color);
  background: #fff8e3;
}

.note-label {
  color: var(--text-dark);
  font-weight: 700;
  margin-bottom: 4px;
}

.note-text {
  color: var(--text-dark);
  line-height: 1.6;
  white-space: pre-wrap;
}

.empty-state {
  text-align: center;
  color: var(--text-light);
  font-style: italic;
  padding: 20px !important;
}

/* Summary Section */
.summary-section {
  background: var(--secondary-color);
  padding: 20px;
  border-radius: 8px;
  border-right: 4px solid var(--primary-color);
}

.summary-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  font-weight: 600;
  color: var(--text-dark);
}

.summary-row.highlight {
  font-size: 16px;
  padding: 12px;
  background: var(--primary-color);
  color: white;
  border-radius: 4px;
  margin-top: 10px;
}

.summary-row .label {
  font-size: 13px;
}

.summary-row .value {
  font-size: 14px;
}

.summary-row.highlight .value {
  font-size: 18px;
}

/* Invoice Footer */
.invoice-footer {
  margin-top: 28px;
  padding-top: 22px;
  border-top: 1px solid var(--border-color);
  break-inside: avoid;
}

.invoice-footer > p:not(.footer-note) {
  display: none;
}

.footer-note {
  text-align: center;
  color: var(--text-dark);
  font-size: 18px;
  font-weight: 800;
  line-height: 1.2;
  margin-bottom: 10px;
}

.footer-approvals {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8mm;
  align-items: start;
  direction: rtl;
  justify-items: end;
}

.footer-approvals.single-approval {
  grid-template-columns: 1fr;
  gap: 0;
  justify-items: end;
}

.footer-approval {
  color: var(--text-dark);
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  text-align: right;
  justify-self: end;
  min-width: 58mm;
}

.footer-title {
  font-size: 16px;
  font-weight: 800;
  margin-bottom: 4px;
  width: 100%;
  text-align: right;
}

.footer-sign-label {
  font-size: 14px;
  font-weight: 700;
  margin-bottom: 2px;
  width: 100%;
  text-align: right;
}

.footer-signature {
  color: #1f6feb;
  font-size: 18px;
  font-family: "Segoe Script", "Bradley Hand ITC", cursive;
  line-height: 1;
  min-height: 18px;
  transform: rotate(-4deg);
  width: 100%;
  text-align: right;
}

.left-approval {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.footer-stamp {
  width: calc(190px + 1cm);
  height: calc(190px + 1cm);
  margin-top: -3cm;
  margin-right: 0.8cm;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: visible;
  align-self: flex-end;
}

.footer-stamp img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  transform: rotate(-6deg);
  transform-origin: center;
  filter: contrast(1.08) saturate(1.1);
}

/* Responsive Design */
@media (max-width: 1200px) {
  .main-container {
    grid-template-columns: 1fr;
  }
  
  .form-row {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 768px) {
  body {
    padding: 8px;
  }

  .auth-overlay {
    align-items: flex-start;
    padding: calc(env(safe-area-inset-top, 0px) + 12px) 12px 12px;
  }

  .auth-card {
    padding: 18px;
    border-radius: 14px;
    width: 100%;
    max-height: calc(100dvh - 24px);
  }

  .auth-title {
    font-size: 21px;
  }

  .auth-subtitle {
    font-size: 14px;
    margin-bottom: 14px;
  }

  .auth-form {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .auth-submit {
    width: 100%;
  }

  body.app-locked .main-container {
    filter: none;
  }

  .main-container.single-pane {
    max-width: 100%;
  }
  
  .editor-card,
  .invoice-preview {
    padding: 14px;
    border-radius: 10px;
  }

  .section-title {
    font-size: 16px;
    margin: 14px 0 10px;
  }

  .form-input input,
  .form-input textarea {
    font-size: 16px;
  }
  
  .form-row {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .table-container {
    margin: 10px 0;
  }

  table {
    font-size: 13px;
  }
  
  .button-group {
    grid-template-columns: 1fr;
  }

  .btn-primary,
  .btn-secondary,
  .btn-add {
    font-size: 15px;
    padding: 12px 14px;
  }

  .invoice-header {
    flex-direction: column;
    text-align: right;
    direction: rtl;
  }
  
  .company-info {
    justify-content: center;
  }

  .invoice-info {
    margin-top: 10px;
    text-align: right;
    align-items: flex-end;
    min-width: auto;
  }

  .company-logo {
    width: 5cm;
  }

  .footer-note {
    font-size: 18px;
  }

  .footer-approvals {
    grid-template-columns: 1fr;
    gap: 14px;
    justify-items: end;
  }

  .footer-title {
    font-size: 17px;
  }

  .footer-sign-label {
    font-size: 15px;
  }

  .footer-signature {
    font-size: 21px;
  }

  .left-approval {
    align-items: flex-end;
  }

  .footer-stamp {
    width: 130px;
    height: 130px;
    font-size: 15px;
  }
}

@media (max-width: 640px) {
  .table-container {
    overflow: visible;
    border: 0;
  }

  .edit-table thead {
    display: none;
  }

  .edit-table,
  .edit-table tbody,
  .edit-table tr,
  .edit-table td {
    display: block;
    width: 100%;
  }

  .edit-table tbody tr {
    background: #ffffff;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 10px;
    margin-bottom: 10px;
  }

  .edit-table tbody td {
    display: grid;
    grid-template-columns: 88px 1fr;
    gap: 10px;
    align-items: center;
    text-align: right;
    padding: 7px 0;
    border-bottom: 1px dashed var(--border-color);
  }

  .edit-table tbody td:last-child {
    border-bottom: 0;
    padding-bottom: 0;
  }

  .edit-table tbody td::before {
    font-weight: 700;
    color: var(--text-dark);
    text-align: right;
  }

  .edit-table tbody td:nth-child(1)::before { content: "\0627\0644\0628\064A\0627\0646"; }
  .edit-table tbody td:nth-child(2)::before { content: "\0627\0644\0639\062F\062F"; }
  .edit-table tbody td:nth-child(3)::before { content: "\0627\0644\0645\0642\0627\0633"; }
  .edit-table tbody td:nth-child(4)::before { content: "\0627\0644\0648\062D\062F\0629"; }
  .edit-table tbody td:nth-child(5)::before { content: "\0633\0639\0631\0020\0627\0644\0648\062D\062F\0629"; }
  .edit-table tbody td:nth-child(6)::before { content: "\0627\0644\0645\062C\0645\0648\0639"; }
  .edit-table tbody td:nth-child(7)::before { content: "\062D\0630\0641"; }

  .edit-table tbody input,
  .edit-table tbody select {
    font-size: 16px;
    text-align: right;
  }

  .edit-table tbody td:nth-child(6) {
    font-weight: 700;
    color: var(--text-dark);
  }

  .edit-table tbody td:nth-child(7) button {
    width: 100%;
    padding: 10px;
    font-size: 14px;
  }
}

/* Print Styles */
@page {
  size: A4;
  margin: 8mm;
}

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

  body {
    background: white;
    padding: 0;
    margin: 0;
  }
  
  .main-container {
    display: block;
    max-width: none;
    margin: 0;
    gap: 0;
  }
  
  .editor-section {
    display: none;
  }
  
  .preview-section {
    display: block;
    width: 100%;
    margin: 0;
    padding: 0;
  }
  
  .invoice-preview {
    box-shadow: none;
    border-radius: 0;
    padding: 8mm 8mm 6mm;
    max-width: 100%;
    margin: 0;
  }

  .invoice-header {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0;
    direction: ltr;
  }

  .company-info {
    display: flex;
    justify-content: flex-start;
    align-items: center;
  }

  .company-logo {
    width: 5cm;
    margin-bottom: 0;
  }

  .invoice-info {
    text-align: right;
    align-items: flex-end;
    gap: 2px;
    min-width: 40mm;
    margin-top: 0;
    direction: rtl;
  }

  .invoice-divider {
    margin: 10px 0;
  }

  .invoice-table thead th,
  .summary-row.highlight {
    background: var(--primary-color) !important;
    color: #fff !important;
  }

  .invoice-footer {
    margin-top: 12px;
    padding-top: 10px;
    break-inside: auto;
    page-break-inside: auto;
  }

  .footer-approvals {
    grid-template-columns: 1fr 1fr;
    gap: 8mm;
    align-items: start;
    direction: rtl;
    justify-items: end;
  }

  .footer-approval {
    text-align: right;
    justify-self: end;
    min-width: 58mm;
  }

  .left-approval {
    align-items: flex-end;
  }

  .footer-note {
    font-size: 18px;
    margin-bottom: 10px;
  }

  .footer-title {
    font-size: 16px;
    margin-bottom: 4px;
  }

  .footer-sign-label {
    font-size: 14px;
    margin-bottom: 2px;
  }

  .footer-signature {
    font-size: 18px;
    min-height: 18px;
  }

  .footer-stamp {
    width: calc(180px + 1cm);
    height: calc(180px + 1cm);
    margin-top: -3cm;
    margin-right: 0.8cm;
  }
  
  .invoice-preview::before {
    content: '';
    display: block;
    height: 2mm;
    background: var(--primary-color);
    margin-bottom: 10mm;
  }
}
