* { box-sizing: border-box; }
body {
    margin: 0;
    font-family: 'Segoe UI', Inter, system-ui, sans-serif;
    background: #0f1220;
    color: #e5e7eb;
    height: 100vh;
    overflow: hidden;
}
button { font-family: inherit; }
.hidden { display: none !important; }

/* ===================== TOP BAR ===================== */
#topbar {
    height: 52px;
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 0 18px;
    background: #161a2e;
    border-bottom: 1px solid #262a45;
    flex-shrink: 0;
}
#topbar .brand { font-weight: 800; font-size: 1rem; letter-spacing: -0.02em; }
#topbar .spacer { flex: 1; }
#topbar button, .btn {
    background: #262a45;
    color: #e5e7eb;
    border: 1px solid #363b5c;
    border-radius: 8px;
    padding: 7px 14px;
    font-size: 0.85rem;
    cursor: pointer;
    font-weight: 600;
}
#topbar button:hover, .btn:hover { background: #323752; }
#topbar select {
    background: #262a45;
    color: #e5e7eb;
    border: 1px solid #363b5c;
    border-radius: 8px;
    padding: 7px 10px;
    font-size: 0.85rem;
    font-weight: 600;
}
#topbar select:disabled { opacity: 0.5; }
.btn-primary { background: linear-gradient(135deg, #4f46e5, #7c3aed); border-color: transparent; }
.btn-primary:hover { filter: brightness(1.1); }
.btn-danger { background: #7f1d1d; border-color: #991b1b; }

/* ===================== LIBRARY VIEW ===================== */
#library-view { height: calc(100vh - 52px); overflow-y: auto; padding: 30px 40px; }
#dropzone {
    border: 2px dashed #363b5c;
    border-radius: 16px;
    padding: 40px;
    text-align: center;
    color: #9ca3af;
    margin-bottom: 34px;
    transition: border-color 0.15s, background 0.15s;
}
#dropzone.drag-over { border-color: #7c3aed; background: rgba(124,58,237,0.08); color: #e5e7eb; }
#dropzone h2 { margin: 0 0 6px; color: #e5e7eb; font-size: 1.1rem; }
#dropzone p { margin: 0 0 14px; font-size: 0.85rem; }

.book-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 18px;
}
.book-card {
    background: #161a2e;
    border: 1px solid #262a45;
    border-radius: 12px;
    overflow: hidden;
    cursor: pointer;
    position: relative;
    transition: transform 0.12s, border-color 0.12s;
}
.book-card:hover { transform: translateY(-3px); border-color: #4f46e5; }
.book-card .cover { width: 100%; height: 130px; object-fit: cover; background: #0f1220; display: block; }
.book-card .info { padding: 10px 12px; }
.book-card .title { font-weight: 700; font-size: 0.88rem; margin-bottom: 3px; }
.book-card .meta { font-size: 0.75rem; color: #9ca3af; }
.card-delete-btn {
    position: absolute;
    top: 6px;
    right: 6px;
    z-index: 2;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: none;
    background: rgba(15, 18, 32, 0.75);
    color: #e5e7eb;
    font-size: 1rem;
    line-height: 1;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.12s, background 0.12s;
}
.book-card:hover .card-delete-btn { opacity: 1; }
.card-delete-btn:hover { background: #7f1d1d; }

/* ===================== EDITOR VIEW ===================== */
#editor-view { height: calc(100vh - 52px); display: flex; }

#page-rail {
    width: 110px;
    background: #12152a;
    border-right: 1px solid #262a45;
    overflow-y: auto;
    flex-shrink: 0;
    padding: 10px 8px;
}
.page-thumb {
    width: 100%;
    margin-bottom: 8px;
    border-radius: 6px;
    overflow: hidden;
    cursor: pointer;
    border: 2px solid transparent;
    position: relative;
}
.page-thumb img { width: 100%; display: block; background: #0f1220; }
.page-thumb.active { border-color: #7c3aed; }
.page-thumb .num { position: absolute; bottom: 2px; right: 4px; font-size: 0.65rem; background: rgba(0,0,0,0.6); padding: 1px 4px; border-radius: 4px; }

#canvas-wrap {
    flex: 1;
    overflow: auto;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 24px;
    background:
        linear-gradient(45deg, #171a30 25%, transparent 25%),
        linear-gradient(-45deg, #171a30 25%, transparent 25%),
        linear-gradient(45deg, transparent 75%, #171a30 75%),
        linear-gradient(-45deg, transparent 75%, #171a30 75%);
    background-size: 20px 20px;
    background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
    background-color: #0f1220;
}
#page-canvas {
    position: relative;
    background: #fff;
    box-shadow: 0 10px 40px rgba(0,0,0,0.5);
    line-height: 0;
}
#page-canvas img.page-img { max-width: 900px; width: 100%; display: block; user-select: none; -webkit-user-drag: none; }

.marker {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    border: 2px solid rgba(255,255,255,0.9);
    border-radius: 8px;
    cursor: grab;
    box-shadow: 0 2px 10px rgba(0,0,0,0.4);
}
.marker.narration-marker { border-radius: 50%; width: 34px; height: 34px; transform: translate(-50%, -50%); }
.marker.hotspot-marker { }
.marker .resize-handle {
    position: absolute;
    right: -5px; bottom: -5px;
    width: 12px; height: 12px;
    background: white;
    border: 2px solid #4f46e5;
    border-radius: 50%;
    cursor: nwse-resize;
}
.marker .del-btn {
    position: absolute;
    top: -9px; left: -9px;
    width: 16px; height: 16px;
    background: #ef4444;
    color: white;
    border-radius: 50%;
    font-size: 11px;
    line-height: 16px;
    text-align: center;
    cursor: pointer;
    display: none;
}
.marker:hover .del-btn { display: block; }

/* Rubber-band box while drawing a new hotspot */
#draw-rect {
    position: absolute;
    border: 2px dashed #7c3aed;
    background: rgba(124,58,237,0.15);
    pointer-events: none;
}

/* ===================== RIGHT SIDEBAR ===================== */
#sidebar {
    width: 300px;
    background: #12152a;
    border-left: 1px solid #262a45;
    flex-shrink: 0;
    overflow-y: auto;
    padding: 16px;
}
.sidebar-section { margin-bottom: 22px; }
.sidebar-section h3 {
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: #9ca3af;
    margin: 0 0 10px;
}
.palette { display: flex; flex-wrap: wrap; gap: 8px; }
.palette-chip {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 7px 10px;
    background: #1c2038;
    border: 1px solid #363b5c;
    border-radius: 8px;
    font-size: 0.78rem;
    cursor: grab;
    user-select: none;
}
.palette-chip .dot { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; }
.palette-chip:active { cursor: grabbing; opacity: 0.7; }

#page-text-panel {
    background: #0f1220;
    border: 1px solid #262a45;
    border-radius: 8px;
    padding: 10px;
    font-size: 0.78rem;
    line-height: 1.5;
    color: #b6bacc;
    max-height: 220px;
    overflow-y: auto;
    white-space: pre-wrap;
}

/* ===================== FORM PANEL (modal) ===================== */
#form-overlay {
    position: fixed;
    inset: 0;
    background: rgba(6,8,24,0.7);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 500;
}
#form-panel {
    width: 560px;
    max-width: 92vw;
    max-height: 86vh;
    overflow-y: auto;
    background: #161a2e;
    border: 1px solid #262a45;
    border-radius: 14px;
    padding: 20px;
}
#form-panel h2 { margin: 0 0 16px; font-size: 1.05rem; }
#form-panel label { display: block; font-size: 0.78rem; color: #9ca3af; margin: 12px 0 5px; font-weight: 600; }
#form-panel input[type=text], #form-panel input[type=number], #form-panel input[type=password], #form-panel textarea, #form-panel select {
    width: 100%;
    background: #0f1220;
    border: 1px solid #363b5c;
    border-radius: 7px;
    color: #e5e7eb;
    padding: 8px 10px;
    font-size: 0.85rem;
    font-family: inherit;
}
#form-panel textarea { min-height: 70px; resize: vertical; }
.form-row { display: flex; gap: 8px; align-items: center; }
.form-row + .form-row { margin-top: 6px; }
.form-list-item {
    background: #0f1220;
    border: 1px solid #262a45;
    border-radius: 8px;
    padding: 10px;
    margin-top: 8px;
    position: relative;
}
.form-list-item .remove-item {
    position: absolute; top: 6px; right: 6px;
    background: none; border: none; color: #ef4444; cursor: pointer; font-size: 0.95rem;
}
.form-actions { display: flex; gap: 8px; margin-top: 20px; justify-content: flex-end; }

/* ===================== AI GENERATION PROGRESS ===================== */
#ai-progress-overlay {
    position: fixed;
    inset: 0;
    background: rgba(6,8,24,0.7);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 600;
}
#ai-progress-panel {
    width: 480px;
    max-width: 92vw;
    background: #161a2e;
    border: 1px solid #262a45;
    border-radius: 14px;
    padding: 20px;
}
#ai-progress-panel h2 {
    margin: 0 0 14px;
    font-size: 1.05rem;
    display: flex;
    align-items: center;
    gap: 10px;
}
#ai-progress-status {
    font-size: 0.85rem;
    color: #e5e7eb;
    min-height: 1.2em;
    margin-bottom: 10px;
}
#ai-progress-log {
    background: #0f1220;
    border: 1px solid #262a45;
    border-radius: 8px;
    padding: 10px 12px;
    max-height: 160px;
    overflow-y: auto;
    font-size: 0.76rem;
    font-family: 'Consolas', 'SFMono-Regular', monospace;
    color: #9ca3af;
}
.ai-progress-line { padding: 2px 0; }
.ai-progress-line:last-child { color: #e5e7eb; }
.ai-spinner {
    width: 15px;
    height: 15px;
    border: 2px solid #363b5c;
    border-top-color: #7c3aed;
    border-radius: 50%;
    display: inline-block;
    animation: ai-spin 0.8s linear infinite;
}
@keyframes ai-spin { to { transform: rotate(360deg); } }
.small-btn {
    background: #262a45; border: 1px solid #363b5c; color: #e5e7eb;
    border-radius: 6px; padding: 5px 9px; font-size: 0.76rem; cursor: pointer;
}
.small-btn:hover { background: #323752; }
.password-row { position: relative; }
.password-row input { padding-right: 56px !important; }
.password-row .toggle-password {
    position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
    background: none; border: none; color: #9ca3af; font-size: 0.72rem;
    font-weight: 600; cursor: pointer; padding: 4px 6px;
}
.password-row .toggle-password:hover { color: #e5e7eb; }
.file-drop {
    border: 1.5px dashed #363b5c;
    border-radius: 8px;
    padding: 14px;
    text-align: center;
    font-size: 0.78rem;
    color: #9ca3af;
    cursor: pointer;
}
.file-drop.drag-over { border-color: #7c3aed; color: #e5e7eb; }
.checkbox-row { display: flex; align-items: center; gap: 6px; font-size: 0.82rem; color: #e5e7eb; }
.checkbox-row input { width: auto; }

/* toast */
#toast {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: #1c2038;
    border: 1px solid #363b5c;
    padding: 10px 18px;
    border-radius: 8px;
    font-size: 0.85rem;
    z-index: 900;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
}
#toast.show { opacity: 1; }
