:root {
    color-scheme: light;
    --background: #f7f9fc;
    --panel-background: #ffffff;
    --accent: #3f6ad8;
    --accent-light: rgba(63, 106, 216, 0.1);
    --text-color: #1f2933;
    --muted-text: #607086;
    --border-color: #d0d7e2;
    --shadow: 0 8px 24px rgba(15, 23, 42, 0.1);
    font-family: 'Noto Sans JP', 'Hiragino Kaku Gothic ProN', 'Meiryo', sans-serif;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--background);
    color: var(--text-color);
    line-height: 1.6;
}

a {
    color: inherit;
    text-decoration: none;
}

.app-header {
    background: linear-gradient(135deg, #3557d4, #5f89ff);
    color: #fff;
    padding: 24px 16px;
    box-shadow: var(--shadow);
}

.header-inner {
    max-width: 1080px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.header-inner h1 {
    margin: 0;
    font-size: 1.8rem;
    font-weight: 700;
}

.header-inner h1 a {
    color: #fff;
}

.tagline {
    margin: 0;
    font-size: 1rem;
    opacity: 0.9;
}

.app-main {
    max-width: 1080px;
    margin: 32px auto;
    padding: 0 16px 48px;
    display: flex;
    flex-direction: column;
    gap: 24px;
}

.app-main--learning {
    gap: 32px;
    padding-bottom: 140px;
}

.panel {
    background: var(--panel-background);
    border-radius: 16px;
    padding: 24px;
    box-shadow: var(--shadow);
}

.panel h2 {
    margin-top: 0;
    font-size: 1.4rem;
    color: #243b6b;
}

.alert {
    background: #fde68a;
    color: #7c4b00;
    padding: 12px 16px;
    border-radius: 12px;
    border: 1px solid #f6ad55;
}

.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 16px;
}
.grade-groups {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.grade-group {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.grade-group__title {
    margin: 0;
    font-size: 1rem;
    color: #425375;
    border-left: 4px solid var(--accent);
    padding-left: 10px;
}

.grade-grid {
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}

.subjects-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
}

@media (max-width: 960px) {
    .subjects-grid {
        grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    }
}

.card {
    display: block;
    padding: 16px;
    background: #f8faff;
    border-radius: 14px;
    border: 1px solid transparent;
    transition: transform 0.2s ease, box-shadow 0.2s ease, border-color 0.2s ease;
    height: 100%;
}

.card h3 {
    margin-top: 0;
    margin-bottom: 8px;
    font-size: 1.1rem;
}

.card p {
    margin: 0;
    color: var(--muted-text);
    font-size: 0.95rem;
}

.card .meta {
    font-size: 0.85rem;
    color: #425375;
    margin-bottom: 8px;
}

.card:hover,
.card.is-active {
    transform: translateY(-4px);
    box-shadow: 0 12px 30px rgba(63, 106, 216, 0.25);
    border-color: #6c8cff;
    background: #ffffff;
}

.start-panel {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.start-panel__summary,
.start-panel__meta,
.start-panel__overview {
    margin: 0;
    color: var(--muted-text);
}

.start-panel__summary strong {
    color: #1f3561;
}

.primary-button {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 999px;
    padding: 12px 24px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 12px 30px rgba(63, 106, 216, 0.25);
    transition: background 0.2s ease, transform 0.2s ease, box-shadow 0.2s ease;
}

.primary-button:hover {
    background: #2e55c7;
    transform: translateY(-1px);
    box-shadow: 0 16px 36px rgba(63, 106, 216, 0.35);
}

.primary-button:focus-visible {
    outline: 3px solid rgba(63, 106, 216, 0.4);
    outline-offset: 4px;
}

.info-panel p {
    margin: 0;
    color: var(--muted-text);
}

.link-button {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-weight: 600;
    color: var(--accent);
}

.link-button::before {
    content: '\2190';
    font-size: 0.95rem;
}

.link-button:hover {
    text-decoration: underline;
}

.learning-panel {
    padding: 24px;
}

.learning-layout {
    display: flex;
    flex-direction: column;
    gap: 24px;
}

.learning-content {
    display: flex;
    flex-direction: column;
    gap: 24px;
}

.learning-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
}

.learning-heading {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.learning-heading h2 {
    margin: 0;
    font-size: 1.5rem;
    color: #2d4a87;
}

.learning-return {
    margin: 0;
    font-size: 0.9rem;
    color: var(--muted-text);
}

.learning-meta {
    margin: 0;
    font-size: 0.95rem;
    color: var(--muted-text);
}

.chat-toggle-button {
    position: fixed;
    right: 24px;
    right: calc(24px + env(safe-area-inset-right));
    bottom: 24px;
    bottom: calc(24px + env(safe-area-inset-bottom));
    display: inline-flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
    border: 1px solid var(--border-color);
    background: #ffffff;
    color: #243b6b;
    border-radius: 999px;
    padding: 12px 20px;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 10px 24px rgba(63, 106, 216, 0.25);
    transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease, opacity 0.2s ease;
    z-index: 1200;
}

.chat-toggle-button:hover {
    transform: translateY(-1px);
    box-shadow: 0 14px 32px rgba(63, 106, 216, 0.3);
    background: #f0f4ff;
}

.chat-toggle-button:focus-visible {
    outline: 3px solid rgba(63, 106, 216, 0.35);
    outline-offset: 3px;
}

.chat-toggle-button[aria-expanded="true"] {
    background: var(--accent);
    color: #fff;
    border-color: transparent;
    box-shadow: 0 12px 30px rgba(63, 106, 216, 0.35);
}

body.chat-sidebar-open .chat-toggle-button {
    opacity: 0;
    pointer-events: none;
    transform: translateY(12px);
}

.chat-toggle-icon {
    font-size: 1.1rem;
    line-height: 1;
}

.chat-toggle-button[aria-expanded="true"] .chat-toggle-icon {
    color: inherit;
}

.learning-content h3 {
    margin-top: 24px;
    margin-bottom: 12px;
    color: #2d4a87;
}

.goals ul {
    margin: 12px 0 0;
    padding-left: 18px;
}

.goals li {
    margin-bottom: 6px;
}

.explanation-body,
.exercises ol {
    background: #f4f7ff;
    border-radius: 12px;
    padding: 16px;
    border: 1px solid rgba(63, 106, 216, 0.1);
}

.exercises ol {
    list-style: decimal;
    margin: 0;
}

.exercises li {
    margin-bottom: 18px;
    padding-bottom: 12px;
    border-bottom: 1px dashed var(--border-color);
}

.exercises li:last-child {
    border-bottom: none;
    padding-bottom: 0;
}

.exercises h4 {
    margin: 0 0 8px;
    color: #1f3561;
}

.exercises details {
    margin-top: 8px;
}

.exercises summary {
    cursor: pointer;
    color: var(--accent);
}

.tutor-chat {
    background: #fffaf0;
    border: 1px solid #fcd9a3;
    display: flex;
    flex-direction: column;
    gap: 12px;
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(92vw, 420px);
    max-width: 100%;
    height: 100dvh;
    padding: calc(20px + env(safe-area-inset-top)) 20px calc(24px + env(safe-area-inset-bottom));
    border-radius: 0;
    border-left: 1px solid #fcd9a3;
    border-right: none;
    border-top: none;
    border-bottom: none;
    box-shadow: -18px 0 36px rgba(15, 23, 42, 0.25);
    transform: translateX(100%);
    transition: transform 0.3s ease;
    z-index: 1100;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.tutor-chat.is-open {
    transform: translateX(0);
}

@media (min-width: 1200px) {
    .tutor-chat {
        width: 460px;
    }
}

body.chat-desktop .app-main--learning {
    max-width: 1360px;
}

body.chat-desktop .learning-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(320px, 380px);
    gap: 32px;
    align-items: flex-start;
}

body.chat-desktop .learning-panel {
    margin: 0;
}

body.chat-desktop .tutor-chat {
    position: sticky;
    top: 24px;
    right: auto;
    bottom: auto;
    width: 100%;
    max-width: none;
    height: auto;
    max-height: calc(100vh - 64px);
    padding: 24px;
    border-radius: 16px;
    border: 1px solid #fcd9a3;
    box-shadow: var(--shadow);
    transform: none;
}

body.chat-desktop .tutor-chat.is-open {
    transform: none;
}

body.chat-desktop .tutor-chat.is-expanded {
    position: fixed;
    top: max(16px, calc(env(safe-area-inset-top) + 16px));
    right: max(16px, calc(env(safe-area-inset-right) + 16px));
    bottom: max(16px, calc(env(safe-area-inset-bottom) + 16px));
    left: max(16px, calc(env(safe-area-inset-left) + 16px));
    width: auto;
    max-width: none;
    height: auto;
    max-height: none;
    padding: calc(28px + env(safe-area-inset-top)) calc(32px + env(safe-area-inset-right)) calc(32px + env(safe-area-inset-bottom)) calc(32px + env(safe-area-inset-left));
    border-radius: 24px;
    box-shadow: 0 30px 80px rgba(15, 23, 42, 0.45);
    z-index: 1600;
    overflow-y: auto;
}

body.chat-desktop .chat-toggle-button,
body.chat-desktop .chat-close-button {
    display: none !important;
}

body.chat-desktop .chat-overlay {
    display: none;
}

.chat-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.45);
    z-index: 1090;
}

.chat-overlay.is-active {
    display: block;
}

body.chat-desktop .chat-expand-button {
    display: inline-flex;
}

body.chat-desktop.chat-expanded .chat-overlay.is-active {
    display: block !important;
    z-index: 1500;
}

body.chat-expanded {
    overflow: hidden;
}

body.chat-sidebar-open {
    overflow: hidden;
}

.chat-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
}

