:root {
  --bg: #f5f1e8;
  --panel: #ffffff;
  --line: #272727;
  --muted: #6d6d6d;
  --accent: #8f2727;
  --soft-accent: #f4e8dc;
  --shadow: 0 18px 40px rgba(0, 0, 0, 0.08);
  --tree-box-shadow: 7px 7px 0 rgba(12, 12, 12, 0.92);
  --tree-box-shadow-selected: 0 0 0 3px rgba(143, 39, 39, 0.22), 9px 9px 0 rgba(12, 12, 12, 0.96);
  --radius: 18px;
  --certificate-width: 1200px;
  --certificate-height: 1180px;
  --certificate-content-width: 1333.3333px;
  --certificate-content-height: 1311.1111px;
  --certificate-content-scale: 0.9;
}

* {
  box-sizing: border-box;
}

html {
  color-scheme: light;
}

body {
  margin: 0;
  font-family: "Segoe UI", Tahoma, Arial, sans-serif;
  background: linear-gradient(180deg, #f4efe5 0%, #ece6da 100%);
  color: #171717;
}

button,
input,
textarea,
select {
  font: inherit;
}

button {
  border: 1px solid #d2c7b7;
  background: #fffaf3;
  border-radius: 12px;
  padding: 12px 16px;
  cursor: pointer;
}

button.primary {
  background: #201914;
  color: #fff;
  border-color: #201914;
}

button:hover,
input:hover,
textarea:hover {
  border-color: #bea98d;
}

input:focus,
textarea:focus,
button:focus,
select:focus {
  outline: 2px solid rgba(143, 39, 39, 0.18);
  outline-offset: 1px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  align-items: flex-start;
  padding: 24px;
}

.topbar h1,
.panel h2,
.preview-head h2 {
  margin: 0 0 8px;
}

.topbar p,
.hint,
.section-head p,
.preview-head p,
.selected-help,
.empty-editor,
.quick-tips {
  margin: 0;
  color: var(--muted);
  line-height: 1.6;
}

.topbar-actions,
.upload-row,
.section-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
}

.upload-row[hidden] {
  display: none !important;
}

#openPhotoCameraBtn,
#openOverlayCameraBtn,
#addOverlayBtn {
  font-weight: 700;
}

.camera-status {
  font-size: 13px;
}

.camera-status[hidden] {
  display: none !important;
}

.camera-status.is-error {
  color: #9a1f1f;
}

.camera-status.is-success {
  color: #245c35;
}

body.camera-modal-open {
  overflow: hidden;
}

.camera-modal {
  position: fixed;
  inset: 0;
  z-index: 1500;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(24, 18, 12, 0.58);
  backdrop-filter: blur(4px);
}

.camera-modal[hidden] {
  display: none !important;
}

.camera-dialog {
  width: min(100%, 760px);
  display: grid;
  gap: 16px;
  padding: 20px;
  border: 1px solid #eadfce;
  border-radius: 24px;
  background: #fffdf9;
  box-shadow: 0 24px 50px rgba(0, 0, 0, 0.22);
}

.camera-dialog-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.camera-dialog-head h2 {
  margin: 0 0 6px;
}

.camera-preview-shell {
  position: relative;
  overflow: hidden;
  border-radius: 20px;
  background: #111;
}

.camera-preview {
  width: 100%;
  display: block;
  aspect-ratio: 4 / 3;
  background: #111;
  object-fit: cover;
}

.camera-error {
  position: absolute;
  right: 16px;
  bottom: 16px;
  left: 16px;
  margin: 0;
  padding: 12px 14px;
  border-radius: 14px;
  background: rgba(28, 22, 22, 0.88);
  color: #fff;
  font-size: 14px;
  line-height: 1.6;
}

.camera-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
}

.layout {
  display: grid;
  grid-template-columns: minmax(320px, 420px) minmax(0, 1fr);
  gap: 24px;
  padding: 0 24px 24px;
  align-items: start;
}

