/* Base responsive improvements */
* {
    box-sizing: border-box;
}

body {
    max-width: 100%;
    overflow-x: hidden;
}

/* Make the main layout fluid */
table#tlayout {
    display: block !important;
    width: 100% !important;
    max-width: 100% !important;
    margin: 0;
    padding: 0;
}

table#tlayout tbody,
table#tlayout tr {
    display: block !important;
    width: 100% !important;
}

#toplink {
    width: 95% !important;
    max-width: 1600px !important;
}

div#footer {
    width: calc(100% - 350px) !important;
    max-width: calc(100% - 350px) !important;
    margin-left: 350px !important;
}

/* Menu - Fixed sidebar on desktop */
#layout-menu {
    position: fixed !important;
    left: 0;
    top: 0;
    width: 350px !important;
    min-width: 350px !important;
    max-width: 350px !important;
    height: 100vh;
    overflow-y: auto;
    padding: 1em 15px;
    vertical-align: top;
    box-sizing: border-box;
}

/* Content area */
#layout-content {
    display: block !important;
    margin-left: 350px !important;
    width: calc(100% - 350px) !important;
    max-width: calc(100% - 350px) !important;
    min-width: 0 !important;
    padding: 1em 3em !important;
    vertical-align: top;
    box-sizing: border-box;
    overflow-wrap: break-word;
    word-wrap: break-word;
}

/* Scholar widget */
.scholar-widget {
    max-width: 100%;
    box-sizing: border-box;
    overflow: hidden;
    margin: 20px 0 !important;
    padding: 15px !important;
}

.scholar-stats {
    display: flex;
    flex-wrap: nowrap;
    justify-content: space-between;
    margin-bottom: 12px !important;
    gap: 4px;
}

.stat-item {
    flex: 1;
    min-width: 0;
    box-sizing: border-box;
    padding: 8px 2px;
    text-align: center;
}

.stat-value {
    font-size: 18px !important;
    line-height: 1.3;
    font-weight: bold;
}

.stat-label {
    font-size: 10px !important;
    line-height: 1.2;
}

.chart-container {
    width: 100%;
    max-width: 100%;
    overflow: hidden;
    height: 170px !important;
}

.chart-container canvas {
    max-width: 100%;
    height: 170px !important;
}

/* Images should scale */
img {
    max-width: 100%;
    height: auto;
}

/* Make tables responsive */
table.imgtable {
    width: 100%;
    max-width: 100%;
}

table.imgtable td {
    display: block;
    width: 100%;
}

/* Definition lists responsive */
dd {
    margin-left: 11em;
    width: auto;
    max-width: 600px;
}

/* ==========================================
   TABLET (max-width: 1100px)
   ========================================== */
@media screen and (max-width: 1100px) {
    /* Smaller fixed menu on tablet */
    #layout-menu {
        position: fixed !important;
        width: 300px !important;
        min-width: 300px !important;
        max-width: 300px !important;
        padding: 1em 12px;
        font-size: 95%;
    }

    #layout-content {
        display: block !important;
        margin-left: 300px !important;
        width: calc(100% - 300px) !important;
        max-width: calc(100% - 300px) !important;
        padding: 1em 2em !important;
        overflow-wrap: break-word;
        word-wrap: break-word;
    }

    div#footer {
        margin-left: 300px !important;
        width: calc(100% - 300px) !important;
        max-width: calc(100% - 300px) !important;
    }

    /* Scholar widget adjustments - keep one line */
    .scholar-widget {
        padding: 12px !important;
    }

    .stat-item {
        flex: 1;
        padding: 6px 2px;
    }

    .stat-value {
        font-size: 16px !important;
    }

    .stat-label {
        font-size: 9px !important;
    }

    .chart-container {
        height: 160px !important;
    }

    .chart-container canvas {
        height: 160px !important;
    }

    /* Adjust heading sizes */
    h1 {
        font-size: 140%;
    }

    h2 {
        font-size: 115%;
    }

    h3 {
        font-size: 105%;
    }

    /* Make definition lists stack on tablet */
    dt {
        width: 100%;
        margin-left: 0;
    }

    dd {
        margin-left: 1em;
        margin-top: 0.5em;
        width: auto;
    }
}

/* ==========================================
   MOBILE (max-width: 800px) - Stack menu on top
   ========================================== */
