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

:root {
  --blue: #2563eb;
  --blue-hover: #1d4ed8;
  --green: #16a34a;
  --red: #dc2626;
  --gray: #6b7280;
  --light: #f3f4f6;
  --border: #e5e7eb;
  --shadow: 0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,.1), 0 4px 6px -2px rgba(0,0,0,.05);
  --radius: 8px;
}

html { font-size: 16px; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'PingFang SC', 'Hiragino Sans GB',
               'Microsoft YaHei', sans-serif;
  background: #f8fafc;
  color: #1e293b;
  min-height: 100vh;
}

/* ===== Header ===== */
.app-header {
  background: #fff;
  border-bottom: 1px solid var(--border);
  padding: 0 20px;
  height: 56px;
  display: flex;
  align-items: center;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow);
}

.app-header h1 {
  font-size: 18px;
  font-weight: 700;
  color: var(--blue);
}

.app-header .subtitle {
  font-size: 13px;
  color: var(--gray);
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s, transform .1s;
  text-decoration: none;
  white-space: nowrap;
}

.btn:active { transform: scale(.97); }
.btn-primary { background: var(--blue); color: #fff; }
.btn-primary:hover { background: var(--blue-hover); }
.btn-success { background: var(--green); color: #fff; }
.btn-success:hover { background: #15803d; }
.btn-danger { background: var(--red); color: #fff; }
.btn-danger:hover { background: #b91c1c; }
.btn-ghost { background: transparent; color: var(--gray); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--light); }
.btn-icon { background: transparent; border: none; color: var(--gray); padding: 6px; border-radius: 6px; cursor: pointer; }
.btn-icon:hover { background: var(--light); color: #1e293b; }
.btn:disabled { opacity: .45; cursor: not-allowed; transform: none; }

/* ===== Admin Layout ===== */
.admin-main {
  max-width: 900px;
  margin: 0 auto;
  padding: 24px 16px;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.section-header h2 {
  font-size: 16px;
  font-weight: 600;
}

/* ===== Template Cards ===== */
.template-list { display: flex; flex-direction: column; gap: 12px; }

.template-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: var(--shadow);
  transition: box-shadow .15s;
}

.template-card:hover { box-shadow: var(--shadow-lg); }

.template-card-icon {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  background: #eff6ff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  flex-shrink: 0;
}

.template-card-info { flex: 1; min-width: 0; }
.template-card-info h3 { font-size: 15px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.template-card-info p { font-size: 12px; color: var(--gray); margin-top: 2px; }

.template-card-actions { display: flex; gap: 6px; flex-shrink: 0; flex-wrap: wrap; justify-content: flex-end; }

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--gray);
}

.empty-state .empty-icon { font-size: 48px; margin-bottom: 12px; }
.empty-state p { font-size: 14px; }

/* ===== Modal Overlay ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  backdrop-filter: blur(2px);
}

.modal-overlay.hidden { display: none; }

.modal-box {
  background: #fff;
  border-radius: 12px;
  padding: 24px;
  width: 100%;
  max-width: 440px;
  box-shadow: var(--shadow-lg);
  animation: slideUp .2s ease;
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(16px); }
  to   { opacity: 1; transform: translateY(0); }
}

.modal-box h2 { font-size: 18px; font-weight: 700; margin-bottom: 20px; }
.modal-footer { display: flex; gap: 8px; justify-content: flex-end; margin-top: 20px; }

/* ===== Form Elements ===== */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; font-weight: 500; color: #374151; margin-bottom: 6px; }

.form-input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  outline: none;
  transition: border-color .15s;
}

.form-input:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(37,99,235,.1); }

.file-drop {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 32px 16px;
  text-align: center;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}

.file-drop:hover, .file-drop.drag-over { border-color: var(--blue); background: #eff6ff; }
.file-drop input { display: none; }
.file-drop .drop-icon { font-size: 36px; margin-bottom: 8px; }
.file-drop .drop-text { font-size: 14px; color: var(--gray); }
.file-drop .drop-hint { font-size: 12px; color: #9ca3af; margin-top: 4px; }
.file-drop .selected-file { font-size: 13px; color: var(--blue); font-weight: 500; margin-top: 8px; }

/* ===== Editor ===== */
#editor-section { display: none; }
#editor-section.active { display: block; }

.editor-toolbar {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.editor-toolbar .toolbar-title {
  font-weight: 600;
  font-size: 15px;
  flex: 1;
  min-width: 120px;
}

.page-nav {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--gray);
}

.doc-wrapper {
  position: relative;
  display: block;
  width: 100%;
  cursor: default;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  background: #f0f0f0;
}

.doc-wrapper.draw-mode { cursor: crosshair; }

#doc-canvas {
  display: block;
  width: 100%;
  height: auto;
}

#areas-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

#areas-layer.draw-mode { pointer-events: none; }

.area-box {
  position: absolute;
  border: 2px dashed var(--blue);
  background: rgba(37,99,235,.08);
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: all;
  cursor: grab;
  transition: background .15s;
  box-sizing: border-box;
  min-width: 20px;
  min-height: 12px;
}

.area-box:hover { background: rgba(37,99,235,.15); }
.area-box.is-dragging { cursor: grabbing; background: rgba(37,99,235,.2); }

.area-box-label {
  background: var(--blue);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 4px;
  pointer-events: none;
  user-select: none;
}

.area-box-delete {
  position: absolute;
  top: -10px;
  right: -10px;
  width: 20px;
  height: 20px;
  background: var(--red);
  color: #fff;
  border: none;
  border-radius: 50%;
  font-size: 12px;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  z-index: 10;
}

/* Resize handle — bottom-right corner */
.resize-handle {
  position: absolute;
  right: -5px;
  bottom: -5px;
  width: 14px;
  height: 14px;
  background: var(--blue);
  border: 2px solid #fff;
  border-radius: 3px;
  cursor: se-resize;
  pointer-events: all;
  z-index: 11;
}

#drawing-rect {
  position: absolute;
  border: 2px dashed #f59e0b;
  background: rgba(245,158,11,.1);
  pointer-events: none;
  display: none;
}

.draw-mode-hint {
  background: #fef3c7;
  border: 1px solid #f59e0b;
  border-radius: var(--radius);
  padding: 8px 12px;
  font-size: 13px;
  color: #92400e;
  margin-bottom: 12px;
  display: none;
}

.draw-mode-hint.active { display: block; }

/* ===== Share Link Modal ===== */
.link-box {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.link-box input {
  flex: 1;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  background: var(--light);
  outline: none;
}

/* ===== Fill Page Layout ===== */
.fill-layout {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.fill-header {
  background: #fff;
  border-bottom: 1px solid var(--border);
  padding: 12px 16px;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow);
}

.fill-header h1 { font-size: 16px; font-weight: 700; color: #1e293b; }
.fill-header .fill-progress { font-size: 13px; color: var(--gray); margin-top: 2px; }

.fill-content {
  flex: 1;
  padding: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.fill-footer {
  background: #fff;
  border-top: 1px solid var(--border);
  padding: 12px 16px;
  position: sticky;
  bottom: 0;
  display: flex;
  justify-content: center;
  box-shadow: 0 -2px 8px rgba(0,0,0,.05);
}

.doc-container {
  position: relative;
  width: 100%;
  max-width: 800px;
}

#fill-doc-canvas {
  display: block;
  width: 100%;
  height: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

#fill-areas-layer {
  position: absolute;
  inset: 0;
}

.fill-area {
  position: absolute;
  border: 2px dashed #f59e0b;
  background: rgba(254,243,199,.6);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .15s, border-color .15s;
  overflow: hidden;
}

.fill-area:hover { background: rgba(254,243,199,.9); }

.fill-area.filled {
  border: 2px solid var(--green);
  background: rgba(240,253,244,.7);
}

.fill-area-hint {
  font-size: 11px;
  color: #92400e;
  font-weight: 500;
  pointer-events: none;
  text-align: center;
  padding: 2px;
}

.fill-area.filled .fill-area-hint { display: none; }

.fill-area-preview {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  display: none;
  background: #f9f9f9;
}

.fill-area.filled .fill-area-preview { display: block; }

/* ===== Drawing Modal ===== */
#draw-modal {
  position: fixed;
  inset: 0;
  background: #1e293b;
  z-index: 9999;
  display: none;
  flex-direction: column;
}

#draw-modal.active { display: flex; }

/* Portrait phone: simulate landscape by rotating */
@media (orientation: portrait) {
  #draw-modal {
    width: 100vh;
    height: 100vw;
    left: calc(50vw - 50vh);
    top: calc(50vh - 50vw);
    transform: rotate(90deg);
    transform-origin: center;
    flex-direction: column;
  }
}

#draw-toolbar {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  gap: 8px;
  background: #0f172a;
  flex-shrink: 0;
}

#draw-toolbar .area-label-text {
  flex: 1;
  font-size: 13px;
  color: #94a3b8;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#draw-canvas-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: #1e293b;
}

