/* =====================================================
   京都恋物語 — Design System
   ===================================================== */

/* --- CSS Variables --- */
:root {
    /* Colors */
    --sakura-pink: hsl(350, 80%, 85%);
    --sakura-deep: hsl(340, 65%, 60%);
    --sakura-light: hsl(350, 90%, 92%);
    --kyoto-purple: hsl(280, 40%, 25%);
    --kyoto-purple-light: hsl(280, 35%, 40%);
    --gold: hsl(45, 90%, 65%);
    --gold-dim: hsl(45, 60%, 45%);
    --warm-white: hsl(40, 30%, 96%);
    --dark-bg: hsl(280, 30%, 12%);
    --dialog-bg: hsla(280, 40%, 10%, 0.85);
    --glass-bg: hsla(280, 30%, 15%, 0.6);
    --glass-border: hsla(350, 80%, 85%, 0.3);
    --text-primary: hsl(40, 30%, 95%);
    --text-secondary: hsl(350, 60%, 80%);
    --text-dim: hsla(40, 30%, 95%, 0.6);

    /* Typography */
    --font-serif: 'Noto Serif JP', serif;
    --font-round: 'Zen Maru Gothic', sans-serif;

    /* Spacing */
    --gap-xs: 4px;
    --gap-sm: 8px;
    --gap-md: 16px;
    --gap-lg: 24px;
    --gap-xl: 40px;

    /* Borders */
    --radius-sm: 8px;
    --radius-md: 16px;
    --radius-lg: 24px;
    --radius-full: 9999px;

    /* Shadows */
    --shadow-soft: 0 4px 24px hsla(280, 40%, 10%, 0.4);
    --shadow-glow: 0 0 20px hsla(350, 80%, 70%, 0.3);
    --shadow-deep: 0 8px 40px hsla(280, 40%, 5%, 0.6);
}

/* --- Reset & Base --- */
*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html,
body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-family: var(--font-round);
    background: var(--dark-bg);
    color: var(--text-primary);
    -webkit-font-smoothing: antialiased;
    user-select: none;
    cursor: default;
}

/* --- Screen System --- */
.screen {
    position: fixed;
    inset: 0;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.8s ease, visibility 0.8s ease;
    z-index: 1;
}

.screen.active {
    opacity: 1;
    visibility: visible;
    z-index: 10;
}

/* =====================================================
   TITLE SCREEN
   ===================================================== */
.title-bg {
    position: absolute;
    inset: 0;
    background: linear-gradient(135deg,
            hsl(280, 40%, 12%) 0%,
            hsl(320, 35%, 18%) 40%,
            hsl(350, 45%, 22%) 70%,
            hsl(280, 40%, 15%) 100%);
    z-index: 0;
}

.title-bg::after {
    content: '';
    position: absolute;
    inset: 0;
    background: radial-gradient(ellipse at 50% 40%,
            hsla(350, 80%, 70%, 0.15) 0%,
            transparent 60%);
}

.title-content {
    position: relative;
    z-index: 2;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    gap: var(--gap-lg);
    animation: titleFadeIn 2s ease-out;
}

.title-text {
    text-align: center;
}

.title-jp {
    display: block;
    font-family: var(--font-serif);
    font-size: clamp(2.5rem, 8vw, 5rem);
    font-weight: 700;
    color: var(--sakura-pink);
    text-shadow: 0 0 40px hsla(350, 80%, 70%, 0.4),
        0 2px 4px hsla(280, 40%, 10%, 0.5);
    letter-spacing: 0.15em;
    animation: titleGlow 3s ease-in-out infinite alternate;
}

.title-sub {
    display: block;
    font-family: var(--font-round);
    font-size: clamp(0.8rem, 2.5vw, 1.2rem);
    font-weight: 300;
    color: var(--gold);
    letter-spacing: 0.4em;
    text-transform: uppercase;
    margin-top: var(--gap-sm);
}

.title-desc {
    font-family: var(--font-serif);
    font-size: clamp(0.9rem, 2vw, 1.1rem);
    color: var(--text-secondary);
    font-weight: 300;
    opacity: 0.8;
}

/* Start Button */
.btn-start {
    position: relative;
    padding: 14px 48px;
    border: 2px solid var(--sakura-deep);
    border-radius: var(--radius-full);
    background: hsla(340, 65%, 50%, 0.15);
    color: var(--sakura-pink);
    font-family: var(--font-serif);
    font-size: 1.1rem;
    font-weight: 500;
    letter-spacing: 0.3em;
    cursor: pointer;
    overflow: hidden;
    transition: all 0.4s ease;
    backdrop-filter: blur(10px);
    margin-top: var(--gap-md);
}