.chat-header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.chat-header h2 {
    margin: 0;
}

.chat-expand-button {
    display: none;
    align-items: center;
    gap: 6px;
    background: none;
    border: 1px solid transparent;
    color: #8b5c11;
    font-weight: 600;
    cursor: pointer;
    padding: 4px 12px;
    border-radius: 999px;
    font-size: 0.9rem;
    transition: background 0.2s ease, color 0.2s ease;
}

.chat-expand-button:hover {
    background: rgba(245, 199, 125, 0.2);
}

.chat-expand-button:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.chat-expand-button[aria-pressed="true"] {
    background: rgba(245, 199, 125, 0.4);
    color: #5c3b05;
}

.chat-expand-icon {
    font-size: 1rem;
    line-height: 1;
}

.chat-expand-button[aria-pressed="true"] .chat-expand-icon {
    transform: rotate(-45deg);
}

.chat-close-button {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: none;
    border: none;
    color: #8b5c11;
    font-weight: 600;
    cursor: pointer;
    padding: 4px 10px;
    border-radius: 999px;
    font-size: 0.9rem;
}

.chat-close-button::before {
    content: '\2715';
    font-size: 0.85rem;
}

.chat-close-button:hover {
    background: rgba(245, 199, 125, 0.25);
}

.chat-close-button:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.chat-header-actions .chat-close-button {
    flex-shrink: 0;
}

