/* EasyPen - mobile-first, RTL */

:root {
  --primary: #2f4fc4;
  --primary-dark: #243a9e;
  --secondary: #1e1b6b;
  --primary-soft: #e8ecfb;
  --bg: #f4f5fa;
  --surface: #ffffff;
  --text: #1c2033;
  --muted: #5d6378;
  --border: #dfe3f0;
  --danger: #d6385a;
  --success: #177a52;
  --radius: 14px;
  --shadow: 0 4px 18px rgba(30, 27, 107, 0.12);
  --bar-h: 72px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, Arial, "Noto Sans Hebrew", sans-serif;
}

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

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
}

h1, h2 {
  margin: 0 0 .25em;
  line-height: 1.25;
}

h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }

img { max-width: 100%; }

.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.muted { color: var(--muted); }
.small { font-size: .85rem; }
[hidden] { display: none !important; }

:focus-visible {
  outline: 3px solid var(--secondary);
  outline-offset: 2px;
}

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .5rem;
  min-height: 44px;
  padding: .6rem 1.1rem;
  border: 0;
  border-radius: 12px;
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  transition: background-color .15s, transform .1s, box-shadow .15s;
}

.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .5; cursor: default; }

.btn-primary {
  color: #fff;
  background: linear-gradient(135deg, var(--primary), var(--secondary));
  box-shadow: 0 4px 14px rgba(47, 79, 196, .35);
}

.btn-secondary {
  color: var(--primary-dark);
  background: var(--primary-soft);
}

.btn-ghost {
  color: var(--primary-dark);
  background: transparent;
}

.btn-danger {
  color: #fff;
  background: var(--danger);
}

.danger-text { color: var(--danger); }

.btn-sm { min-height: 44px; padding: .35rem .8rem; font-size: .9rem; }
.btn-xl { min-height: 64px; padding: 1rem 1.5rem; font-size: 1.25rem; border-radius: 16px; }
.btn-block { display: flex; width: 100%; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  padding: 0 .5rem;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.icon-btn:active { background: rgba(0, 0, 0, .06); }

/* ---------- Header ---------- */

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  min-height: 56px;
  padding: .5rem 1rem;
  padding-top: calc(.5rem + env(safe-area-inset-top, 0px));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: .5rem;
  font-size: 1.2rem;
  font-weight: 800;
  color: var(--primary-dark);
}

/* ---------- Home ---------- */

.home-main {
  max-width: 560px;
  margin: 0 auto;
  padding: 1rem 1rem 0;
}

.upload-card,
.signatures-section {
  padding: 1.25rem;
  margin-bottom: 1rem;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.upload-card { text-align: center; }
.upload-card p { margin: 0 0 1rem; }

.upload-btn {
  position: relative;
  width: 100%;
  margin-bottom: .5rem;
}

.upload-btn:focus-within {
  outline: 3px solid var(--secondary);
  outline-offset: 3px;
}

.privacy-note {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: .4rem;
  margin: .75rem 0 0 !important;
  font-size: .85rem;
  color: var(--success);
}

.form-error {
  min-height: 1.5em;
  margin: .25rem 0;
  color: var(--danger);
  font-weight: 600;
}

.form-error:empty { min-height: 0; margin: 0; }

.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: .75rem;
}

.sig-list {
  display: grid;
  gap: .75rem;
  margin: 0 0 1rem;
  padding: 0;
  list-style: none;
}

.sig-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  padding: .5rem .75rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fff;
}

.sig-card img {
  width: 55%;
  height: 64px;
  object-fit: contain;
}

.sig-card-actions {
  display: flex;
  flex-direction: column;
  gap: .25rem;
}

.empty-state {
  margin: 0 0 1rem;
  text-align: center;
}

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  inset-inline: 1rem;
  bottom: calc(var(--bar-h) + 1rem + var(--safe-bottom));
  z-index: 100;
  max-width: 480px;
  margin: 0 auto;
  padding: .75rem 1rem;
  border-radius: 12px;
  background: #1f2440;
  color: #fff;
  text-align: center;
  font-weight: 600;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(12px);
  pointer-events: none;
  transition: opacity .2s, transform .2s;
}

