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

:root {
  --bg: #f5f6fa;
  --surface: #ffffff;
  --border: #e1e4e8;
  --text: #1a1a2e;
  --text-secondary: #6b7280;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-light: #eef2ff;
  --success: #10b981;
  --danger: #ef4444;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-lg: 0 10px 25px rgba(0,0,0,0.1);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.hidden { display: none !important; }

/* --- Buttons --- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}

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

.btn-success {
  background: var(--success);
  color: #fff;
}
.btn-success:hover { background: #0da472; }

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover { background: var(--bg); }

.btn-copy {
  background: var(--primary-light);
  color: var(--primary);
  border: 1px solid var(--primary);
  padding: 8px 16px;
  font-size: 13px;
  white-space: nowrap;
}
.btn-copy:hover { background: var(--primary); color: #fff; }

.btn-small {
  padding: 6px 14px;
  font-size: 13px;
}

/* --- Header --- */

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 32px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.logo {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  font-size: 20px;
  font-weight: 800;
  color: var(--primary);
  text-decoration: none;
  letter-spacing: -0.5px;
}

.logo-img {
  height: 32px;
  width: auto;
  object-fit: contain;
}

.nav-link {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
}
.nav-link:hover { color: var(--primary); text-decoration: none; }

/* --- Upload Page --- */

.page-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.upload-main {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
}

.upload-card {
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
  padding: 48px;
  max-width: 520px;
  width: 100%;
}

.upload-dropzone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 60px 40px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s ease;
}

.upload-dropzone:hover,
.upload-dropzone.dragover {
  border-color: var(--primary);
  background: var(--primary-light);
}

.dropzone-icon {
  color: var(--text-secondary);
  margin-bottom: 16px;
}
.upload-dropzone:hover .dropzone-icon,
.upload-dropzone.dragover .dropzone-icon {
  color: var(--primary);
}

.dropzone-text {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 4px;
}

.dropzone-subtext {
  font-size: 14px;
  color: var(--text-secondary);
}

/* --- Progress --- */

.upload-progress { text-align: center; padding: 40px 0; }

.progress-bar {
  width: 100%;
  height: 6px;
  background: var(--bg);
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 16px;
}

.progress-fill {
  height: 100%;
  width: 0;
  background: var(--primary);
  border-radius: 3px;
  transition: width 0.3s ease;
}

.progress-text {
  font-size: 14px;
  color: var(--text-secondary);
}

/* --- Upload Result --- */

.upload-result { text-align: center; }

.result-icon { margin-bottom: 16px; }
.success-icon { color: var(--success); }

.upload-result h2 {
  font-size: 24px;
  margin-bottom: 4px;
}

.result-filename {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 24px;
}

.share-link-box {
  text-align: left;
  margin-bottom: 24px;
}

.share-link-box label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.link-copy-row {
  display: flex;
  gap: 8px;
}

.link-copy-row input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  outline: none;
}

.copy-feedback {
  font-size: 13px;
  color: var(--success);
  margin-top: 6px;
}

.result-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}

/* --- Dashboard --- */

.dashboard-main {
  flex: 1;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
  padding: 32px 20px;
}

.dashboard-title {
  font-size: 24px;
  font-weight: 700;
  margin-bottom: 24px;
}

.empty-state {
  text-align: center;
  padding: 80px 20px;
  color: var(--text-secondary);
}
.empty-state svg { margin-bottom: 16px; opacity: 0.5; }
.empty-state p { margin-bottom: 20px; font-size: 16px; }

.pdf-list { display: flex; flex-direction: column; gap: 16px; }

.pdf-card {
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: 24px;
  box-shadow: var(--shadow);
}

.pdf-card-header {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 16px;
}

.pdf-icon { color: var(--danger); flex-shrink: 0; }

.pdf-name {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 2px;
}

.pdf-date {
  font-size: 13px;
  color: var(--text-secondary);
}

.share-link-box.compact {
  margin-bottom: 16px;
}

.edits-section {
  border-top: 1px solid var(--border);
  padding-top: 16px;
}

