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

:root {
  --bg: #f5f5f7;
  --card-bg: #fff;
  --text: #1d1d1f;
  --text-secondary: #6e6e73;
  --border: #d2d2d7;
  --primary: #0071e3;
  --primary-hover: #0077ed;
  --primary-bg: #e8f0fe;
  --danger: #ff3b30;
  --radius: 12px;
  --shadow: 0 2px 12px rgba(0,0,0,0.08);
}

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

/* Header */
header {
  background: rgba(255,255,255,0.8);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}
.topbar {
  max-width: 1200px;
  margin: 0 auto;
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.brand b { font-size: 18px; }
.brand span {
  font-size: 11px;
  color: var(--text-secondary);
  margin-left: 8px;
  letter-spacing: 1px;
}
.back-link {
  color: var(--primary);
  text-decoration: none;
  font-size: 14px;
}
.back-link:hover { text-decoration: underline; }

/* Main */
main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px 20px 60px;
}

.step { display: none; }
.step.active { display: block; }

.card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 32px;
}

h2 { font-size: 22px; font-weight: 600; margin-bottom: 8px; }
.hint { color: var(--text-secondary); font-size: 14px; margin-bottom: 20px; }

/* Tabs */
.tabs { display: flex; gap: 4px; margin-bottom: 16px; }
.tab {
  padding: 8px 20px;
  border: 1px solid var(--border);
  background: var(--card-bg);
  border-radius: 8px 8px 0 0;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-secondary);
  border-bottom: none;
}
.tab.active {
  background: var(--primary-bg);
  color: var(--primary);
  border-color: var(--primary);
  font-weight: 500;
}

.tab-content { display: flex; flex-direction: column; gap: 12px; }
.tab-content.hidden { display: none; }

/* Input */
#url-input {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid var(--border);
  border-radius: 8px;
  font-size: 15px;
  outline: none;
  transition: border-color 0.2s;
}
#url-input:focus { border-color: var(--primary); }

/* File drop */
.file-drop {
  border: 2px dashed var(--border);
  border-radius: 8px;
  padding: 48px 20px;
  text-align: center;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s;
  position: relative;
}
.file-drop:hover, .file-drop.drag-over {
  border-color: var(--primary);
  background: var(--primary-bg);
  color: var(--primary);
}
.file-drop input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}
.file-drop .file-name {
  margin-top: 8px;
  font-size: 13px;
  color: var(--primary);
  font-weight: 500;
}

