/* Main Styles - NPPL-A MHG Interactive Textbook */

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

:root {
    --primary: #3CB4E5;
    --primary-dark: #2898c8;
    --primary-light: #5fc4ed;
    --primary-lighter: #E8F4F8;
    --primary-color: #3CB4E5;
    --secondary-color: #1a365d;
    --accent-color: #f5a623;
    --text-color: #1f2937;
    --text-light: #6b7280;
    --bg-color: #ffffff;
    --bg-light: #f9fafb;
    --bg-dark: #111827;
    --border-color: #e5e7eb;
    --radius-md: 0.5rem;
    --radius-lg: 0.75rem;
    --radius-xl: 1rem;
    --radius-full: 9999px;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    --sidebar-width: 300px;
    --library-width: 320px;
    --header-height: 76px;
    --footer-height: 60px;
}

html, body {
    height: 100%;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    color: var(--text-color);
    background-color: var(--bg-light);
    line-height: 1.6;
    overflow-x: hidden;
}

/* ===== APP CONTAINER ===== */
.app-container {
    display: grid;
    grid-template-columns: var(--sidebar-width) 1fr var(--library-width);
    grid-template-rows: var(--header-height) 1fr var(--footer-height);
    grid-template-areas: 
        "header header header"
        "sidebar main library"
        "footer footer footer";
    min-height: 100vh;
}

/* ===== HEADER ===== */
.main-header {
    grid-area: header;
    background: var(--primary-color);
    color: white;
    padding: 0 1.5rem;
    box-shadow: var(--shadow-md);
    z-index: 1000;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--header-height);
    display: flex;
    align-items: center;
    gap: 1rem;
}

/* When in iframe, hide header completely - main platform has its own header */
body.in-iframe .main-header {
    display: none !important; /* Completely hide header when in iframe */
    height: 0 !important;
}

body.in-iframe .header-content {
    display: none !important; /* Hide title when in iframe - main platform shows it */
}

body.in-iframe .header-back-btn {
    display: none !important; /* Hide back button when in iframe - main platform has it */
}

body.in-iframe .header-search {
    display: none !important; /* Hide search when in iframe to save space */
}

/* Adjust grid when header is hidden in iframe */
body.in-iframe .app-container {
    grid-template-rows: 0 1fr 70px var(--footer-height);
    grid-template-areas: 
        "header header header"
        "sidebar main library"
        "sidebar submenu submenu"
        "footer footer footer";
}

/* Ensure footer is always visible in iframe */
body.in-iframe .main-footer {
    display: flex !important;
    visibility: visible !important;
    opacity: 1 !important;
}

/* Adjust horizontal submenu position when header is hidden - it's at bottom */
body.in-iframe .horizontal-submenu {
    bottom: 0 !important; /* At bottom when header is hidden */
    top: auto !important;
}

body.in-iframe .main-content {
    margin-top: 0 !important;
    padding-top: 70px !important; /* Само horizontal menu */
}

/* Adjust sidebars when header is hidden */
body.in-iframe .sidebar-nav {
    top: 0 !important;
    height: 100vh !important;
}

body.in-iframe .library-sidebar {
    top: 0 !important;
    height: 100vh !important;
}

@media (max-width: 768px) {
    body.in-iframe .main-content {
        margin-top: 120px !important; /* Header (50px) + horizontal menu (70px) */
    }
}

.header-content {
    flex: 1;
    min-width: 0;
    overflow: hidden;
}

