/* 007 iOS single-screen — soft spirit world */
*, *::before, *::after {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
}

html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
    background: #f3e6ea;
    color: #2b2226;
    font-family: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic", sans-serif;
}

body {
    position: fixed;
    inset: 0;
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
}

input {
    -webkit-user-select: text;
    user-select: text;
    touch-action: manipulation;
}

.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;
}

/* Start */
.start-overlay {
    position: fixed;
    inset: 0;
    z-index: 2000;
    display: grid;
    place-items: center;
    padding: max(20px, env(safe-area-inset-top)) 20px max(20px, env(safe-area-inset-bottom));
    background:
        radial-gradient(circle at 20% 20%, rgba(255, 154, 158, 0.55), transparent 45%),
        radial-gradient(circle at 80% 70%, rgba(52, 152, 219, 0.35), transparent 40%),
        linear-gradient(160deg, #2a1830 0%, #5a2a45 45%, #1a1020 100%);
}

.start-overlay.is-hidden { display: none; }

.start-card {
    width: min(320px, 100%);
    text-align: center;
    color: #fff;
    padding: 28px 22px;
    border-radius: 24px;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.22);
    backdrop-filter: blur(10px);
}

.start-card h1 {
    margin: 0 0 8px;
    font-size: 1.7rem;
    font-weight: 800;
}

.start-lead {
    margin: 0 0 22px;
    opacity: 0.9;
    font-size: 0.95rem;
}

/* App shell: no page scroll */
.app {
    height: 100vh;
    height: -webkit-fill-available;
    height: 100dvh;
    height: 100svh;
    display: flex;
    flex-direction: column;
    padding:
        max(8px, env(safe-area-inset-top))
        max(12px, env(safe-area-inset-right))
        max(8px, env(safe-area-inset-bottom))
        max(12px, env(safe-area-inset-left));
    gap: 8px;
    background: linear-gradient(160deg, #ff9a9e 0%, #fecfef 42%, #f7d9c4 100%);
}

.topbar {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

.topbar-text {
    flex: 1;
    min-width: 0;
}

.topbar h1 {
    margin: 0;
    font-size: 1.15rem;
    font-weight: 800;
    color: #c0392b;
    line-height: 1.2;
}

.topbar p {
    margin: 2px 0 0;
    font-size: 0.75rem;
    color: rgba(74, 48, 58, 0.75);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.icon-btn {
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.92);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12);
    font-size: 1rem;
    cursor: pointer;
    touch-action: manipulation;
    flex-shrink: 0;
}

.icon-btn:active { transform: scale(0.92); }

/* Mix block */
.mix {
    flex-shrink: 0;
    background: rgba(255, 255, 255, 0.94);
    border-radius: 16px;
    padding: 10px 10px 8px;
    box-shadow: 0 6px 18px rgba(120, 60, 80, 0.1);
}

.mix-row {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 4px;
}

.slot-unit {
    flex: 1;
    max-width: 92px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 0;
    border: none;
    background: transparent;
    cursor: pointer;
    touch-action: manipulation;
}

.slot-unit:active { transform: scale(0.94); }

.slot-unit.result-unit {
    cursor: default;
    max-width: 100px;
}

.slot-unit.result-unit:active { transform: none; }

.slot-swatch,
.result-slot {
    width: min(72px, 19vw);
    height: min(72px, 19vw);
    border-radius: 50%;
    display: grid;
    place-items: center;
}

.slot-swatch {
    border: 2px dashed #d2c4cb;
    background: #efe8eb;
}

.slot-unit.active .slot-swatch {
    border-style: solid;
    border-color: #2f9e6a;
}

.slot-label {
    width: 100%;
    min-height: 2.2em;
    font-size: 0.62rem;
    font-weight: 700;
    line-height: 1.15;
    text-align: center;
    color: #5c4c54;
    word-break: break-word;
}

.mix-op {
    flex-shrink: 0;
    width: 14px;
    text-align: center;
    font-weight: 800;
    color: #8a7782;
    line-height: min(72px, 19vw);
    font-size: 0.95rem;
}

.result-slot {
    width: min(80px, 21vw);
    height: min(80px, 21vw);
    border: 2px solid #f39c12;
    background: #fff;
    color: #e0a020;
    font-size: 1.25rem;
    font-weight: 800;
    box-shadow: 0 0 12px rgba(243, 156, 18, 0.25);
    view-transition-name: mix-result;
}

.result-slot.has-color { color: transparent; }

.mix-actions {
    display: grid;
    grid-template-columns: 0.7fr 1.3fr;
    gap: 8px;
    margin-top: 8px;
}

.game-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    padding: 10px 12px;
    border: none;
    border-radius: 999px;
    font-size: 0.95rem;
    font-weight: 700;
    cursor: pointer;
    touch-action: manipulation;
}

