:root {
  --brand: #0284c7;
  --brand-dark: #0369a1;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --bg: #f1f5f9;
  --card: #ffffff;
  --success: #16a34a;
  --success-dark: #15803d;
  --danger: #dc2626;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 8px 24px rgba(15, 23, 42, 0.06);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  display: flex;
  flex-direction: column;
}

/* Header */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 22px; background: #fff; border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  display: grid; place-items: center; width: 40px; height: 40px;
  border-radius: 10px; background: var(--brand); color: #fff; font-weight: 800; font-size: 18px; letter-spacing: -1px;
}
.brand h1 { margin: 0; font-size: 17px; }
.brand p { margin: 2px 0 0; font-size: 12px; color: var(--muted); }
.topbar-right { display: flex; align-items: center; gap: 14px; }
.restart { font-size: 13px; color: var(--brand); cursor: pointer; text-decoration: none; font-weight: 600; }

/* Admin / public mode switch */
.mode-pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 11px; border-radius: 99px; font-size: 12px; font-weight: 700;
  cursor: pointer; border: 1px solid transparent; transition: all .15s;
}
.mode-pill::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.mode-pill.admin { background: #dcfce7; color: #15803d; border-color: #86efac; }
.mode-pill.public { background: #f1f5f9; color: #475569; border-color: #cbd5e1; }
.mode-pill:hover { filter: brightness(0.96); }
.restart:hover { text-decoration: underline; }

/* Layout */
.layout {
  flex: 1; min-height: 0;
  display: grid; grid-template-columns: 340px 1fr; gap: 20px; padding: 20px;
}
@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; overflow-y: auto; }
  .preview { min-height: 70vh; }
}

.panel {
  display: flex; flex-direction: column; gap: 16px; min-width: 0;
  overflow-y: auto; padding-right: 4px;
}

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; flex-shrink: 0;
}
.card h2 {
  margin: 0 0 14px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted);
  display: flex; align-items: center; gap: 8px;
}
.live-tag {
  font-size: 10px; letter-spacing: 0.04em; background: #dcfce7; color: #15803d;
  padding: 2px 7px; border-radius: 99px; text-transform: none; font-weight: 700;
}