.panel {
  background: var(--panel);
  border: 1px solid #e6dccf;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.controls-panel,
.preview-panel {
  padding: 20px;
}

.preview-panel {
  width: 100%;
  max-width: none;
  min-width: 0;
  justify-self: stretch;
}

.controls-panel {
  display: grid;
  gap: 20px;
}

.panel-section {
  border: 1px solid #eee3d7;
  border-radius: 16px;
  padding: 16px;
  background: #fffcf8;
}

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 12px;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.form-grid.compact {
  grid-template-columns: 1fr;
}

.form-grid label,
.field-label {
  display: grid;
  gap: 6px;
}

.form-grid .full {
  grid-column: 1 / -1;
}

input[type="text"],
input[type="number"],
textarea,
input[type="file"],
select {
  width: 100%;
  border: 1px solid #d7ccbe;
  border-radius: 10px;
  padding: 10px 12px;
  background: #fff;
}

textarea {
  resize: vertical;
}

.tips-section {
  background: #fffaf3;
}

.quick-tips {
  padding-inline-start: 20px;
}

.quick-tips li + li {
  margin-top: 8px;
}

.selected-node-editor {
  display: grid;
  gap: 14px;
}

.selected-node-editor.is-empty {
  border: 1px dashed #d7ccbe;
  border-radius: 14px;
  padding: 16px;
  background: #fffdf9;
}

.selected-node-card {
  border: 1px solid #e9dfd1;
  border-radius: 14px;
  padding: 14px;
  background: #fff;
  display: grid;
  gap: 14px;
}

.selected-node-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
}

.selected-node-head h3,
.selected-node-summary {
  margin: 0;
}

.selected-node-summary {
  color: var(--muted);
  font-size: 13px;
}

.status-badge {
  padding: 6px 10px;
  border-radius: 999px;
  background: #f7efe3;
  color: #6e4c2d;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

.preview-stage {
  direction: ltr;
  display: flex;
  width: 100%;
  min-width: 0;
  justify-content: center;
  align-items: flex-start;
  overflow: auto;
  padding-top: 10px;
}

.preview-scaler {
  --preview-scale: 1;
  position: relative;
  flex: 0 0 auto;
  width: calc(var(--certificate-width) * var(--preview-scale));
  height: calc(var(--certificate-height) * var(--preview-scale));
}

.certificate-sheet {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--certificate-width);
  height: var(--certificate-height);
  margin: 0;
  background: linear-gradient(180deg, #fcfbf8 0%, #faf7f1 100%);
  border: 1px solid #d7cec1;
  box-shadow: 0 24px 48px rgba(0, 0, 0, 0.12);
  transform: scale(var(--preview-scale));
  transform-origin: top left;
  direction: rtl;
  overflow: hidden;
}

.certificate-content-shell {
  position: absolute;
  inset: 0;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  pointer-events: none;
}

.certificate-content {
  position: relative;
  width: var(--certificate-content-width);
  height: var(--certificate-content-height);
  flex: 0 0 auto;
  transform: scale(var(--certificate-content-scale));
  transform-origin: top center;
  direction: rtl;
  pointer-events: auto;
}

.certificate-title {
  position: absolute;
  top: 26px;
  left: 316px;
  right: 34px;
  text-align: center;
}

.title-code,
.title-meta,
.info-block,
.owner-block,
.qr-placeholder,
.footer-block {
  position: relative;
}

.title-code {
  font-size: 34px;
  font-weight: 800;
  letter-spacing: 0.6px;
}

.title-meta {
  margin-top: 10px;
  font-size: 15px;
  color: var(--muted);
}

.title-code,
.title-meta,
.info-block,
.owner-block,
.qr-placeholder,
.footer-block {
  cursor: pointer;
}

.preview-inline-editor {
  display: grid;
  gap: 6px;
  align-content: start;
  padding: 6px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.94);
  border: 1px dashed #cfbea7;
}

.preview-inline-editor.compact {
  padding: 5px;
}

.preview-inline-field {
  display: grid;
  gap: 3px;
  text-align: start;
}

.preview-inline-field span {
  font-size: 10px;
  font-weight: 700;
  color: #6c6255;
}

.preview-inline-field input,
.preview-inline-field textarea {
  width: 100%;
  border: 1px solid #d7ccbd;
  border-radius: 8px;
  padding: 5px 7px;
  background: rgba(255, 255, 255, 0.98);
  color: #231c16;
  font: inherit;
  font-size: 12px;
  line-height: 1.35;
}