.btn-start:hover {
    background: hsla(340, 65%, 50%, 0.3);
    border-color: var(--sakura-pink);
    transform: translateY(-2px);
    box-shadow: var(--shadow-glow);
}

.btn-start:active {
    transform: translateY(0);
}

.btn-glow {
    position: absolute;
    inset: -2px;
    border-radius: inherit;
    background: linear-gradient(45deg, transparent, hsla(350, 80%, 70%, 0.3), transparent);
    animation: btnGlowSweep 3s linear infinite;
    z-index: -1;
}

/* =====================================================
   GAME SCREEN
   ===================================================== */

/* Background */
.game-bg {
    position: absolute;
    inset: 0;
    z-index: 0;
}

.game-bg img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    filter: brightness(0.7) saturate(1.1);
}

/* Sakura Canvas */
#sakuraCanvas {
    position: absolute;
    inset: 0;
    z-index: 1;
    pointer-events: none;
}

/* --- Character --- */
.character-container {
    position: absolute;
    bottom: 25px;
    left: 50%;
    transform: translateX(-50%);
    width: clamp(300px, 50vw, 600px);
    height: 80vh;
    z-index: 5;
    display: flex;
    align-items: flex-end;
    justify-content: center;
}

.character-img {
    position: absolute;
    bottom: 0;
    max-height: 90%;
    max-width: 100%;
    object-fit: contain;
    opacity: 0;
    transition: opacity 0.6s ease;
    filter: drop-shadow(0 4px 30px hsla(280, 40%, 10%, 0.5));
    pointer-events: none;
    mix-blend-mode: multiply;
}

.character-img.active {
    opacity: 1;
}

/* Touch Zones */
.touch-zone {
    position: absolute;
    border-radius: 50%;
    cursor: pointer;
    z-index: 6;
    transition: background 0.3s ease;
    /* positions set dynamically by JS */
    display: none;
    /* hidden until JS calculates positions */
}

.touch-zone.positioned {
    display: block;
}

.touch-zone:hover {
    background: hsla(350, 80%, 85%, 0.15);
}

.touch-zone:active {
    background: hsla(350, 80%, 85%, 0.25);
}

.touch-ripple {
    position: absolute;
    inset: 0;
    border-radius: 50%;
    pointer-events: none;
}

.touch-zone.rippling .touch-ripple {
    animation: touchRipple 0.8s ease-out;
}

/* --- Affinity Panel --- */
.affinity-panel {
    position: absolute;
    top: var(--gap-lg);
    right: var(--gap-lg);
    z-index: 20;
    display: flex;
    align-items: center;
    gap: var(--gap-sm);
    padding: 10px 18px;
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-full);
    backdrop-filter: blur(20px);
    box-shadow: var(--shadow-soft);
}

.affinity-icon {
    font-size: 1.4rem;
    animation: heartbeat 1.5s ease-in-out infinite;
}

.affinity-bar-container {
    width: 120px;
    height: 8px;
    background: hsla(350, 30%, 30%, 0.5);
    border-radius: var(--radius-full);
    overflow: hidden;
}

.affinity-bar {
    height: 100%;
    background: linear-gradient(90deg, var(--sakura-deep), var(--sakura-pink), var(--gold));
    border-radius: var(--radius-full);
    transition: width 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
    box-shadow: 0 0 10px hsla(350, 80%, 70%, 0.4);
}

.affinity-value {
    font-family: var(--font-round);
    font-size: 0.9rem;
    font-weight: 700;
    color: var(--sakura-pink);
    min-width: 28px;
    text-align: right;
}

/* --- Status Bar --- */
.status-bar {
    position: absolute;
    top: var(--gap-lg);
    left: var(--gap-lg);
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--gap-md);
    padding: 8px 16px;
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-full);
    backdrop-filter: blur(20px);
    box-shadow: var(--shadow-soft);
}

.status-left {
    display: flex;
    align-items: center;
    gap: var(--gap-sm);
}

.status-indicator {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: hsl(0, 60%, 50%);
    transition: background 0.4s ease;
}

