/* Common CSS for all GUI Tools */

/* ========== Global sizing sanity ========== */
:root {
    --iw-font-sans: Arial, sans-serif;
    --iw-font-serif: Georgia, "Times New Roman", serif;
    --iw-color-scheme: light;

    --iw-bg: #f3f5f7;
    --iw-surface: #ffffff;
    --iw-surface-muted: #f3f6f9;
    --iw-surface-subtle: #f8fafc;
    --iw-border: #d7dde4;
    --iw-border-strong: #a9b7c6;
    --iw-text: #17202a;
    --iw-text-muted: #526171;
    --iw-text-soft: #697789;
    --iw-accent: #2563a9;
    --iw-accent-hover: #1f5a99;
    --iw-accent-contrast: #ffffff;
    --iw-success: #17663a;
    --iw-success-bg: #e7f5ee;
    --iw-warning: #765b08;
    --iw-warning-bg: #fff7dc;
    --iw-danger: #9b2c2c;
    --iw-danger-bg: #fff0ef;
    --iw-focus-ring: rgba(37, 99, 169, 0.22);
    --iw-shadow-sm: 0 1px 2px rgba(20, 32, 45, 0.04);
    --iw-shadow-hover: 0 0 10px rgba(0, 0, 0, 0.18);
    --iw-overlay-bg: rgba(255, 255, 255, 0.8);
    --iw-prose-text: #333333;
    --iw-input-border: #bcc7d3;
    --iw-plot-header-border: #e5eaf0;
    --iw-button-hover-bg: #eef3f8;
    --iw-radius-sm: 6px;
    --iw-radius-md: 8px;
    --iw-space-1: 4px;
    --iw-space-2: 6px;
    --iw-space-3: 8px;
    --iw-space-4: 10px;
    --iw-space-5: 12px;
    --iw-space-6: 16px;
    --iw-font-size-xs: 0.74rem;
    --iw-font-size-sm: 0.82rem;
    --iw-font-size-md: 0.9rem;
    --iw-font-size-lg: 1rem;

    --header-bg: var(--iw-surface-muted);
    --header-border: var(--iw-border);
    --header-text: var(--iw-text);
    --nav-button-bg: var(--iw-surface);
    --nav-button-hover: var(--iw-button-hover-bg);
    --nav-button-text: var(--iw-text);
    --nav-button-size: 2rem;
}

:root[data-theme="dark"] {
    --iw-color-scheme: dark;
    --iw-bg: #0f141b;
    --iw-surface: #171d26;
    --iw-surface-muted: #202833;
    --iw-surface-subtle: #151b23;
    --iw-border: #33404f;
    --iw-border-strong: #536273;
    --iw-text: #e7edf5;
    --iw-text-muted: #b5c0cd;
    --iw-text-soft: #94a1b2;
    --iw-accent: #72a7ff;
    --iw-accent-hover: #9abfff;
    --iw-accent-contrast: #08111f;
    --iw-success: #8ee0ad;
    --iw-success-bg: #143222;
    --iw-warning: #f3d37a;
    --iw-warning-bg: #372b12;
    --iw-danger: #ff9b92;
    --iw-danger-bg: #3b1b1d;
    --iw-focus-ring: rgba(114, 167, 255, 0.36);
    --iw-shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.36);
    --iw-shadow-hover: 0 0 0 1px rgba(114, 167, 255, 0.28), 0 8px 24px rgba(0, 0, 0, 0.32);
    --iw-overlay-bg: rgba(15, 20, 27, 0.82);
    --iw-prose-text: #d6dee8;
    --iw-input-border: #536273;
    --iw-plot-header-border: #2b3644;
    --iw-button-hover-bg: #263242;

    --header-bg: #171d26;
    --header-border: #33404f;
    --header-text: var(--iw-text);
    --nav-button-bg: #202833;
    --nav-button-hover: #263242;
    --nav-button-text: var(--iw-text);
}

html, body {
    height: 100%;
    margin: 0;
}

*, *::before, *::after {
    box-sizing: border-box;
}