.preview-inline-field textarea {
  min-height: 52px;
  resize: vertical;
}

.photo-box {
  position: absolute;
  width: 256px;
  height: 210px;
  border: 2px solid var(--line);
  overflow: hidden;
  background: #efebe2;
  z-index: 2;
}

#pigeonPhoto {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: none;
}

.photo-overlay-layer {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: none;
  pointer-events: none;
}

.overlay-photo-item {
  position: absolute;
  border: 2px solid rgba(39, 39, 39, 0.92);
  border-radius: 10px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 10px 22px rgba(0, 0, 0, 0.18);
  cursor: grab;
  user-select: none;
  touch-action: none;
  pointer-events: auto;
}

.overlay-photo-item.dragging {
  cursor: grabbing;
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.24);
}

.overlay-photo-item img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

.overlay-photo-delete {
  position: absolute;
  top: 6px;
  right: 6px;
  z-index: 5;
  width: 28px;
  height: 28px;
}

.photo-placeholder {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  font-size: 22px;
  color: #746d60;
  background: linear-gradient(135deg, #ede7dd, #d8cfbf);
}

.movable-item {
  position: absolute;
  cursor: grab;
  user-select: none;
  touch-action: none;
  pointer-events: auto;
}

.movable-item:hover {
  box-shadow: 0 10px 22px rgba(0, 0, 0, 0.08);
}

.movable-item.dragging {
  cursor: grabbing;
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.12);
}

.main-info-box {
  position: absolute;
  top: 276px;
  left: 18px;
  width: 156px;
  height: 606px;
  border: 2px solid var(--line);
  background: #fff;
  padding: 14px 12px;
  display: grid;
  align-content: start;
  gap: 16px;
  z-index: 2;
  overflow: auto;
  box-shadow: var(--tree-box-shadow);
}

.info-block {
  border-bottom: 1px solid #ded6c8;
  padding-bottom: 12px;
}

.info-block:last-child {
  border-bottom: 0;
  padding-bottom: 0;
}

.info-label {
  color: var(--accent);
  font-weight: 700;
  font-size: 12.5px;
  margin-bottom: 7px;
}

.info-value,
.owner-block,
.footer-block,
.box-line {
  white-space: pre-line;
  word-break: break-word;
  line-height: 1.35;
}

.info-value,
.owner-block,
.footer-block {
  font-size: 13.5px;
}

.owner-block {
  width: 270px;
  min-height: 92px;
  padding: 12px 14px;
  border: 2px solid var(--line);
  background: rgba(255, 255, 255, 0.96);
  z-index: 2;
}

.tree-lines,
.tree-boxes {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.tree-lines {
  z-index: 0;
  pointer-events: none;
  display: block;
}

.tree-boxes {
  z-index: 1;
  pointer-events: none;
}

.tree-box {
  position: absolute;
  border: 2px solid var(--line);
  background: #fff;
  padding: 46px 14px 14px;
  display: grid;
  align-content: start;
  gap: 8px;
  overflow: hidden;
  cursor: grab;
  user-select: none;
  touch-action: none;
  box-shadow: var(--tree-box-shadow);
  pointer-events: auto;
}

.tree-box.editing {
  padding: 46px 10px 10px;
  overflow: auto;
  cursor: default;
}

.tree-box.selected {
  box-shadow: var(--tree-box-shadow-selected);
}

.tree-box.dragging {
  cursor: grabbing;
  opacity: 0.96;
  box-shadow: 12px 12px 0 rgba(12, 12, 12, 0.98);
}

.tree-box.manual-node {
  border-style: dashed;
}

.resize-handle {
  position: absolute;
  z-index: 4;
  border: 1px solid #caa889;
  background: rgba(255, 250, 243, 0.98);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.14);
  opacity: 1;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.tree-box:hover .resize-handle,
.tree-box.selected .resize-handle,
.photo-box:hover .resize-handle,
.photo-box.dragging .resize-handle,
.overlay-photo-item:hover .resize-handle,
.overlay-photo-item.dragging .resize-handle,
.main-info-box:hover .resize-handle,
.main-info-box.dragging .resize-handle {
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.18);
}