.game-btn:active {
    transform: scale(0.96);
    opacity: 0.9;
}

.game-btn.primary {
    background: #e74c3c;
    color: #fff;
    box-shadow: 0 4px 14px rgba(231, 76, 60, 0.3);
}

.game-btn.secondary {
    background: #ece4e8;
    color: #4a3a44;
}

.game-btn.ghost {
    background: #f4eef1;
    color: #6d5a64;
}

.game-btn:disabled {
    background: #ddd5d9;
    color: #999;
    box-shadow: none;
    cursor: not-allowed;
    transform: none;
    opacity: 1;
}

/* Tabs */
.tabs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    flex-shrink: 0;
}

.tab {
    min-height: 40px;
    border: none;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.35);
    color: #7a6670;
    font-size: 0.9rem;
    font-weight: 700;
    cursor: pointer;
    touch-action: manipulation;
}

.tab.is-on {
    background: #fff;
    color: #2b2226;
    box-shadow: 0 2px 8px rgba(120, 60, 80, 0.1);
}

.count {
    display: inline-block;
    min-width: 1.4em;
    padding: 0 0.35em;
    border-radius: 999px;
    background: #e74c3c;
    color: #fff;
    font-size: 0.75em;
}

/* Panels */
.panel {
    flex: 1;
    min-height: 0;
    background: rgba(255, 255, 255, 0.94);
    border-radius: 16px;
    padding: 8px;
    overflow: hidden;
    box-shadow: 0 6px 18px rgba(120, 60, 80, 0.08);
}

.panel[hidden] { display: none !important; }

#panelBook.is-on {
    flex: 0 0 auto;
    min-height: 0;
    overflow: hidden;
    align-self: stretch;
    padding: 4px;
}

.app.app--book #panelBook.is-on {
    margin-bottom: auto;
}

/* パレット: 4x4 = 16マス（初期12＋混色枠） */
#basicColors.spirit-grid {
    height: 100%;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-template-rows: repeat(4, minmax(0, 1fr));
    gap: 6px;
    overflow: hidden;
    touch-action: manipulation;
    overscroll-behavior: none;
}

#basicColors .spirit-item {
    aspect-ratio: auto;
    height: 100%;
    min-height: 0;
    touch-action: manipulation;
    cursor: pointer;
    -webkit-user-select: none;
    user-select: none;
}

#basicColors .spirit-item.is-empty {
    background: #f6f1f3;
    box-shadow: inset 0 0 0 1.5px dashed #d8cdd3;
    opacity: 0.95;
    pointer-events: none;
}

.spirit-grid {
    height: 100%;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-auto-rows: auto;
    align-content: start;
    gap: 8px;
    overflow: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
    padding: 2px;
}

/* 図鑑: パレット同様、枠内を行分割して重なり・スクロールなし */
#collectionGrid.spirit-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: repeat(var(--book-rows, 1), minmax(0, 1fr));
    grid-auto-rows: unset;
    gap: 6px;
    overflow: hidden;
    overscroll-behavior: none;
    touch-action: none;
    padding: 0;
    align-content: stretch;
    align-items: stretch;
}

.book-empty {
    grid-column: 1 / -1;
    margin: 0;
    padding: 16px 12px;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    color: #8a6670;
    font-size: 0.82rem;
    line-height: 1.5;
}