/* Use dynamic viewport units on modern browsers; fallback is fine */
.app {
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    font-family: var(--iw-font-sans);
    color: var(--iw-text);
    background: var(--iw-bg);
    color-scheme: var(--iw-color-scheme);
}

/* ========== Header ========== */
.header {
    flex: 0 0 auto;
    padding: 6px 10px;
    background-color: var(--header-bg);
    border-bottom: 1px solid var(--header-border);
}

.header-title {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.brand-link {
    display: inline-flex;
    flex: 0 0 auto;
    align-items: center;
    border-radius: var(--iw-radius-sm);
}

.logo {
    display: block;
    width: 36px;
    height: auto;
}

.page-title {
    min-width: 0;
    margin: 0;
    color: var(--header-text);
    font-size: 1.25rem;
    line-height: 1.2;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.main-nav {
    margin-left: auto;
    flex: 0 0 auto;
}

.main-nav ul {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 0;
    padding: 0;
    list-style: none;
}

.nav-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: var(--nav-button-size);
    height: var(--nav-button-size);
    padding: 0 0.75rem;
    border: 0;
    border-radius: 999px;
    appearance: none;
    color: var(--nav-button-text);
    font-family: inherit;
    text-align: center;
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 700;
    background-color: var(--nav-button-bg);
    box-shadow: var(--iw-shadow-sm);
    cursor: pointer;
    transition: background-color 0.2s ease, box-shadow 0.2s ease, transform 0.2s ease;
}

.nav-button-home,
.nav-button-help,
.theme-toggle {
    position: relative;
    flex: 0 0 var(--nav-button-size);
    width: var(--nav-button-size);
    min-width: var(--nav-button-size);
    padding: 0;
    line-height: 1;
}

.nav-icon,
.theme-icon {
    position: absolute;
    width: 1.1rem;
    height: 1.1rem;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
    transition: opacity 0.18s ease, transform 0.18s ease;
}

.theme-icon-moon {
    opacity: 0;
    transform: scale(0.78) rotate(-18deg);
}

:root[data-theme="dark"] .theme-icon-sun {
    opacity: 0;
    transform: scale(0.78) rotate(18deg);
}

:root[data-theme="dark"] .theme-icon-moon {
    opacity: 1;
    transform: scale(1) rotate(0deg);
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.brand-link:hover,
.brand-link:focus-visible,
.nav-button:hover,
.nav-button:focus-visible {
    background-color: var(--nav-button-hover);
    box-shadow: var(--iw-shadow-hover);
    outline: 2px solid var(--iw-accent);
    outline-offset: 2px;
}

.nav-button:hover,
.nav-button:focus-visible {
    transform: translateY(-1px);
}

/* ========== Tool area: fills remaining viewport ========== */
.tool-canvas {
    flex: 1 1 auto;
    /* critical: allows children to be scroll containers without overflow bugs */
    min-height: 0;
    min-width: 0;

    /* choose your default: padding here is safe and consistent */
    padding: 10px;

    /* pick one:
       - hidden: tool manages internal scroll
       - auto: page scrolls if tool overflows
    */
    overflow: hidden;
}

/* The contract container every tool gets */
.tool-root {
    height: 100%;
    width: 100%;
    min-height: 0;
    min-width: 0;
}

/* ========== IkaWeb shared tool primitives ========== */
.iw-workbench {
    height: 100%;
    width: 100%;
    min-height: 0;
    min-width: 0;
    color: var(--iw-text);
    background: var(--iw-bg);
}

.iw-workbench--column {
    display: flex;
    flex-direction: column;
}

.iw-workbench--sidebar-grid {
    display: grid;
    grid-template-columns: var(--iw-sidebar-width, minmax(280px, 340px)) minmax(0, 1fr);
}

.iw-scroll {
    min-height: 0;
    min-width: 0;
    overflow: auto;
}

.iw-scroll-root {
    height: 100%;
    min-height: 0;
    min-width: 0;
    overflow-y: auto;
    overflow-x: hidden;
}

.iw-sidebar,
.iw-main {
    min-height: 0;
    min-width: 0;
}

.iw-sidebar {
    background: var(--iw-surface);
}

.iw-sidebar--stack {
    display: flex;
    flex-direction: column;
    gap: var(--iw-space-3);
    padding: var(--iw-space-4);
    overflow-y: auto;
    border-right: 1px solid var(--iw-border);
}

.iw-toolbar {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: var(--iw-space-4);
    flex-wrap: wrap;
    padding: var(--iw-space-4);
    border-bottom: 1px solid var(--iw-border);
    background: var(--iw-surface-subtle);
}

.iw-panel {
    min-width: 0;
    border: 1px solid var(--iw-border);
    border-radius: var(--iw-radius-md);
    background: var(--iw-surface);
}

.iw-section {
    display: flex;
    flex-direction: column;
    gap: var(--iw-space-3);
    padding: var(--iw-space-4);
    border: 1px solid var(--iw-border);
    border-radius: var(--iw-radius-md);
    background: var(--iw-surface);
}

.iw-section-header {
    display: flex;
    align-items: center;
    gap: var(--iw-space-4);
    min-width: 0;
}

.iw-section-title,
.iw-panel-title {
    margin: 0;
    color: var(--iw-text);
    font-size: var(--iw-font-size-md);
    line-height: 1.2;
    font-weight: 800;
}

.iw-card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 300px));
    gap: var(--iw-space-5);
    align-items: stretch;
    justify-content: start;
}

