:root {
    /* Carbon Light Theme (White/Gray 10) */
    --bg-color: #f4f4f4; /* ui-background */
    --card-bg: #ffffff; /* ui-01 */
    --text-main: #161616; /* text-01 */
    --text-secondary: #525252; /* text-02 */
    --accent-color: #0f62fe; /* interactive-01 */
    --accent-hover: #0353e9; /* hover-primary */
    --border-color: #e0e0e0; /* ui-03 */
    --highlight-bg: #e5f0ff; /* hover-selected-ui */
    --highlight-border: #0f62fe;
    --danger: #da1e28; /* support-error */
    --success: #24a148; /* support-success */
    --warning: #f1c21b; /* support-warning */
}

[data-theme="dark"] {
    /* Carbon Dark Theme (Gray 100) */
    --bg-color: #161616;
    --card-bg: #262626;
    --text-main: #f4f4f4;
    --text-secondary: #c6c6c6;
    --accent-color: #4589ff;
    --accent-hover: #0f62fe;
    --border-color: #393939;
    --highlight-bg: #353535;
    --highlight-border: #4589ff;
}

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

body {
    font-family: 'IBM Plex Sans', "Helvetica Neue", Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-main);
    line-height: 1.5;
    transition: background-color 0.2s ease, color 0.2s ease;
    -webkit-font-smoothing: antialiased;
}

.app-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 2rem;
}

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

.nav-brand {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.nav-brand h1 {
    font-size: 1.5rem;
    font-weight: 600;
}

.icon-button {
    background: transparent;
    border: none;
    color: var(--text-main);
    width: 48px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background-color 0.2s ease;
}

.icon-button:hover {
    background-color: var(--border-color);
}

.icon-button i {
    font-size: 1.25rem;
}

/* Grid Layout */
.dashboard-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

.span-2 {
    grid-column: span 2;
}

/* Cards (Carbon Tiles) */
.dashboard-card {
    background-color: var(--card-bg);
    border: 1px solid var(--border-color); /* Carbon rarely uses borders on tiles unless high contrast, but we'll use them for separation */
    display: flex;
    flex-direction: column;
}

.card-header {
    padding: 1.25rem 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border-color);
}

.card-header h2 {
    font-size: 1.125rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.card-content {
    padding: 1.5rem;
    flex-grow: 1;
}

/* Only scroll inside announcements and homework, not schedule */
.card-scrollable .card-content {
    overflow-y: auto;
    max-height: 500px;
}

.overflow-x-auto {
    overflow-x: auto;
}

/* Time Badge (Carbon Tags) */
.time-badge {
    background: var(--border-color);
    color: var(--text-main);
    padding: 0.25rem 0.75rem;
    font-size: 0.875rem;
    font-weight: 500;
    border-radius: 1rem;
}

/* Loading State */
.loading-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 150px;
    color: var(--text-secondary);
    gap: 1rem;
}

.spinner {
    width: 24px;
    height: 24px;
    border: 3px solid var(--border-color);
    border-top-color: var(--accent-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Schedule Table (Carbon Data Table) */
.schedule-table {
    width: 100%;
    /* MUST be separate for position:sticky to work on cells */
    border-collapse: separate;
    border-spacing: 0;
    min-width: 600px;
}

.schedule-table th, .schedule-table td {
    padding: 0.875rem 0.5rem;
    text-align: center;
    border-bottom: 1px solid var(--border-color);
    border-right: 1px solid var(--border-color);
    min-height: 80px;
    vertical-align: top;
}

.schedule-table th:last-child,
.schedule-table td:last-child {
    border-right: none;
}

.schedule-table th {
    font-weight: 600;
    color: var(--text-main);
    font-size: 0.9375rem;
    background-color: var(--bg-color);
    text-align: center;
    white-space: nowrap;
}

.schedule-table td {
    font-size: 0.875rem;
}

.time-col {
    font-weight: 600;
    text-align: center;
    white-space: nowrap;
    min-width: 80px;
    /* Sticky: pins left column when scrolling horizontally */
    position: sticky;
    left: 0;
    z-index: 3;
    background-color: var(--card-bg);
    /* Simulate collapsed border on right side */
    border-right: 2px solid var(--border-color);
    box-shadow: 2px 0 4px rgba(0,0,0,0.04);
}

.class-cell {
    min-width: 90px;
}

/* Multi-line subject + teacher format */
.cell-subject {
    display: block;
    font-weight: 700;
    font-size: 0.875rem;
    color: var(--accent-color);
    margin-bottom: 0.3rem;
    line-height: 1.4;
}

.cell-teacher {
    display: block;
    font-size: 0.8rem;
    color: var(--text-secondary);
    line-height: 1.3;
}

.time-sub-header {
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-secondary);
    white-space: nowrap;
    background-color: var(--bg-color);
}

.day-label {
    font-weight: 600;
    font-size: 0.9375rem;
    color: var(--text-main);
}

.class-cell.active-class {
    background-color: var(--highlight-bg);
    border-left: 3px solid var(--accent-color);
}

.class-cell.active-class .cell-subject {
    color: var(--accent-color);
}

/* Announcements */
.announcement-list {
    display: flex;
    flex-direction: column;
}

.announcement-card {
    padding: 1.25rem 0;
    border-bottom: 1px solid var(--border-color);
}

.announcement-card:first-child {
    padding-top: 0;
}

.announcement-card:last-child {
    border-bottom: none;
    padding-bottom: 0;
}

.announcement-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 0.5rem;
}

.announcement-title {
    font-weight: 600;
    font-size: 1.125rem;
}

