:root {
  --primary: #6a0dad;
  --bg-dim: #f5f5f5;
  --border: #e5e7eb;
  --ios-shadow: 0 1px 3px rgba(15, 23, 42, 0.06), 0 4px 14px rgba(15, 23, 42, 0.04);
  --ios-radius: 12px;
  --btn-radius: 14px;
}

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

body {
  margin: 0;
  font-family: 'Inter', sans-serif;
  background-color: #fff;
  height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  color: #1f2937;
}

.topbar {
  height: 60px;
  background: #fff;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 20px 0 25px;
  flex-shrink: 0;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.topbar-notes-icon {
  width: 28px;
  height: 28px;
  flex-shrink: 0;
  color: var(--primary);
}

.title {
  color: var(--primary);
  font-weight: 700;
  font-size: 18px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-credit {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
  text-decoration: none;
  color: #6b7280;
  font-size: 13px;
  font-weight: 600;
  transition: color 0.2s ease;
}

.topbar-credit:hover {
  color: var(--primary);
}

.topbar-github-icon {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
  display: block;
}

.main-layout {
  display: flex;
  flex: 1;
  min-height: 0;
  height: calc(100vh - 60px);
  overflow: hidden;
}

.left-panel {
  width: 380px;
  max-width: 42vw;
  border-right: 1px solid var(--border);
  padding: 18px 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 0;
  overflow-y: auto;
  overflow-x: hidden;
  flex-shrink: 0;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}

.section {
  margin-bottom: 18px;
  flex-shrink: 0;
}

.section-title {
  font-size: 11px;
  font-weight: 700;
  color: #9ca3af;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 10px;
}

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px 12px;
}

.full {
  grid-column: span 2;
}

.range-row label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.range-val {
  font-weight: 600;
  color: #6b7280;
  font-variant-numeric: tabular-nums;
}

label {
  font-size: 11px;
  font-weight: 600;
  color: #4b5563;
  display: block;
  margin-bottom: 6px;
}

.inline-label {
  margin-top: 4px;
}

.field-hint {
  font-size: 11px;
  color: #6b7280;
  line-height: 1.45;
  margin: 4px 0 8px;
}

.custom-font-area {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid #f3f4f6;
}

/* iOS-style controls */
.ios-input,
select.ios-input,
input.ios-input,
textarea.ios-input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #e8eaed;
  border-radius: var(--ios-radius);
  font-size: 14px;
  font-family: inherit;
  background: #fafafa;
  box-shadow: var(--ios-shadow);
  transition: border-color 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}

.ios-input:hover {
  border-color: #dce0e6;
  background: #fff;
}

.ios-input:focus {
  outline: none;
  border-color: rgba(106, 13, 173, 0.35);
  background: #fff;
  box-shadow: 0 0 0 3px rgba(106, 13, 173, 0.12), var(--ios-shadow);
}

textarea.notes-textarea {
  min-height: 220px;
  resize: vertical;
  line-height: 1.5;
}

.ios-range {
  width: 100%;
  accent-color: var(--primary);
  height: 6px;
  border-radius: 999px;
}

input[type='number'].ios-input {
  -moz-appearance: textfield;
}

input[type='number'].ios-input::-webkit-outer-spin-button,
input[type='number'].ios-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Mode tabs (unchanged theme) */
.mode-toggle {
  background: #f9fafb;
  padding: 4px;
  border-radius: 10px;
  border: 1px solid #eee;
  display: flex;
  margin-bottom: 4px;
}

.mode-toggle .tab-btn {
  flex: 1;
  padding: 10px 8px;
  font-size: 11px;
  font-weight: 700;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  background: transparent;
  color: #6b7280;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.mode-toggle .tab-btn:hover:not(.active) {
  background: rgba(0,0,0,0.03);
  color: #4b5563;
}

.mode-toggle .tab-btn.active {
  background: var(--primary);
  color: #fff;
  box-shadow: 0 4px 12px rgba(106, 13, 173, 0.2);
  transform: scale(1.02);
}

.mode-toggle .tab-btn:active {
  transform: scale(0.97);
}

