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

@font-face {
  font-family: "JetBrains Mono";
  src: url("fonts/regular.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "JetBrains Mono";
  src: url("fonts/semibold.woff2") format("woff2");
  font-weight: 600;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "OCR-B";
  src: url("fonts/OCR-B.ttf") format("truetype");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

:root {
  --bg: #f7f7f7;
  --panel: #ffffff;
  --fg: #0f172a;
  --muted: #6b7280;
  --border: #d6dbe1;
  --accent: #0ea5e9;
  --bad: #dc2626;
  --ok: #16a34a;

  --radius-sm: 4px;
  --radius-md: 6px;
  --radius-lg: 10px;

  --space-1: 4px;
  --space-2: 6px;
  --space-3: 10px;
  --space-4: 12px;
  --space-5: 18px;
  --space-6: 24px;
  --space-7: 48px;
  --space-8: 96px;

  --shadow-sm: 0 2px 6px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 10px 25px rgba(15, 23, 42, 0.12);

  --font-mono: "JetBrains Mono", Consolas, "Cascadia Mono", "Courier New", monospace;
  --font-ocr: var(--font-mono);

  --layout-max: 1180px;
  --main-width: 780px;
  --guide-width: 380px;
  --guide-gap: 12px;
}

body {
  font-family: var(--font-mono);
  background: var(--bg);
  color: var(--fg);
  margin: 0;
  padding: var(--space-5) var(--space-4) var(--space-6);
  min-height: 100vh;
}

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

.page {
  width: 100%;
  max-width: var(--layout-max);
  margin: 0 auto;
}

.layout {
  width: 100%;
  max-width: var(--layout-max);
  margin: 0 auto;
  position: relative;
}

.stack {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.row {
  display: flex;
  gap: var(--space-2);
  align-items: center;
  flex-wrap: wrap;
}

.header {
  position: relative;
  width: 100%;
  max-width: var(--layout-max);
  margin: 0 auto var(--space-3);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 var(--space-1);
  gap: var(--space-2);
  flex-wrap: wrap;
}

.header-title {
  margin: 0;
  font-size: 1.25rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
}

.header-sub {
  font-size: 0.6em;
  letter-spacing: 0;
  margin-left: 1px;
  opacity: 0.38;
  text-transform: none;
  position: relative;
  top: 0.32em;
}

.card {
  background: var(--panel);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
}

.card--app {
  display: grid;
  gap: var(--space-3);
  width: min(100%, var(--main-width));
  margin: 0 auto;
}

.panel {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--space-3);
  background: #fbfbfb;
}

.guide {
  font-size: 0.78rem;
  line-height: 1.4;
  color: var(--muted);
}

.guide-title {
  margin: 0 0 var(--space-2);
  font-size: 0.78rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--fg);
}

.guide ol {
  margin: 0 0 var(--space-3) var(--space-5);
  padding: 0;
  line-height: 1.4;
}

.guide b {
  color: var(--fg);
}

.guide-tip {
  font-size: 0.72rem;
  color: var(--muted);
  border-top: 1px dashed var(--border);
  padding-top: var(--space-3);
}

.guide-kicker {
  margin-top: var(--space-5);
  font-size: 0.78rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--fg);
}

.guide-list {
  margin: var(--space-2) 0 0 var(--space-5);
  padding: 0;
  line-height: 1.4;
}

.guide ol li,
.guide-list li {
  margin: var(--space-2) 0;
}

.like-row {
  margin-top: var(--space-3);
  padding-top: var(--space-3);
  border-top: 1px dashed var(--border);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-3);
}

.like-text {
  font-size: 0.78rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
  white-space: nowrap;
}

#bmc-wbtn {
  opacity: 0 !important;
  pointer-events: none !important;
}

.preview {
  position: relative;
  height: 32vh;
  min-height: 220px;
  max-height: 380px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-2);
  background: #fdfdfd;
  overflow: hidden;
  border: none;
  outline: none;
  box-shadow: none;
  touch-action: none;
}

.preview__img {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: var(--radius-md);
  object-fit: contain;
  object-position: center;
  position: relative;
  z-index: 2;
  touch-action: none;
}

.preview:not(.has-image) .preview__img {
  display: none;
}

.preview::before {
  content: "";
  position: absolute;
  inset: var(--space-2);
  border-radius: var(--radius-md);
  border: 1px dashed #c5cbd3;
  pointer-events: none;
  z-index: 4;
}