/* Dropzone */
.dropzone {
  border: 2px dashed #cbd5e1; border-radius: 10px; padding: 24px 16px; text-align: center;
  color: var(--muted); cursor: pointer; transition: all 0.15s ease;
}
.dropzone:hover, .dropzone.drag { border-color: var(--brand); background: #f0f9ff; color: var(--brand-dark); }
.dz-title { margin: 8px 0 2px; font-weight: 600; color: var(--ink); }
.dz-sub { margin: 0; font-size: 12.5px; }
.file-row {
  display: flex; align-items: center; justify-content: space-between; margin-top: 12px;
  padding: 10px 12px; background: #f8fafc; border: 1px solid var(--line); border-radius: 8px;
}
.file-name { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.link { background: none; border: none; color: var(--brand); cursor: pointer; font-size: 13px; font-weight: 600; }

/* Fields */
.field { display: block; margin-bottom: 14px; }
.field:last-child { margin-bottom: 0; }
.field > span { display: block; font-size: 12px; font-weight: 600; color: #475569; margin-bottom: 6px; }
.field input[type="number"], .field input[type="text"], .field select {
  width: 100%; padding: 9px 11px; border: 1px solid #cbd5e1; border-radius: 8px; font-size: 14px; background: #fff;
}
.field input:focus, .field select:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px rgba(2, 132, 199, 0.15); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.seg button {
  padding: 9px; border: 1px solid #cbd5e1; background: #fff; border-radius: 8px; cursor: pointer; font-size: 14px; color: #475569;
}
.seg button.active { border-color: var(--brand); background: #f0f9ff; color: var(--brand-dark); font-weight: 600; box-shadow: inset 0 0 0 1px var(--brand); }

.color-wrap { display: flex; gap: 8px; align-items: center; }
.color-wrap input[type="color"] { width: 44px; height: 38px; flex-shrink: 0; padding: 2px; border: 1px solid #cbd5e1; border-radius: 8px; background: #fff; cursor: pointer; }
.color-wrap input[type="text"] { flex: 1; min-width: 0; font-family: ui-monospace, Menlo, monospace; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; padding: 11px 16px;
  border: 1px solid #cbd5e1; background: #fff; color: var(--ink); border-radius: 9px;
  font-size: 14px; font-weight: 600; cursor: pointer; transition: all 0.15s;
}
.btn:hover { background: #f8fafc; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn.block { width: 100%; }
.btn.success { background: var(--success); border-color: var(--success); color: #fff; }
.btn.success:hover { background: var(--success-dark); }
.actions .muted { font-size: 13px; color: var(--muted); margin: 0 0 8px; }
.block-link { display: block; margin: 0 0 12px; font-size: 13px; text-decoration: none; color: var(--brand); }
.block-link:hover { text-decoration: underline; }
.fineprint { font-size: 12px; color: var(--muted); margin: 10px 0 0; text-align: center; }

/* Preview */
.preview {
  background: #fff; border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
  display: flex; flex-direction: column; min-width: 0; min-height: 0; overflow: hidden;
}
.preview-bar {
  display: flex; align-items: center; gap: 10px; padding: 8px 14px;
  border-bottom: 1px solid var(--line); background: #fafafa; flex-shrink: 0;
}
.navbtn {
  width: 30px; height: 30px; border: 1px solid #cbd5e1; background: #fff; border-radius: 7px;
  cursor: pointer; font-size: 18px; line-height: 1; color: #475569;
}
.navbtn:hover { background: #f1f5f9; }
.navbtn:disabled { opacity: 0.4; cursor: not-allowed; }
.pageinfo { font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; min-width: 56px; text-align: center; }
.bar-hint { margin-left: auto; font-size: 12px; color: var(--muted); }

.preview-scroll { position: relative; flex: 1; min-height: 0; overflow: auto; background: #eef2f6; padding: 20px; }

.stage { position: relative; display: block; width: fit-content; margin: 0 auto; box-shadow: 0 6px 24px rgba(15,23,42,0.18); background: #fff; }
#page-img { display: block; max-width: 100%; height: auto; }
.overlay { position: absolute; inset: 0; pointer-events: none; }
.marker { position: absolute; line-height: 1; white-space: nowrap; font-weight: 400; }
.pagenum { position: absolute; line-height: 1; white-space: nowrap; color: #000; font-weight: 400; }

.subsection { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--line); }
.toggle-row { display: flex; align-items: center; gap: 9px; cursor: pointer; font-size: 14px; color: #334155; font-weight: 600; }
.toggle-row input { width: 17px; height: 17px; cursor: pointer; accent-color: var(--brand); }
.toggle-row + .grid2 { margin-top: 14px; }

.preview-empty, .preview-busy, .preview-error {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; color: var(--muted); text-align: center; padding: 24px;
}
.preview-empty svg { color: #cbd5e1; }
.spinner { width: 42px; height: 42px; border-radius: 50%; border: 4px solid #e2e8f0; border-top-color: var(--brand); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.bar { width: 260px; max-width: 70%; height: 8px; background: #e2e8f0; border-radius: 99px; overflow: hidden; }
.bar-fill { height: 100%; width: 0%; background: var(--brand); transition: width 0.3s ease; }
.preview-error p { color: var(--danger); font-weight: 600; max-width: 420px; }

.footer { text-align: center; padding: 12px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); background: #fff; flex-shrink: 0; }


/* Admin sign-in */
.footer { position: relative; }
.admin-link {
  position: absolute; right: 16px; top: 50%; transform: translateY(-50%);
  background: none; border: none; color: #94a3b8; font-size: 11px; cursor: pointer;
  padding: 4px 8px; border-radius: 6px;
}
.admin-link:hover { color: var(--brand); background: #f1f5f9; }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15,23,42,.45);
  display: flex; align-items: center; justify-content: center; z-index: 100; padding: 20px;
}
.modal {
  background: #fff; border-radius: var(--radius); box-shadow: 0 20px 50px rgba(15,23,42,.3);
  padding: 24px; width: 100%; max-width: 360px;
}
.modal h3 { margin: 0 0 4px; font-size: 17px; }
.modal-sub { margin: 0 0 16px; font-size: 13px; color: var(--muted); }
.modal input[type="password"] {
  width: 100%; padding: 10px 12px; border: 1px solid #cbd5e1; border-radius: 8px; font-size: 14px;
}
.modal input[type="password"]:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px rgba(2,132,199,.15); }
.modal-error { color: var(--danger); font-size: 13px; margin: 8px 0 0; font-weight: 600; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
