* { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Segoe UI', system-ui, sans-serif; }

body { background: #0f0f12; color: #e8e8ec; }

.app { display: flex; height: 100vh; }

/* ── SIDEBAR ── */
.sidebar {
  width: 280px;
  background: #16161a;
  border-right: 1px solid #27272e;
  display: flex;
  flex-direction: column;
  padding: 20px;
  overflow-y: auto;
}

.logo { display: flex; align-items: center; gap: 10px; margin-bottom: 24px; }
.logo-icon { font-size: 24px; }
.logo-text { font-size: 18px; font-weight: 700; letter-spacing: -0.3px; }

.section { margin-bottom: 24px; }
.section-header {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 13px; font-weight: 600; color: #9b9ba8;
  text-transform: uppercase; letter-spacing: 0.5px;
  margin-bottom: 10px;
}

.btn-icon {
  background: #27272e; border: none; color: #e8e8ec;
  width: 24px; height: 24px; border-radius: 6px; cursor: pointer;
  font-size: 14px; line-height: 1;
}
.btn-icon:hover { background: #3a3a44; }

.char-list { display: flex; flex-direction: column; gap: 6px; }
.char-chip {
  display: flex; align-items: center; gap: 8px;
  background: #1e1e24; padding: 8px 10px; border-radius: 8px;
  font-size: 13px; cursor: pointer; transition: background 0.15s;
}
.char-chip:hover { background: #27272e; }
.char-chip .dot { width: 8px; height: 8px; border-radius: 50%; }
.dot-male { background: #5b9bd5; }
.dot-female { background: #e87aa0; }
.dot-any { background: #999; }
.char-chip .del-btn { margin-left: auto; opacity: 0.4; cursor: pointer; }
.char-chip .del-btn:hover { opacity: 1; }

.book-summary { font-size: 12px; color: #9b9ba8; line-height: 1.6; }

.setting-row {
  display: flex; align-items: center; gap: 8px;
  font-size: 12px; color: #c0c0c8; margin-bottom: 10px;
}
.setting-row input[type=range] { flex: 1; }
.setting-row span { width: 30px; text-align: right; color: #e8e8ec; }

/* ── MAIN ── */
.main { flex: 1; padding: 24px 32px; overflow-y: auto; }
.hidden { display: none !important; }

/* Story input */
.story-input-area { display: flex; flex-direction: column; height: 100%; }
.story-header { display: flex; gap: 12px; margin-bottom: 16px; align-items: center; }
.story-title-input {
  flex: 1; background: #1a1a20; border: 1px solid #27272e;
  color: #e8e8ec; padding: 10px 14px; border-radius: 8px; font-size: 15px;
}
.story-actions { display: flex; align-items: center; gap: 8px; }
.chapter-input {
  width: 50px; background: #1a1a20; border: 1px solid #27272e;
  color: #e8e8ec; padding: 8px; border-radius: 6px; text-align: center;
}

#story-text {
  flex: 1; background: #16161a; border: 1px solid #27272e;
  color: #e8e8ec; padding: 20px; border-radius: 12px; font-size: 15px;
  line-height: 1.7; resize: none; min-height: 400px;
}
#story-text::placeholder { color: #54545e; }

/* Buttons */
.btn-primary {
  background: #5b6cf0; color: white; border: none;
  padding: 10px 20px; border-radius: 8px; font-size: 14px; font-weight: 600;
  cursor: pointer; transition: background 0.15s;
}
.btn-primary:hover { background: #4a5bdf; }
.btn-secondary {
  background: #27272e; color: #e8e8ec; border: 1px solid #34343c;
  padding: 10px 18px; border-radius: 8px; font-size: 14px; cursor: pointer;
}
.btn-secondary:hover { background: #34343c; }

/* Panels grid */
.panels-area { display: flex; flex-direction: column; height: 100%; }
.panels-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
.panels-header h2 { font-size: 20px; display: flex; align-items: center; gap: 10px; }
.badge { background: #5b6cf0; color: white; font-size: 12px; padding: 3px 10px; border-radius: 12px; }
.panels-actions { display: flex; gap: 10px; align-items: center; }
.layout-select { background: #1a1a20; border: 1px solid #27272e; color: #e8e8ec; padding: 8px 12px; border-radius: 8px; }

.panel-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px; overflow-y: auto; padding-bottom: 20px;
}
.panel-card {
  background: #16161a; border: 1px solid #27272e; border-radius: 10px;
  padding: 12px; cursor: pointer; transition: border-color 0.15s;
}
.panel-card:hover { border-color: #5b6cf0; }
.panel-card-header { display: flex; justify-content: space-between; font-size: 11px; color: #9b9ba8; margin-bottom: 8px; }
.panel-card-mode {
  background: #27272e; padding: 2px 8px; border-radius: 4px;
  text-transform: uppercase; letter-spacing: 0.5px; font-weight: 600;
}
.panel-card-mode.fight { background: #4a1f1f; color: #ff7a7a; }
.panel-card-mode.duo { background: #1f3a4a; color: #7ac3ff; }
.panel-card-mode.solo { background: #2a2a4a; color: #a3a3ff; }
.panel-card-mode.environment { background: #1f4a2a; color: #7aff9d; }
.panel-card-mode.object { background: #4a3a1f; color: #ffc97a; }
.panel-card-scene { font-size: 12px; color: #c0c0c8; line-height: 1.5; min-height: 60px; }
.panel-card-dialogue { font-size: 11px; color: #9b9ba8; margin-top: 8px; font-style: italic; }
.panel-card-bubble-icon { color: #5b6cf0; }

/* Page viewer */
.page-area { display: flex; flex-direction: column; height: 100%; align-items: center; }
.page-header { display: flex; gap: 10px; margin-bottom: 20px; align-self: flex-start; }
.page-viewer { background: #1a1a20; border-radius: 12px; padding: 20px; overflow: auto; max-height: 85vh; }
.page-viewer img { max-width: 100%; max-height: 80vh; border-radius: 4px; }

/* Modal */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center; z-index: 100;
}
.modal-box {
  background: #1a1a20; border-radius: 14px; width: 420px;
  max-height: 85vh; overflow-y: auto; border: 1px solid #27272e;
}
.modal-box.wide { width: 760px; }
.modal-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 18px 22px; border-bottom: 1px solid #27272e;
}
.modal-header h3 { font-size: 16px; }
.modal-header button { background: none; border: none; color: #9b9ba8; cursor: pointer; font-size: 16px; }
.modal-body { padding: 20px 22px; display: flex; flex-direction: column; gap: 12px; }
.modal-body.two-col { flex-direction: row; gap: 20px; }
.panel-preview-col { flex: 1; }
.panel-preview-col img { width: 100%; border-radius: 8px; }
.panel-settings-col { flex: 1; display: flex; flex-direction: column; gap: 10px; }
.modal-footer {
  display: flex; justify-content: flex-end; gap: 10px;
  padding: 16px 22px; border-top: 1px solid #27272e;
}

.input-full {
  width: 100%; background: #16161a; border: 1px solid #27272e;
  color: #e8e8ec; padding: 10px 12px; border-radius: 8px; font-size: 13px;
}
.input-full::placeholder { color: #54545e; }
label { font-size: 12px; color: #9b9ba8; font-weight: 600; }

.tab-row { display: flex; gap: 8px; margin: 6px 0; }
.tab-btn {
  flex: 1; background: #16161a; border: 1px solid #27272e; color: #9b9ba8;
  padding: 8px; border-radius: 8px; cursor: pointer; font-size: 12px;
}
.tab-btn.active { background: #5b6cf0; color: white; border-color: #5b6cf0; }

.upload-area {
  border: 2px dashed #34343c; border-radius: 10px; padding: 30px;
  text-align: center; cursor: pointer; color: #9b9ba8;
}
.upload-area:hover { border-color: #5b6cf0; }
.upload-area img { max-width: 100%; max-height: 180px; border-radius: 8px; }
.upload-area small { display: block; margin-top: 6px; font-size: 11px; }

.hint { font-size: 12px; color: #76768a; line-height: 1.5; }

.new-chars-list { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }
.new-char-tag {
  background: #2a2a3a; padding: 6px 12px; border-radius: 16px; font-size: 13px;
}

.dialogue-preview { margin-top: 10px; font-size: 12px; color: #c0c0c8; }
.bubbles-info { font-size: 12px; color: #9b9ba8; background: #16161a; padding: 10px; border-radius: 8px; }

/* Loading */
.loading {
  position: fixed; inset: 0; background: rgba(15,15,18,0.92);
  display: flex; align-items: center; justify-content: center; z-index: 200;
}
.loading-box { text-align: center; }
.spinner {
  width: 44px; height: 44px; border: 4px solid #27272e;
  border-top-color: #5b6cf0; border-radius: 50%;
  animation: spin 0.8s linear infinite; margin: 0 auto 16px;
}
@keyframes spin { to { transform: rotate(360deg); } }
#loading-msg { font-size: 15px; margin-bottom: 4px; }
#loading-sub { color: #9b9ba8; font-size: 12px; }

/* ── BUBBLE EDITOR ── */
.bubble-editor-canvas {
  position: relative;
  display: inline-block;
  width: 100%;
  user-select: none;
}
.bubble-editor-canvas img { width: 100%; display: block; border-radius: 8px; }

#bubble-overlay-container {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
}

.bubble-box {
  position: absolute;
  border: 2px dashed #5b6cf0;
  background: rgba(91, 108, 240, 0.12);
  cursor: move;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px;
  box-sizing: border-box;
}
.bubble-box.selected {
  border-color: #ff7a7a;
  background: rgba(255, 122, 122, 0.15);
}
.bubble-box-text {
  font-size: 11px;
  color: #e8e8ec;
  text-align: center;
  pointer-events: none;
  overflow: hidden;
  line-height: 1.3;
  text-shadow: 0 1px 2px rgba(0,0,0,0.8);
}
.bubble-box .resize-handle {
  position: absolute;
  width: 12px; height: 12px;
  background: #5b6cf0;
  border: 2px solid white;
  border-radius: 50%;
  bottom: -6px; right: -6px;
  cursor: nwse-resize;
}
.bubble-box .del-handle {
  position: absolute;
  width: 18px; height: 18px;
  background: #ff4444;
  color: white;
  border-radius: 50%;
  top: -9px; right: -9px;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; cursor: pointer; line-height: 1;
}
.bubble-box .type-label {
  position: absolute;
  top: -9px; left: -2px;
  background: #16161a;
  border: 1px solid #34343c;
  font-size: 9px;
  padding: 1px 6px;
  border-radius: 8px;
  color: #9b9ba8;
  text-transform: uppercase;
}

.bubble-editor-toolbar {
  display: flex; gap: 8px; margin-top: 10px;
}
.btn-secondary.small { padding: 6px 12px; font-size: 12px; }

.bubble-edit-popup {
  position: absolute;
  background: #1a1a20;
  border: 1px solid #34343c;
  border-radius: 8px;
  padding: 10px;
  z-index: 50;
  width: 220px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.4);
}
.bubble-edit-popup textarea {
  width: 100%; background: #16161a; border: 1px solid #27272e;
  color: #e8e8ec; padding: 6px 8px; border-radius: 6px;
  font-size: 12px; margin-bottom: 6px; resize: none;
}
.bubble-edit-popup select {
  width: 100%; background: #16161a; border: 1px solid #27272e;
  color: #e8e8ec; padding: 5px; border-radius: 6px; font-size: 11px;
}

/* ══════════════════════════════════════════════════════════════════════════
   PAGE EDITOR
   ══════════════════════════════════════════════════════════════════════════ */
.pe-wrap { display: flex; gap: 20px; height: 100%; }

.pe-stage {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; min-width: 0; overflow: auto;
}

.pe-canvas {
  position: relative;
  background: #fff;
  aspect-ratio: 1748 / 2480;
  width: 100%;
  max-width: 620px;
  box-shadow: 0 6px 30px rgba(0,0,0,0.45);
  user-select: none;
}

/* Panels */
.pe-panel {
  position: absolute;
  overflow: hidden;
  border: 3px solid #000;
  background: #e9e9ef;
  box-sizing: border-box;
  cursor: grab;
}
.pe-panel.selected { outline: 3px solid #5b6cf0; outline-offset: 2px; z-index: 5; }
.pe-panel:active { cursor: grabbing; }
.pe-panel-img {
  width: 100%; height: 100%;
  object-fit: cover;
  display: block;
  transform-origin: center center;
  pointer-events: auto;
}
.pe-empty {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  height: 100%; color: #76768a; font-size: 12px; text-align: center;
}

/* Divider handles — drag to resize both sides together */
.pe-divider { position: absolute; z-index: 10; }
.pe-divider-row { cursor: ns-resize; }
.pe-divider-col { cursor: ew-resize; }
.pe-divider:hover { background: rgba(91,108,240,0.35); }

/* Bubbles — page space, may cross panel edges */
.pe-bubble {
  position: absolute; z-index: 20;
  border: 2px dashed #5b6cf0;
  background: rgba(255,255,255,0.9);
  border-radius: 14px;
  display: flex; align-items: center; justify-content: center;
  padding: 6px; box-sizing: border-box; cursor: move;
}
.pe-bubble.selected { border-color: #ff7a7a; background: rgba(255,240,240,0.95); }
.pe-bubble-text { font-size: 10px; color: #111; text-align: center; overflow: hidden; pointer-events: none; }
.pe-bubble-type {
  position: absolute; top: -9px; left: 4px;
  background: #16161a; color: #9b9ba8;
  font-size: 8px; padding: 1px 6px; border-radius: 8px; text-transform: uppercase;
}
.pe-bubble-del {
  position: absolute; top: -9px; right: -9px;
  width: 18px; height: 18px; background: #ff4444; color: #fff;
  border-radius: 50%; display: flex; align-items: center; justify-content: center;
  font-size: 11px; cursor: pointer;
}
.pe-bubble-resize {
  position: absolute; bottom: -6px; right: -6px;
  width: 12px; height: 12px; background: #5b6cf0;
  border: 2px solid #fff; border-radius: 50%; cursor: nwse-resize;
}

/* Sidebar + tabs */
.pe-side {
  width: 300px; flex-shrink: 0;
  display: flex; flex-direction: column; gap: 12px; overflow-y: auto;
}
.pe-page-tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.pe-tab {
  background: #1e1e24; border: 1px solid #27272e; color: #c0c0c8;
  padding: 6px 12px; border-radius: 8px; cursor: pointer; font-size: 12px;
}
.pe-tab.active { background: #5b6cf0; color: #fff; border-color: #5b6cf0; }
.pe-tab small { opacity: 0.7; margin-left: 4px; }
.pe-btn-row { display: flex; gap: 6px; flex-wrap: wrap; }
.pe-toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }

/* ── Page editor sizing fix ────────────────────────────────────────────────
   #page-area inherits `align-items: center` from the old static page viewer,
   which sizes .pe-wrap to its content. The canvas is width:100% of a parent
   with no resolved width, so it collapses to zero and the whole editor renders
   invisibly. These rules give the flex chain a real width to work from. */
#page-area {
  align-items: stretch;
  width: 100%;
  overflow: hidden;
}
.pe-wrap {
  width: 100%;
  flex: 1 1 auto;
  min-height: 0;
}
.pe-stage {
  flex: 1 1 0%;
  min-width: 0;
  justify-content: flex-start;
  padding-bottom: 20px;
}
.pe-canvas {
  width: 100%;
  max-width: 560px;
  flex: 0 0 auto;
}
.pe-toolbar, .pe-page-tabs { flex: 0 0 auto; }

@media (max-width: 1100px) {
  .pe-wrap { flex-direction: column; }
  .pe-side { width: 100%; }
}

/* Fallback for browsers/layouts where aspect-ratio doesn't resolve a height */
.pe-canvas::before {
  content: "";
  display: block;
  padding-top: 141.87%;   /* 2480 / 1748 */
}
.pe-canvas > * { position: absolute; }

/* ── Make dividers discoverable ────────────────────────────────────────────
   Dividers were only visible on hover, so there was no way to know where to
   grab to resize. Give them a persistent subtle affordance plus grip dots. */
.pe-divider {
  background: rgba(91, 108, 240, 0.10);
  transition: background 0.12s;
  display: flex;
  align-items: center;
  justify-content: center;
}
.pe-divider::after {
  content: "";
  background: rgba(91, 108, 240, 0.55);
  border-radius: 3px;
}
.pe-divider-row::after { width: 46px; height: 4px; }
.pe-divider-col::after { width: 4px;  height: 46px; }

.pe-divider:hover {
  background: rgba(91, 108, 240, 0.35);
}
.pe-divider:hover::after {
  background: #5b6cf0;
  box-shadow: 0 0 0 2px rgba(91,108,240,0.25);
}

/* Panel reorder + reframe controls (on the selected panel) */
.pe-panel-bar {
  position: absolute; top: 4px; left: 50%; transform: translateX(-50%);
  display: flex; gap: 4px; z-index: 8;
  background: rgba(15,15,18,0.85); padding: 3px; border-radius: 8px;
}
.pe-panel-bar button {
  background: #27272e; color: #e8e8ec; border: 1px solid #3a3a44;
  border-radius: 5px; font-size: 10px; padding: 3px 7px; cursor: pointer;
  line-height: 1;
}
.pe-panel-bar button:hover { background: #5b6cf0; border-color: #5b6cf0; }

.pe-panel-bar button.on { background: #5b6cf0; border-color: #5b6cf0; color: #fff; }
/* In Whole mode the image is letterboxed; give the panel a light field so the
   letterbox reads as intentional rather than broken. */
.pe-panel-img { background: #f2f2f5; }

/* Window mode: frame masks the image; the image underneath may be larger. */
.pe-panel { background: #f2f2f5; }
.pe-panel-img { cursor: grab; }
.pe-panel-img:active { cursor: grabbing; }

.pe-layout-select {
  background: #1e1e24; border: 1px solid #27272e; color: #e8e8ec;
  border-radius: 8px; padding: 8px 12px; font-size: 13px; cursor: pointer;
}

/* Daily quota badge */
.quota-badge {
  font-size: 11px; padding: 3px 10px; border-radius: 10px;
  background: #1e1e24; color: #9b9ba8; border: 1px solid #27272e;
  margin-left: 10px; white-space: nowrap;
}
.quota-badge.owner { background: #1c3a1c; color: #7ee87e; border-color: #2c5c2c; }
.quota-badge.empty { background: #3a1c1c; color: #ff9b9b; border-color: #5c2c2c; }