.home .toast { bottom: calc(1rem + var(--safe-bottom)); }
.toast.show { opacity: 1; transform: none; }
.toast[data-type="error"] { background: var(--danger); }
.toast[data-type="success"] { background: var(--success); }

/* ---------- Modals (native <dialog>) ---------- */

.modal {
  width: min(100% - 1.5rem, 520px);
  max-height: calc(100% - 1.5rem);
  padding: 0;
  border: 0;
  border-radius: 18px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 10px 40px rgba(15, 18, 50, .3);
}

.modal::backdrop { background: rgba(17, 20, 45, .55); }

/* Bottom sheet style on phones */
.modal.sheet {
  margin-bottom: 0;
  width: 100%;
  max-width: 560px;
  border-radius: 18px 18px 0 0;
  padding-bottom: var(--safe-bottom);
}

.modal-card { padding: 1rem 1.1rem 1.1rem; }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: .5rem;
}

.modal-header h2 { margin: 0; }
.modal-hint { margin: 0 0 .5rem; font-size: .9rem; color: var(--muted); }

.modal-actions {
  display: flex;
  gap: .5rem;
  margin-top: .75rem;
}

.modal-actions .btn { flex: 1; }

/* Signature drawing */
.sig-canvas-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 2 / 1;
  max-height: 45vh;
  border: 2px dashed #c3cbef;
  border-radius: 12px;
  background: #fff;
  overflow: hidden;
}

.sig-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
}

.sig-baseline {
  position: absolute;
  inset-inline: 8%;
  bottom: 25%;
  border-bottom: 1px solid #d6dcf2;
  pointer-events: none;
}

.sig-tools {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: .5rem;
}

.ink-colors { display: flex; gap: .75rem; }

.ink {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  min-height: 44px;
  cursor: pointer;
}

.ink input { position: absolute; opacity: 0; }

.ink-dot {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 3px solid transparent;
  box-shadow: 0 0 0 1px var(--border);
}