.iw-card {
    position: relative;
    display: block;
    min-width: 0;
    overflow: hidden;
    border: 1px solid var(--iw-border);
    border-radius: var(--iw-radius-sm);
    background: var(--iw-surface);
    color: inherit;
    text-decoration: none;
    transition: box-shadow 0.2s ease, transform 0.2s ease;
}

.iw-card:hover,
.iw-card:focus-visible {
    box-shadow: var(--iw-shadow-hover);
    transform: translateY(-1px);
    outline: 2px solid var(--iw-accent);
    outline-offset: 2px;
}

.iw-prose {
    max-width: 800px;
    margin: 0 auto;
    color: var(--iw-text);
    line-height: 1.6;
}

.iw-prose p {
    color: var(--iw-prose-text);
}

.iw-details {
    border: 1px solid var(--iw-border);
    border-radius: var(--iw-radius-sm);
    background: var(--iw-surface-subtle);
}

.iw-details[open] {
    background: var(--iw-surface);
}

.iw-details summary {
    cursor: pointer;
    font-weight: 700;
    line-height: 1.35;
    list-style: none;
    overflow-wrap: anywhere;
}

.iw-details summary:focus-visible {
    outline: 2px solid var(--iw-focus-ring);
    outline-offset: 3px;
}

.iw-field,
.iw-field-row {
    min-width: 0;
    color: var(--iw-text-muted);
    font-size: var(--iw-font-size-sm);
    font-weight: 700;
}

.iw-field {
    display: grid;
    gap: var(--iw-space-1);
}

.iw-field-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(96px, 128px);
    align-items: center;
    gap: var(--iw-space-3);
}

.iw-input,
.iw-select,
.iw-sidebar input:not([type="range"]):not([type="checkbox"]):not([type="file"]),
.iw-sidebar select {
    width: 100%;
    min-width: 0;
    min-height: 34px;
    padding: 4px 7px;
    border: 1px solid var(--iw-input-border);
    border-radius: var(--iw-radius-sm);
    background: var(--iw-surface);
    color: var(--iw-text);
    font: inherit;
}

.iw-range,
.iw-sidebar input[type="range"] {
    accent-color: var(--iw-accent);
}

.iw-input:focus-visible,
.iw-select:focus-visible,
.iw-sidebar input:focus-visible,
.iw-sidebar select:focus-visible,
.iw-button:focus-visible {
    border-color: var(--iw-accent);
    outline: 2px solid var(--iw-focus-ring);
    outline-offset: 1px;
}

.iw-actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--iw-space-3);
}

.iw-button {
    min-height: 34px;
    border: 1px solid var(--iw-border-strong);
    border-radius: var(--iw-radius-sm);
    background: var(--iw-surface-subtle);
    color: var(--iw-text);
    font: inherit;
    font-size: var(--iw-font-size-sm);
    font-weight: 700;
    cursor: pointer;
}

