:root {
    --bg: #f6fbff;
    --panel: #ffffff;
    --ink: #38424a;
    --muted: #65737d;
    --line: #cfe5f1;
    --blue: #3f9fcb;
    --blue-dark: #24799f;
    --yellow: #ffd166;
    --orange: #ff9f1c;
    --green: #64b96a;
    --red: #e85d75;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    color: var(--ink);
    font-family: "UD Digital Kyokasho-tai-R", "Hiragino Kaku Gothic ProN", "Meiryo", sans-serif;
    background:
        radial-gradient(700px 360px at 15% 8%, #fff3c4 0%, transparent 62%),
        radial-gradient(760px 420px at 92% 12%, #cdeeff 0%, transparent 64%),
        linear-gradient(145deg, #f8fdff 0%, #eaf8ff 48%, #fff8ee 100%);
}

button,
a {
    font: inherit;
    -webkit-tap-highlight-color: transparent;
}

button {
    cursor: pointer;
}

.app {
    width: min(1180px, 100%);
    margin: 0 auto;
    padding: 16px;
}

.screen {
    display: none;
}

.screen.active {
    display: block;
}

.hero {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(280px, 420px);
    gap: 18px;
    align-items: stretch;
    min-height: 360px;
    padding: 30px 0 18px;
}

.hero-copy {
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.eyebrow {
    margin: 0 0 6px;
    color: var(--blue-dark);
    font-size: 0.8rem;
    font-weight: bold;
    letter-spacing: 0;
}

h1,
h2,
h3,
p {
    overflow-wrap: anywhere;
}

h1 {
    margin: 0;
    font-size: clamp(2rem, 7vw, 4.4rem);
    line-height: 1.08;
    color: #263f4d;
}

.subtitle {
    max-width: 620px;
    margin: 16px 0 0;
    color: var(--muted);
    font-size: clamp(1rem, 2.8vw, 1.25rem);
    line-height: 1.7;
}

.guide-box,
.panel,
.stage-panel,
.howto-item {
    border: 2px solid var(--line);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.9);
    box-shadow: 0 12px 26px rgba(42, 114, 150, 0.12);
}

.guide-box {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 18px;
    align-self: center;
}

.guide-img {
    width: 104px;
    height: 104px;
    object-fit: contain;
    flex: 0 0 auto;
}

.guide-talk {
    line-height: 1.55;
    font-weight: bold;
    color: #315469;
}

.howto-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 12px;
}

.howto-item {
    padding: 14px;
}

.step-mark {
    display: inline-grid;
    place-items: center;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: var(--yellow);
    color: #5d4614;
    font-weight: bold;
    margin-bottom: 8px;
}

.howto-item strong {
    display: block;
    font-size: 1.05rem;
}

.howto-item p {
    margin: 6px 0 0;
    color: var(--muted);
    line-height: 1.45;
    font-size: 0.92rem;
}

.home-actions {
    display: flex;
    justify-content: center;
    gap: 10px;
    flex-wrap: wrap;
    margin-top: 18px;
}

.primary-button,
.tool-button,
.secondary-link {
    min-height: 44px;
    border-radius: 8px;
    padding: 10px 18px;
    border: 0;
    font-weight: bold;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.primary-button {
    background: var(--blue);
    color: #fff;
    box-shadow: 0 8px 16px rgba(63, 159, 203, 0.24);
}

.primary-button.small {
    min-height: 40px;
    padding-inline: 20px;
}

.tool-button {
    background: #e9f5fb;
    color: #24566f;
    border: 2px solid #bfdeeb;
}

.tool-button.danger {
    background: #fff0f3;
    border-color: #f5b8c3;
    color: #a72f46;
}

.tool-button.point {
    background: #fff4cf;
    border-color: #f3c95d;
    color: #7a5310;
}

.tool-button:disabled {
    opacity: 0.65;
    cursor: default;
}

.secondary-link {
    background: #fff;
    color: #426170;
    border: 2px solid var(--line);
}

.secondary-link.compact {
    padding-inline: 12px;
}

.lab-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    margin-bottom: 12px;
}

.lab-header h1 {
    font-size: clamp(1.6rem, 4vw, 2.6rem);
}

.run-controls {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.user-switch {
    min-height: 40px;
    border-radius: 8px;
    padding: 6px 10px;
    border: 2px solid #bfdeeb;
    background: #fff;
    color: #24566f;
    display: inline-flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    line-height: 1.1;
    font-weight: bold;
}

.user-switch-label {
    color: var(--muted);
    font-size: 0.68rem;
}

.user-switch strong {
    max-width: 90px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.9rem;
}

.mode-badge {
    min-height: 40px;
    border-radius: 8px;
    padding: 8px 12px;
    background: #fff7e8;
    border: 2px solid #f2c46b;
    color: #74500c;
    font-weight: bold;
    display: inline-flex;
    align-items: center;
}

.quiz-panel {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(260px, 360px);
    gap: 12px;
    align-items: center;
    border: 2px solid #f2c46b;
    border-radius: 8px;
    background: #fff9e8;
    padding: 12px;
    margin-bottom: 12px;
    box-shadow: 0 10px 20px rgba(146, 107, 21, 0.12);
}

.quiz-panel.hidden {
    display: none;
}

.quiz-panel h2,
.quiz-panel p {
    margin: 0;
}

.quiz-panel h2 {
    color: #67480e;
    font-size: 1.2rem;
}

.quiz-count {
    font-weight: bold;
    font-size: 0.9rem;
}

.quiz-panel p {
    color: #6f5c34;
    line-height: 1.5;
}

.quiz-actions {
    display: grid;
    gap: 8px;
}

.quiz-result {
    min-height: 40px;
    border-radius: 8px;
    border: 1px solid #efd185;
    background: #fff;
    color: #6f5c34;
    font-weight: bold;
    padding: 8px 10px;
}

.quiz-result.clear {
    background: #e9f8e7;
    border-color: #91d28d;
    color: #27652d;
}

.quiz-nav {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 6px;
    border: 1px dashed #e5c86d;
    border-radius: 8px;
    padding: 8px;
    background: #fffdf4;
}

.quiz-nav.hidden {
    display: none;
}

.quiz-nav button {
    min-height: 34px;
    border: 2px solid #e5c86d;
    border-radius: 8px;
    background: #fff;
    color: #74500c;
    font-weight: bold;
}

.quiz-nav button.active {
    background: #ffcf66;
    border-color: #d79316;
    color: #533704;
}

.quiz-nav button.just-cleared {
    animation: quizCheckPop 780ms ease both;
}

.quiz-nav button.cleared::after {
    content: "✓";
    display: inline-grid;
    place-items: center;
    width: 17px;
    height: 17px;
    margin-left: 4px;
    border-radius: 50%;
    background: #e9f8e7;
    color: #27652d;
    font-size: 0.78rem;
    border: 1px solid #91d28d;
}

.quiz-next-ready {
    animation: nextReady 1200ms ease-in-out 2;
}

.user-select-dialog {
    position: fixed;
    inset: 0;
    z-index: 10000;
    display: grid;
    place-items: center;
    padding: 16px;
    background: rgba(38, 63, 77, 0.58);
}

.user-select-dialog.hidden {
    display: none;
}

.user-select-card {
    width: min(420px, 100%);
    border-radius: 8px;
    border: 2px solid var(--line);
    background: #fff;
    box-shadow: 0 18px 42px rgba(42, 114, 150, 0.22);
    padding: 18px;
}

.user-select-card h2 {
    margin: 0 0 12px;
    color: #263f4d;
}

.user-choice-list {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
}

.user-choice-list button {
    min-height: 44px;
    border: 2px solid #bfdeeb;
    border-radius: 8px;
    background: #e9f5fb;
    color: #24566f;
    font-weight: bold;
}

.user-choice-list button.active {
    background: #fff7e8;
    border-color: var(--orange);
    color: #704a0a;
}

.new-user-form {
    border-top: 2px dashed #d5e8f1;
    margin-top: 14px;
    padding-top: 12px;
}

.new-user-form label {
    display: block;
    margin-bottom: 6px;
    color: var(--muted);
    font-size: 0.86rem;
    font-weight: bold;
}

.new-user-form > div {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
}

.new-user-form input {
    min-height: 40px;
    border: 2px solid var(--line);
    border-radius: 8px;
    padding: 8px 10px;
    font: inherit;
}

@keyframes clearPop {
    0% {
        opacity: 0;
        transform: scale(0.72) rotate(-8deg);
    }
    58% {
        opacity: 1;
        transform: scale(1.08) rotate(3deg);
    }
    78% {
        transform: scale(0.96) rotate(-2deg);
    }
    100% {
        opacity: 1;
        transform: scale(1) rotate(0deg);
    }
}

@keyframes confettiFall {
    0% {
        opacity: 0;
        transform: translateY(-20px) rotate(0deg);
    }
    18% {
        opacity: 1;
    }
    100% {
        opacity: 0;
        transform: translateY(310px) translateX(var(--drift)) rotate(var(--rotate));
    }
}

@keyframes rewardToast {
    0% {
        opacity: 0;
        transform: translateY(-6px);
    }
    12%,
    78% {
        opacity: 1;
        transform: translateY(0);
    }
    100% {
        opacity: 0;
        transform: translateY(-4px);
    }
}

@keyframes quizCheckPop {
    0%,
    100% {
        transform: scale(1);
    }
    45% {
        transform: scale(1.14);
    }
}

@keyframes nextReady {
    0%,
    100% {
        box-shadow: 0 8px 16px rgba(63, 159, 203, 0.24);
    }
    50% {
        box-shadow: 0 0 0 5px rgba(255, 209, 102, 0.55), 0 8px 18px rgba(63, 159, 203, 0.28);
    }
}

.workspace {
    display: grid;
    grid-template-columns: 170px minmax(360px, 1fr) 330px;
    gap: 12px;
    align-items: start;
}

.panel,
.stage-panel {
    padding: 12px;
}

.panel h2,
.panel h3 {
    margin: 0 0 10px;
    color: #315469;
}

.panel h2 {
    font-size: 1.15rem;
}

.panel h3 {
    font-size: 0.95rem;
}

.asset-palette {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
}

.asset-button {
    min-height: 68px;
    border: 2px solid var(--line);
    border-radius: 8px;
    background: #f9fdff;
    color: var(--ink);
    display: grid;
    grid-template-columns: 42px 1fr;
    gap: 8px;
    align-items: center;
    padding: 8px;
    font-weight: bold;
    text-align: left;
}

.asset-button.active {
    border-color: var(--orange);
    background: #fff7e8;
}

.asset-button img {
    width: 42px;
    height: 42px;
    object-fit: contain;
}

.hint {
    margin: 10px 0 0;
    color: var(--muted);
    font-size: 0.85rem;
    line-height: 1.45;
}

.stage-panel {
    min-width: 0;
}

.stage-frame {
    position: relative;
}

#stage-canvas {
    display: block;
    width: 100%;
    aspect-ratio: 3 / 2;
    border: 3px solid #b8dff0;
    border-radius: 8px;
    background: #f8fdff;
    touch-action: none;
}

.clear-celebration {
    position: absolute;
    inset: 0;
    pointer-events: none;
    overflow: hidden;
    border-radius: 8px;
    display: grid;
    place-items: center;
    opacity: 0;
}

.clear-celebration.active {
    opacity: 1;
}

.clear-pop {
    width: min(210px, 46%);
    aspect-ratio: 1;
    border-radius: 50%;
    display: grid;
    place-items: center;
    align-content: center;
    border: 7px solid #e85d75;
    background: rgba(255, 255, 255, 0.94);
    color: #d33855;
    font-size: clamp(1.4rem, 4vw, 2.2rem);
    font-weight: bold;
    line-height: 1;
    box-shadow: 0 14px 28px rgba(216, 73, 102, 0.22);
    transform: scale(0.72) rotate(-7deg);
}

.clear-celebration.active .clear-pop {
    animation: clearPop 900ms cubic-bezier(0.18, 1.35, 0.38, 1) both;
}

.confetti-piece {
    position: absolute;
    top: -20px;
    left: var(--x);
    width: var(--size);
    height: var(--size);
    border-radius: 3px;
    background: var(--color);
    opacity: 0;
    transform: translateY(-20px) rotate(0deg);
}

.clear-celebration.active .confetti-piece {
    animation: confettiFall var(--duration) ease-out var(--delay) both;
}

.stage-controls {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
    margin-top: 8px;
}

.reward-toast {
    min-height: 0;
    max-height: 0;
    margin-top: 0;
    overflow: hidden;
    border-radius: 8px;
    background: #fff4cf;
    border: 0 solid #f3c95d;
    color: #7a5310;
    font-weight: bold;
    text-align: center;
    transform: translateY(-6px);
    opacity: 0;
}

.reward-toast.active {
    max-height: 88px;
    margin-top: 8px;
    padding: 8px 10px;
    border-width: 2px;
    animation: rewardToast 2600ms ease both;
}

.status-text {
    margin-top: 8px;
    min-height: 42px;
    border-radius: 8px;
    background: #f0f9ff;
    border: 2px solid #cfebf7;
    color: #315469;
    font-weight: bold;
    display: flex;
    align-items: center;
    padding: 8px 10px;
}

.selected-card {
    display: grid;
    grid-template-columns: 58px 1fr;
    gap: 10px;
    align-items: center;
    padding: 10px;
    background: #f6fbff;
    border: 2px solid var(--line);
    border-radius: 8px;
    margin-bottom: 12px;
}

.selected-card img {
    width: 58px;
    height: 58px;
    object-fit: contain;
}

.selected-card h2 {
    margin: 0;
}

.edit-section {
    border-top: 2px dashed #d5e8f1;
    padding-top: 10px;
    margin-top: 10px;
}

.edit-section.disabled {
    opacity: 0.55;
}

.edit-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 8px;
}

.edit-heading h3 {
    margin: 0;
}

#size-value {
    min-width: 42px;
    border-radius: 8px;
    background: #eef8ff;
    border: 1px solid #cfe5f1;
    color: #315469;
    font-weight: bold;
    text-align: center;
    padding: 4px 6px;
}

