/* CSS Reset & Variables */
:root {
  --bg-dark: #090d16;
  --bg-panel: #111827;
  --bg-card: #1f2937;
  --bg-hover: #374151;
  --border-color: #374151;
  --border-light: #4b5563;
  
  --text-main: #f9fafb;
  --text-muted: #9ca3af;
  --text-dark: #6b7280;

  --accent-primary: #3b82f6;
  --accent-primary-hover: #2563eb;
  --accent-success: #10b981;
  --accent-danger: #ef4444;
  --accent-warning: #f59e0b;

  /* Indicator Colors matching User Diagram */
  --paper-green: #16a34a;
  --spine-red: #dc2626;
  --box-blue: #2563eb;

  --sidebar-width: 380px;
  --header-height: 64px;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --shadow-sm: 0 2px 4px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
  --shadow-lg: 0 10px 25px rgba(0,0,0,0.5);
  
  --font-family: 'Plus Jakarta Sans', system-ui, -apple-system, sans-serif;
  --font-mono: 'JetBrains Mono', monospace;
}

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

body {
  font-family: var(--font-family);
  background-color: var(--bg-dark);
  color: var(--text-main);
  line-height: 1.5;
  height: 100vh;
  overflow: hidden;
}

/* App Container */
.app-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* Header */
.app-header {
  height: var(--header-height);
  background-color: var(--bg-panel);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  z-index: 20;
}

.logo-area {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-icon {
  width: 40px;
  height: 40px;
  background: rgba(59, 130, 246, 0.15);
  border: 1px solid rgba(59, 130, 246, 0.3);
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent-primary);
}

.logo-text h1 {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.2;
}

.badge {
  font-size: 0.72rem;
  font-weight: 500;
  color: var(--text-muted);
  background: rgba(255, 255, 255, 0.06);
  padding: 2px 8px;
  border-radius: 12px;
  display: inline-block;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  font-family: inherit;
  font-size: 0.88rem;
  font-weight: 600;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all 0.2s ease;
  border: 1px solid transparent;
  white-space: nowrap;
}

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

.btn-primary {
  background-color: var(--accent-primary);
  color: #fff;
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3);
}

.btn-primary:hover:not(:disabled) {
  background-color: var(--accent-primary-hover);
  transform: translateY(-1px);
}

.btn-secondary {
  background-color: var(--bg-card);
  color: var(--text-main);
  border-color: var(--border-light);
}

.btn-secondary:hover:not(:disabled) {
  background-color: var(--bg-hover);
  border-color: var(--text-muted);
}

.btn-icon {
  width: 34px;
  height: 34px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  background-color: var(--bg-card);
  color: var(--text-main);
  border: 1px solid var(--border-color);
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-icon:hover:not(:disabled) {
  background-color: var(--bg-hover);
  color: #fff;
}

.btn-icon.danger:hover:not(:disabled) {
  background-color: rgba(239, 68, 68, 0.2);
  color: var(--accent-danger);
  border-color: var(--accent-danger);
}

/* Main Layout Grid */
.app-main {
  display: flex;
  flex: 1;
  height: calc(100vh - var(--header-height));
  max-height: calc(100vh - var(--header-height));
  overflow: hidden;
  position: relative;
}

/* Sidebar Settings */
.settings-sidebar {
  width: var(--sidebar-width);
  background-color: var(--bg-panel);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  height: 100%;
  max-height: 100%;
  min-height: 0;
  flex-shrink: 0;
  overflow: hidden;
  z-index: 10;
  position: relative;
}

.sidebar-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: rgba(0,0,0,0.15);
  flex-shrink: 0;
}

.sidebar-header h2 {
  font-size: 0.95rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-main);
}

.sidebar-actions {
  display: flex;
  gap: 6px;
}

.sidebar-content {
  flex: 1 1 0%;
  height: 0;
  min-height: 0;
  overflow-y: scroll;
  overflow-x: hidden;
  padding: 16px;
  padding-bottom: 120px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
}

/* Custom Scrollbar for Sidebar */
.sidebar-content::-webkit-scrollbar {
  width: 8px;
}

