@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700&display=swap');

:root {
    --bg-main: #0a0b0e;
    --bg-card: rgba(22, 25, 35, 0.7);
    --border-color: rgba(255, 255, 255, 0.08);
    --text-primary: #f3f4f6;
    --text-secondary: #9ca3af;
    --color-primary: #3b82f6;
    --color-primary-hover: #2563eb;
    --color-success: #10b981;
    --color-warning: #f59e0b;
    --color-danger: #ef4444;
}

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

body {
    font-family: 'Outfit', sans-serif;
    background-color: var(--bg-main);
    background-image: radial-gradient(circle at 10% 20%, rgba(59, 130, 246, 0.05) 0%, transparent 40%),
                      radial-gradient(circle at 90% 80%, rgba(16, 185, 129, 0.03) 0%, transparent 40%);
    color: var(--text-primary);
    min-height: 100vh;
    padding: 2rem 1rem;
    line-height: 1.5;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
}

/* Header & Navigation */
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2.5rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--border-color);
}

h1 {
    font-size: 1.8rem;
    font-weight: 700;
    letter-spacing: -0.025em;
    background: linear-gradient(to right, #3b82f6, #60a5fa);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

nav {
    display: flex;
    gap: 1rem;
}

.logout-form {
    margin: 0;
}

nav a {
    color: var(--text-secondary);
    text-decoration: none;
    font-weight: 500;
    padding: 0.5rem 1rem;
    border-radius: 8px;
    transition: all 0.2s ease;
}

nav a:hover, nav a.active {
    color: var(--text-primary);
    background-color: rgba(255, 255, 255, 0.05);
}

/* Alert Boxes */
.alert {
    padding: 1rem;
    border-radius: 8px;
    margin-bottom: 1.5rem;
    font-weight: 500;
}
.alert-success {
    background-color: rgba(16, 185, 129, 0.15);
    border: 1px solid rgba(16, 185, 129, 0.3);
    color: #34d399;
}
.alert-danger {
    background-color: rgba(239, 68, 68, 0.15);
    border: 1px solid rgba(239, 68, 68, 0.3);
    color: #f87171;
}

/* Cards & Layout */
.grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 2rem;
}

.grid > * {
    min-width: 0;
}

@media (min-width: 768px) {
    .grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
    .grid-full {
        grid-column: span 2;
    }
}

.card {
    background-color: var(--bg-card);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    padding: 1.5rem;
    box-shadow: 0 4px 30px rgba(0, 0, 0, 0.2);
}

.card-title {
    font-size: 1.2rem;
    font-weight: 600;
    margin-bottom: 1.25rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

/* Forms & Tables */
table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 0.5rem;
}

th, td {
    padding: 0.75rem 1rem;
    text-align: left;
    border-bottom: 1px solid var(--border-color);
}

th {
    color: var(--text-secondary);
    font-weight: 500;
    font-size: 0.9rem;
}

td {
    font-size: 0.95rem;
}

.input-group {
    margin-bottom: 1.25rem;
}

label {
    display: block;
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
    font-weight: 500;
}

input, select {
    width: 100%;
    padding: 0.75rem 1rem;
    background-color: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.95rem;
    transition: all 0.2s ease;
}

input:focus, select:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.25);
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.6rem 1.2rem;
    font-weight: 550;
    font-size: 0.9rem;
    border-radius: 8px;
    border: none;
    cursor: pointer;
    transition: all 0.2s ease;
    text-decoration: none;
}

.btn-primary {
    background-color: var(--color-primary);
    color: white;
}

.btn-primary:hover {
    background-color: var(--color-primary-hover);
}

.btn-secondary {
    background-color: rgba(255, 255, 255, 0.08);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover {
    background-color: rgba(255, 255, 255, 0.12);
}

.btn-danger {
    background-color: rgba(239, 68, 68, 0.15);
    color: #f87171;
    border: 1px solid rgba(239, 68, 68, 0.25);
}

.btn-danger:hover {
    background-color: rgba(239, 68, 68, 0.25);
}

.btn-small {
    padding: 0.35rem 0.75rem;
    font-size: 0.8rem;
}

/* Badges */
.badge {
    display: inline-block;
    padding: 0.25rem 0.5rem;
    border-radius: 6px;
    font-size: 0.75rem;
    font-weight: 600;
}

.badge-success {
    background-color: rgba(16, 185, 129, 0.15);
    color: #34d399;
}

.badge-warning {
    background-color: rgba(245, 158, 11, 0.15);
    color: #fbbf24;
}

.badge-danger {
    background-color: rgba(239, 68, 68, 0.15);
    color: #f87171;
}

.badge-info {
    background-color: rgba(59, 130, 246, 0.15);
    color: #60a5fa;
}

/* Date Navigator */
.date-nav {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.date-nav span {
    font-size: 1.2rem;
    font-weight: 600;
}

/* Modal styling */
.modal {
    display: none;
    position: fixed;
    z-index: 100;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    align-items: center;
    justify-content: center;
}

.modal.active {
    display: flex;
}

.modal-content {
    background-color: #12141a;
    border: 1px solid var(--border-color);
    border-radius: 16px;
    padding: 2rem;
    width: 90%;
    max-width: 450px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.5);
}

.modal-header {
    font-size: 1.3rem;
    font-weight: 600;
    margin-bottom: 1.5rem;
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 1rem;
    margin-top: 1.5rem;
}

/* Info Tooltip Icon */
.info-icon-wrapper {
    position: relative;
    display: inline-flex;
    align-items: center;
    margin-left: 0.4rem;
    cursor: help;
    vertical-align: middle;
}

.info-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background-color: rgba(59, 130, 246, 0.15);
    color: #60a5fa;
    font-size: 11px;
    font-weight: 700;
    font-style: normal;
    border: 1px solid rgba(59, 130, 246, 0.35);
    transition: all 0.2s ease;
    line-height: 1;
}