.size-range {
    width: 100%;
    accent-color: var(--orange);
}

.size-presets {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
    margin-top: 8px;
}

.size-presets button {
    min-height: 36px;
    border: 2px solid #c7e2ee;
    border-radius: 8px;
    background: #f9fdff;
    color: #315469;
    font-weight: bold;
}

.size-presets button.active {
    border-color: var(--orange);
    background: #fff7e8;
}

.size-presets button:disabled {
    cursor: default;
}

.command-section {
    border-top: 2px dashed #d5e8f1;
    padding-top: 10px;
    margin-top: 10px;
}

.command-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 8px;
}

.command-buttons button {
    min-height: 34px;
    border: 2px solid #c7e2ee;
    border-radius: 8px;
    background: #f9fdff;
    color: #315469;
    font-size: 0.82rem;
    font-weight: bold;
    padding: 6px 8px;
}

.rule-command-groups,
.rule-command-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    width: 100%;
}

.rule-command-groups {
    margin-bottom: 6px;
}

.command-buttons .rule-group-button {
    background: #e9f5fb;
    border-color: #bfdeeb;
    color: #24566f;
}

.command-buttons .rule-group-button.active {
    background: #fff7e8;
    border-color: var(--orange);
    color: #704a0a;
}

.rule-command-actions {
    padding: 8px;
    border: 1px dashed #cfe5f1;
    border-radius: 8px;
    background: #f7fbfd;
}