.iw-button:hover:not(:disabled) {
    border-color: var(--iw-accent);
    background: var(--iw-button-hover-bg);
}

.iw-button--primary {
    border-color: var(--iw-accent-hover);
    background: var(--iw-accent);
    color: var(--iw-accent-contrast);
}

.iw-button--primary:hover:not(:disabled) {
    background: var(--iw-accent-hover);
}

.iw-button--icon {
    width: 34px;
    padding: 0;
    border-radius: 50%;
    line-height: 1;
}

.iw-button:disabled {
    cursor: not-allowed;
    opacity: 0.62;
}

.iw-status {
    min-height: 1.2rem;
    margin: 0;
    color: var(--iw-text-muted);
    font-size: 0.8rem;
    line-height: 1.35;
}

.iw-status--error {
    color: var(--iw-danger);
}

.iw-kpi-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--iw-space-2);
    margin: 0;
}

.iw-kpi {
    min-width: 0;
    padding: var(--iw-space-2) var(--iw-space-3);
    border-radius: var(--iw-radius-sm);
    background: var(--iw-surface-muted);
}

.iw-kpi dt {
    margin: 0 0 2px;
    color: var(--iw-text-muted);
    font-size: var(--iw-font-size-xs);
    font-weight: 700;
}

.iw-kpi dd {
    margin: 0;
    color: var(--iw-text);
    font-size: 0.88rem;
    font-variant-numeric: tabular-nums;
    font-weight: 800;
    overflow-wrap: anywhere;
}

.iw-plot-card,
.iw-plot-surface {
    min-width: 0;
    min-height: 0;
    border: 1px solid var(--iw-border);
    border-radius: var(--iw-radius-md);
    background: var(--iw-surface);
}

.iw-plot-split {
    flex: 1 1 auto;
    min-height: 0;
    min-width: 0;
    display: flex;
}

.iw-plot-main {
    flex: 1 1 50%;
    min-width: 0;
    min-height: 0;
    position: relative;
}

.iw-plot-stack {
    flex: 1 1 50%;
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.iw-plot-cell {
    flex: 1 1 0;
    width: 100%;
    height: 100%;
    min-height: 0;
    min-width: 0;
}

.iw-plot-card {
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.iw-plot-header {
    flex: 0 0 auto;
    min-height: 36px;
    margin: 0;
    padding: 9px 10px 7px;
    border-bottom: 1px solid var(--iw-plot-header-border);
}

.iw-plot-surface {
    width: 100%;
    height: 100%;
}

.iw-plot-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--iw-space-6);
    background: var(--iw-overlay-bg);
    color: var(--iw-text);
    text-align: center;
    font-size: 1.5rem;
    z-index: 10;
}

.iw-hidden {
    display: none !important;
}

@media (max-width: 900px) {
    .iw-workbench--mobile-scroll {
        overflow: auto;
    }

    .iw-plot-split {
        min-height: 1100px;
        flex-direction: column;
    }

    .iw-plot-main,
    .iw-plot-stack {
        flex: 1 0 auto;
        min-height: 360px;
    }

    .iw-plot-cell {
        min-height: 320px;
    }
}

@media (max-width: 1120px) {
    .iw-workbench--sidebar-grid {
        height: 100%;
        min-height: 0;
        overflow-y: auto;
        overflow-x: hidden;
        display: block;
    }

    .iw-sidebar--stack {
        overflow: visible;
        border-right: 0;
        border-bottom: 1px solid var(--iw-border);
    }
}

/* Loading overlay: make it actually cover the viewport */
#loadingOverlay:not(.iw-plot-overlay) {
    position: fixed;
    inset: 0;
    background: var(--iw-overlay-bg);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 24px;
    z-index: 1000;
}

/* ========== Compat for small screens ========== */
@media (max-width: 640px) {
    .header {
        padding: 6px 8px;
    }

    .header-title {
        gap: 8px;
    }

    .logo {
        width: 32px;
    }

    .page-title {
        font-size: 1rem;
    }
}