.preview::after {
  content: "";
  position: absolute;
  inset: var(--space-4);
  border-radius: var(--radius-md);
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
  opacity: 0;
  filter: grayscale(1) contrast(1.08) brightness(1.08);
  pointer-events: none;
  z-index: 1;
}

.preview:not(.has-image)::after {
  opacity: 0.34;
}

.card--app[data-mrz-type="td1"] .preview:not(.has-image)::after { background-image: url("img/TD1.jpeg"); }
.card--app[data-mrz-type="td2"] .preview:not(.has-image)::after { background-image: url("img/TD2.JPG"); }
.card--app[data-mrz-type="td3"] .preview:not(.has-image)::after { background-image: url("img/TD3.jpg"); }
.card--app[data-mrz-type="mrva"] .preview:not(.has-image)::after { background-image: url("img/MRV-A.png"); }
.card--app[data-mrz-type="mrvb"] .preview:not(.has-image)::after { background-image: url("img/MRV-B.JPG"); }

.center-upload {
  position: absolute;
  z-index: 2;
}

.preview.has-image .center-upload {
  display: none;
}

.toolbar {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: var(--space-2);
}

.tools {
  display: none;
}

.preview.has-image + .tools {
  display: flex;
}

.image-actions {
  position: absolute;
  top: var(--space-2);
  left: var(--space-2);
  z-index: 7;
  display: none;
  gap: var(--space-2);
  flex-wrap: wrap;
  align-items: flex-start;
  max-width: calc(100% - (var(--space-2) * 2));
}

.preview.has-image .image-actions {
  display: flex;
}

.image-action-btn {
  background: rgba(255, 255, 255, 0.92);
}

.icon-reset {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%235b6574' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><polyline points='1 4 1 10 7 10'/><path d='M3.51 15a9 9 0 1 0 2.13-9.36L1 10'/></svg>");
}

.icon-clear {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%235b6574' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M3 6h18'/><path d='M8 6V4h8v2'/><path d='M19 6l-1 14H6L5 6'/><path d='M10 11v6'/><path d='M14 11v6'/></svg>");
}

.icon-rot-left {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%235b6574' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><polyline points='1 4 1 10 7 10'/><path d='M3.51 15a9 9 0 1 0 2.13-9.36L1 10'/></svg>");
}

.icon-rot-right {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%235b6574' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><polyline points='23 4 23 10 17 10'/><path d='M20.49 15a9 9 0 1 1-2.13-9.36L23 10'/></svg>");
}

.icon-rotation-right {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%235b6574' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M9 4.55a8 8 0 0 1 6 14.9'/><path d='M15 15v5h5'/></svg>");
}

.action-group {
  position: relative;
}

.rot-menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  background: rgba(255, 255, 255, 0.98);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
  padding: var(--space-2);
  display: none;
  grid-template-columns: repeat(2, minmax(90px, 1fr));
  gap: var(--space-2);
  min-width: 210px;
}

.rot-menu.open {
  display: grid;
}

.btn-group {
  display: flex;
  gap: var(--space-2);
  align-items: center;
  flex-wrap: wrap;
  width: 100%;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  border: 1px solid #c6ccd4;
  border-radius: 7px;
  background: #ffffff;
  color: #4b5563;
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  transition: border-color 0.12s ease, box-shadow 0.12s ease, background-color 0.12s ease, color 0.12s ease;
  padding: 6px 10px;
  min-height: 40px;
  font-size: 0.72rem;
}

.btn:focus-visible {
  outline: 3px solid rgba(14, 165, 233, 0.45);
  outline-offset: 2px;
}

.btn:hover {
  border-color: #94a3b8;
  color: #374151;
}

.btn:active {
  box-shadow: none;
}

.btn--primary {
  background: #0f172a;
  border-color: #0f172a;
  color: #ffffff;
  font-weight: 700;
}

.btn--primary:hover {
  background: #111827;
  border-color: #111827;
  color: #ffffff;
}

.btn--ghost {
  background: transparent;
  border-style: dashed;
}

.btn.is-active {
  border-color: #0f172a;
  box-shadow: 0 0 0 2px rgba(15, 23, 42, 0.1);
}

.status {
  color: var(--muted);
  font-size: 0.8rem;
  min-height: 1em;
}

.status:empty {
  display: none;
}

.field {
  width: 100%;
  padding: var(--space-3) var(--space-4);
  font-size: 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: #ffffff;
  color: var(--fg);
  min-height: 44px;
}

