:root {
  --a4-width: 29.7cm;
  --a4-height: 21cm;
  --cheque-width: 18.42cm;
  --cheque-height: 7.62cm;
  --print-cheque-right: 0cm;
  --print-cheque-top: 6.69cm;
  --print-cheque-right-effective: 0cm;
  --print-cheque-top-effective: 6.69cm;
  --print-rotate: 0deg;
  --bg: #f4f7fb;
  --panel: #ffffff;
  --line: #dce4ef;
  --text: #172033;
  --muted: #66748a;
  --brand: #1769aa;
  --brand-dark: #0d4d82;
  --soft: #eef5fb;
  --danger: #b42318;
  --shadow: 0 10px 30px rgba(24, 39, 75, .08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Tahoma, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

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

.topbar {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 20px;
  padding: 18px 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.topbar h1 {
  margin: 0 0 4px;
  font-size: 24px;
}

.topbar p {
  margin: 0;
  color: var(--muted);
}

.tabs {
  display: flex;
  gap: 8px;
}

.tab,
button {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 14px;
  cursor: pointer;
}

.tab.active,
.primary,
button:hover {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}

.ghost {
  background: var(--soft);
  color: var(--brand-dark);
  border-color: #c8dcec;
}

main {
  padding: 20px;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

#printView {
  grid-template-columns: 390px minmax(0, 1fr);
  gap: 20px;
}

#printView.active {
  display: grid;
}

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

.form-panel {
  padding: 18px;
}

.full-panel {
  padding: 20px;
}

.panel-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.panel-title h2 {
  margin: 0;
  font-size: 20px;
}

label {
  display: grid;
  gap: 6px;
  margin-bottom: 12px;
  color: #26344f;
  font-weight: 700;
}

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

textarea {
  resize: vertical;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.inline-control {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
}

.crossing-control {
  display: grid;
  gap: 8px;
}

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 14px;
}

.workspace {
  min-width: 0;
}

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

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.autosave-note {
  color: var(--brand-dark);
  background: var(--soft);
  border: 1px solid #c8dcec;
  border-radius: 8px;
  padding: 9px 10px;
  font-size: 12px;
  font-weight: 700;
}

#modelInfo {
  color: var(--muted);
  margin-inline-start: 8px;
  font-size: 13px;
}

.switch {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  font-weight: 700;
}

.switch input {
  width: auto;
}

.compact-label,
.mini-input {
  display: grid;
  gap: 4px;
  margin: 0;
  font-size: 12px;
  color: var(--muted);
}

.compact-label select {
  min-width: 130px;
}

.mini-input input {
  width: 70px;
  padding: 8px;
}

.nudge-pad {
  display: grid;
  grid-template-columns: repeat(3, 28px);
  grid-template-rows: repeat(2, 28px);
  gap: 4px;
  align-items: center;
}

.nudge-btn {
  min-width: 28px;
  height: 28px;
  padding: 0;
  border-radius: 6px;
  font-size: 14px;
  line-height: 1;
}

.nudge-btn:nth-child(1) {
  grid-column: 2;
}

.nudge-btn:nth-child(2) {
  grid-column: 1;
  grid-row: 2;
}

.nudge-btn:nth-child(3) {
  grid-column: 3;
  grid-row: 2;
}

.nudge-btn:nth-child(4) {
  grid-column: 2;
  grid-row: 2;
}

.cheque-stage {
  position: relative;
  width: 100%;
  overflow: auto;
  background: #e8eef6;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
}

.cheque-canvas {
  position: relative;
  width: 100%;
  max-width: 1180px;
  aspect-ratio: 184.2 / 76.2;
  margin: 0 auto;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 8px 20px rgba(24, 39, 75, .16);
}

#chequeImage {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center center;
  border-radius: 4px;
}

.overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.field {
  position: absolute;
  min-width: 0;
  min-height: 0;
  padding: 1px 3px;
  color: #111;
  white-space: nowrap;
  pointer-events: auto;
  font-family: Arial, Tahoma, sans-serif;
  line-height: 1.4;
  cursor: move;
  transform-origin: center;
  width: max-content;
  max-width: none;
}

.field:hover {
  outline: 1px dashed var(--brand);
  background: rgba(255, 255, 255, .55);
}

.field.selected-field {
  outline: 2px solid var(--danger);
  background: rgba(255, 255, 255, .55);
}

.field-amount {
  white-space: nowrap;
}

.field-purpose {
  white-space: pre-wrap;
  width: auto;
}

.field-amountWords {
  white-space: nowrap;
  width: max-content;
}

.field small {
  display: none;
}

.field.selected-field small {
  display: block;
  color: var(--brand-dark);
  font-size: 10px;
  font-weight: 700;
}