/* Buttons */
.btn {
  padding: 10px 20px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card-bg);
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
}
.btn:hover { background: #f0f0f0; }
.btn.primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.btn.primary:hover { background: var(--primary-hover); }
.btn.primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.btn.large {
  padding: 14px 40px;
  font-size: 16px;
  font-weight: 600;
}

/* Status */
.status {
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 14px;
  margin-top: 8px;
}
.status.loading { background: #fef9e7; color: #b7950b; }
.status.error { background: #fde8e8; color: var(--danger); }
.status.hidden { display: none; }

/* Zoom */
.zoom-controls {
  display: flex;
  align-items: center;
  gap: 4px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 2px;
  background: #fafafa;
}
.btn-sm {
  padding: 5px 10px;
  font-size: 14px;
  border: none;
  background: transparent;
  min-width: 30px;
  text-align: center;
}
.btn-sm:hover { background: #e8e8ed; border-radius: 6px; }
.zoom-label {
  font-size: 12px;
  color: var(--text-secondary);
  min-width: 36px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* Preview */
.preview-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 12px;
}
.preview-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.preview-container {
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: auto;
  max-height: 70vh;
  background: #e8e8ed;
  position: relative;
}
.selection-overlay {
  position: relative;
  display: inline-block;
  min-width: 100%;
  cursor: crosshair;
  -webkit-user-select: none;
  user-select: none;
}
.selection-overlay.selecting { cursor: crosshair; }
.selection-overlay.resizing { cursor: move; }

#preview-img {
  display: block;
  max-width: 100%;
  height: auto;
  pointer-events: none;
}

/* Crop box */
.crop-overlay {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  z-index: 10;
}
.crop-overlay.hidden { display: none; }
.crop-overlay .mask-top,
.crop-overlay .mask-bottom,
.crop-overlay .mask-left,
.crop-overlay .mask-right {
  position: absolute;
  background: rgba(0,0,0,0.45);
  pointer-events: none;
}
.crop-overlay .crop-border {
  position: absolute;
  border: 2px solid #0071e3;
  pointer-events: none;
  box-shadow: 0 0 0 1px rgba(255,255,255,0.4);
}
.crop-overlay .crop-dim-label {
  position: absolute;
  background: rgba(0,0,0,0.75);
  color: #fff;
  font-size: 12px;
  padding: 3px 8px;
  border-radius: 4px;
  white-space: nowrap;
  pointer-events: none;
  font-family: -apple-system, monospace;
}
.crop-handle {
  position: absolute;
  width: 18px;
  height: 18px;
  background: #fff;
  border: 2px solid #0071e3;
  border-radius: 3px;
  z-index: 11;
  box-shadow: 0 1px 4px rgba(0,0,0,0.2);
}
.crop-handle:hover { background: #e8f0fe; transform: scale(1.15); }
.crop-handle.nw { top: -9px; left: -9px; cursor: nw-resize; }
.crop-handle.n { top: -9px; left: 50%; margin-left: -9px; cursor: n-resize; }
.crop-handle.ne { top: -9px; right: -9px; cursor: ne-resize; }
.crop-handle.e { top: 50%; right: -9px; margin-top: -9px; cursor: e-resize; }
.crop-handle.se { bottom: -9px; right: -9px; cursor: se-resize; }
.crop-handle.s { bottom: -9px; left: 50%; margin-left: -9px; cursor: s-resize; }
.crop-handle.sw { bottom: -9px; left: -9px; cursor: sw-resize; }
.crop-handle.w { top: 50%; left: -9px; margin-top: -9px; cursor: w-resize; }

/* Crop info bar */
.crop-info {
  text-align: center;
  margin-top: 12px;
  font-size: 13px;
  color: var(--text-secondary);
}
.crop-info.hidden { display: none; }

/* Export */
#btn-export {
  display: block;
  margin: 20px auto 0;
}

/* Page break preview */
.page-breaks-overlay {
  position: absolute;
  pointer-events: none;
  z-index: 20;
}
.page-breaks-overlay.hidden { display: none; }

.page-preview-mask {
  position: absolute;
  left: 0; right: 0;
  background: rgba(255,255,255,0.15);
  border: 2px dashed rgba(0, 102, 204, 0.50);
  pointer-events: none;
  z-index: 0;
}
.page-preview-mask.alt {
  background: rgba(0,102,204,0.06);
  border-color: rgba(0, 102, 204, 0.35);
}
.page-preview-mask.over-a4 {
  border-color: rgba(255, 59, 48, 0.7) !important;
  background: rgba(255, 59, 48, 0.08) !important;
}

.page-break-line {
  position: absolute;
  left: 0; right: 0;
  height: 0;
  border-top: 2px dashed rgba(255, 59, 48, 0.7);
  z-index: 21;
}
.page-break-line .pb-handle {
  position: absolute;
  left: 4px;
  background: #ff3b30;
  color: #fff;
  border-radius: 4px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: ns-resize;
  pointer-events: auto;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}
.page-break-line .pb-label {
  position: absolute;
  right: 4px;
  background: rgba(255,59,48,0.85);
  color: #fff;
  padding: 1px 6px;
  border-radius: 3px;
  font-weight: 600;
  white-space: nowrap;
}

/* Loading spinner */
@keyframes spin { to { transform: rotate(360deg); } }
.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid #ccc;
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
  vertical-align: middle;
  margin-right: 6px;
}

/* Bookmarklet */
.mode-badge {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
  padding: 10px 14px;
  background: #f8f9fa;
  border-radius: 8px;
}
.badge {
  padding: 3px 10px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}
.badge.green { background: #d4edda; color: #155724; }
.badge.blue { background: #d1ecf1; color: #0c5460; }
.badge.gray { background: #e2e3e5; color: #383d41; }
.badge-label { font-size: 13px; color: var(--text-secondary); }
.bm-instructions {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 24px;
}
.bm-step {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 15px;
  line-height: 1.4;
}
.bm-num {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 700;
  flex-shrink: 0;
}
.bm-drag-area {
  text-align: center;
  padding: 28px;
  border: 2px dashed var(--border);
  border-radius: 12px;
  background: #fafafa;
  margin-bottom: 16px;
}
.bm-bookmarklet {
  display: inline-block;
  padding: 14px 36px;
  background: var(--primary);
  color: #fff;
  border-radius: 10px;
  font-size: 18px;
  font-weight: 600;
  cursor: grab;
  text-decoration: none;
  box-shadow: 0 4px 16px rgba(0,113,227,0.3);
  transition: all 0.2s;
  user-select: none;
  -webkit-user-drag: element;
}
.bm-bookmarklet:hover {
  box-shadow: 0 6px 24px rgba(0,113,227,0.4);
  transform: translateY(-1px);
}
.bm-bookmarklet:active { cursor: grabbing; }
.bm-hint {
  margin-top: 12px;
  font-size: 13px;
  color: var(--text-secondary);
}
.bm-hint kbd {
  padding: 2px 6px;
  background: #e8e8ed;
  border-radius: 4px;
  font-size: 12px;
  border: 1px solid var(--border);
}
.bm-mobile-note {
  background: #fef9e7;
  border-radius: 8px;
  padding: 14px 18px;
  font-size: 13px;
  line-height: 1.5;
}
.bm-code {
  width: 100%;
  margin-top: 8px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-family: monospace;
  font-size: 12px;
  resize: vertical;
  background: #fff;
  word-break: break-all;
}

/* Responsive */
@media (max-width: 640px) {
  .card { padding: 20px; }
  .preview-header { flex-direction: column; }
  .preview-actions { width: 100%; }
  .topbar { padding: 10px 16px; }
  main { padding: 16px 12px 40px; }
}