.spirit-item {
    position: relative;
    min-height: 0;
    min-width: 0;
    width: 100%;
    aspect-ratio: 2 / 1;
    height: auto;
    border: none;
    border-radius: 8px;
    padding: 2px 4px;
    cursor: pointer;
    touch-action: manipulation;
    display: grid;
    place-items: center;
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.45);
    background-clip: padding-box;
}

.spirit-item:active { transform: scale(0.96); }

.spirit-item .spirit-name {
    font-size: clamp(0.48rem, 2.2vw, 0.68rem);
    font-weight: 700;
    line-height: 1.1;
    text-align: center;
    word-break: break-word;
    max-height: 100%;
    overflow: hidden;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
}

/* 図鑑: 色面いっぱいで妖精が動く一枚カード（二重枠なし） */
.kobito-cell {
    position: relative;
    width: 100%;
    height: 100%;
    min-height: 0;
    max-height: 100%;
    border-radius: 14px;
    overflow: hidden;
    cursor: pointer;
    touch-action: manipulation;
    border: 2px solid rgba(255, 255, 255, 0.92);
    box-shadow: 0 2px 8px rgba(80, 50, 60, 0.1);
    isolation: isolate;
}

.kobito-cell:active {
    opacity: 0.92;
}

/* 互換: ステージはセル全体と同義（余白ゼロ） */
.kobito-stage {
    position: absolute;
    inset: 0;
    margin: 0;
    border-radius: 0;
    overflow: hidden;
    box-shadow: none;
    isolation: isolate;
}

.kobito-actor {
    position: absolute;
    left: 0;
    top: 0;
    width: var(--kobito-w, 36px);
    height: var(--kobito-h, 48px);
    transform-origin: center center;
    will-change: left, top, transform;
    pointer-events: none;
    z-index: 1;
}

#collectionGrid[data-rows="4"] {
    --kobito-w: 28px;
    --kobito-h: 36px;
    gap: 4px;
}

#collectionGrid[data-rows="5"],
#collectionGrid[data-rows="6"],
#collectionGrid[data-rows="7"],
#collectionGrid[data-rows="8"] {
    --kobito-w: 24px;
    --kobito-h: 30px;
    gap: 3px;
}

.kobito-name {
    position: absolute;
    left: 4px;
    right: 4px;
    bottom: 3px;
    z-index: 2;
    margin: 0;
    padding: 0;
    border-radius: 0;
    font-size: clamp(0.5rem, 2.2vw, 0.65rem);
    font-weight: 700;
    text-align: center;
    line-height: 1.15;
    color: #fff;
    background: none;
    backdrop-filter: none;
    text-shadow:
        0 1px 2px rgba(20, 12, 18, 0.75),
        0 0 6px rgba(20, 12, 18, 0.45);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    pointer-events: none;
}

#collectionGrid[data-rows="4"] .kobito-name,
#collectionGrid[data-rows="5"] .kobito-name,
#collectionGrid[data-rows="6"] .kobito-name,
#collectionGrid[data-rows="7"] .kobito-name,
#collectionGrid[data-rows="8"] .kobito-name {
    padding: 0;
    font-size: clamp(0.42rem, 1.8vw, 0.55rem);
    bottom: 2px;
    left: 2px;
    right: 2px;
}

.kobito-idle {
    width: 100%;
    height: 100%;
    transform-origin: center bottom;
    transform: rotate(var(--figure-tilt, 0deg)) scale(var(--figure-scale, 1));
}

.kobito-idle[data-motion="ember"] .kobito-svg {
    animation: svg-ember 0.82s ease-in-out infinite;
    animation-delay: var(--idle-delay, 0s);
}

.kobito-idle[data-motion="sunburst"] .kobito-svg {
    animation: svg-sunburst 1.05s ease-in-out infinite;
    animation-delay: var(--idle-delay, 0s);
}

.kobito-idle[data-motion="leaf"] .kobito-svg {
    animation: svg-leaf 1.6s ease-in-out infinite;
    animation-delay: var(--idle-delay, 0s);
}