.ink-black { background: #111827; }
.ink-blue { background: #1e3a8a; }
.ink input:checked + .ink-dot { border-color: #fff; box-shadow: 0 0 0 2px var(--primary); }
.ink input:focus-visible + .ink-dot { outline: 3px solid var(--secondary); outline-offset: 2px; }

.check {
  display: flex;
  align-items: center;
  gap: .5rem;
  min-height: 44px;
  font-weight: 600;
  cursor: pointer;
}

.check input { width: 20px; height: 20px; accent-color: var(--primary); }

/* Signature picker */
.sig-choices {
  display: grid;
  gap: .6rem;
  margin: 0 0 .75rem;
  padding: 0;
  list-style: none;
}

.sig-choice {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 84px;
  padding: .5rem;
  border: 2px solid var(--border);
  border-radius: 12px;
  background: #fff;
  cursor: pointer;
}

.sig-choice:active,
.sig-choice:hover { border-color: var(--primary); }
.sig-choice img { max-height: 100%; object-fit: contain; }

/* ---------- Viewer ---------- */

.viewer {
  padding-bottom: calc(var(--bar-h) + var(--safe-bottom));
  background: #e6e9f2;
}

.viewer-header { justify-content: flex-start; }

.doc-title {
  display: flex;
  flex-direction: column;
  min-width: 0;
  line-height: 1.25;
}

.doc-title #doc-name {
  overflow: hidden;
  font-weight: 700;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.pages {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 12px 8px 24px;
}

.page {
  position: relative;
  width: 100%;
  max-width: 900px;
  background: #fff;
  box-shadow: 0 2px 10px rgba(20, 25, 70, .15);
}

.page.has-dragging { z-index: 2; }

.page canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.page:not(.is-rendered)::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, #f1f3f9, #fbfbfe, #f1f3f9);
}

.overlay-layer {
  position: absolute;
  inset: 0;
  direction: ltr;   /* overlay coordinates are physical (left/top) */
}

.text-mode .overlay-layer {
  cursor: text;
  outline: 2px dashed rgba(47, 79, 196, .5);
  outline-offset: -2px;
}

/* Overlay items */
.ov {
  position: absolute;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  cursor: grab;
}

.ov.is-selected { outline: 2px dashed var(--primary); outline-offset: 2px; }
.ov.is-dragging { cursor: grabbing; opacity: .9; }

.ov-sig img {
  display: block;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.ov-resize {
  position: absolute;
  bottom: -14px;
  left: -14px;
  display: none;
  width: 28px;
  height: 28px;
  border: 3px solid #fff;
  border-radius: 50%;
  background: var(--primary);
  box-shadow: 0 1px 4px rgba(0, 0, 0, .3);
  cursor: nesw-resize;
}

/* Bigger invisible touch target around the handle */
.ov-resize::after {
  content: "";
  position: absolute;
  inset: -10px;
}

.ov.is-selected .ov-resize { display: block; }

.ov-text {
  font-family: Arial, Helvetica, "Noto Sans Hebrew", sans-serif;
  font-size: calc(var(--fs, 14) * var(--pt, 1) * 1px);
  line-height: 1.3;
  color: #111827;
}

.ov-text-content {
  min-width: 1.5em;
  min-height: 1.3em;
  padding: .15em .3em;
  white-space: pre;
  outline: none;
}

.ov-text.is-editing {
  cursor: text;
  user-select: text;
  -webkit-user-select: text;
  background: rgba(47, 79, 196, .08);
}

.ov-text.is-editing .ov-text-content { user-select: text; -webkit-user-select: text; }

.ov-move {
  position: absolute;
  top: -30px;
  right: -4px;
  display: none;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 16px;
  line-height: 28px;
  text-align: center;
  cursor: move;
}

.ov-text.is-selected .ov-move { display: block; }

/* Status / loading */
.status-screen {
  position: fixed;
  inset: 56px 0 calc(var(--bar-h) + var(--safe-bottom));
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 2rem;
  text-align: center;
  background: var(--bg);
}

.status-screen.is-error .spinner { display: none; }
.status-screen.is-error p { color: var(--danger); font-weight: 700; }

.spinner {
  width: 44px;
  height: 44px;
  border: 4px solid var(--primary-soft);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.busy {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  background: rgba(255, 255, 255, .85);
  font-weight: 700;
}

/* Contextual bars above the bottom bar */
.mode-hint,
.item-toolbar {
  position: fixed;
  inset-inline: .5rem;
  bottom: calc(var(--bar-h) + .5rem + var(--safe-bottom));
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  max-width: 560px;
  margin: 0 auto;
  padding: .4rem .6rem;
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.mode-hint { background: var(--primary-soft); font-weight: 600; }

.text-tools {
  display: flex;
  align-items: center;
  gap: .25rem;
  margin-inline-end: auto;
}

.font-size { min-width: 3.2em; text-align: center; font-weight: 700; }

/* Sticky bottom action bar */
.bottom-bar {
  position: fixed;
  inset-inline: 0;
  bottom: 0;
  z-index: 40;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  height: calc(var(--bar-h) + var(--safe-bottom));
  padding: 8px 8px calc(8px + var(--safe-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
  box-shadow: 0 -4px 16px rgba(30, 27, 107, .08);
}

.bar-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--primary-dark);
  font: inherit;
  font-size: .85rem;
  font-weight: 700;
  cursor: pointer;
}

.bar-btn:active { background: var(--primary-soft); }
.bar-btn[aria-pressed="true"] { background: var(--primary-soft); }
.bar-btn:disabled { opacity: .45; cursor: default; }

.bar-btn-primary {
  color: #fff;
  background: linear-gradient(135deg, var(--primary), var(--secondary));
}

.bar-btn-primary:active { background: var(--secondary); }

/* ---------- Home footer ---------- */

.site-footer {
  padding: 0 1rem calc(1.25rem + var(--safe-bottom));
  font-size: .95rem;
  line-height: 1.4;
  text-align: center;
}

.footer-row a {
  display: inline-block;
  padding: .4rem .15rem;
  color: var(--muted);
  text-decoration: none;
}

.site-footer a:hover,
.site-footer a:focus-visible { color: var(--primary-dark); text-decoration: underline; }

.footer-dot { margin: 0 .4rem; color: #aab0c4; }

.footer-copy { margin: .35rem 0 0; font-size: .8rem; color: var(--muted); }

.footer-version { margin: .15rem 0 0; font-size: .75rem; color: var(--muted); }

/* ---------- Legal documents ---------- */

.legal-nav {
  position: sticky;
  top: calc(56px + env(safe-area-inset-top, 0px));
  z-index: 10;
  display: flex;
  gap: .25rem;
  padding: .5rem 1rem;
  overflow-x: auto;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.legal-nav a {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0 .9rem;
  border-radius: 999px;
  color: var(--primary-dark);
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
}

/* Centered when the tabs fit, scrollable from the start when they don't */
.legal-nav a:first-child { margin-inline-start: auto; }
.legal-nav a:last-child { margin-inline-end: auto; }

.legal-nav a[aria-current="page"] {
  color: #fff;
  background: var(--primary);
}

.legal-content {
  max-width: 720px;
  margin: 0 auto;
  padding: 1.5rem max(1.5rem, env(safe-area-inset-right, 0px)) calc(2.5rem + var(--safe-bottom)) max(1.5rem, env(safe-area-inset-left, 0px));
  line-height: 1.75;
}

.legal-content h1 { margin-top: .5rem; font-size: 1.6rem; }
.legal-content h2 { margin-top: 1.75rem; font-size: 1.2rem; color: var(--primary-dark); }
.legal-content h3 { margin-top: 1.25rem; font-size: 1.05rem; }
.legal-content p,
.legal-content ul,
.legal-content ol { margin: .5rem 0 .9rem; }
.legal-content ul,
.legal-content ol { padding-inline-start: 1.4rem; }
.legal-content li { margin-bottom: .35rem; }
.legal-content a { color: var(--primary-dark); }
.legal-content code { padding: 0 .3em; border-radius: 4px; background: var(--primary-soft); font-size: .9em; }
.legal-content hr { margin: 2rem 0; border: 0; border-top: 1px solid var(--border); }

.table-wrap { overflow-x: auto; margin: .75rem 0 1rem; }

.legal-content table {
  width: 100%;
  border-collapse: collapse;
  font-size: .95rem;
  background: var(--surface);
}

.legal-content th,
.legal-content td {
  padding: .5rem .6rem;
  border: 1px solid var(--border);
  text-align: start;
  vertical-align: top;
}

.legal-content th { background: var(--primary-soft); }

/* Phones: each table row becomes a card, each cell shows its column name */
@media (max-width: 599px) {
  .legal-content table,
  .legal-content tbody,
  .legal-content tr,
  .legal-content td { display: block; }
  .legal-content thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .legal-content table { background: none; }
  .legal-content tr {
    margin-bottom: .75rem;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--surface);
    overflow: hidden;
  }
  .legal-content td { border: 0; border-bottom: 1px solid var(--border); }
  .legal-content td:last-child { border-bottom: 0; }
  .legal-content td::before {
    content: attr(data-label);
    display: block;
    font-size: .8rem;
    font-weight: 700;
    color: var(--primary-dark);
  }
  .legal-content td:first-child { background: var(--primary-soft); font-weight: 700; }
}

.placeholder {
  padding: 0 .25em;
  border-radius: 4px;
  background: #fff1b8;
  color: #6b4e00;
  font-family: ui-monospace, Menlo, monospace;
  font-size: .85em;
}

/* ---------- Share target fallback page ---------- */

.simple-page {
  max-width: 480px;
  margin: 15vh auto 0;
  padding: 1.5rem;
  text-align: center;
}

/* ---------- Larger screens ---------- */

@media (min-width: 600px) {
  .pages { padding: 20px 16px 32px; gap: 20px; }
  .modal.sheet { margin-bottom: auto; border-radius: 18px; }
  .bottom-bar { max-width: 640px; margin: 0 auto; border-radius: 18px 18px 0 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