.sidebar-content::-webkit-scrollbar-track {
  background: rgba(0, 0, 0, 0.3);
}

.sidebar-content::-webkit-scrollbar-thumb {
  background: var(--border-light);
  border-radius: 4px;
}

.sidebar-content::-webkit-scrollbar-thumb:hover {
  background: var(--accent-primary);
}

/* Accordion Groups */
.setting-group {
  position: relative;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  overflow: visible;
  transition: all 0.2s ease;
  z-index: 1;
}

.setting-group:hover {
  border-color: var(--border-light);
  z-index: 2;
}

.setting-group[open] {
  border-color: rgba(59, 130, 246, 0.4);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  z-index: 5;
}

.group-title {
  padding: 12px 16px;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  display: flex;
  align-items: center;
  gap: 8px;
  list-style: none;
}

.group-title::-webkit-details-marker {
  display: none;
}

.group-title::after {
  content: '';
  margin-left: auto;
  width: 6px;
  height: 6px;
  border-right: 2px solid var(--text-muted);
  border-bottom: 2px solid var(--text-muted);
  transform: rotate(45deg);
  transition: transform 0.2s ease;
}

.setting-group[open] .group-title::after {
  transform: rotate(-135deg);
  border-color: var(--accent-primary);
}

.icon-indicator {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}

.green-dot { background-color: var(--paper-green); box-shadow: 0 0 6px var(--paper-green); }
.blue-dot { background-color: var(--box-blue); box-shadow: 0 0 6px var(--box-blue); }
.purple-dot { background-color: #8b5cf6; box-shadow: 0 0 6px #8b5cf6; }
.gray-dot { background-color: var(--text-muted); }

.group-body {
  padding: 16px;
  border-top: 1px solid var(--border-color);
  background-color: rgba(0, 0, 0, 0.2);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.form-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-row label {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-muted);
}

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

.form-control {
  width: 100%;
  padding: 8px 12px;
  background-color: var(--bg-dark);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  color: var(--text-main);
  font-family: inherit;
  font-size: 0.85rem;
  transition: all 0.2s ease;
}

select.form-control {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%9ca3af' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 30px;
}

.form-control:focus {
  outline: none;
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

.info-badge {
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  font-size: 0.78rem;
  display: flex;
  align-items: center;
  gap: 6px;
}

.success-badge {
  background: rgba(22, 163, 74, 0.15);
  border: 1px solid rgba(22, 163, 74, 0.3);
  color: #4ade80;
}

.box-dimensions-summary {
  background: rgba(37, 99, 235, 0.1);
  border: 1px dashed rgba(37, 99, 235, 0.4);
  border-radius: var(--radius-sm);
  padding: 10px;
}

.summary-item {
  display: flex;
  justify-content: space-between;
  font-size: 0.8rem;
}

.summary-label { color: var(--text-muted); }
.summary-value { font-weight: 700; color: #60a5fa; font-family: var(--font-mono); }

.help-text {
  font-size: 0.78rem;
  color: var(--text-muted);
}

/* Signature Grid Map */
.signature-grid-map {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.side-map-card {
  background: var(--bg-dark);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 10px;
}

.side-card-title {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--accent-primary);
  display: block;
  margin-bottom: 8px;
}

.side-slots {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.slot-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.slot-item label {
  font-size: 0.78rem;
  color: var(--text-muted);
  min-width: 100px;
}

.slot-select {
  padding: 4px 8px;
  font-size: 0.8rem;
  font-family: var(--font-mono);
  max-width: 160px;
}

/* Toggles */
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.82rem;
  color: var(--text-muted);
  padding: 4px 0;
}

.toggle-row input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--accent-primary);
  cursor: pointer;
}

/* Workspace Preview Area */
.preview-workspace {
  flex: 1;
  display: flex;
  flex-direction: column;
  background-color: var(--bg-dark);
  position: relative;
  overflow: hidden;
}

/* Toolbar */
.workspace-toolbar {
  height: 48px;
  background-color: var(--bg-panel);
  border-bottom: 1px solid var(--border-color);
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  z-index: 10;
}

.mode-switcher {
  display: flex;
  gap: 4px;
  background: rgba(0, 0, 0, 0.4);
  padding: 3px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
}

.mode-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  padding: 6px 14px;
  font-size: 0.8rem;
  font-weight: 600;
  font-family: inherit;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all 0.2s ease;
}

