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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #f0f0f0;
  color: #222;
  min-height: 100vh;
}

/* Toolbar */
#toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: #fff;
  border-bottom: 1px solid #ccc;
  flex-wrap: wrap;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 4px;
}

.toolbar-right {
  margin-left: auto;
}

.toolbar-divider {
  width: 1px;
  height: 24px;
  background: #ddd;
  flex-shrink: 0;
}

#toolbar button {
  padding: 6px 12px;
  border: 1px solid #bbb;
  border-radius: 4px;
  background: #fafafa;
  cursor: pointer;
  font-size: 14px;
}

#toolbar button:hover {
  background: #e8e8e8;
}

#toolbar button.active {
  background: #d0d0ff;
  border-color: #88a;
}

#toolbar select {
  padding: 5px 8px;
  border: 1px solid #bbb;
  border-radius: 4px;
  font-size: 14px;
  max-width: 160px;
}

#toolbar label {
  font-size: 13px;
  color: #555;
}

/* Main layout */
#main {
  display: flex;
  height: calc(100vh - 52px);
  overflow: hidden;
}

/* Panel navigation sidebar */
#panel-nav {
  width: 140px;
  min-width: 140px;
  background: #fff;
  border-right: 1px solid #ccc;
  overflow-y: auto;
  padding: 8px;
}

.nav-panel {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
  margin-bottom: 4px;
}

.nav-panel:hover {
  background: #eef;
}

.nav-panel.active {
  background: #d8d8ff;
  font-weight: 600;
}

.nav-thumb {
  width: 36px;
  height: 56px;
  border: 1px solid #aaa;
  border-radius: 3px;
  background: #fff;
  flex-shrink: 0;
  overflow: hidden;
  position: relative;
}

.nav-thumb-inner {
  width: 330px;
  height: 510px;
  padding: 16px;
  font-size: 16px;
  line-height: 1.5;
  transform: scale(0.1091);
  transform-origin: top left;
  position: relative;
  overflow: hidden;
}

.nav-thumb-inner img {
  position: absolute;
  display: block;
}

/* Editor area */
#editor-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 24px;
  overflow-y: auto;
}

#editor-label {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 12px;
  color: #444;
}

/* Two-layer editor wrapper */
#editor-wrapper {
  width: 330px;
  height: 510px;
  position: relative;
  border: 2px solid #aaa;
  border-radius: 4px;
  background: #fff;
  overflow: hidden;
}

#editor-wrapper:focus-within {
  border-color: #66a;
}

#panel-editor {
  width: 100%;
  height: 100%;
  padding: 16px;
  font-size: 16px;
  line-height: 1.5;
  overflow: hidden;
  outline: none;
  cursor: text;
}

/* Image layer - sits on top of text */
#image-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

#image-layer img {
  position: absolute;
  pointer-events: auto;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
}

#image-layer img:active {
  cursor: grabbing;
}

#image-layer img.selected-image {
  outline: 2px solid #66a;
  outline-offset: 2px;
}

/* Snap guides */
.snap-guide {
  position: absolute;
  pointer-events: none;
  z-index: 10;
}

#snap-guide-h {
  left: 50%;
  top: 0;
  bottom: 0;
  width: 1px;
  border-left: 1px dashed #e55;
}

#snap-guide-v {
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  border-top: 1px dashed #e55;
}

/* Sheet preview */
#preview-toggle-bar {
  text-align: center;
  padding: 8px;
  background: #fff;
  border-top: 1px solid #ccc;
}

#preview-toggle-bar button {
  padding: 6px 16px;
  border: 1px solid #bbb;
  border-radius: 4px;
  background: #fafafa;
  cursor: pointer;
  font-size: 13px;
}

#preview-toggle-bar button:hover {
  background: #e8e8e8;
}

.hidden {
  display: none !important;
}

#sheet-preview {
  background: #e8e8e8;
  padding: 16px;
  border-top: 1px solid #ccc;
  overflow-x: auto;
}

.sheet-grid {
  display: grid;
  grid-template-columns: repeat(4, 165px);
  grid-template-rows: repeat(2, 255px);
  gap: 2px;
  width: fit-content;
  margin: 0 auto;
  background: #999;
  border: 1px solid #999;
}

.sheet-cell {
  background: #fff;
  position: relative;
  overflow: hidden;
  cursor: pointer;
}

.sheet-cell:hover {
  outline: 2px solid #66a;
  z-index: 1;
}

.cell-content {
  width: 330px;
  height: 510px;
  overflow: hidden;
  font-size: 16px;
  line-height: 1.5;
  padding: 16px;
  position: absolute;
  top: 0;
  left: 0;
  transform: scale(0.5);
  transform-origin: top left;
}

.sheet-cell.rotated .cell-content {
  transform: translate(165px, 255px) rotate(180deg) scale(0.5);
  transform-origin: top left;
}