#draw-canvas {
  background: #fff;
  touch-action: none;
  display: block;
  cursor: crosshair;
}

.draw-btn {
  padding: 7px 14px;
  border: none;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s;
  color: #fff;
}

.draw-btn-undo { background: #475569; }
.draw-btn-undo:hover { background: #334155; }
.draw-btn-clear { background: #7c3aed; }
.draw-btn-clear:hover { background: #6d28d9; }
.draw-btn-done { background: var(--green); font-size: 14px; padding: 8px 20px; }
.draw-btn-done:hover { background: #15803d; }
.draw-btn-cancel { background: #64748b; }
.draw-btn-cancel:hover { background: #475569; }

/* ===== Loading / Toast ===== */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px;
  color: var(--gray);
  font-size: 14px;
  gap: 10px;
}

.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid var(--border);
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin .6s linear infinite;
}

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

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #1e293b;
  color: #fff;
  padding: 10px 20px;
  border-radius: 24px;
  font-size: 14px;
  z-index: 9000;
  box-shadow: var(--shadow-lg);
  animation: fadeInUp .25s ease;
  pointer-events: none;
  white-space: nowrap;
}

.toast.success { background: var(--green); }
.toast.error   { background: var(--red); }

@keyframes fadeInUp {
  from { opacity: 0; transform: translateX(-50%) translateY(8px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ===== Responsive ===== */
@media (max-width: 640px) {
  .admin-main { padding: 16px 12px; }
  .template-card { flex-wrap: wrap; }
  .template-card-actions { width: 100%; }
  .editor-toolbar { gap: 6px; }
  .modal-box { padding: 20px 16px; }
}
