:root {
    --primary: #106a72;
    --primary-light: #e6f3f4;
    --primary-dark: #0a4d53;
    --bg-color: #ffffff;
    --surface: #f8fafc;
    --text-main: #1e293b;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --radius-lg: 16px;
    --radius-md: 12px;
    --radius-sm: 8px;
    --transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: 'Montserrat', sans-serif;
    background-color: var(--surface);
    color: var(--text-main);
    line-height: 1.6;
    overflow-x: hidden;
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* App Bar & Search */
header {
    background-color: var(--primary);
    color: white;
    padding: 1rem;
    display: flex;
    align-items: center;
    gap: 1rem;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
}

.back-btn {
    background: none;
    border: none;
    color: white;
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0.25rem;
    border-radius: var(--radius-sm);
}

header h1 {
    font-size: 1.1rem;
    font-weight: 600;
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.search-icon-btn {
    background: none;
    border: none;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.5rem;
    border-radius: 50%;
    transition: background 0.2s;
}

.search-icon-btn:hover {
    background: rgba(255, 255, 255, 0.2);
}

.search-dropdown {
    position: absolute;
    top: calc(100% + 5px);
    right: 0;
    background: white;
    border-radius: var(--radius-sm);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    width: 300px;
    max-height: 300px;
    overflow-y: auto;
    display: none;
    z-index: 200;
}

.search-dropdown.active {
    display: block;
}

.search-item {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
    color: var(--text-main);
    font-size: 0.9rem;
    display: flex;
    flex-direction: column;
    text-decoration: none;
}

.search-item:hover {
    background: var(--surface);
}

.symptom-chip {
    background: var(--primary-light);
    color: var(--primary-dark);
    padding: 0.25rem 0.75rem;
    border-radius: 9999px;
    font-size: 0.85rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.symptom-chip button {
    background: none;
    border: none;
    color: var(--primary);
    cursor: pointer;
    font-size: 1.1rem;
    line-height: 1;
    display: flex;
    align-items: center;
}

.symptom-chip button:hover {
    color: red;
}

.search-item .type {
    color: var(--primary);
    font-size: 0.7rem;
    text-transform: uppercase;
    font-weight: 700;
    margin-bottom: 0.25rem;
}

.search-item .title {
    font-weight: 600;
}

/* Container */
.container {
    flex: 1;
    background: var(--bg-color);
    border-top-left-radius: var(--radius-lg);
    border-top-right-radius: var(--radius-lg);
    margin-top: -10px;
    z-index: 10;
    position: relative;
    box-shadow: 0 -4px 10px rgba(0, 0, 0, 0.05);
    display: flex;
    flex-direction: column;
}

/* Loader */
.loader {
    text-align: center;
    padding: 2rem;
    color: var(--text-muted);
    font-weight: 600;
    display: none;
}

/* Diagnosis Header */
.diagnosis-header {
    padding: 1.5rem 1.25rem 0.5rem;
}

.diagnosis-title {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary-dark);
    margin-bottom: 0.5rem;
}

.tags {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.tag {
    background: var(--primary-light);
    color: var(--primary);
    padding: 0.25rem 0.75rem;
    border-radius: 9999px;
    font-size: 0.75rem;
    font-weight: 600;
}

/* Horizontal Tabs */
.tabs-container {
    overflow-x: auto;
    scrollbar-width: none;
    border-bottom: 1px solid var(--border);
    background: var(--bg-color);
    position: sticky;
    top: 0;
    z-index: 50;
}

.tabs-container::-webkit-scrollbar {
    display: none;
}

.tabs {
    display: flex;
    padding: 0 1rem;
    gap: 1rem;
    min-width: max-content;
}

.tab {
    padding: 1rem 0.5rem;
    color: var(--text-muted);
    font-weight: 600;
    font-size: 0.9rem;
    cursor: pointer;
    position: relative;
    border-bottom: 3px solid transparent;
    transition: var(--transition);
}

.tab.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

/* Content Area */
.tab-panels-wrapper {
    flex: 1;
    overflow-y: auto;
    padding: 1.25rem;
    background: var(--bg-color);
}

.tab-pane {
    display: none;
    animation: fadeIn 0.3s ease-out;
}

.tab-pane.active {
    display: block;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Cards & Links */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 1rem;
    margin-bottom: 1rem;
    transition: var(--transition);
    text-decoration: none;
    display: block;
    color: var(--text-main);
}

a.card:hover,
a.card:active {
    transform: scale(0.98);
    border-color: var(--primary);
}

.card-title {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--primary);
    margin-bottom: 0.25rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.card-icon {
    width: 1.25rem;
    height: 1.25rem;
    fill: currentColor;
    flex-shrink: 0;
}

.card-body {
    font-size: 0.95rem;
}

.empty-state {
    padding: 2rem;
    text-align: center;
    color: var(--text-muted);
    font-style: italic;
    font-size: 0.95rem;
}

/* Text Content Rendering */
.text-html-content p {
    margin-bottom: 1rem;
}

.text-html-content ul {
    padding-left: 1.5rem;
    margin-bottom: 1rem;
}

.text-html-content a {
    color: var(--primary);
    font-weight: 600;
    text-decoration: none;
}

.text-html-content img {
    max-width: 100%;
    height: auto;
    cursor: zoom-in;
    transition: transform 0.2s ease;
}

.text-html-content img.zoomed {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    max-width: 100vw !important;
    max-height: 100vh !important;
    width: auto;
    height: auto;
    z-index: 100000;
    box-shadow: 0 0 0 100vmax rgba(0,0,0,0.8);
    cursor: zoom-out;
    margin: 0;
    padding: 0;
}

/* Befundomat */
.befundomat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 0.75rem;
    margin-bottom: 1.5rem;
}

.btn-toggle {
    background: var(--bg-color);
    border: 2px solid var(--border);
    color: var(--text-muted);
    padding: 0.75rem;
    border-radius: var(--radius-sm);
    font-family: inherit;
    font-weight: 600;
    font-size: 0.85rem;
    cursor: pointer;
    transition: var(--transition);
    text-align: center;
}

.btn-toggle.active {
    background: var(--primary-light);
    border-color: var(--primary);
    color: var(--primary);
}

.result-box {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 1rem;
    margin-bottom: 3rem;
}

.result-textarea {
    width: 100%;
    border: none;
    background: transparent;
    font-family: inherit;
    font-size: 0.95rem;
    color: var(--text-main);
    resize: vertical;
    min-height: 100px;
    margin-bottom: 1rem;
}

.result-textarea:focus {
    outline: none;
}

.btn-copy {
    background: var(--primary);
    color: white;
    border: none;
    padding: 0.75rem 1.5rem;
    border-radius: var(--radius-sm);
    font-family: inherit;
    font-weight: 600;
    cursor: pointer;
    width: 100%;
    transition: var(--transition);
}

.btn-copy:active {
    background: var(--primary-dark);
}

/* FAB */
.fab {
    position: fixed;
    bottom: 2rem;
    right: 1.5rem;
    width: 3.5rem;
    height: 3.5rem;
    background: var(--primary);
    color: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    box-shadow: 0 4px 12px rgba(16, 106, 114, 0.4);
    cursor: pointer;
    transition: var(--transition);
    z-index: 100;
}

.fab:hover {
    transform: scale(1.05);
    background: var(--primary-dark);
}

/* Sorting Buttons */
.btn-sort {
    background: var(--surface-bg);
    border: 1px solid var(--border-color);
    color: var(--primary);
    border-radius: var(--radius-sm);
    padding: 0.25rem 0.5rem;
    font-size: 0.7rem;
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
}

.btn-sort:hover {
    background: var(--primary);
    color: white;
}

.wysiwyg-editor {
    outline: none;
    overflow-y: auto;
    /* Enforce rigid fonts and lines inside editor to prevent paste formatting overrides */
    font-family: 'Montserrat', sans-serif !important;
    font-size: 0.95rem !important;
    line-height: 1.6 !important;
    color: var(--text-main) !important;
    letter-spacing: normal !important;
}

.wysiwyg-editor * {
    font-family: inherit !important;
    font-size: inherit !important;
    line-height: inherit !important;
    color: inherit !important;
    letter-spacing: inherit !important;
    background-color: transparent !important;
}

.wysiwyg-editor:empty:before {
    content: attr(data-placeholder);
    color: var(--text-muted);
    font-style: italic;
    pointer-events: none;
}

.fab-edit {
    position: fixed;
    bottom: 2rem;
    left: 1.5rem;
    width: 3.5rem;
    height: 3.5rem;
    background: var(--text-muted);
    color: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    cursor: pointer;
    transition: var(--transition);
    z-index: 100;
}

.fab-edit:hover {
    transform: scale(1.05);
    background: var(--text-main);
}

/* Desktop */
@media (min-width: 768px) {
    body {
        align-items: center;
        background-color: var(--surface);
    }

    header,
    .container {
        width: 100%;
        max-width: 800px;
        border-radius: var(--radius-lg);
        margin-top: 2rem;
    }

    header {
        border-bottom-left-radius: 0;
        border-bottom-right-radius: 0;
        position: relative;
    }

    .search-container input {
        width: 300px;
    }

    .container {
        height: 80vh;
        margin-top: 0;
        border-top-left-radius: 0;
        border-top-right-radius: 0;
    }

    .fab {
        position: absolute;
    }
}

/* Modals */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(3px);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal-card {
    background: var(--bg-color);
    padding: 2rem;
    border-radius: var(--radius-lg);
    width: 90%;
    max-width: 400px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
    animation: slideUpModal 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    display: flex;
    flex-direction: column;
}

@keyframes slideUpModal {
    from {
        transform: translateY(30px);
        opacity: 0;
    }

    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.modal-card h3 {
    color: var(--primary-dark);
    margin-bottom: 1.5rem;
}

.modal-label {
    display: block;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 0.25rem;
    margin-top: 1rem;
}

.modal-input {
    width: 100%;
    padding: 0.75rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    font-family: inherit;
    font-size: 1rem;
    margin-bottom: 0.5rem;
}

.modal-input:focus {
    outline: none;
    border-color: var(--primary);
}

.error-text {
    color: #dc2626;
    font-size: 0.85rem;
    font-weight: 600;
    margin-bottom: 1rem;
    display: none;
}

.btn {
    padding: 0.75rem 1.5rem;
    border-radius: var(--radius-sm);
    font-weight: 600;
    cursor: pointer;
    border: none;
    flex: 1;
    transition: var(--transition);
}

.btn-primary {
    background: var(--primary);
    color: white;
}

.btn-primary:hover {
    background: var(--primary-dark);
}

.btn-secondary {
    background: var(--surface);
    color: var(--text-main);
    border: 1px solid var(--border);
}

.btn-secondary:hover {
    background: var(--border);
}

/* --- Sidebar Menu Drawer --- */
.sidebar-menu {
    position: fixed;
    top: 0;
    left: -100vw;
    width: 300px;
    height: 100vh;
    background: var(--surface);
    box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
    transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    flex-direction: column;
}

.sidebar-menu.open {
    left: 0;
}

@media (max-width: 768px) {
    .sidebar-menu {
        width: 85vw;
        left: -100vw;
    }

    .sidebar-menu.open {
        left: 0;
    }
}

/* --- Map Controls Top Alignment --- */
.map-controls-left,
.map-controls-right {
    top: 90px;
    transition: top 0.3s;
}

@media (min-width: 769px) {

    .map-controls-left,
    .map-controls-right {
        top: 120px;
    }
}

/* --- Phase 17: Map View (CSS Tree Diagram) --- */
#mapView {
    display: none;
    width: 100vw;
    height: 100vh;
    background: var(--surface);
    overflow: hidden;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 50;
    /* Under header (z-index 100) */
    cursor: grab;
    user-select: none;
    padding-top: 60px;
    /* Offset for Fixed Header */
}

#mapView:active {
    cursor: grabbing;
}

#mapContainer {
    transform-origin: 0 0;
    padding: 100px;
    display: inline-block;
    min-width: 100%;
}