.field--select {
  flex: 1;
  min-width: 200px;
  font-size: 0.82rem;
  font-weight: 600;
  color: #0f172a;
  border-color: #aeb8c5;
  background-color: #f8fafc;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.9);
  appearance: none;
  background-image:
    linear-gradient(45deg, transparent 50%, #6b7280 50%),
    linear-gradient(135deg, #6b7280 50%, transparent 50%),
    linear-gradient(to right, transparent, transparent);
  background-position:
    calc(100% - 18px) calc(50% - 3px),
    calc(100% - 12px) calc(50% - 3px),
    calc(100% - 44px) 50%;
  background-size:
    6px 6px,
    6px 6px,
    1px 26px;
  background-repeat: no-repeat;
  padding-right: 52px;
}

.field--select:hover {
  border-color: #94a3b8;
  background-color: #ffffff;
}

.field--text {
  font-family: var(--font-ocr);
  text-transform: uppercase;
}

.field:focus-visible {
  outline: 3px solid rgba(14, 165, 233, 0.35);
  outline-offset: 2px;
  border-color: rgba(14, 165, 233, 0.8);
}

.field.filled {
  border-color: #38bdf8;
  background: #f0f9ff;
}

.mrz-inputs {
  display: grid;
  gap: var(--space-2);
}

.mrz-editor {
  display: grid;
  grid-template-columns: 44px 1fr;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  background: #ffffff;
}

.mrz-editor.filled {
  border-color: var(--border);
  background: #ffffff;
}

.mrz-editor__gutter {
  font-family: "OCR-B", monospace;
  padding: var(--space-3) var(--space-2);
  background: #f8fafc;
  color: var(--muted);
  border-right: 1px solid var(--border);
  text-align: right;
  user-select: none;
  white-space: pre;
  font-size: 1.1rem;
  line-height: 1.3;
  font-variant-numeric: tabular-nums;
  font-kerning: none;
  font-variant-ligatures: none;
}

.mrz-editor__textarea {
  width: 100%;
  border: 0;
  border-radius: 0;
  margin: 0;
  padding: var(--space-3) var(--space-4);
  min-height: calc((3 * 1.3em) + (2 * var(--space-3)));
  height: calc((3 * 1.3em) + (2 * var(--space-3)));
  resize: none;
  overflow: hidden;
  font-family: "OCR-B", monospace;
  font-size: 1.1rem;
  line-height: 1.3;
  text-transform: uppercase;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1, "liga" 0;
  font-kerning: none;
  font-variant-ligatures: none;
  font-synthesis: none;
  letter-spacing: 0;
  tab-size: 1;
  background: transparent;
  color: var(--fg);
  caret-color: var(--fg);
}

#btn-upload,
.check-btn,
#donate-btn {
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 0.69rem;
  letter-spacing: 0.1em;
  background: #0f172a !important;
  color: #ffffff !important;
  border: 1px solid #0f172a !important;
  box-shadow: none !important;
  filter: none !important;
  transform: none !important;
  transition: background-color 0.12s ease, color 0.12s ease, border-color 0.12s ease;
}

#btn-upload {
  min-width: 124px;
}

.check-btn {
  min-width: 116px;
  padding: 6px 14px;
  font-size: 0.7rem;
  letter-spacing: 0.11em;
}

#donate-btn {
  min-width: 96px;
}

#btn-upload:hover,
#btn-upload:focus-visible,
#btn-upload:active,
.check-btn:hover,
.check-btn:focus-visible,
.check-btn:active,
#donate-btn:hover,
#donate-btn:focus-visible,
#donate-btn:active {
  background: #f8fafc !important;
  color: #0f172a !important;
  border-color: #c6ccd4 !important;
  box-shadow: none !important;
  filter: none !important;
  transform: none !important;
}

.mrz-editor__textarea:focus-visible {
  outline: 2px solid rgba(148, 163, 184, 0.45);
  outline-offset: -3px;
}

.mrz-editor__textarea::placeholder {
  color: #94a3b8;
}

.mrz-result {
  padding: var(--space-3) var(--space-4);
  background: #f9fafb;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  font-size: 0.82rem;
  line-height: 1.5;
  font-family: var(--font-ocr);
  white-space: pre;
  overflow-x: auto;
  display: none;
  position: relative;
}

.mrz-result.show {
  display: block;
}