.status-indicator.connected {
    background: hsl(140, 70%, 50%);
    box-shadow: 0 0 8px hsla(140, 70%, 50%, 0.5);
    animation: statusPulse 2s ease-in-out infinite;
}

.status-indicator.connecting {
    background: hsl(45, 90%, 55%);
    animation: statusPulse 1s ease-in-out infinite;
}

.status-text {
    font-size: 0.8rem;
    font-weight: 400;
    color: var(--text-dim);
}

/* Mic Button */
.btn-icon {
    width: 38px;
    height: 38px;
    border: none;
    border-radius: 50%;
    background: hsla(350, 60%, 50%, 0.2);
    color: var(--sakura-pink);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
}

.btn-icon:hover:not(:disabled) {
    background: hsla(350, 60%, 50%, 0.4);
    transform: scale(1.08);
}

.btn-icon:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.btn-icon.active {
    background: hsla(350, 70%, 55%, 0.5);
    box-shadow: 0 0 15px hsla(350, 80%, 60%, 0.4);
    animation: micPulse 1.5s ease-in-out infinite;
}

.btn-icon svg {
    width: 20px;
    height: 20px;
    fill: currentColor;
}

/* --- Dialog Box --- */
.dialog-box {
    position: absolute;
    bottom: var(--gap-xl);
    left: 50%;
    transform: translateX(-50%);
    width: min(85vw, 700px);
    z-index: 20;
    padding: var(--gap-md) var(--gap-lg);
    background: var(--dialog-bg);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-md);
    backdrop-filter: blur(25px);
    box-shadow: var(--shadow-deep);
    min-height: 100px;
}

.dialog-name {
    font-family: var(--font-serif);
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--gold);
    margin-bottom: var(--gap-xs);
    letter-spacing: 0.1em;
}

.dialog-text {
    font-family: var(--font-serif);
    font-size: 1rem;
    line-height: 1.8;
    color: var(--text-primary);
    min-height: 1.8em;
}

.dialog-user-text {
    font-size: 0.8rem;
    color: var(--text-dim);
    margin-top: var(--gap-sm);
    font-style: italic;
    min-height: 0;
}

.dialog-user-text:empty {
    display: none;
}

/* --- Touch Toast --- */
.touch-toast {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 30;
    padding: 10px 24px;
    background: hsla(350, 80%, 75%, 0.2);
    border: 1px solid hsla(350, 80%, 80%, 0.3);
    border-radius: var(--radius-full);
    backdrop-filter: blur(15px);
    color: var(--sakura-pink);
    font-family: var(--font-serif);
    font-size: 0.95rem;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease, transform 0.3s ease;
}

.touch-toast.visible {
    opacity: 1;
    transform: translate(-50%, -60%);
}

/* --- Audio Visualizer --- */
.audio-viz {
    position: absolute;
    bottom: 160px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 15;
    display: flex;
    gap: 4px;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.audio-viz.active {
    opacity: 1;
}

.viz-bar {
    width: 4px;
    height: 20px;
    background: var(--sakura-pink);
    border-radius: 2px;
    animation: vizBounce 0.6s ease-in-out infinite alternate;
}

.viz-bar:nth-child(1) {
    animation-delay: 0s;
}

.viz-bar:nth-child(2) {
    animation-delay: 0.1s;
    height: 28px;
}

.viz-bar:nth-child(3) {
    animation-delay: 0.2s;
    height: 36px;
}

.viz-bar:nth-child(4) {
    animation-delay: 0.3s;
    height: 28px;
}

.viz-bar:nth-child(5) {
    animation-delay: 0.4s;
}

/* =====================================================
   ANIMATIONS
   ===================================================== */
@keyframes titleFadeIn {
    0% {
        opacity: 0;
        transform: translateY(30px);
    }

    100% {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes titleGlow {
    0% {
        text-shadow: 0 0 30px hsla(350, 80%, 70%, 0.3), 0 2px 4px hsla(280, 40%, 10%, 0.5);
    }

    100% {
        text-shadow: 0 0 50px hsla(350, 80%, 70%, 0.5), 0 2px 4px hsla(280, 40%, 10%, 0.5);
    }
}

@keyframes btnGlowSweep {
    0% {
        transform: translateX(-100%) rotate(45deg);
    }

    100% {
        transform: translateX(200%) rotate(45deg);
    }
}

@keyframes heartbeat {

    0%,
    100% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.2);
    }
}

@keyframes statusPulse {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.5;
    }
}