/* Input source tabs */
.input-tabs {
  background: #f3f4f6;
  padding: 4px;
  border-radius: var(--ios-radius);
  border: 1px solid #e8eaed;
  display: flex;
  gap: 4px;
  margin-bottom: 12px;
}

.input-tabs .input-tab-btn {
  flex: 1;
  padding: 9px 8px;
  font-size: 12px;
  font-weight: 600;
  border-radius: 10px;
  border: none;
  cursor: pointer;
  background: transparent;
  color: #6b7280;
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

.input-tabs .input-tab-btn:hover:not(.active) {
  background: rgba(0,0,0,0.03);
}

.input-tabs .input-tab-btn.active {
  background: #fff;
  color: var(--primary);
  box-shadow: 0 1px 4px rgba(15, 23, 42, 0.08);
  transform: scale(1.02);
}

.input-tabs .input-tab-btn:active {
  transform: scale(0.97);
}

.tab-content {
  display: none;
}

.tab-content.active {
  display: block;
  animation: tabFadeIn 0.38s cubic-bezier(0.16, 1, 0.3, 1);
}

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

.ai-panel:not([hidden]),
#manual-input-area:not([hidden]) {
  animation: tabFadeIn 0.45s cubic-bezier(0.16, 1, 0.3, 1);
}


/* AI panel */
.ai-panel {
  padding-bottom: 4px;
}

.ai-intro {
  font-size: 13px;
  color: #6b7280;
  line-height: 1.5;
  margin: 0 0 12px;
}

.ai-upload-box {
  border: 2px dashed rgba(106, 13, 173, 0.35);
  padding: 14px;
  border-radius: var(--ios-radius);
  background: linear-gradient(180deg, #f8f5ff 0%, #f3f0ff 100%);
  text-align: center;
}

.ai-file-input {
  background: #fff !important;
}

.ai-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 16px;
}

.generate-btn-ai {
  width: 100%;
  padding: 13px 14px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: var(--btn-radius);
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(106, 13, 173, 0.22);
  transition: transform 0.18s ease, background 0.18s ease, box-shadow 0.18s ease;
}

.generate-btn-ai:hover:not(:disabled) {
  transform: translateY(-1px);
  background: #5a0bb5;
  box-shadow: 0 8px 22px rgba(106, 13, 173, 0.28);
}

.generate-btn-ai:disabled {
  opacity: 0.65;
  cursor: not-allowed;
  transform: none;
}

/* iOS-style secondary buttons */
.btn-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 6px;
  padding-top: 4px;
}

.btn-ios {
  width: 100%;
  padding: 12px 14px;
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  color: #111827;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: var(--btn-radius);
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
  transition: background 0.18s ease, border-color 0.18s ease, box-shadow 0.18s ease,
    transform 0.18s ease;
}

.btn-ios:hover {
  background: #fafafa;
  border-color: #d1d5db;
  box-shadow: 0 4px 14px rgba(15, 23, 42, 0.07);
  transform: translateY(-0.5px);
}

.btn-ios:active {
  transform: translateY(0);
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.06);
}

/* Preview */
.right-panel {
  flex: 1;
  min-width: 0;
  min-height: 0;
  background: var(--bg-dim);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  overflow: auto;
  position: relative;
  padding-top: 8px;
}

.error-toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  max-width: min(420px, 92vw);
  padding: 12px 18px;
  background: #1f2937;
  color: #f9fafb;
  font-size: 14px;
  font-weight: 600;
  border-radius: 12px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
  text-align: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s ease;
}

.error-toast.is-visible {
  opacity: 1;
}

.error-toast[hidden] {
  display: none;
}

#paperWrapper {
  padding: 32px 24px 48px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  transform: scale(0.78);
  transform-origin: top center;
}

.paper-stack {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.paper.notebook-page {
  width: 500px;
  height: 700px;
  position: relative;
  overflow: hidden;
  border-radius: 6px;
  box-shadow:
    0 2px 6px rgba(15, 23, 42, 0.06),
    0 12px 32px rgba(15, 23, 42, 0.08);
  transition: box-shadow 0.25s ease, transform 0.25s ease, filter 0.25s ease;
  background-size: cover;
  background-position: center;
}

/* FX layers (stack: shadow → handwriting → spiral → text → scanner) */
.paper-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  border-radius: inherit;
}