.chat-description {
    margin: 0;
    font-size: 0.9rem;
    color: #8b5c11;
}

.chat-status {
    margin: 0;
    font-size: 0.9rem;
    color: #8b5c11;
}

.chat-status.is-error {
    color: #b45309;
    background: rgba(245, 158, 11, 0.12);
    border: 1px solid rgba(245, 158, 11, 0.35);
    border-radius: 8px;
    padding: 8px 12px;
}

.chat-history {
    flex: 1 1 auto;
    min-height: 260px;
    overflow-y: auto;
    background: #fff;
    border-radius: 12px;
    border: 1px solid #f5c77d;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.chat-bubble {
    padding: 10px 12px;
    border-radius: 12px;
    max-width: 100%;
    white-space: pre-wrap;
    word-break: break-word;
    line-height: 1.5;
}

.chat-bubble.user {
    align-self: flex-end;
    background: #dbeafe;
    color: #1e3a8a;
}

.chat-bubble.assistant {
    align-self: flex-start;
    background: #fef3c7;
    color: #7c3e00;
}

.chat-bubble.system {
    align-self: center;
    background: #e5e7eb;
    color: #374151;
    font-size: 0.85rem;
}

.chat-form {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.chat-form textarea {
    resize: vertical;
    min-height: 88px;
    border-radius: 10px;
    border: 1px solid #e5b35e;
    padding: 10px;
    font-size: 1rem;
    font-family: inherit;
}

.chat-form button {
    align-self: stretch;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 999px;
    padding: 10px 24px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s ease, transform 0.2s ease;
}

.chat-form button:hover {
    background: #2e55c7;
    transform: translateY(-1px);
}

.app-footer {
    text-align: center;
    color: var(--muted-text);
    font-size: 0.85rem;
    padding: 24px 0 32px;
}

@media (max-width: 600px) {
    .app-header {
        padding: 20px 12px;
    }

    .panel {
        padding: 20px;
    }

    .learning-header {
        flex-direction: column;
        align-items: stretch;
    }

    .chat-toggle-button {
        right: 16px;
        right: calc(16px + env(safe-area-inset-right));
        bottom: 16px;
        bottom: calc(16px + env(safe-area-inset-bottom));
        padding: 12px 18px;
        font-size: 0.9rem;
    }