@keyframes micPulse {

    0%,
    100% {
        box-shadow: 0 0 10px hsla(350, 80%, 60%, 0.3);
    }

    50% {
        box-shadow: 0 0 25px hsla(350, 80%, 60%, 0.6);
    }
}

@keyframes touchRipple {
    0% {
        box-shadow: 0 0 0 0 hsla(350, 80%, 80%, 0.4);
        opacity: 1;
    }

    100% {
        box-shadow: 0 0 0 40px hsla(350, 80%, 80%, 0);
        opacity: 0;
    }
}

@keyframes vizBounce {
    0% {
        transform: scaleY(0.3);
    }

    100% {
        transform: scaleY(1);
    }
}

@keyframes rippleExpand {
    0% {
        width: 0;
        height: 0;
        opacity: 0.6;
    }

    100% {
        width: 120px;
        height: 120px;
        opacity: 0;
    }
}

@keyframes float {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(-8px);
    }
}

/* Character entrance */
/* .character-container {
    animation: float 4s ease-in-out infinite;
} */

/* =====================================================
   RESPONSIVE — PC Widescreen
   ===================================================== */
@media (min-width: 1200px) {
    .character-container {
        width: 500px;
        height: 85vh;
    }

    .dialog-box {
        width: 700px;
    }

    .affinity-bar-container {
        width: 160px;
    }
}

/* =====================================================
   RESPONSIVE — Tablet
   ===================================================== */
@media (max-width: 1199px) and (min-width: 769px) {
    .character-container {
        width: 45vw;
        height: 80vh;
    }

    .dialog-box {
        width: min(88vw, 600px);
    }
}

/* =====================================================
   RESPONSIVE — Mobile Portrait
   ===================================================== */
@media (max-width: 768px) {
    .affinity-panel {
        top: var(--gap-sm);
        right: var(--gap-sm);
        padding: 6px 12px;
        gap: 6px;
    }

    .affinity-icon {
        font-size: 1.1rem;
    }

    .affinity-bar-container {
        width: 70px;
        height: 6px;
    }

    .affinity-value {
        font-size: 0.75rem;
    }

    .status-bar {
        top: var(--gap-sm);
        left: var(--gap-sm);
        padding: 6px 12px;
        gap: var(--gap-sm);
    }

    .status-text {
        font-size: 0.7rem;
    }

    .btn-icon {
        width: 34px;
        height: 34px;
    }

    .character-container {
        width: 143vw;
        height: 103vh;
    }

    .dialog-box {
        bottom: var(--gap-md);
        width: 94vw;
        padding: var(--gap-sm) var(--gap-md);
        min-height: 80px;
        border-radius: var(--radius-sm);
    }

    .dialog-name {
        font-size: 0.75rem;
    }

    .dialog-text {
        font-size: 0.9rem;
        line-height: 1.6;
    }

    .dialog-user-text {
        font-size: 0.7rem;
    }

    .touch-toast {
        font-size: 0.85rem;
        padding: 8px 18px;
    }

    .audio-viz {
        bottom: 120px;
    }

    .title-jp {
        font-size: clamp(2rem, 12vw, 3.5rem);
    }

    .btn-start {
        padding: 12px 36px;
        font-size: 1rem;
    }
}

/* =====================================================
   RESPONSIVE — Mobile Landscape
   ===================================================== */
@media (max-height: 500px) and (orientation: landscape) {
    .character-container {
        width: 30vw;
        height: 90vh;
        left: 25%;
    }

    .dialog-box {
        width: 50vw;
        left: 65%;
        bottom: var(--gap-md);
        min-height: 60px;
    }

    .affinity-panel {
        top: var(--gap-sm);
        right: var(--gap-sm);
    }

    .status-bar {
        top: var(--gap-sm);
        left: var(--gap-sm);
    }
}

/* =====================================================
   SAFE AREA (notch devices)
   ===================================================== */
@supports (padding: env(safe-area-inset-top)) {
    .affinity-panel {
        top: calc(var(--gap-lg) + env(safe-area-inset-top));
        right: calc(var(--gap-lg) + env(safe-area-inset-right));
    }

    .status-bar {
        top: calc(var(--gap-lg) + env(safe-area-inset-top));
        left: calc(var(--gap-lg) + env(safe-area-inset-left));
    }

    .dialog-box {
        bottom: calc(var(--gap-xl) + env(safe-area-inset-bottom));
    }
}