.kobito-idle[data-motion="wave"] .kobito-svg {
    animation: svg-wave 2.2s ease-in-out infinite;
    animation-delay: var(--idle-delay, 0s);
}

.kobito-idle[data-motion="mystic"] .kobito-svg {
    animation: svg-mystic 2.6s ease-in-out infinite;
    animation-delay: var(--idle-delay, 0s);
}

.kobito-idle[data-motion="gloom"] .kobito-svg {
    animation: svg-gloom 3.2s ease-in-out infinite;
    animation-delay: var(--idle-delay, 0s);
}

.kobito-idle[data-motion="drift"] .kobito-svg {
    animation: svg-drift 2.4s ease-in-out infinite;
    animation-delay: var(--idle-delay, 0s);
}

.kobito-idle[data-motion="pulse"] .kobito-svg {
    animation: svg-pulse 1.35s ease-in-out infinite;
    animation-delay: var(--idle-delay, 0s);
}

.kobito-idle[data-archetype="breeze"] .kobito-wing,
.kobito-idle[data-archetype="spark"] .kobito-prop-star {
    animation: svg-wing 0.55s ease-in-out infinite alternate;
    transform-origin: center;
}

.kobito-idle[data-archetype="fierce"] .kobito-svg {
    animation: svg-ember 0.72s ease-in-out infinite;
    animation-delay: var(--idle-delay, 0s);
}

.kobito-idle[data-archetype="sage"] .kobito-svg {
    animation: svg-leaf 2.1s ease-in-out infinite;
    animation-delay: var(--idle-delay, 0s);
}

.kobito-idle[data-archetype="knight"] .kobito-svg {
    animation: svg-gloom 2.8s ease-in-out infinite;
    animation-delay: var(--idle-delay, 0s);
}

@keyframes svg-ember {
    0%, 100% { transform: translateY(0) scale(1); }
    40% { transform: translateY(-4px) scale(1.05); }
    60% { transform: translateY(-2px) scale(1.02); }
}

@keyframes svg-sunburst {
    0%, 100% { transform: rotate(-5deg) scale(1); }
    50% { transform: rotate(5deg) scale(1.07); }
}

@keyframes svg-leaf {
    0%, 100% { transform: rotate(-4deg); }
    50% { transform: rotate(4deg); }
}

@keyframes svg-wave {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-3px); }
}

@keyframes svg-mystic {
    0%, 100% { transform: translateY(0) rotate(0deg); opacity: 1; }
    50% { transform: translateY(-2px) rotate(3deg); opacity: 0.92; }
}

@keyframes svg-gloom {
    0%, 100% { transform: translateY(1px) scale(0.98); }
    50% { transform: translateY(0) scale(1); }
}

@keyframes svg-drift {
    0%, 100% { transform: translateY(-2px); }
    50% { transform: translateY(2px); }
}

@keyframes svg-pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.08); }
}

@keyframes svg-wing {
    0% { transform: rotate(-8deg) scale(1); }
    100% { transform: rotate(8deg) scale(1.05); }
}