.header-content h1 {
    font-size: 1.2rem;
    font-weight: 600;
    margin-bottom: 0.15rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.header-content .subtitle {
    font-size: 0.75rem;
    opacity: 0.9;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.header-content .subtitle #headerVersion {
    font-weight: 600;
    color: var(--accent-color);
    margin-left: 0.5rem;
}

/* Back to Training Button */
.header-back-btn {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background: rgba(255, 255, 255, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 8px;
    padding: 0.5rem 1rem;
    color: white;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
    flex-shrink: 0;
}

.header-back-btn:hover {
    background: rgba(255, 255, 255, 0.25);
    border-color: rgba(255, 255, 255, 0.3);
}

.header-back-btn .back-icon {
    font-size: 1.1rem;
}

.header-back-btn .back-text {
    display: inline;
}

@media (max-width: 768px) {
    .header-back-btn .back-text {
        display: none;
    }
    .header-back-btn {
        padding: 0.5rem;
        min-width: 44px;
    }
    .header-content h1 {
        font-size: 1rem;
    }
    .header-content .subtitle {
        font-size: 0.7rem;
    }
}

/* Header Search */
.header-search {
    display: flex;
    align-items: center;
    background: rgba(255, 255, 255, 0.15);
    border-radius: 25px;
    padding: 0.3rem 0.5rem;
    max-width: 200px;
    flex-shrink: 1;
    min-width: 0;
}

@media (max-width: 768px) {
    .header-search {
        max-width: 150px;
    }
    .header-search input {
        width: 120px;
    }
}

.header-search input {
    background: transparent;
    border: none;
    color: white;
    padding: 0.4rem 0.6rem;
    font-size: 0.9rem;
    width: 180px;
    outline: none;
}

.header-search input::placeholder {
    color: rgba(255, 255, 255, 0.7);
}

.header-search button {
    background: transparent;
    border: none;
    color: white;
    cursor: pointer;
    padding: 0.3rem;
    font-size: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

.header-search button:hover {
    opacity: 0.8;
}

/* Mobile Menu Buttons - Show hamburger on desktop, full menu on mobile */
.mobile-menu-btn,
.mobile-library-btn {
    display: none; /* Скрито по подразбиране, показва се на мобилни */
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.1);
    border: none;
    color: white;
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 8px;
    min-width: 44px;
    min-height: 44px;
    transition: background-color 0.2s ease;
}

.mobile-menu-btn:hover,
.mobile-library-btn:hover {
    background: rgba(255, 255, 255, 0.2);
}

/* ===== NAVIGATION SIDEBAR ===== */
.sidebar-nav {
    grid-area: sidebar;
    background-color: var(--bg-color);
    border-right: 1px solid var(--border-color);
    overflow-y: auto !important; /* Винаги скролване */
    overflow-x: hidden;
    position: fixed; /* Fixed за да не се губят при скролване */
    left: 0;
    top: var(--header-height); /* Само header, менюто е долу */
    width: var(--sidebar-width);
    height: calc(100vh - var(--header-height) - var(--footer-height)); /* Header + footer */
    z-index: 500;
    transition: transform 0.3s ease, left 0.3s ease;
    /* Уверете се, че скролването работи правилно */
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    scrollbar-color: var(--primary-color) transparent;
}

/* Collapsed state - скрит наляво */
.sidebar-nav.collapsed {
    left: calc(-1 * var(--sidebar-width)) !important;
    transform: translateX(0);
    visibility: hidden; /* Скриваме напълно когато е collapsed */
}

/* Когато header е скрит в iframe */
body.in-iframe .sidebar-nav {
    top: 0; /* Header е скрит */
    height: calc(100vh - var(--footer-height)); /* Header е скрит, само footer */
}

/* ===== MAIN CONTENT ===== */
.main-content {
    grid-area: main;
    background-color: var(--bg-light);
    overflow-y: auto;
    margin-left: 0; /* Grid управлява позицията */
    margin-right: 0; /* Grid управлява позицията */
    margin-top: var(--header-height); /* Само header */
    margin-bottom: 0; /* Без хоризонтално меню на десктоп */
    padding-top: 1rem;
    padding-left: 1rem;
    padding-right: 1rem;
    width: 100%; /* Заема цялата налична ширина в grid area */
    min-height: calc(100vh - var(--header-height) - var(--footer-height));
}

.content-wrapper {
    padding: 2rem;
    max-width: 1000px;
    margin: 0 auto;
}

/* ===== LIBRARY SIDEBAR ===== */
.library-sidebar {
    grid-area: library;
    background-color: var(--bg-color);
    border-left: 1px solid var(--border-color);
    overflow-y: auto !important; /* Винаги скролване за да се виждат всички учебници */
    overflow-x: hidden;
    position: fixed; /* Fixed за да не се губят при скролване */
    right: 0;
    top: var(--header-height); /* Само header, менюто е долу */
    width: var(--library-width);
    height: calc(100vh - var(--header-height) - var(--footer-height)); /* Header + footer */
    z-index: 500;
    transition: transform 0.3s ease, right 0.3s ease;
    /* Уверете се, че скролването работи правилно */
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    scrollbar-color: var(--primary-color) transparent;
}

/* Collapsed state - скрит надясно */
.library-sidebar.collapsed {
    right: calc(-1 * var(--library-width)) !important;
    transform: translateX(0);
    visibility: hidden; /* Скриваме напълно когато е collapsed */
}

/* Когато header е скрит в iframe */
body.in-iframe .library-sidebar {
    top: 0; /* Header е скрит */
    height: calc(100vh - var(--footer-height)); /* Header е скрит, само footer */
}

.library-header {
    padding: 1rem;
    background-color: var(--primary-color);
    color: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: sticky;
    top: 0;
    z-index: 10;
}

.nav-header-buttons,
.library-header-buttons {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.nav-collapse-btn,
.library-collapse-btn {
    display: none; /* По подразбиране скрито, ще се покаже на десктоп */
    background: transparent;
    border: none;
    color: white;
    font-size: 1rem;
    cursor: pointer;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    transition: background 0.2s;
}

.nav-collapse-btn:hover,
.library-collapse-btn:hover {
    background: rgba(255, 255, 255, 0.1);
}

.library-header h3 {
    font-size: 1.1rem;
    font-weight: 600;
}

.library-close-btn {
    display: none;
    background: transparent;
    border: none;
    color: white;
    font-size: 1.2rem;
    cursor: pointer;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
}

.library-close-btn:hover {
    background: rgba(255, 255, 255, 0.1);
}

/* ===== MOBILE OVERLAY ===== */
.mobile-overlay {
    display: none;
    position: fixed;
    top: var(--header-height);
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 400;
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
}

.mobile-overlay.active {
    display: block;
    opacity: 1;
    pointer-events: auto;
}

/* ===== FOOTER ===== */
.main-footer {
    grid-area: footer;
    background-color: var(--primary-color); /* Използваме основния цвят вместо --bg-dark */
    color: white;
    padding: 1rem 2rem;
    text-align: center;
    font-size: 0.85rem;
    position: relative;
    z-index: 100;
    min-height: var(--footer-height);
    display: flex !important; /* Винаги показваме футъра */
    align-items: center;
    justify-content: center;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
    visibility: visible !important;
    opacity: 1 !important;
}

.footer-content {
    max-width: 1200px;
    margin: 0 auto;
    width: 100%;
}

.footer-content p {
    margin: 0.3rem 0;
    line-height: 1.5;
    color: white;
}

.footer-content p:first-child {
    font-size: 0.95rem;
    margin-bottom: 0.5rem;
}

.footer-version {
    font-size: 0.9rem;
    color: rgba(255, 255, 255, 0.95);
    margin: 0.3rem 0;
}

.footer-version strong {
    font-weight: 600;
}

.footer-contact {
    font-size: 0.85rem;
    color: rgba(255, 255, 255, 0.9);
    margin: 0.3rem 0;
}

.footer-contact a {
    color: var(--accent-color);
    text-decoration: none;
    transition: opacity 0.2s;
    font-weight: 500;
}

.footer-contact a:hover {
    opacity: 1;
    text-decoration: underline;
    color: var(--accent-color);
}

.footer-description {
    font-size: 0.85rem;
    color: rgba(255, 255, 255, 0.8);
    margin-top: 0.5rem;
    font-style: italic;
}

#appVersion {
    font-weight: 600;
    color: var(--accent-color);
}

/* ===== SEARCH MODAL ===== */
.search-modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.6);
    z-index: 2000;
    align-items: flex-start;
    justify-content: center;
    padding-top: calc(var(--header-height) + 20px);
}