.cell-content img {
  position: absolute;
  display: block;
}

.cell-label {
  position: absolute;
  bottom: 2px;
  right: 4px;
  font-size: 9px;
  color: #999;
  pointer-events: none;
  z-index: 1;
}

.sheet-cell.rotated .cell-label {
  top: 2px;
  bottom: auto;
  left: 4px;
  right: auto;
}

/* Print layout - hidden on screen, shown when printing */
#print-layout {
  display: none;
  position: relative;
}

.print-grid {
  display: grid;
  grid-template-columns: repeat(4, 2.75in);
  grid-template-rows: repeat(2, 4.25in);
  width: 11in;
  height: 8.5in;
}

.print-cell {
  overflow: hidden;
  position: relative;
}

.print-cell.rotated {
  transform: rotate(180deg);
}

/* Inner wrapper matches editor dimensions, scaled to fit print cell */
.print-cell-inner {
  width: 330px;
  height: 510px;
  padding: 16px;
  font-size: 16px;
  line-height: 1.5;
  position: relative;
  transform: scale(0.8);
  transform-origin: top left;
}

.print-cell-inner img {
  position: absolute;
  display: block;
}

/* Page numbers on print cells (shown when fold guides enabled) */
.print-page-number {
  position: absolute;
  bottom: 6px;
  right: 8px;
  font-size: 9px;
  color: #bbb;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  pointer-events: none;
  z-index: 1;
}

/* Print media query */
@media print {
  html, body {
    margin: 0;
    padding: 0;
    height: auto;
    min-height: 0;
    overflow: hidden;
  }

  body > *:not(#print-layout) {
    display: none !important;
  }

  #print-layout {
    display: block !important;
    overflow: hidden;
    page-break-after: avoid;
    break-after: avoid;
  }

  @page {
    size: 11in 8.5in landscape;
    margin: 0;
  }

  .print-grid {
    position: absolute;
    top: 0;
    left: 0;
  }

  .fold-guides:not(.hidden) {
    display: block !important;
  }
}

/* Fallback: if browser ignores @page size and renders portrait, scale to fit */
@media print and (orientation: portrait) {
  .print-grid {
    transform: scale(0.727);
    transform-origin: top left;
  }

  #print-layout {
    max-height: 100vh;
    overflow: hidden;
  }

  .fold-guides {
    transform: scale(0.727);
    transform-origin: top left;
  }
}

/* Fold guide toggle in toolbar */
.toggle-label {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
  color: #555;
  cursor: pointer;
  padding: 0 8px;
}

/* Fold guides SVG overlay on print layout */
.fold-guides {
  position: absolute;
  top: 0;
  left: 0;
  width: 11in;
  height: 8.5in;
  pointer-events: none;
}

/* Image resize popover */
#image-resize-popover {
  position: fixed;
  z-index: 200;
  background: #fff;
  border: 1px solid #bbb;
  border-radius: 6px;
  padding: 4px 6px;
  display: flex;
  align-items: center;
  gap: 3px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

#image-resize-popover.hidden {
  display: none;
}

#image-resize-popover button {
  padding: 4px 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
  background: #fafafa;
  cursor: pointer;
  font-size: 12px;
  white-space: nowrap;
}

#image-resize-popover button:hover {
  background: #e0e0ff;
}

#image-resize-popover .delete-btn {
  color: #c44;
  border-color: #daa;
}

#image-resize-popover .delete-btn:hover {
  background: #fee;
}

.popover-divider {
  width: 1px;
  height: 20px;
  background: #ddd;
  flex-shrink: 0;
}

#custom-size-input {
  width: 48px;
  padding: 4px 4px;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 12px;
  text-align: center;
}

/* Drag-over visual for image drop */
#editor-wrapper.drag-over {
  border-color: #4a8;
  background: #f0fff4;
}

/* Mobile sidebar toggle button - hidden by default */
#sidebar-toggle {
  display: none;
}

/* Mobile responsive layout */
@media (max-width: 600px) {
  #toolbar {
    gap: 6px;
    padding: 6px 8px;
  }

  #toolbar button, #toolbar select {
    padding: 4px 8px;
    font-size: 12px;
  }

  #sidebar-toggle {
    display: block;
    position: fixed;
    bottom: 16px;
    left: 16px;
    z-index: 100;
    padding: 10px 16px;
    border: 1px solid #bbb;
    border-radius: 8px;
    background: #fff;
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
  }

  #sidebar-toggle:active {
    background: #e8e8e8;
  }

  #panel-nav {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 90;
    width: 160px;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 2px 0 12px rgba(0,0,0,0.15);
  }

  #panel-nav.open {
    transform: translateX(0);
  }

  #sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.3);
    z-index: 80;
  }

  #sidebar-overlay.open {
    display: block;
  }

  #editor-area {
    padding: 16px 8px;
  }

  #editor-wrapper {
    max-width: 100%;
  }
}