.crossing-field {
  display: grid;
  align-items: center;
  justify-items: center;
  min-height: 72px;
  transform-origin: center;
  padding: 0;
  text-align: center;
}

.crossing-field.lines-only {
  align-content: center;
  min-height: 18px;
}

.crossing-line {
  display: block;
  width: 100%;
  height: 0;
  border-top: 4px solid #000;
  background: transparent;
}

.crossing-line-only {
  margin-top: 4px;
}

.crossing-line-thin {
  border-top-width: 2px;
}

.crossing-text {
  display: block;
  padding: 3px 0;
  white-space: pre-line;
  font-weight: 800;
  line-height: 1.25;
}

.banks-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 14px;
}

.bank-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  background: #fff;
}

.bank-card h3 {
  margin: 0 0 8px;
  font-size: 16px;
}

.bank-card p {
  margin: 0 0 10px;
  color: var(--muted);
}

.thumbs {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
}

.thumbs img {
  width: 120px;
  height: 58px;
  object-fit: cover;
  border: 1px solid var(--line);
  border-radius: 6px;
}

.filters {
  display: grid;
  grid-template-columns: repeat(4, minmax(160px, 1fr));
  gap: 12px;
}

.summary {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin: 8px 0 14px;
}

.summary span {
  background: var(--soft);
  border: 1px solid #c8dcec;
  border-radius: 8px;
  padding: 9px 12px;
  font-weight: 700;
}

.table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}

table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  min-width: 980px;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 10px;
  text-align: right;
  vertical-align: top;
}

th {
  background: var(--soft);
  font-size: 13px;
}

.toast {
  position: fixed;
  bottom: 18px;
  left: 18px;
  transform: translateY(120px);
  transition: transform .2s ease;
  background: #172033;
  color: #fff;
  border-radius: 8px;
  padding: 12px 14px;
  max-width: 420px;
  z-index: 10;
}

.toast.show {
  transform: translateY(0);
}

@media (max-width: 980px) {
  .topbar,
  .toolbar,
  .toolbar-actions {
    align-items: stretch;
    flex-direction: column;
  }

  #printView.active {
    grid-template-columns: 1fr;
  }

  .filters,
  .grid-2 {
    grid-template-columns: 1fr;
  }
}

@media print {
  @page {
    size: A4 landscape;
    margin: 0;
  }

  html,
  body {
    background: #fff;
    width: var(--a4-width);
    height: var(--a4-height);
    margin: 0;
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }

  .topbar,
  .form-panel,
  .toolbar,
  .toast {
    display: none !important;
  }

  main {
    padding: 0;
    width: var(--a4-width);
    height: var(--a4-height);
  }

  #printView.active {
    display: block;
    width: var(--a4-width);
    height: var(--a4-height);
  }

  .workspace {
    width: var(--a4-width);
    height: var(--a4-height);
  }

  .cheque-stage {
    width: var(--a4-width);
    height: var(--a4-height);
    display: block;
    position: fixed;
    inset: 0;
    border: 0;
    padding: 0;
    overflow: visible;
    background: #fff;
    min-height: 0;
    box-sizing: border-box;
  }

  .cheque-canvas {
    position: fixed;
    right: var(--print-cheque-right-effective);
    top: var(--print-cheque-top-effective);
    left: auto;
    margin: 0;
    width: var(--cheque-width);
    height: var(--cheque-height);
    max-width: none;
    aspect-ratio: auto;
    border-radius: 0;
    box-shadow: none;
    background: #fff;
    transform: rotate(var(--print-rotate));
    transform-origin: top right;
  }

  body.calibration-mode .cheque-canvas {
    border: 1px solid #000;
  }

  body.calibration-mode .cheque-canvas::before {
    content: "معايرة الشيك - قِس الإطار: " var(--cheque-width) " × " var(--cheque-height);
    position: absolute;
    top: 3mm;
    right: 3mm;
    font: 700 12px Arial, Tahoma, sans-serif;
    color: #000;
  }

  #chequeImage {
    width: 100%;
    height: 100%;
    max-width: none;
    box-shadow: none;
    visibility: hidden;
  }

  body.calibration-mode .overlay {
    display: none !important;
  }

  .overlay {
    inset: 0 !important;
    max-width: none;
    width: 100% !important;
    height: 100% !important;
    margin: 0;
  }

  .field {
    outline: 0 !important;
    border: 0 !important;
    background: transparent !important;
    cursor: default;
    color: #111 !important;
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }

  .crossing-line {
    border-top: 4px solid #000 !important;
    background: transparent !important;
  }

  .crossing-line-thin {
    border-top-width: 2px !important;
  }

  .field small {
    display: none !important;
  }
}