.search-modal.active {
    display: flex;
}

.search-modal-content {
    background: var(--bg-color);
    border-radius: 12px;
    width: 90%;
    max-width: 600px;
    max-height: 70vh;
    overflow: hidden;
    box-shadow: var(--shadow-lg);
}

.search-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.25rem;
    background-color: var(--primary-color);
    color: white;
}

.search-modal-header h3 {
    font-size: 1.1rem;
}

.search-modal-close {
    background: transparent;
    border: none;
    color: white;
    font-size: 1.3rem;
    cursor: pointer;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
}

.search-modal-close:hover {
    background: rgba(255, 255, 255, 0.1);
}

.search-results {
    padding: 1rem;
    overflow-y: auto;
    max-height: calc(70vh - 60px);
}

.search-result-item {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    transition: background-color 0.2s;
}

.search-result-item:hover {
    background-color: var(--bg-light);
}

.search-result-item:last-child {
    border-bottom: none;
}

.search-result-title {
    font-weight: 600;
    color: var(--primary-color);
    margin-bottom: 0.25rem;
}

.search-result-context {
    font-size: 0.85rem;
    color: var(--text-light);
}

.search-no-results {
    text-align: center;
    color: var(--text-light);
    padding: 2rem;
}

/* ===== WELCOME SCREEN ===== */
.welcome-screen {
    padding: 2rem;
    max-width: 900px;
    margin: 0 auto;
}

.welcome-screen h2 {
    color: var(--primary-color);
    margin-bottom: 1rem;
    font-size: 1.8rem;
}

.welcome-screen p {
    margin-bottom: 1rem;
    font-size: 1.05rem;
    color: var(--text-light);
}