.info-icon-wrapper:hover .info-icon {
    background-color: var(--color-primary);
    color: #ffffff;
    border-color: var(--color-primary);
}

.info-tooltip {
    visibility: hidden;
    opacity: 0;
    position: absolute;
    bottom: calc(100% + 8px);
    left: 50%;
    transform: translateX(-50%);
    width: 280px;
    background-color: #1e2230;
    color: #e5e7eb;
    text-align: left;
    padding: 0.6rem 0.8rem;
    border-radius: 6px;
    border: 1px solid rgba(255, 255, 255, 0.15);
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
    font-size: 0.8rem;
    font-weight: 400;
    line-height: 1.35;
    z-index: 100;
    transition: opacity 0.2s ease, visibility 0.2s ease;
    pointer-events: none;
    white-space: normal;
}

.info-tooltip::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -5px;
    border-width: 5px;
    border-style: solid;
    border-color: #1e2230 transparent transparent transparent;
}

.info-icon-wrapper:hover .info-tooltip,
.info-icon-wrapper:focus .info-tooltip,
.info-icon-wrapper:focus-within .info-tooltip {
    visibility: visible;
    opacity: 1;
}

.info-icon-wrapper:focus {
    outline: none;
}

/* Shared building blocks used by the dashboards */
.chart-box {
    height: 350px;
    position: relative;
    min-width: 0;
    overflow: hidden;
}

.chart-box canvas {
    max-width: 100%;
}

.chart-legend {
    flex-wrap: wrap;
}

.card-title {
    flex-wrap: wrap;
    gap: 0.5rem 1rem;
}