.resize-handle-width {
  top: 50%;
  right: 4px;
  width: 10px;
  height: 34px;
  border-radius: 999px;
  cursor: ew-resize;
  transform: translateY(-50%);
}

.resize-handle-height {
  left: 50%;
  bottom: 4px;
  width: 34px;
  height: 10px;
  border-radius: 999px;
  cursor: ns-resize;
  transform: translateX(-50%);
}

.resize-handle-both {
  right: 4px;
  bottom: 4px;
  width: 14px;
  height: 14px;
  border-radius: 4px;
  cursor: nwse-resize;
  background:
    linear-gradient(135deg, transparent 0 38%, #a16b3f 38% 46%, transparent 46% 62%, #a16b3f 62% 70%, transparent 70% 100%),
    rgba(255, 250, 243, 0.98);
}

.tree-box,
.photo-box,
.main-info-box,
.owner-block,
.qr-placeholder {
  border-radius: 4px;
}

.tree-box-toolbar {
  position: absolute;
  top: 8px;
  right: 8px;
  display: flex;
  gap: 6px;
  align-items: center;
}

.tool-btn,
.tool-color {
  width: 30px;
  height: 30px;
  padding: 0;
  border-radius: 999px;
  display: inline-grid;
  place-items: center;
}

.tool-btn {
  border: 1px solid #ccbfae;
  background: rgba(255, 250, 243, 0.95);
  font-weight: 800;
  line-height: 1;
}

.tool-btn.delete {
  color: #8a1d1d;
  border-color: #e6bfbf;
  background: rgba(255, 241, 241, 0.96);
}

.tool-btn.clear {
  color: #575757;
}

.tool-color {
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid #ccbfae;
  overflow: hidden;
  background: #fff;
}

.tool-color::-webkit-color-swatch-wrapper {
  padding: 0;
}

.tool-color::-webkit-color-swatch,
.tool-color::-moz-color-swatch {
  border: 0;
}

.box-label-chip {
  display: inline-flex;
  align-items: center;
  width: fit-content;
  max-width: 100%;
  padding: 5px 11px;
  border-radius: 999px;
  background: var(--soft-accent);
  color: #5d4030;
  font-size: 12.5px;
  font-weight: 700;
}

.manual-node .box-label-chip {
  background: #f3f0ff;
  color: #534194;
}

.box-ring {
  color: var(--accent);
  font-weight: 800;
  font-size: 14px;
}

.box-name {
  font-size: 16px;
  font-weight: 700;
}

.box-desc,
.box-note,
.box-empty-tip {
  font-size: 12.5px;
  line-height: 1.45;
}

.box-empty-tip {
  color: var(--muted);
}

.inline-node-editor {
  display: grid;
  gap: 6px;
  align-content: start;
}

.inline-node-editor-hint {
  color: var(--accent);
  font-size: 11px;
  font-weight: 700;
}

.inline-node-field {
  display: grid;
  gap: 3px;
}

.inline-node-field span {
  color: #6c6255;
  font-size: 10px;
  font-weight: 700;
}

.inline-node-field input,
.inline-node-field textarea {
  width: 100%;
  border: 1px solid #d7ccbd;
  border-radius: 8px;
  padding: 5px 7px;
  background: rgba(255, 255, 255, 0.94);
  color: #231c16;
  font: inherit;
  font-size: 11px;
  line-height: 1.3;
}

.inline-node-field textarea {
  min-height: 42px;
  resize: vertical;
}

.certificate-footer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.qr-placeholder {
  width: 104px;
  height: 104px;
  border: 2px solid var(--line);
  display: grid;
  place-items: center;
  font-weight: 800;
  letter-spacing: 3px;
  background:
    linear-gradient(90deg, #000 10%, transparent 10%) 0 0/17px 17px,
    linear-gradient(#000 10%, transparent 10%) 0 0/17px 17px,
    #fff;
  z-index: 2;
  pointer-events: auto;
}

.qr-placeholder.editing-inline {
  letter-spacing: normal;
  background: rgba(255, 255, 255, 0.96);
  place-items: stretch;
  padding: 6px;
}

.qr-placeholder.editing-inline .preview-inline-editor {
  align-self: stretch;
  justify-self: stretch;
  height: 100%;
}

.footer-block {
  width: 390px;
  min-height: 46px;
  padding: 10px 16px;
  z-index: 2;
  pointer-events: auto;
}

.watermark-block {
  text-align: center;
  font-size: 16.5px;
  font-weight: 800;
  color: rgba(74, 53, 33, 0.94);
  border: 1px dashed #cfbea7;
  border-radius: 999px;
  background: rgba(255, 249, 241, 0.92);
  letter-spacing: 0.28px;
  text-shadow: 0 0 0.01px currentColor;
}

/* العلامة المائية: مخفية أثناء الاستخدام وتظهر فقط عند الطباعة (حفظ الحقوق). */
@media screen {
  .watermark-block {
    display: none !important;
  }
}

.tree-path {
  fill: none;
  stroke: #363636;
  stroke-width: 2;
  stroke-linecap: square;
  stroke-linejoin: round;
}

[dir="auto"] {
  unicode-bidi: plaintext;
}

body.is-dragging {
  cursor: grabbing;
}

.certificate-sheet.is-exporting .overlay-photo-delete,
.certificate-sheet.is-exporting .overlay-resize-handle {
  display: none !important;
}

@media (max-width: 1320px) {
  .layout {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 820px) {
  .section-head,
  .selected-node-head,
  .topbar {
    flex-direction: column;
    align-items: stretch;
  }

  .form-grid {
    grid-template-columns: 1fr;
  }

  .camera-modal {
    padding: 12px;
  }

  .camera-dialog {
    padding: 16px;
    border-radius: 20px;
  }

  .camera-actions > button {
    flex: 1 1 180px;
  }
}

@media print {
  @page {
    size: A4 portrait;
    margin: 0;
  }

  html,
  body {
    width: 210mm;
    height: 297mm;
    margin: 0;
    padding: 0;
    background: linear-gradient(180deg, #fcfbf8 0%, #faf7f1 100%);
    overflow: hidden;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  .topbar,
  .controls-panel,
  .preview-head,
  .tree-box-toolbar {
    display: none !important;
  }

  .info-label,
  .box-label-chip,
  .box-empty-tip,
  .inline-node-editor,
  .preview-inline-editor,
  .resize-handle,
  .overlay-photo-delete {
    display: none !important;
  }

  .main-info-box .info-block {
    border-bottom: 0 !important;
  }

  .layout,
  .preview-panel,
  .preview-stage {
    display: block;
    direction: ltr;
    width: 210mm;
    height: 297mm;
    padding: 0;
    margin: 0;
    border: 0;
    box-shadow: none;
    background: linear-gradient(180deg, #fcfbf8 0%, #faf7f1 100%);
    overflow: hidden;
  }

  .preview-scaler {
    position: relative;
    width: 210mm;
    height: 297mm;
    background: linear-gradient(180deg, #fcfbf8 0%, #faf7f1 100%);
    overflow: hidden;
  }

  .certificate-sheet {
    position: absolute;
    top: 0;
    left: 0;
    width: 210mm;
    height: 297mm;
    border: 0;
    box-shadow: none;
    margin: 0;
    background: linear-gradient(180deg, #fcfbf8 0%, #faf7f1 100%);
    transform: none !important;
    transform-origin: top left;
    overflow: hidden;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  .certificate-content-shell {
    position: absolute;
    inset: 0;
    display: block;
    padding-top: 0;
    overflow: hidden;
  }

  .certificate-content {
    position: absolute;
    top: 0;
    left: 0;
    width: var(--certificate-content-width);
    height: var(--certificate-content-height);
    transform: scale(0.5952756);
    transform-origin: top left;
  }

  .tree-box {
    position: absolute;
    overflow: hidden;
    box-shadow: none !important;
    filter: drop-shadow(5px 5px 0 #111);
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  .tree-box.selected {
    filter: drop-shadow(7px 7px 0 #111);
  }

  .movable-item {
    box-shadow: none !important;
  }

  .watermark-block {
    visibility: visible;
  }
}