.edits-title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 12px;
  color: var(--text-secondary);
}

.edits-list { display: flex; flex-direction: column; gap: 8px; }

.edit-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: var(--bg);
  border-radius: 8px;
}

.edit-label {
  font-size: 14px;
  color: var(--text);
}

.edit-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.no-edits {
  font-size: 14px;
  color: var(--text-secondary);
  font-style: italic;
}

.autosave-row {
  background: #fef9e7;
  border: 1px dashed #f0c040;
}

.autosave-badge {
  display: inline-block;
  background: #f0c040;
  color: #5a4800;
  font-size: 11px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-right: 6px;
}

.btp-row {
  background: #ecfdf5;
  border: 1px solid var(--success);
}

.btp-badge {
  display: inline-block;
  background: var(--success);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-right: 6px;
}

/* --- Editor --- */

.editor-layout {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  touch-action: pan-y;
}

.editor-header {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 8px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  overflow-x: auto;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
}

.tool-group {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0 8px;
  border-right: 1px solid var(--border);
}
.tool-group:last-child { border-right: none; }

.tool-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 12px;
  border: none;
  background: transparent;
  border-radius: 8px;
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 11px;
  transition: all 0.15s ease;
}
.tool-btn:hover {
  background: var(--bg);
  color: var(--text);
}
.tool-btn.active {
  background: var(--primary-light);
  color: var(--primary);
}

.tool-options {
  gap: 12px;
}

.tool-option {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
}

.tool-option span {
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.tool-option input[type="color"] {
  width: 28px;
  height: 28px;
  border: 2px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  padding: 0;
  background: none;
}

.tool-option input[type="range"] {
  width: 80px;
  accent-color: var(--primary);
}

.tool-option input[type="number"] {
  width: 56px;
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  text-align: center;
}

/* --- Canvas Area --- */

.editor-canvas-area {
  flex: 1;
  overflow-y: auto;
  background: #e5e5e5;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 24px;
  touch-action: pan-y;
}

.pages-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.page-wrapper {
  position: relative;
  box-shadow: var(--shadow-lg);
  background: white;
}

.page-wrapper .pdf-canvas {
  display: block;
}

.page-wrapper .canvas-container {
  position: absolute !important;
  top: 0;
  left: 0;
}

/* --- Loading --- */

.loading-spinner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 80px 20px;
  color: var(--text-secondary);
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

.error-text {
  color: var(--danger);
  font-size: 16px;
}

/* --- Save Overlay --- */

.save-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.save-modal {
  background: var(--surface);
  border-radius: 16px;
  padding: 48px;
  text-align: center;
  box-shadow: var(--shadow-lg);
}

.save-modal .spinner { margin: 0 auto 16px; }

.save-modal p {
  font-size: 16px;
  font-weight: 600;
}

/* --- Built to Plan Page Picker --- */

.btp-picker {
  background: var(--surface);
  border-radius: 16px;
  padding: 32px;
  box-shadow: var(--shadow-lg);
  max-width: 400px;
  width: 100%;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}

.btp-picker h3 {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 16px;
  text-align: center;
}

.btp-pages {
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  max-height: 50vh;
  margin-bottom: 20px;
  padding-right: 4px;
}

.btp-page-option {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--bg);
  border-radius: 8px;
  font-size: 15px;
  cursor: pointer;
  user-select: none;
}

.btp-page-option:hover {
  background: var(--primary-light);
}

.btp-page-option input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--success);
}

.btp-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}

/* --- Mobile logo toggle --- */

.logo-mobile { display: none; }

/* --- Mobile bottom bar (hidden on desktop) --- */

.mobile-bottom-bar {
  display: none;
}

/* --- Responsive --- */