.command-list {
    display: grid;
    gap: 6px;
    min-height: 36px;
}

.empty-list {
    align-items: center;
    color: #7a8790;
    font-size: 0.85rem;
    background: #f7fbfd;
    border: 1px dashed #cfe5f1;
    border-radius: 8px;
    padding: 8px;
}

.command-chip {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 6px;
    align-items: center;
    min-height: 36px;
    padding: 6px 8px;
    border-radius: 8px;
    background: #eef8ff;
    border: 1px solid #cfe5f1;
    color: #315469;
    font-size: 0.86rem;
    font-weight: bold;
}

.command-chip button {
    border: 0;
    border-radius: 6px;
    background: #fff;
    color: #8d3c4b;
    font-weight: bold;
    padding: 4px 7px;
}

.panel-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    margin-top: 12px;
}

@media (max-width: 980px) {
    .workspace {
        grid-template-columns: 1fr;
    }

    .asset-palette {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    .asset-button {
        grid-template-columns: 38px 1fr;
    }
}

@media (max-width: 720px) {
    .app {
        padding: 10px;
    }

    .hero {
        grid-template-columns: 1fr;
        min-height: auto;
        padding-top: 12px;
    }

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

    .lab-header {
        align-items: flex-start;
        flex-direction: column;
    }

    .run-controls {
        width: 100%;
        justify-content: stretch;
    }

    .run-controls > * {
        flex: 1 1 0;
    }

    .user-switch {
        align-items: center;
    }

    .quiz-panel {
        grid-template-columns: 1fr;
    }

    .asset-palette {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .stage-controls {
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 6px;
    }

    .stage-controls .primary-button,
    .stage-controls .tool-button {
        min-height: 40px;
        padding: 8px 4px;
        font-size: 0.9rem;
    }

    .panel-actions {
        grid-template-columns: 1fr;
    }

    .user-choice-list {
        grid-template-columns: 1fr;
    }
}
