/* ── Reset & Base ──────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary:   #2563EB;
  --primary-h: #1D4ED8;
  --primary-lt:#EFF6FF;
  --surface:   #FFFFFF;
  --bg:        #F1F5F9;
  --border:    #E2E8F0;
  --text:      #0F172A;
  --muted:     #64748B;
  --subtle:    #94A3B8;
  --toolbar-h: 52px;
  --sidebar-w: 148px;
}

html, body {
  height: 100%;
  /* Inherits --font-stack from style.css :root when loaded together.
     Fallback inline stack covers all 25-language scripts independently. */
  font-family:
    -apple-system, BlinkMacSystemFont,
    'Inter', 'Segoe UI', Roboto,
    'PingFang SC', 'PingFang TC', 'Hiragino Sans',
    'Noto Sans CJK SC', 'Noto Sans CJK TC', 'Noto Sans CJK JP', 'Noto Sans CJK KR',
    'Microsoft YaHei', 'Microsoft JhengHei', Meiryo, 'Malgun Gothic',
    'Geeza Pro', Tahoma, 'Arial Unicode MS',
    'Kohinoor Devanagari', 'Noto Sans Devanagari', Mangal,
    'Noto Sans Thai', Thonburi, 'Leelawadee UI',
    sans-serif;
}
body { background: var(--bg); color: var(--text); }

/* ── Upload Screen ─────────────────────────────────────────── */
#upload-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 60vh;
  padding: 3rem 1.5rem 4rem;
}
#upload-screen[hidden] { display: none !important; }

.ed-upload-body {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 620px;
}

.ed-upload-header { text-align: center; width: 100%; }
.ed-upload-header .hero-sub { margin-bottom: 1rem; }

/* ── Feature pills ─────────────────────────────────────────── */
.ed-pills {
  display: flex; flex-wrap: wrap; gap: .4rem;
  justify-content: center; margin-bottom: 2rem;
}
.ed-pill {
  display: inline-flex; align-items: center; gap: .25rem;
  padding: .25rem .7rem; border-radius: 99px;
  font-size: .75rem; font-weight: 500;
  background: var(--surface); border: 1px solid var(--border);
  color: var(--muted); white-space: nowrap;
}
.ed-pill--free {
  background: #ECFDF5; border-color: #6EE7B7; color: #065F46; font-weight: 700;
}

/* ── Below-fold sections ───────────────────────────────────── */
.ed-section {
  width: 100%; max-width: 860px;
  padding: 3.5rem 1.5rem 0;
  text-align: center;
}
.ed-section-label {
  font-size: .72rem; font-weight: 700; letter-spacing: .1em;
  color: var(--primary); text-transform: uppercase; margin-bottom: .5rem;
}
.ed-section-title {
  font-size: 1.5rem; font-weight: 800; letter-spacing: -.02em;
  color: var(--text); margin-bottom: 2rem;
}

/* ── How it works ──────────────────────────────────────────── */
.ed-howto-steps {
  display: flex; align-items: flex-start;
  gap: 1rem; justify-content: center;
}
.ed-howto-step {
  flex: 1; max-width: 220px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 1.5rem 1.25rem; text-align: center;
}
.ed-step-num {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--primary); color: #fff;
  font-size: .95rem; font-weight: 800;
  display: flex; align-items: center; justify-content: center;
  margin: 0 auto 1rem;
}
.ed-step-title { font-size: .95rem; font-weight: 700; margin-bottom: .35rem; }
.ed-step-desc  { font-size: .83rem; color: var(--muted); line-height: 1.55; }
.ed-howto-arrow {
  font-size: 1.25rem; color: var(--border);
  align-self: center; flex-shrink: 0; padding-top: .5rem;
}