.mode-btn:hover:not(.active) {
  color: var(--text-main);
  background: rgba(255, 255, 255, 0.05);
}

.mode-btn.active {
  background: linear-gradient(135deg, var(--accent-primary), #2563eb);
  color: #ffffff;
  box-shadow: 0 2px 6px rgba(37, 99, 235, 0.4);
}

.sheet-tabs {
  display: flex;
  gap: 4px;
  background: var(--bg-dark);
  padding: 3px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
}

.tab-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  padding: 5px 12px;
  font-size: 0.8rem;
  font-weight: 500;
  font-family: inherit;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all 0.2s ease;
}

.tab-btn.active {
  background: var(--accent-primary);
  color: #fff;
}

.pagination-controls, .zoom-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
  color: var(--text-muted);
}

#zoomPercent, #sheetIndicator {
  font-family: var(--font-mono);
  min-width: 50px;
  text-align: center;
}

/* Canvas Viewport */
.canvas-viewport {
  flex: 1;
  overflow: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 30px;
  position: relative;
  background-radial-gradient: circle at center, rgba(30, 41, 59, 0.5) 0%, rgba(9, 13, 22, 1) 100%;
  background: radial-gradient(circle at center, #1e293b 0%, #090d16 100%);
}

/* Empty State / Dropzone */
.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

.upload-dropzone {
  border: 2px dashed var(--border-light);
  border-radius: var(--radius-lg);
  padding: 50px 40px;
  text-align: center;
  background: rgba(31, 41, 55, 0.4);
  backdrop-filter: blur(8px);
  max-width: 480px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.upload-dropzone:hover, .upload-dropzone.drag-over {
  border-color: var(--accent-primary);
  background: rgba(59, 130, 246, 0.1);
  transform: translateY(-2px);
}

.dropzone-icon {
  color: var(--accent-primary);
  margin-bottom: 16px;
}

.upload-dropzone h3 {
  font-size: 1.1rem;
  font-weight: 600;
  margin-bottom: 6px;
}

.upload-dropzone p {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 12px;
}

.sub-text {
  font-size: 0.75rem;
  color: var(--text-dark);
}

/* Paper Stage */
.paper-stage {
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.2s ease-out;
}

/* The Paper Sheet Container */
.paper-container {
  background-color: #ffffff;
  position: relative;
  box-shadow: var(--shadow-lg), 0 0 0 1px rgba(255,255,255,0.1);
  border-radius: 2px;
  transform-origin: center center;
  transition: width 0.2s ease, height 0.2s ease;
}

/* Green Paper Boundary Guide (Exact match to User diagram) */
.paper-boundary-guide {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border: 4px solid var(--paper-green);
  pointer-events: none;
  z-index: 5;
}

/* Red Fold Center Line Guide (Exact match to User diagram) */
.paper-center-line {
  position: absolute;
  top: -12px;
  bottom: -12px;
  left: 50%;
  width: 3px;
  background-color: var(--spine-red);
  transform: translateX(-50%);
  pointer-events: none;
  z-index: 6;
}

.spine-tag {
  position: absolute;
  top: -20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--spine-red);
  color: #fff;
  font-size: 9px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 3px;
  white-space: nowrap;
  letter-spacing: 0.5px;
}

/* Blue Printable Boxes (Exact match to User diagram) */
.printable-box {
  position: absolute;
  border: 3px solid var(--box-blue);
  background-color: rgba(37, 99, 235, 0.03);
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  z-index: 4;
  transition: all 0.2s ease;
}

.page-canvas-wrapper {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.page-canvas-wrapper canvas {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
}

.box-badge {
  position: absolute;
  top: 6px;
  left: 6px;
  background: rgba(37, 99, 235, 0.9);
  color: #ffffff;
  font-size: 11px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 4px;
  font-family: var(--font-mono);
  box-shadow: 0 2px 4px rgba(0,0,0,0.2);
  z-index: 10;
  pointer-events: none;
}

/* 3D Realistic Book Reader Stage */
.book-stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  width: 100%;
  height: 100%;
  padding: 20px;
}

.book-mockup {
  display: flex;
  position: relative;
  background: #fdfbf7;
  border-radius: 4px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.6), 0 5px 15px rgba(0, 0, 0, 0.4);
  padding: 12px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  perspective: 1200px;
  transition: transform 0.3s ease;
}