.kobito-svg {
    display: block;
    width: 100%;
    height: 100%;
    /* 実色カード上でもシルエットが立つ白縁＋光彩 */
    filter:
        drop-shadow(0 0 1.5px #fff)
        drop-shadow(0 0 2.5px rgba(255, 255, 255, 0.95))
        drop-shadow(0 0 1px rgba(20, 12, 18, 0.35))
        drop-shadow(0 3px 4px rgba(20, 12, 18, 0.35));
}

.kobito-actor.is-moving .kobito-svg {
    filter:
        drop-shadow(0 0 1.5px #fff)
        drop-shadow(0 0 3px rgba(255, 255, 255, 0.9))
        drop-shadow(-3px 0 4px rgba(255, 255, 255, 0.25))
        drop-shadow(0 3px 4px rgba(20, 12, 18, 0.35));
}

/* Modal — Popover APIを使わず自前で中央固定（iOSで崩れない） */
.modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 2100;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px 18px calc(20px + var(--kb-offset, 0px));
    box-sizing: border-box;
    background: rgba(42, 24, 36, 0.48);
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
}

.modal-overlay.is-open {
    display: flex !important;
}

.name-card {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    width: min(300px, 100%);
    max-width: 100%;
    box-sizing: border-box;
    margin: 0 auto;
    background: #fffafb;
    border-radius: 22px;
    border: 1px solid rgba(255, 255, 255, 0.95);
    padding: 22px 20px 18px;
    text-align: center;
    box-shadow:
        0 12px 32px rgba(80, 40, 60, 0.22),
        0 2px 8px rgba(80, 40, 60, 0.1);
}

.detail-title {
    margin: 0 0 8px;
    font-size: 1.05rem;
    font-weight: 800;
    color: #2b2226;
    line-height: 1.3;
    text-align: center;
}

.spirit-swatch {
    width: 88px;
    height: 88px;
    border-radius: 50%;
    margin: 0 auto 12px;
    border: 3px solid rgba(255, 255, 255, 0.95);
    box-shadow:
        0 0 0 2px #2f9e6a,
        0 8px 18px rgba(80, 50, 60, 0.16);
    flex: 0 0 auto;
    align-self: center;
}

.sheet-lead,
.sheet-parents,
.sheet-meta {
    margin: 0 0 12px;
    color: #6d5a64;
    font-size: 0.8rem;
    line-height: 1.45;
    text-align: center;
    word-break: break-word;
}

.sheet-parents {
    font-weight: 700;
    color: #4a3842;
    font-size: 0.78rem;
}

.sheet-parents:empty,
.sheet-meta:empty {
    display: none;
}

#spiritName {
    display: block;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    box-sizing: border-box;
    min-height: 56px;
    padding: 16px 14px;
    border: 2px solid #e4d6dc;
    border-radius: 14px;
    font-size: 16px;
    line-height: 1.3;
    text-align: center;
    margin: 0 0 14px;
    outline: none;
    background: #fff;
    color: #2b2226;
    touch-action: manipulation;
    -webkit-appearance: none;
    appearance: none;
    -webkit-user-select: text;
    user-select: text;
    caret-color: #e74c3c;
}

.modal-overlay,
.name-card {
    touch-action: manipulation;
}

#spiritName:focus { border-color: #e74c3c; }

.sheet-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    width: 100%;
}

.sheet-actions .game-btn {
    width: 100%;
    min-height: 46px;
    padding: 11px 8px;
    border-radius: 999px;
    font-size: 0.9rem;
    line-height: 1.2;
    white-space: nowrap;
}

.toast {
    position: fixed;
    left: 50%;
    bottom: max(18px, calc(env(safe-area-inset-bottom) + 12px));
    transform: translateX(-50%);
    z-index: 1800;
    max-width: min(300px, calc(100vw - 28px));
    padding: 10px 14px;
    border-radius: 12px;
    background: rgba(40, 28, 36, 0.92);
    color: #fff;
    font-size: 0.85rem;
    font-weight: 700;
    text-align: center;
    pointer-events: none;
}

.toast[hidden] { display: none !important; }

@supports (view-transition-name: none) {
    ::view-transition-old(root),
    ::view-transition-new(root) {
        animation-duration: 0.25s;
    }
}

@media (max-height: 700px) {
    .app { gap: 6px; }
    .mix { padding: 8px; }
    .slot-swatch,
    .result-slot {
        width: min(64px, 17vw);
        height: min(64px, 17vw);
    }
    .result-slot {
        width: min(70px, 18vw);
        height: min(70px, 18vw);
    }
    .mix-op { line-height: min(64px, 17vw); }
    .slot-label { font-size: 0.58rem; min-height: 2em; }
    .game-btn { min-height: 40px; }
    .spirit-grid { gap: 4px; }
}

.spirit-grid[data-dense="1"] {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.spirit-grid[data-dense="2"] {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

@media (min-width: 640px) {
    .app {
        max-width: 430px;
        margin: 0 auto;
    }

    .name-card {
        width: min(300px, 100%);
        padding: 22px 20px 20px;
    }
}