.tree ul {
    padding-top: 20px;
    position: relative;
    display: flex;
    justify-content: center;
    margin: 0;
    padding-left: 0;
}

.tree li {
    float: left;
    text-align: center;
    list-style-type: none;
    position: relative;
    padding: 20px 5px 0 5px;
}

.tree li::before,
.tree li::after {
    content: '';
    position: absolute;
    top: 0;
    right: 50%;
    border-top: 2px solid var(--border);
    width: 50%;
    height: 20px;
}

.tree li::after {
    right: auto;
    left: 50%;
    border-left: 2px solid var(--border);
}

.tree li:only-child::after,
.tree li:only-child::before {
    display: none;
}

.tree li:only-child {
    padding-top: 0;
}

.tree li:first-child::before,
.tree li:last-child::after {
    border: 0 none;
}

.tree li:last-child::before {
    border-right: 2px solid var(--border);
    border-radius: 0 6px 0 0;
}

.tree li:first-child::after {
    border-radius: 6px 0 0 0;
}

.tree ul ul::before {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    border-left: 2px solid var(--border);
    width: 0;
    height: 20px;
    transform: translateX(-1px);
}

.tree .map-node {
    border: 1px solid var(--border);
    padding: 0.5rem 1rem;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    background: var(--bg-color);
    color: var(--text-main);
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 600;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
    transition: transform 0.2s, box-shadow 0.2s, border-color 0.2s;
    cursor: pointer;
    white-space: nowrap;
}