@media (max-width: 768px) {
  .header { padding: 12px 16px; }
  .upload-card { padding: 24px; }
  .upload-dropzone { padding: 40px 20px; }
  .result-actions { flex-direction: column; }

  .editor-layout {
    touch-action: pan-y;
  }

  .logo-desktop { display: none; }
  .logo-mobile { display: block; }

  /* --- Fixed top bar --- */

  .editor-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 100;
    flex-wrap: nowrap;
    padding: 6px 8px;
    gap: 0;
    overflow: visible;
  }

  .editor-header .logo {
    flex-shrink: 0;
    margin-right: 6px;
  }

  .editor-header .logo-img {
    height: 26px;
    width: 26px;
    border-radius: 5px;
  }

  /* Toolbar fills remaining width, distributes items evenly */
  .toolbar {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    gap: 0;
    flex: 1;
    justify-content: space-between;
  }

  .tool-group {
    display: flex;
    align-items: center;
    gap: 0;
    padding: 0;
    border-right: none;
    flex: 1;
    justify-content: space-evenly;
  }

  .tool-group.tool-options {
    flex: 0 0 auto;
    justify-content: flex-end;
    gap: 4px;
    padding-left: 4px;
  }

  .tool-btn {
    padding: 8px;
    flex-shrink: 0;
    border-radius: 8px;
  }
  .tool-btn span { display: none; }
  .tool-btn svg { width: 22px; height: 22px; }

  .tool-option { gap: 3px; }
  .tool-option span { display: none; }
  .tool-option input[type="color"] { width: 28px; height: 28px; border-width: 1px; }
  .tool-option input[type="range"] { width: 52px; }
  .tool-option input[type="number"] { width: 40px; padding: 2px; font-size: 12px; }

  /* Hide undo/redo and save from top bar on mobile */
  .tool-group-save,
  .tool-group-undoredo { display: none !important; }

  /* --- Fixed bottom bar --- */

  .mobile-bottom-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 10px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    background: var(--surface);
    border-top: 1px solid var(--border);
  }

  .mobile-bottom-tool {
    flex-shrink: 0;
    padding: 10px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface);
    color: var(--text-secondary);
  }
  .mobile-bottom-tool svg { width: 20px; height: 20px; }

  .mobile-save-btn {
    flex: 1;
    padding: 10px 12px;
    font-size: 14px;
    font-weight: 700;
    border-radius: 10px;
  }

  .mobile-btp-btn {
    flex: 1;
    padding: 10px 12px;
    font-size: 14px;
    font-weight: 700;
    border-radius: 10px;
  }

  /* Canvas area: offset for fixed top + bottom bars */
  .editor-canvas-area {
    margin-top: 46px;
    margin-bottom: 56px;
    padding: 8px 0;
    align-items: center;
  }

  .pages-container {
    width: 100%;
    align-items: center;
  }

  .page-wrapper {
    transform-origin: top center;
    max-width: 100vw;
  }
}

/* --- PIN Gate --- */

.pin-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.pin-modal {
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
  padding: 40px 32px;
  text-align: center;
  max-width: 340px;
  width: 100%;
}

.pin-logo {
  height: 48px;
  width: auto;
  margin-bottom: 20px;
}

.pin-modal h2 {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 16px;
}

.pin-display {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  font-size: 28px;
  letter-spacing: 8px;
  min-height: 54px;
  margin-bottom: 20px;
  text-align: center;
}

.pin-keypad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 16px;
}

.key-btn {
  padding: 16px;
  font-size: 22px;
  font-weight: 600;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
  transition: all 0.12s ease;
  user-select: none;
}

.key-btn:hover {
  background: var(--bg);
}

.key-btn:active {
  background: var(--primary-light);
  border-color: var(--primary);
}

.key-clear {
  color: var(--danger);
  font-weight: 700;
}

.key-enter {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.key-enter:hover {
  background: var(--primary-hover);
}

.pin-error {
  color: var(--danger);
  font-size: 14px;
  font-weight: 600;
  margin-top: 4px;
}

/* --- PDF Preview Modal --- */

.preview-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  padding: 24px;
}

.preview-modal {
  background: var(--surface);
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 900px;
  height: 85vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.preview-title {
  font-size: 16px;
  font-weight: 600;
}

.preview-frame {
  flex: 1;
  width: 100%;
  border: none;
}