@media screen and (max-width: 800px) {
    body {
        margin: 5px;
        padding: 0;
    }

    /* Force table to display as block for stacking */
    table#tlayout, table#tlayout tbody, table#tlayout tr {
        display: block !important;
        width: 100% !important;
    }

    /* Menu on top - Remove fixed positioning */
    #layout-menu {
        position: relative !important;
        left: auto !important;
        top: auto !important;
        display: block !important;
        width: 100% !important;
        max-width: 100% !important;
        min-width: 100% !important;
        height: auto !important;
        border-bottom: 2px solid #527bbd;
        border-right: none;
        margin-bottom: 1em;
        padding: 0.5em !important;
        font-size: 90%;
    }

    /* Content below menu - Remove left margin */
    #layout-content {
        display: block !important;
        width: 100% !important;
        max-width: 100% !important;
        margin-left: 0 !important;
        padding: 0.5em 1em !important;
        font-size: 95%;
    }

    div#footer {
        margin-left: 0 !important;
    }

    /* Stack menu items vertically on mobile */
    div.menu-category {
        display: block;
        margin-right: 0;
        border-right: none;
        border-bottom: 1px solid gray;
        padding-right: 0;
        padding-bottom: 0.5em;
        margin-bottom: 0.5em;
    }

    div.menu-item {
        display: block;
        margin-right: 0;
        padding-left: 8px;
        margin-bottom: 0.3em;
    }

    /* Smaller headings on mobile */
    h1 {
        font-size: 130%;
    }

    h2 {
        font-size: 110%;
    }

    h3 {
        font-size: 100%;
    }

    /* Scholar widget adjustments - 2x2 grid on mobile */
    .scholar-widget {
        padding: 8px;
        margin: 10px 0;
    }

    .scholar-stats {
        flex-wrap: wrap;
        gap: 6px;
    }

    .stat-item {
        flex: 0 0 48%;
        margin-bottom: 8px;
    }

    .stat-value {
        font-size: 14px !important;
    }

    .stat-label {
        font-size: 8px !important;
    }

    .chart-container {
        height: 120px !important;
    }

    .chart-container canvas {
        height: 120px !important;
    }

    .scholar-link {
        font-size: 10px;
    }

    /* Adjust publication list spacing */
    ul {
        padding-left: 1em;
    }

    li p {
        font-size: 90%;
        line-height: 1.4;
    }

    /* Make definition lists stack vertically */
    dt {
        float: none;
        width: 100%;
        margin-left: 0;
    }

    dd {
        margin-left: 0;
        margin-top: 0.5em;
        padding-left: 1em;
    }

    /* Adjust footer */
    div#footer {
        font-size: 80%;
        padding: 0.5em;
        width: 100% !important;
    }
}

/* ==========================================
   VERY SMALL MOBILE (max-width: 400px)
   ========================================== */
@media screen and (max-width: 400px) {
    #layout-content {
        padding: 0.5em !important;
    }

    h1 {
        font-size: 120%;
    }

    h2 {
        font-size: 105%;
    }

    /* Scholar widget - stack metrics 2x2 */
    .stat-item {
        flex: 0 0 50%;
    }

    li p {
        font-size: 85%;
    }
}

/* ==========================================
   LARGE SCREENS (min-width: 1600px)
   ========================================== */
@media screen and (min-width: 1600px) {
    table#tlayout {
        max-width: 1800px !important;
    }

    #layout-menu {
        width: 380px !important;
        min-width: 380px !important;
        max-width: 380px !important;
        padding: 1em 18px;
    }

    #layout-content {
        display: block !important;
        margin-left: 380px !important;
        width: calc(100% - 380px) !important;
        max-width: calc(100% - 380px) !important;
        padding: 1.5em 4em !important;
        overflow-wrap: break-word;
        word-wrap: break-word;
    }

    #toplink, div#footer {
        max-width: 1800px !important;
    }

    div#footer {
        margin-left: 380px !important;
        width: calc(100% - 380px) !important;
        max-width: calc(100% - 380px) !important;
    }

    /* Bigger widget on large screens - keep one line */
    .scholar-widget {
        padding: 18px !important;
    }

    .stat-item {
        flex: 1;
        padding: 8px 3px;
    }

    .stat-value {
        font-size: 20px !important;
    }

    .stat-label {
        font-size: 11px !important;
    }

    .chart-container {
        height: 190px !important;
    }

    .chart-container canvas {
        height: 190px !important;
    }
}

/* Print styles - hide menu and optimize layout */
@media print {
    #layout-menu {
        display: none !important;
    }

    #layout-content {
        width: 100% !important;
        max-width: 100% !important;
    }

    .scholar-widget {
        display: none;
    }
}