.tree .map-node:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
    border-color: var(--primary);
    color: var(--primary);
    z-index: 10;
}

.tree .map-node svg {
    flex-shrink: 0;
}

/* --- Horizontal Tree Orientation --- */
.tree.tree-horizontal ul {
    padding-top: 0;
    padding-left: 20px;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
}

.tree.tree-horizontal li {
    float: none;
    text-align: left;
    display: flex;
    flex-direction: row;
    align-items: center;
    padding: 10px 0 10px 20px;
}

.tree.tree-horizontal li::before,
.tree.tree-horizontal li::after {
    content: '';
    position: absolute;
    right: auto;
    left: 0;
    border-top: 0 none;
    border-left: 2px solid var(--border);
    width: 20px;
    height: 50%;
}

.tree.tree-horizontal li::before {
    top: 0;
    bottom: 50%;
    border-bottom: 2px solid var(--border);
}

.tree.tree-horizontal li::after {
    top: 50%;
    border-bottom: 0 none;
}

.tree.tree-horizontal li:only-child::after,
.tree.tree-horizontal li:only-child::before {
    display: none;
}

.tree.tree-horizontal li:first-child::before {
    border-left: 0 none;
}

.tree.tree-horizontal li:first-child::after {
    border-radius: 6px 0 0 0;
    border-top: 2px solid var(--border);
}

.tree.tree-horizontal li:last-child::before {
    border-bottom: 2px solid var(--border);
    border-left: 2px solid var(--border);
    border-radius: 0 0 0 6px;
    border-right: 0 none;
}

.tree.tree-horizontal li:last-child::after {
    border: 0 none;
}

.tree.tree-horizontal ul ul::before {
    content: '';
    position: absolute;
    border-left: 0 none;
    border-top: 2px solid var(--border);
    width: 20px;
    height: 0;
    top: 50%;
    left: 0;
    transform: translateY(-50%);
}