.book-page-leaf {
  width: 320px;
  height: 440px;
  background: #ffffff;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.05);
}

.left-leaf {
  border-radius: 4px 0 0 4px;
  border-right: 1px solid #e5e7eb;
}

.right-leaf {
  border-radius: 0 4px 4px 0;
  border-left: 1px solid #e5e7eb;
}

.leaf-inner {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
}

.leaf-inner canvas {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.book-spine-crease {
  width: 16px;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  background: linear-gradient(to right, 
    rgba(0, 0, 0, 0.25) 0%, 
    rgba(0, 0, 0, 0.08) 35%, 
    rgba(0, 0, 0, 0.3) 50%, 
    rgba(0, 0, 0, 0.08) 65%, 
    rgba(0, 0, 0, 0.25) 100%
  );
  z-index: 10;
  pointer-events: none;
}

.leaf-shadow {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 40px;
  pointer-events: none;
  z-index: 8;
}

.left-shadow {
  right: 0;
  background: linear-gradient(to left, rgba(0, 0, 0, 0.15), transparent);
}

.right-shadow {
  left: 0;
  background: linear-gradient(to right, rgba(0, 0, 0, 0.15), transparent);
}

.leaf-number-badge {
  position: absolute;
  bottom: 10px;
  background: rgba(17, 24, 39, 0.85);
  color: #ffffff;
  font-size: 11px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 10px;
  font-family: var(--font-mono);
  z-index: 12;
}

.left-leaf .leaf-number-badge { left: 16px; }
.right-leaf .leaf-number-badge { right: 16px; }

/* Book Reader Control Bar */
.book-reader-bar {
  display: flex;
  align-items: center;
  gap: 16px;
  background: var(--bg-panel);
  border: 1px solid var(--border-color);
  padding: 8px 20px;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  z-index: 15;
}

.book-read-info {
  font-size: 0.88rem;
  font-weight: 600;
  font-family: var(--font-mono);
  color: var(--text-main);
  min-width: 140px;
  text-align: center;
}

/* Workspace Statusbar */
.workspace-statusbar {
  height: 32px;
  background-color: var(--bg-panel);
  border-top: 1px solid var(--border-color);
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.status-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-indicator-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: var(--accent-success);
}

.status-indicator-dot.working {
  background-color: var(--accent-warning);
  animation: pulse 1s infinite alternate;
}

@keyframes pulse {
  0% { opacity: 0.4; }
  100% { opacity: 1; }
}

/* Toast Notifications */
.toast-container {
  position: fixed;
  bottom: 40px;
  right: 24px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
  pointer-events: none;
}

.toast {
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  color: var(--text-main);
  padding: 10px 16px;
  border-radius: var(--radius-md);
  font-size: 0.85rem;
  box-shadow: var(--shadow-md);
  pointer-events: auto;
  animation: slideIn 0.3s ease;
  display: flex;
  align-items: center;
  gap: 10px;
}

.toast.success { border-color: var(--accent-success); }
.toast.error { border-color: var(--accent-danger); }

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* Responsive Mobile Layout */
@media (max-width: 900px) {
  body {
    overflow: auto;
  }

  .app-container {
    height: auto;
    min-height: 100vh;
  }

  .app-main {
    flex-direction: column;
    overflow: visible;
  }

  .settings-sidebar {
    width: 100%;
    height: auto;
    max-height: 50vh;
    border-right: none;
    border-bottom: 1px solid var(--border-color);
  }

  .workspace-toolbar {
    flex-wrap: wrap;
    height: auto;
    padding: 8px;
    gap: 8px;
  }

  .preview-workspace {
    min-height: 500px;
  }
}