.announcement-date {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.announcement-message {
    font-size: 1rem;
    color: var(--text-main);
    margin-bottom: 1rem;
}

.announcement-author {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-secondary);
}

/* Homework */
.homework-list {
    display: flex;
    flex-direction: column;
}

.homework-item {
    display: flex;
    align-items: flex-start;
    padding: 1rem 0;
    border-bottom: 1px solid var(--border-color);
    gap: 1rem;
}

.homework-item:first-child {
    padding-top: 0;
}

.homework-item:last-child {
    border-bottom: none;
    padding-bottom: 0;
}

.hw-finished {
    opacity: 0.5;
    transition: opacity 0.2s ease;
}

.hw-finished .hw-title {
    text-decoration: line-through;
    color: var(--text-secondary);
}

.hw-checkbox {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 0.25rem;
    margin-top: 0.4rem;
}

.hw-check-input {
    width: 1.25rem;
    height: 1.25rem;
    accent-color: var(--accent-color);
    cursor: pointer;
}

.hw-icon {
    color: var(--accent-color);
    font-size: 1.25rem;
    margin-top: 0.25rem;
}

.hw-content {
    flex-grow: 1;
}

.hw-subject {
    font-size: 0.875rem;
    color: var(--text-secondary);
    font-weight: 500;
    margin-bottom: 0.25rem;
    text-transform: uppercase;
    letter-spacing: 0.02em;
}

.hw-title {
    font-weight: 600;
    font-size: 1rem;
}

.hw-due {
    font-size: 0.875rem;
    color: var(--danger);
    margin-top: 0.5rem;
}

/* Forms & Admin (Carbon Forms) */
.form-input {
    width: 100%;
    padding: 0.75rem 1rem;
    background-color: var(--bg-color);
    border: none;
    border-bottom: 1px solid var(--text-secondary);
    color: var(--text-main);
    font-family: inherit;
    font-size: 0.875rem;
    transition: outline 0.1s ease;
    border-radius: 0; /* Carbon uses sharp corners */
}

.form-input:focus {
    outline: 2px solid var(--accent-color);
    outline-offset: -2px;
    border-bottom-color: transparent;
}

.btn-primary {
    background: var(--accent-color);
    color: #fff;
    border: none;
    padding: 0.75rem 1rem;
    padding-right: 4rem; /* Carbon primary buttons are typically wide */
    font-weight: 400;
    font-size: 0.875rem;
    cursor: pointer;
    transition: background 0.2s ease;
    display: inline-flex;
    align-items: center;
    justify-content: space-between;
    min-height: 3rem;
    border-radius: 0; /* Sharp corners */
}

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

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

.btn-primary:disabled {
    background: var(--border-color);
    color: var(--text-secondary);
    cursor: not-allowed;
}

/* ===== Responsive ===== */

/* Tablet: stack grid, tighten padding */
@media (max-width: 1024px) {
    .dashboard-grid {
        grid-template-columns: 1fr;
        gap: 0.75rem;
    }

    .span-2 {
        grid-column: span 1;
    }

    .app-container {
        padding: 1rem;
    }
}

/* Mobile: phone-sized layout */
@media (max-width: 640px) {
    .app-container {
        padding: 0.75rem 0.5rem;
    }

    .top-nav {
        margin-bottom: 1rem;
        padding-bottom: 0.75rem;
    }

    .nav-brand h1 {
        font-size: 1.1rem;
    }

    .icon-button {
        width: 40px;
        height: 40px;
    }

    .card-header {
        padding: 0.875rem 1rem;
    }

    .card-header h2 {
        font-size: 1rem;
    }

    .card-content {
        padding: 1rem 0.75rem;
    }

    /* Schedule: smaller cells on mobile, still scrollable */
    .schedule-table th,
    .schedule-table td {
        padding: 0.5rem 0.375rem;
        min-height: 60px;
    }

    .cell-subject {
        font-size: 0.75rem;
    }

    .cell-teacher {
        font-size: 0.7rem;
    }

    .time-sub-header {
        font-size: 0.65rem;
    }

    .time-col {
        min-width: 70px;
        font-size: 0.8rem;
    }

    /* Announcements */
    .announcement-title {
        font-size: 1rem;
    }

    .announcement-message {
        font-size: 0.9rem;
    }

    /* Homework */
    .hw-title {
        font-size: 0.9rem;
    }

    .hw-subject,
    .hw-due {
        font-size: 0.8rem;
    }

    /* Admin forms: stack fields */
    .form-input {
        font-size: 1rem; /* Prevent iOS zoom on focus */
        min-height: 48px;
    }

    .btn-primary {
        width: 100%;
        justify-content: center;
        padding-right: 1rem;
    }

    /* Card scrollable max-height smaller on mobile */
    .card-scrollable .card-content {
        max-height: 350px;
    }
}

/* Very small phones */
@media (max-width: 380px) {
    .nav-brand h1 {
        font-size: 0.95rem;
    }

    .app-container {
        padding: 0.5rem 0.375rem;
    }
}

/* ===== Admin Mobile ===== */
@media (max-width: 640px) {
    /* Stack admin form fields vertically */
    #manage-users-section .card-content > div,
    #manage-schedule-section .card-content > div {
        flex-direction: column;
    }

    #manage-schedule-section input,
    #manage-users-section input,
    #manage-users-section select {
        min-width: 100% !important;
    }

    /* Login card full-width on phones */
    #login-container .dashboard-card {
        margin: 40px auto;
        padding: 1.25rem;
        max-width: 100% !important;
    }

    /* Admin grid single column */
    #admin-container main.dashboard-grid {
        grid-template-columns: 1fr;
    }

    #admin-container .span-2 {
        grid-column: span 1;
    }
}