/* ── Features grid ─────────────────────────────────────────── */
.ed-feat-section { padding-bottom: 3rem; }
.ed-feat-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
  text-align: left;
}
.ed-feat-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 1.25rem;
  transition: box-shadow .15s, border-color .15s;
}
.ed-feat-card:hover {
  border-color: var(--primary); box-shadow: 0 2px 16px rgba(99,102,241,.08);
}
.ed-feat-icon {
  width: 36px; height: 36px; border-radius: 10px;
  background: var(--primary-lt, #EEF2FF);
  display: flex; align-items: center; justify-content: center;
  margin-bottom: .75rem;
}
.ed-feat-icon svg { width: 18px; height: 18px; stroke: var(--primary); }
.ed-feat-title { font-size: .9rem; font-weight: 700; margin-bottom: .3rem; }
.ed-feat-desc  { font-size: .8rem; color: var(--muted); line-height: 1.5; }

@media (max-width: 640px) {
  .ed-howto-steps { flex-direction: column; align-items: center; }
  .ed-howto-arrow { transform: rotate(90deg); }
  .ed-feat-grid { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 420px) {
  .ed-feat-grid { grid-template-columns: 1fr; }
}

.upload-zone {
  width: 100%;
  border: 2px dashed var(--border); border-radius: 16px;
  padding: 3rem 2rem; text-align: center; cursor: pointer;
  transition: border-color .15s, background .15s;
  background: var(--surface); margin-bottom: .75rem;
}
.upload-zone:hover, .upload-zone.drag-over {
  border-color: var(--primary); background: var(--primary-lt);
}
.upload-zone__icon { font-size: 3rem; margin-bottom: .75rem; }
.upload-zone__text { font-size: 1rem; color: var(--muted); margin-bottom: 1rem; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: .4rem; padding: .6rem 1.25rem; border-radius: 8px; font-size: .95rem; font-weight: 600; cursor: pointer; border: none; transition: background .15s; text-decoration: none; }
.btn--primary { background: var(--primary); color: #fff; }
.btn--primary:hover { background: var(--primary-h); }

.ed-note { font-size: .78rem; color: var(--subtle); text-align: center; margin-top: .5rem; }

/* ── Editor Screen ─────────────────────────────────────────── */
#editor-screen {
  position: fixed;
  inset: 0;
  z-index: 9000;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}
#editor-screen[hidden] { display: none !important; }

/* ── Title / Header bar ──────────────────────────────────── */
.editor-header {
  height: 54px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px;
  flex-shrink: 0;
  box-shadow: 0 1px 4px rgba(0,0,0,.07);
}

/* Back arrow */
.tb-back {
  display: flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; border-radius: 8px;
  color: var(--muted); text-decoration: none;
  transition: background .15s, color .15s; flex-shrink: 0;
}
.tb-back:hover { background: var(--bg); color: var(--text); }

/* Doc name box */
.doc-name-wrap {
  display: flex; align-items: center; gap: 6px;
  border: 1.5px solid var(--border); border-radius: 8px;
  padding: 0 10px 0 12px; height: 36px;
  transition: border-color .15s;
}
.doc-name-wrap:focus-within { border-color: var(--primary); }

.doc-name {
  font-size: .9rem; font-weight: 600;
  border: none; outline: none; color: var(--text);
  background: transparent; min-width: 0; width: 160px;
}

.doc-edit-icon { color: var(--subtle); flex-shrink: 0; }
.header-spacer { flex: 1; }

/* Export button */
.export-btn {
  flex-shrink: 0;
  display: flex; align-items: center; gap: 5px;
  background: var(--primary); color: #fff;
  border: none; padding: .42rem 1.1rem;
  border-radius: 8px; font-weight: 700; font-size: .85rem;
  cursor: pointer; white-space: nowrap;
  transition: background .15s;
}
.export-btn:hover { background: var(--primary-h); }
.export-btn:disabled { opacity: .5; cursor: not-allowed; }

/* ── Tool bar ─────────────────────────────────────────────── */
.editor-toolbar {
  height: var(--toolbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 0 12px;
  flex-shrink: 0;
  overflow-x: auto;
  scrollbar-width: none;
}
.editor-toolbar::-webkit-scrollbar { display: none; }

.toolbar-sep { width: 1px; height: 20px; background: var(--border); margin: 0 6px; flex-shrink: 0; }

/* Tool buttons */
.tool-btn {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: .32rem .55rem; border-radius: 7px; border: none;
  cursor: pointer; background: none;
  font-size: .63rem; font-weight: 600; color: var(--muted);
  transition: background .12s, color .12s;
  min-width: 42px; flex-shrink: 0; line-height: 1;
}
.tool-btn:hover { background: var(--bg); color: var(--text); }
.tool-btn.active { background: #DBEAFE; color: var(--primary); }
.tool-btn:disabled { opacity: .4; cursor: not-allowed; }

.tb-icon { width: 17px; height: 17px; flex-shrink: 0; color: inherit; display: block; }

/* Color swatch */
.tb-color-wrap { display: flex; align-items: center; padding: 0 4px; flex-shrink: 0; }
.color-swatch { width: 26px; height: 26px; border-radius: 6px; border: 2px solid var(--border); cursor: pointer; padding: 0; }

/* Selects */
.tb-select {
  font-size: .78rem; border: 1px solid var(--border);
  border-radius: 6px; padding: .3rem .45rem;
  outline: none; background: var(--surface); color: var(--text);
  flex-shrink: 0; cursor: pointer;
}
.tb-select:focus { border-color: var(--primary); }

/* ── Editor Body ─────────────────────────────────────────── */
.editor-body { flex: 1; display: flex; overflow: hidden; }

/* ── Thumbnail Sidebar ───────────────────────────────────── */
.thumbnails {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  max-width: 320px;
  background: #F8FAFC;
  border-right: 1px solid var(--border);
  overflow-y: auto; overflow-x: hidden;
  padding: 10px 8px;
  flex-shrink: 0;
  scrollbar-width: thin;
  scrollbar-color: #CBD5E1 transparent;
}
.thumbnails::-webkit-scrollbar { width: 4px; }
.thumbnails::-webkit-scrollbar-thumb { background: #CBD5E1; border-radius: 4px; }

/* ── Resize handle ───────────────────────────────────────── */
.resize-handle {
  width: 5px; flex-shrink: 0; cursor: col-resize;
  background: var(--border); position: relative;
  transition: background .15s; user-select: none;
}
.resize-handle:hover { background: #BFDBFE; }
.resize-handle.dragging { background: var(--primary); }

/* ── Thumbnail items ─────────────────────────────────────── */
.thumb-item {
  display: block;
  cursor: pointer;
  border-radius: 6px;
  overflow: hidden;
  border: 2px solid transparent;
  transition: border-color .15s, box-shadow .15s;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.1);
  width: 100%;
  margin-bottom: 10px;
}
.thumb-item:last-child { margin-bottom: 0; }
.thumb-item:hover  { border-color: #93C5FD; }
.thumb-item.active { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.15); }

.thumb-item img {
  display: block;
  width: 100%;
  height: auto;
}

.thumb-num {
  font-size: .7rem; color: var(--muted);
  padding: 3px 0 4px; font-weight: 700;
  text-align: center; background: #F1F5F9;
  letter-spacing: .02em;
}
.thumb-item.active .thumb-num { color: var(--primary); }

/* ── Canvas Area ─────────────────────────────────────────── */
.canvas-area {
  flex: 1; overflow: auto;
  display: flex; flex-direction: column;
  align-items: center; padding: 24px 20px;
  gap: 20px; background: #E2E8F0;
}

.page-wrapper {
  position: relative;
  box-shadow: 0 4px 20px rgba(0,0,0,.15);
  border-radius: 2px; overflow: hidden;
  background: #fff; flex-shrink: 0;
}
.pdf-render-canvas { display: block; position: absolute; top: 0; left: 0; pointer-events: none; }
.page-wrapper .upper-canvas { cursor: crosshair; }

/* ── Page Nav ────────────────────────────────────────────── */
.page-nav {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: rgba(15,23,42,.88); color: #fff;
  border-radius: 999px; display: flex; align-items: center;
  gap: 6px; padding: 6px 14px; font-size: .82rem;
  backdrop-filter: blur(10px); user-select: none; z-index: 10;
  box-shadow: 0 4px 16px rgba(0,0,0,.25);
}
.page-nav button {
  background: none; border: none; color: #fff; font-size: 1rem;
  cursor: pointer; line-height: 1; padding: .1rem .3rem; border-radius: 4px;
}
.page-nav button:hover { background: rgba(255,255,255,.15); }
.page-nav input[type="number"] {
  width: 2.8rem; text-align: center;
  background: rgba(255,255,255,.12); border: none;
  color: #fff; border-radius: 5px; padding: .12rem .2rem;
  font-size: .82rem; outline: none;
}
.page-nav input[type="number"]::-webkit-inner-spin-button { display: none; }

/* ── Zoom bar ────────────────────────────────────────────── */
.zoom-bar {
  position: fixed; bottom: 20px; right: 20px;
  display: flex; align-items: center; gap: 4px;
  background: rgba(15,23,42,.88); color: #fff;
  border-radius: 999px; padding: 5px 12px; font-size: .8rem;
  backdrop-filter: blur(10px); z-index: 10;
  box-shadow: 0 4px 16px rgba(0,0,0,.25);
}
.zoom-bar button {
  background: none; border: none; color: #fff; cursor: pointer;
  font-size: 1rem; line-height: 1; padding: .1rem .25rem; border-radius: 4px; font-weight: 700;
}
.zoom-bar button:hover { background: rgba(255,255,255,.15); }

/* ── Export overlay ──────────────────────────────────────── */
#export-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 9999; backdrop-filter: blur(6px);
}
#export-overlay[hidden] { display: none; }
.export-card {
  background: var(--surface); border-radius: 16px;
  padding: 2.25rem; min-width: 300px; text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,.2); border: 1px solid var(--border);
}
.export-card__icon { font-size: 1.75rem; margin-bottom: .75rem; }
.export-title { font-size: 1.1rem; font-weight: 700; margin-bottom: 1.1rem; }
.exp-bar { height: 6px; background: var(--border); border-radius: 999px; overflow: hidden; }
.exp-bar__fill { height: 100%; background: var(--primary); border-radius: 999px; transition: width .3s; }
.exp-label { font-size: .83rem; color: var(--muted); margin-top: .6rem; }

/* ── Signature Modal ─────────────────────────────────────── */
#sign-modal {
  position: absolute; inset: 0; z-index: 200;
  display: flex; align-items: center; justify-content: center;
}
#sign-modal[hidden] { display: none; }

.sign-overlay {
  position: absolute; inset: 0;
  background: rgba(0,0,0,.6); backdrop-filter: blur(4px);
}

.sign-card {
  position: relative; z-index: 1;
  background: #1A2035; border-radius: 14px;
  width: 620px; max-width: calc(100vw - 32px);
  box-shadow: 0 32px 80px rgba(0,0,0,.6);
  overflow: hidden;
}

.sign-header { padding: 22px 24px 0; }
.sign-header-title { font-size: 1.1rem; font-weight: 700; color: #fff; }

.sign-tabs {
  display: flex; padding: 14px 24px 0;
  border-bottom: 1px solid rgba(255,255,255,.1);
}
.sign-tab {
  background: none; border: none; border-bottom: 2px solid transparent;
  color: rgba(255,255,255,.45); font-size: .88rem; font-weight: 500;
  padding: .35rem .8rem; cursor: pointer; margin-bottom: -1px;
  transition: color .15s, border-color .15s;
}
.sign-tab.active { color: #fff; border-bottom-color: #fff; }
.sign-tab:hover:not(.active) { color: rgba(255,255,255,.75); }

.sign-tab-panel { padding: 14px 24px 6px; }

.sign-colors {
  display: flex; align-items: center; justify-content: flex-end; gap: 8px; margin-bottom: 10px;
}
.sign-bg-toggle {
  display: flex; align-items: center; gap: 5px;
  color: rgba(255,255,255,.6); font-size: .78rem; cursor: pointer;
  margin-left: 4px;
}
.sign-bg-toggle input { cursor: pointer; accent-color: #6366F1; }
.sign-color-btn {
  width: 26px; height: 26px; border-radius: 50%;
  border: 2.5px solid transparent; cursor: pointer; padding: 0;
  transition: border-color .15s; outline: none;
}
.sign-color-btn.active { border-color: #fff; }

.sign-canvas-wrap {
  position: relative; border-radius: 8px; overflow: hidden;
  border: 1.5px dashed rgba(255,255,255,.22); background: #fff;
  cursor: crosshair;
}
.sign-canvas-wrap.transparent-bg {
  background: repeating-conic-gradient(#ccc 0% 25%, #fff 0% 50%) 0 0 / 16px 16px;
}
.sign-hint {
  position: absolute; bottom: 12px; left: 50%;
  transform: translateX(-50%);
  color: #94A3B8; font-size: .82rem; pointer-events: none;
  user-select: none;
}

.sign-clear-btn {
  background: none; border: 1px solid rgba(255,255,255,.2);
  color: rgba(255,255,255,.55); border-radius: 6px;
  padding: .28rem .75rem; font-size: .8rem; cursor: pointer;
  margin-top: 8px; transition: border-color .15s, color .15s;
}
.sign-clear-btn:hover { border-color: rgba(255,255,255,.5); color: #fff; }

.sign-upload-area {
  display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 10px;
  border: 2px dashed rgba(255,255,255,.18); border-radius: 8px;
  min-height: 180px; cursor: pointer;
  color: rgba(255,255,255,.45); font-size: .88rem;
  transition: border-color .15s, color .15s;
}
.sign-upload-area:hover { border-color: rgba(255,255,255,.4); color: rgba(255,255,255,.8); }
.sign-image-preview {
  display: block; max-width: 100%; max-height: 180px;
  margin: 12px auto 0; border-radius: 6px;
  border: 1px solid rgba(255,255,255,.12);
}

.sign-type-input {
  width: 100%; padding: .65rem 1rem;
  background: rgba(255,255,255,.07); border: 1px solid rgba(255,255,255,.15);
  border-radius: 8px; color: #fff; font-size: 1rem;
  outline: none; transition: border-color .15s; box-sizing: border-box;
}
.sign-type-input:focus { border-color: rgba(255,255,255,.4); }
.sign-type-input::placeholder { color: rgba(255,255,255,.25); }

.sign-type-preview-wrap {
  margin-top: 12px; background: #fff;
  border-radius: 8px; overflow: hidden;
  border: 1px solid rgba(255,255,255,.1);
  min-height: 90px;
}
.sign-type-preview-wrap canvas { display: block; width: 100%; height: 90px; }

.sign-footer {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px; border-top: 1px solid rgba(255,255,255,.08);
}
.sign-save-label {
  display: flex; align-items: center; gap: 8px;
  color: rgba(255,255,255,.6); font-size: .85rem; cursor: pointer;
  user-select: none;
}
.sign-actions { display: flex; gap: 8px; }

.sign-cancel-btn {
  background: rgba(255,255,255,.1); border: none; color: rgba(255,255,255,.85);
  padding: .42rem 1.1rem; border-radius: 8px; cursor: pointer;
  font-size: .85rem; transition: background .15s;
}
.sign-cancel-btn:hover { background: rgba(255,255,255,.18); }

.sign-done-btn {
  background: rgba(255,255,255,.18); border: none; color: #fff;
  padding: .42rem 1.1rem; border-radius: 8px; cursor: pointer;
  font-size: .85rem; font-weight: 600; transition: background .15s;
}
.sign-done-btn:hover { background: rgba(255,255,255,.28); }

/* ── Mobile ──────────────────────────────────────────────── */
@media (max-width: 700px) {
  :root { --toolbar-h: 46px; --sidebar-w: 88px; }

  /* Header: tighter, doc name shorter */
  .editor-header { height: 48px; padding: 0 10px; gap: 6px; }
  .doc-name { width: 100px; font-size: .82rem; }
  .doc-name-wrap { padding: 0 6px 0 8px; height: 34px; }

  /* Export button: tighter */
  .export-btn { padding: .38rem .8rem; font-size: .8rem; }

  /* Stack sidebar above canvas */
  .editor-body { flex-direction: column; }
  .resize-handle { display: none; }

  /* Thumbnails: horizontal strip at top */
  .thumbnails {
    width: 100% !important;
    height: 100px;
    min-width: 0;
    flex-direction: row;
    overflow-x: auto; overflow-y: hidden;
    border-right: none; border-bottom: 1px solid var(--border);
    padding: 6px 8px; gap: 6px;
    /* smooth momentum scroll on iOS */
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .thumbnails::-webkit-scrollbar { display: none; }
  .thumb-item { width: auto; min-width: 56px; height: 80px; overflow: hidden; margin-bottom: 0; }
  .thumb-item img { width: auto; height: 60px; }

  /* Toolbar: horizontally scrollable, touch-friendly */
  .editor-toolbar {
    padding: 0 8px;
    gap: 0;
    /* allow touch-scroll on toolbar */
    -webkit-overflow-scrolling: touch;
  }
  /* Tool buttons: 44px wide for touch */
  .tool-btn {
    min-width: 44px;
    padding: .35rem .5rem;
  }
  /* Selects: prevent iOS auto-zoom */
  .tb-select { font-size: 1rem; }

  /* Canvas area: reduce padding to maximise canvas space */
  .canvas-area { padding: 12px 8px; }

  /* Page nav: stay visible above keyboard */
  .page-nav { bottom: 10px; font-size: .78rem; padding: 5px 12px; }

  /* Zoom bar: hidden on mobile (pinch-to-zoom via touch events) */
  .zoom-bar { display: none; }

  /* Signature modal: full-width on small screens */
  .sign-card { width: calc(100vw - 24px); }
  .sign-header { padding: 16px 16px 0; }
  .sign-tabs { padding: 10px 16px 0; }
  .sign-tab-panel { padding: 10px 16px 4px; }
  .sign-footer { padding: 12px 16px; }

  /* Upload screen: tighter */
  #upload-screen { padding: 2rem 1rem 3rem; }

}

@media (max-width: 420px) {
  .doc-name { width: 70px; }
}