.features {
    margin-top: 2rem;
    padding: 1.5rem;
    background-color: var(--bg-color);
    border-radius: 8px;
    box-shadow: var(--shadow);
    border-left: 4px solid var(--accent-color);
}

.features h3 {
    color: var(--primary-color);
    margin-bottom: 1rem;
}

.features ul {
    list-style-position: inside;
    color: var(--text-color);
}

.features li {
    margin-bottom: 0.5rem;
    padding-left: 0.5rem;
}

/* ===== LOADING AND ERROR STATES ===== */
.loading, .error {
    padding: 2rem;
    text-align: center;
    color: var(--text-light);
}

.error {
    color: #dc2626;
    background-color: #fef2f2;
    border: 1px solid #fecaca;
    border-radius: 0.5rem;
    margin: 2rem;
}

/* ===== RESPONSIVE DESIGN ===== */

/* Tablet (up to 1200px) */
@media (max-width: 1200px) {
    :root {
        --library-width: 280px;
    }
    
    .header-search {
        max-width: 200px;
    }
    
    .header-search input {
        width: 140px;
    }
}

/* Tablet Portrait / Large Mobile (up to 992px) */
@media (max-width: 992px) {
    :root {
        --sidebar-width: 280px;
        --library-width: 280px;
    }
    
    .app-container {
        grid-template-columns: 1fr;
        grid-template-areas: 
            "header"
            "main"
            "footer";
    }
    
    /* Show mobile buttons */
    .mobile-menu-btn,
    .mobile-library-btn {
        display: flex;
    }
    
    /* Show close buttons in sidebars */
    .nav-close-btn,
    .library-close-btn {
        display: block;
    }
    
    /* Sidebars become off-canvas */
    .sidebar-nav {
        transform: translateX(-100%);
        box-shadow: var(--shadow-lg);
    }
    
    .sidebar-nav.open {
        transform: translateX(0);
    }
    
    .library-sidebar {
        transform: translateX(100%);
        box-shadow: var(--shadow-lg);
    }
    
    .library-sidebar.open {
        transform: translateX(0);
    }
    
    /* Main content takes full width */
    .main-content {
        width: 100%;
        margin-top: var(--header-height);
    }
    
    .content-wrapper {
        padding: 1.5rem;
    }
}

/* Mobile (up to 768px) */
@media (max-width: 768px) {
    :root {
        --header-height: 60px;
        --sidebar-width: 85%;
        --library-width: 85%;
    }
    
    .main-header {
        padding: 0 0.75rem;
        gap: 0.5rem;
    }
    
    .header-content h1 {
        font-size: 1rem;
        margin-bottom: 0.1rem;
    }
    
    .header-content .subtitle {
        font-size: 0.7rem;
    }
    
    .header-search {
        max-width: none;
        flex: 0 0 auto;
        padding: 0.2rem 0.4rem;
    }
    
    .header-search input {
        width: 100px;
        font-size: 0.85rem;
        padding: 0.3rem 0.4rem;
    }
    
    .mobile-menu-btn,
    .mobile-library-btn {
        font-size: 1.3rem;
        min-width: 40px;
        min-height: 40px;
        padding: 0.4rem;
    }
    
    .sidebar-nav,
    .library-sidebar {
        width: 85%;
        max-width: 320px;
    }
    
    .content-wrapper {
        padding: 1rem;
    }
    
    .main-footer {
        padding: 0.75rem 1rem;
        font-size: 0.75rem;
    }
    
    .welcome-screen {
        padding: 1rem;
    }
    
    .welcome-screen h2 {
        font-size: 1.4rem;
    }
    
    .welcome-screen p {
        font-size: 0.95rem;
    }
    
    .features {
        padding: 1rem;
    }
}

/* Small Mobile (up to 480px) */
@media (max-width: 480px) {
    :root {
        --header-height: 55px;
        --footer-height: 70px;
    }
    
    .main-header {
        padding: 0 0.5rem;
    }
    
    .header-content h1 {
        font-size: 0.9rem;
    }
    
    .header-content .subtitle {
        display: none;
    }
    
    .header-search input {
        width: 80px;
        font-size: 0.8rem;
    }
    
    .mobile-menu-btn,
    .mobile-library-btn {
        font-size: 1.2rem;
        min-width: 36px;
        min-height: 36px;
    }
    
    .content-wrapper {
        padding: 0.75rem;
    }
    
    .welcome-screen h2 {
        font-size: 1.25rem;
    }
    
    .welcome-screen p {
        font-size: 0.9rem;
    }
    
    .footer-content {
        line-height: 1.5;
    }
    
    .footer-contact {
        display: block;
        margin-top: 0.25rem;
    }
}