.paper-layer--shadow {
  display: none;
  z-index: 1;
  background-image:
    radial-gradient(ellipse 92% 88% at 50% 48%, transparent 35%, rgba(0, 0, 0, 0.055) 100%),
    url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.55'/%3E%3C/svg%3E");
  background-size: cover, 180px 180px;
  mix-blend-mode: multiply;
  opacity: 0.42;
}

.notebook-page.effect-shadow .paper-layer--shadow {
  display: block;
}

.notebook-page.effect-shadow {
  box-shadow:
    0 2px 8px rgba(15, 23, 42, 0.07),
    inset 0 0 50px rgba(0, 0, 0, 0.04),
    inset 0 0 140px rgba(0, 0, 0, 0.025);
}

.paper-layer--handwriting {
  display: none;
  z-index: 2;
  opacity: 0.055;
  mix-blend-mode: multiply;
  background-repeat: no-repeat;
  background-position: center;
}

.notebook-page.has-handwriting .paper-layer--handwriting {
  display: block;
}

.paper-layer--scanner {
  display: none;
  z-index: 6;
  background-image:
    repeating-linear-gradient(
      0deg,
      transparent,
      transparent 3px,
      rgba(0, 0, 0, 0.022) 3px,
      rgba(0, 0, 0, 0.022) 4px
    ),
    linear-gradient(
      118deg,
      rgba(255, 255, 255, 0.14) 0%,
      transparent 42%,
      rgba(0, 0, 0, 0.05) 100%
    );
  mix-blend-mode: multiply;
  opacity: 0.65;
}

.notebook-page.effect-scanner .paper-layer--scanner {
  display: block;
}

/* Spiral binding */
.page-spiral {
  position: absolute;
  left: 10px;
  top: 24px;
  bottom: 24px;
  width: 18px;
  background: radial-gradient(circle at center, #d1d5db 4px, transparent 4.5px);
  background-size: 18px 26px;
  opacity: 0.45;
  pointer-events: none;
  z-index: 4;
}

.paper-inner {
  position: relative;
  z-index: 5;
  width: 100%;
  height: 100%;
  overflow: hidden;
  box-sizing: border-box;
}

.margin-line::before {
  content: '';
  position: absolute;
  left: 72px;
  top: 0;
  bottom: 0;
  width: 2px;
  background: rgba(100, 116, 139, 0.35);
  z-index: 5;
  pointer-events: none;
}

.notebook-page.effect-scanner {
  transform: rotate(var(--page-skew, 0deg));
  filter: contrast(0.9) brightness(1.03) saturate(0.78) blur(0.35px);
  box-shadow:
    0 2px 6px rgba(15, 23, 42, 0.06),
    inset 0 0 0 1px rgba(0, 0, 0, 0.07);
}

.nav-controls {
  position: sticky;
  top: 10px;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(10px);
  padding: 8px 16px;
  border-radius: 999px;
  border: 1px solid #e8eaed;
  box-shadow: var(--ios-shadow);
}

.nav-controls #pageNumber {
  font-size: 13px;
  font-weight: 600;
  color: #4b5563;
  min-width: 108px;
  text-align: center;
}

.nav-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid #e8eaed;
  background: #fff;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.05);
  transition: background 0.15s ease, border-color 0.15s ease;
}

.nav-btn:hover:not(:disabled) {
  background: #f9fafb;
  border-color: #d1d5db;
}

.nav-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.mobile-preview-header,
.mobile-preview-footer {
  display: none;
}