.mrz-result b {
  display: block;
  margin-bottom: var(--space-2);
  border-bottom: 1px solid var(--border);
  padding-bottom: var(--space-1);
}

.mrz-result .ok,
.mrz-result .bad {
  font-weight: 700;
  border-radius: 999px;
  padding: 2px 6px;
  display: inline-block;
}

.mrz-result .ok {
  color: var(--ok);
  background: rgba(22, 163, 74, 0.12);
}

.mrz-result .bad {
  color: var(--bad);
  background: rgba(220, 38, 38, 0.12);
}

.export-actions {
  display: flex;
  gap: var(--space-2);
  margin-top: var(--space-2);
  justify-content: flex-end;
}

.export-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  border: 1px solid #c6ccd4;
  border-radius: 7px;
  background: #ffffff;
  color: #4b5563;
  padding: 4px 6px;
  font-size: 0.62rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  cursor: pointer;
}

.export-btn:hover {
  border-color: #94a3b8;
  color: #374151;
}

.export-btn:focus-visible {
  outline: 3px solid rgba(14, 165, 233, 0.35);
  outline-offset: 2px;
}

.export-icon {
  width: 14px;
  height: 14px;
  display: inline-block;
  background: no-repeat center / contain;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%235b6574' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M14 3h7v7'/><path d='M21 3l-9 9'/><path d='M21 14v6a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1h6'/></svg>");
}

.export-icon.icon-rotation-right {
  width: 16px;
  height: 16px;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%230b0f14' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M21 12a9 9 0 1 1-9-9c2.52 0 4.93 1 6.74 2.74L21 8'/><path d='M21 3v5h-5'/></svg>");
}

.export-icon.icon-ocr {
  width: 16px;
  height: 16px;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%230b0f14' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M3 7V5a2 2 0 0 1 2-2h2'/><path d='M17 3h2a2 2 0 0 1 2 2v2'/><path d='M21 17v2a2 2 0 0 1-2 2h-2'/><path d='M7 21H5a2 2 0 0 1-2-2v-2'/><path d='M7 8h8'/><path d='M7 12h10'/><path d='M7 16h6'/></svg>");
}

.export-icon.icon-select-area {
  width: 16px;
  height: 16px;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%230b0f14' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M12.034 12.681a.498.498 0 0 1 .647-.647l9 3.5a.5.5 0 0 1-.033.943l-3.444 1.068a1 1 0 0 0-.66.66l-1.067 3.443a.5.5 0 0 1-.943.033z'/><path d='M5 3a2 2 0 0 0-2 2'/><path d='M19 3a2 2 0 0 1 2 2'/><path d='M5 21a2 2 0 0 1-2-2'/><path d='M9 3h1'/><path d='M9 21h2'/><path d='M14 3h1'/><path d='M3 9v1'/><path d='M21 9v2'/><path d='M3 14v1'/></svg>");
}

.export-icon.icon-reset {
  width: 16px;
  height: 16px;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%230b0f14' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8'/><path d='M3 3v5h5'/></svg>");
}

.export-icon.icon-clear {
  width: 16px;
  height: 16px;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%230b0f14' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M10 11v6'/><path d='M14 11v6'/><path d='M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6'/><path d='M3 6h18'/><path d='M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2'/></svg>");
}

.export-label {
  font-weight: 700;
}

.crop-preview {
  display: none;
  background: #f9fafb;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  padding: var(--space-2);
  overflow: hidden;
}

.crop-preview canvas {
  display: block;
  max-width: 100%;
  height: auto;
  border-radius: var(--radius-md);
}

.disclaimer {
  margin-top: var(--space-3);
  color: var(--muted);
  font-size: 0.68rem;
  text-align: center;
}

#selection-rect {
  position: absolute;
  border: 2px dotted var(--fg);
  pointer-events: none;
  display: none;
  z-index: 10;
}

#selection-rect.is-detected {
  border-style: dotted;
  border-color: var(--fg);
}

@media (min-width: 980px) {
  .toolbar {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }

  .btn-group {
    width: auto;
  }

  .preview {
    height: 34vh;
    min-height: 260px;
    max-height: 420px;
  }

  .guide {
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(calc((var(--main-width) / 2) + var(--guide-gap)));
    width: var(--guide-width);
  }

  .export-actions {
    position: absolute;
    top: var(--space-2);
    right: var(--space-2);
    margin-top: 0;
  }

  .mrz-result {
    padding-right: var(--space-8);
  }
}