/* ===== Mobile / responsive ===== */
@media (max-width: 768px) {
    body {
        padding: 0.75rem;
    }

    .card {
        padding: 1rem;
        border-radius: 12px;
    }

    .grid {
        gap: 1rem;
    }

    /* Header: title + logout on one row, scrollable nav below */
    header {
        flex-wrap: wrap;
        gap: 0.5rem 1rem;
        margin-bottom: 1.25rem;
    }

    h1 {
        font-size: 1.3rem;
        order: 1;
    }

    .logout-form {
        order: 2;
    }

    /* Nav never scrolls sideways: items share the row and wrap onto a second row if needed */
    nav {
        order: 3;
        width: 100%;
        flex-wrap: wrap;
        gap: 0.25rem;
    }

    nav a {
        flex: 1 1 auto;
        text-align: center;
        padding: 0.55rem 0.4rem;
        font-size: 0.9rem;
    }

    /* Touch targets; 16px inputs stop iOS from zooming on focus */
    .btn {
        min-height: 40px;
    }

    .btn-small {
        min-height: 36px;
    }

    input, select {
        font-size: 16px;
    }

    .date-nav {
        gap: 0.75rem;
        margin-bottom: 1.25rem;
    }

    .date-nav .btn {
        flex: 0 0 52px;
    }

    .date-nav span {
        font-size: 1.05rem;
    }

    .chart-box {
        height: 280px;
    }

    /* Data tables fit the screen: compact cells, wrapping headers, secondary columns hidden.
       Only the vertical scroll of long tables remains. */
    .table-scroll {
        overflow-x: hidden !important;
        max-height: 70vh !important;
    }

    .table-scroll table {
        width: 100%;
    }

    th, td {
        padding: 0.5rem 0.35rem;
        font-size: 0.8rem;
    }

    .t-heating td:first-child,
    .t-water td:first-child,
    .t-elec-raw td:first-child {
        white-space: nowrap;
    }

    /* very narrow phones: let cells shrink instead of overflowing the card */
    .table-scroll th,
    .table-scroll td {
        overflow-wrap: anywhere;
    }

    /* sessions tables: only the description wraps; times, power and kWh stay on one line */
    .table-scroll .t-sessions th:not(:first-child),
    .table-scroll .t-sessions td:not(:first-child) {
        white-space: nowrap;
        overflow-wrap: normal;
    }

    .table-scroll .t-sessions th,
    .table-scroll .t-sessions td {
        padding-left: 0.3rem;
        padding-right: 0.3rem;
    }

    .table-scroll .t-heating th:first-child,
    .table-scroll .t-water th:first-child,
    .table-scroll .t-elec-raw th:first-child {
        overflow-wrap: normal;
    }

    th {
        font-size: 0.7rem;
    }

    .badge {
        white-space: normal;
        font-size: 0.65rem;
        padding: 0.2rem 0.4rem;
    }

    /* heating: keep Aika, Menoilma, Ulkolämpö, Kompressori, Tila */
    .t-heating th:nth-child(3), .t-heating td:nth-child(3),
    .t-heating th:nth-child(5), .t-heating td:nth-child(5) { display: none; }

    /* water: keep Aika, Käyttövesi, Ulkona, Lämmitystila */
    .t-water th:nth-child(4), .t-water td:nth-child(4) { display: none; }

    /* electricity raw: keep Aika, Yhteensä, Spot-hinta */
    .t-elec-raw th:nth-child(2), .t-elec-raw td:nth-child(2),
    .t-elec-raw th:nth-child(3), .t-elec-raw td:nth-child(3),
    .t-elec-raw th:nth-child(4), .t-elec-raw td:nth-child(4) { display: none; }

    /* Tooltip: spans the routine card (tap the (i) icon) so it can't overflow the viewport */
    .info-icon-wrapper {
        position: static;
    }

    .info-tooltip {
        left: 0.5rem;
        right: 0.5rem;
        top: 2.9rem;
        bottom: auto;
        transform: none;
        width: auto;
    }

    .info-tooltip::after {
        display: none;
    }

    /* Scheduler tables become one card per routine */
    .table-stack thead {
        display: none;
    }

    .table-stack,
    .table-stack tbody,
    .table-stack tr {
        display: block;
        width: 100%;
    }

    .table-stack tr {
        position: relative;
        border: 1px solid var(--border-color);
        border-radius: 12px;
        padding: 0.5rem 0.9rem;
        margin-bottom: 0.75rem;
        background: rgba(255, 255, 255, 0.02);
    }

    .table-stack td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 1rem;
        border: none;
        padding: 0.4rem 0;
        text-align: right;
    }

    .table-stack td:first-child {
        font-size: 1.05rem;
        flex-wrap: wrap;
        justify-content: flex-start;
        border-bottom: 1px solid var(--border-color);
        padding-bottom: 0.6rem;
        margin-bottom: 0.25rem;
    }

    .table-stack td:not(:first-child)::before {
        color: var(--text-secondary);
        font-size: 0.8rem;
        text-align: left;
        flex: 0 0 auto;
    }

    .table-stack td:nth-child(2)::before { content: "Tila"; }
    .table-stack td:nth-child(3)::before { content: "Edellinen ajo"; }
    .table-stack td:nth-child(4)::before { content: "Seuraava ajo"; }
    .table-stack td:nth-child(5)::before { content: "Epäonnistumiset"; }
    .table-stack td:nth-child(6)::before { content: "Ajastus (cron)"; }
    .table-stack td:nth-child(7)::before { content: "Toiminnot"; }

    /* Cron editor and action buttons get a full-width row */
    .table-stack td:nth-child(6),
    .table-stack td:nth-child(7) {
        flex-direction: column;
        align-items: stretch;
        text-align: left;
    }

    .table-stack td:nth-child(6) form {
        display: flex !important;
        width: 100%;
    }

    .table-stack td:nth-child(6) input[type="text"] {
        width: auto !important;
        flex: 1;
        min-width: 0;
        padding: 0.5rem 0.75rem !important;
        font-size: 16px !important;
    }

    .table-stack td:nth-child(7) > div {
        flex-wrap: wrap;
    }

    .table-stack td[colspan] {
        display: block;
        text-align: center;
    }

    .table-stack td[colspan]::before {
        content: none !important;
    }

    /* Modals: near full width, scroll if the keyboard shrinks the viewport */
    .modal-content {
        width: calc(100% - 1.5rem);
        max-height: 92vh;
        overflow-y: auto;
        padding: 1.25rem;
    }

    .modal-header {
        font-size: 1.1rem;
        margin-bottom: 1rem;
    }

    .modal-footer .btn {
        flex: 1;
    }
}

@media (max-width: 480px) {
    .chart-legend {
        font-size: 0.75rem;
    }
}