@media (max-width: 768px) {
  :root {
    --mobile-safe-top: max(12px, env(safe-area-inset-top));
  }

  body {
    height: 100dvh;
  }

  .topbar {
    min-height: calc(60px + var(--mobile-safe-top));
    height: auto;
    padding: var(--mobile-safe-top) 14px 0 16px;
  }

  .main-layout {
    position: relative;
    height: calc(100dvh - (60px + var(--mobile-safe-top)));
  }

  .left-panel {
    width: 100%;
    max-width: 100%;
    border-right: none;
    padding: 16px 16px calc(28px + env(safe-area-inset-bottom));
  }

  .right-panel {
    position: fixed;
    left: 0;
    right: 0;
    top: calc(60px + var(--mobile-safe-top));
    bottom: 0;
    z-index: 40;
    padding-top: 0;
    border-top: 1px solid var(--border);
    border-radius: 24px 24px 0 0;
    box-shadow: 0 -18px 44px rgba(15, 23, 42, 0.18);
    transform: translateY(calc(100% + 24px));
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: transform 0.32s ease, opacity 0.32s ease;
    overflow-x: hidden;
    overflow-y: auto;
  }

  .right-panel.mobile-sheet-open {
    transform: translateY(0);
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
  }

  body.preview-open .left-panel {
    overflow: hidden;
  }

  .mobile-preview-header {
    width: 100%;
    position: sticky;
    top: 0;
    z-index: 30;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 16px 12px;
    background: rgba(245, 245, 245, 0.94);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid rgba(229, 231, 235, 0.95);
  }

  .mobile-preview-title {
    font-size: 15px;
    font-weight: 700;
    color: #111827;
  }

  .mobile-preview-close {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    border: 1px solid #d1d5db;
    border-radius: 999px;
    background: #fff;
    color: #111827;
    font-size: 24px;
    line-height: 1;
    cursor: pointer;
    box-shadow: 0 1px 2px rgba(15, 23, 42, 0.06);
  }

  .nav-controls {
    top: 74px;
    margin-top: 10px;
  }

  #paperWrapper {
    padding: 18px 12px 28px;
    transform: scale(0.62);
  }

  .mobile-preview-footer {
    width: 100%;
    position: sticky;
    bottom: 0;
    z-index: 25;
    display: block;
    padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
    background: linear-gradient(180deg, rgba(245, 245, 245, 0.35) 0%, rgba(245, 245, 245, 0.95) 28%);
    backdrop-filter: blur(12px);
    border-top: 1px solid rgba(229, 231, 235, 0.92);
  }

  .mobile-preview-download {
    width: 100%;
  }
}

@media (min-width: 769px) and (max-width: 900px) {
  .left-panel {
    width: 320px;
    max-width: 100%;
  }

  #paperWrapper {
    transform: scale(0.62);
  }
}

/* AI Mode Reordering & Visibility */
body.ai-mode .mode-selection-section {
  display: block; /* Restore visibility */
}

.left-panel {
  display: flex !important;
  flex-direction: column !important;
}
body.ai-mode #handwriting-section { order: 1; }
body.ai-mode #ai-input-area { order: 2; display: block !important; }
body.ai-mode #settings-area { order: 3; }
body.ai-mode .main-btn-group { order: 4; }

/* Filter settings area in AI mode */
body.ai-mode .setting-item-vspacing,
body.ai-mode .setting-item-wordgap,
body.ai-mode .setting-item-chargap,
body.ai-mode .setting-item-effects {
  display: none !important;
}

/* Visibility toggles based on mode */
body.ai-mode .manual-only { display: none !important; }
body:not(.ai-mode) .ai-only { display: none !important; }
body.ai-mode #ai-desc-area { margin-bottom: 0px; }

/* Dev Overlay Styles */
.dev-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(4px);
  z-index: 9999;
  display: flex !important;
  align-items: center;
  justify-content: center;
}
.dev-overlay[hidden] {
  display: none !important;
}
.dev-modal {
  background: white;
  padding: 40px;
  border-radius: 20px;
  position: relative;
  text-align: center;
  box-shadow: 0 20px 40px rgba(0,0,0,0.2);
  min-width: 300px;
}
.dev-close {
  position: absolute;
  right: 15px;
  top: 10px;
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #666;
}
.dev-icon {
  font-size: 48px;
  display: block;
  margin-bottom: 20px;
}
.dev-text {
  color: #ef4444;
  font-weight: 700;
  margin: 0;
}

