/* Nardora experience polish: active-game fit, compact controls and calm modals. */

:root {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

#rotate-notice {
    display: none !important;
}

.ui-icon {
    display: inline-block;
    width: 20px;
    height: 20px;
    flex: 0 0 20px;
    background: currentColor;
    -webkit-mask: var(--ui-icon) center / contain no-repeat;
    mask: var(--ui-icon) center / contain no-repeat;
}

.ui-icon-globe { --ui-icon: url('./assets/ui/icons/globe-2.svg'); }
.ui-icon-palette { --ui-icon: url('./assets/ui/icons/palette.svg'); }
.ui-icon-volume { --ui-icon: url('./assets/ui/icons/volume-2.svg'); }
.ui-icon-volume-off { --ui-icon: url('./assets/ui/icons/volume-x.svg'); }
.ui-icon-vibrate { --ui-icon: url('./assets/ui/icons/vibrate.svg'); }

#appearance-controls {
    grid-column: 1 / -1;
    min-width: 0;
    display: grid;
    grid-template-columns: minmax(84px, 0.72fr) minmax(0, 1.28fr);
    gap: 7px;
}

#appearance-controls #theme-container,
#appearance-controls #sound-controls {
    width: 100%;
    min-width: 0;
    min-height: 46px;
    box-sizing: border-box;
}

#appearance-controls #theme-container {
    padding: 0;
    border: 0;
    background: transparent;
}

#theme-manager-button {
    width: 100%;
    min-width: 0;
    min-height: 46px;
    flex: 1 1 auto;
    gap: 7px;
    padding: 7px 10px;
    border-radius: 12px;
}

#theme-manager-button .theme-manager-button-label {
    position: static;
    width: auto;
    height: auto;
    margin: 0;
    overflow: visible;
    clip: auto;
    white-space: nowrap;
    font-size: 0.82rem;
}

#sound-controls {
    grid-template-columns: 38px minmax(54px, 1fr);
    gap: 7px;
    min-height: 46px;
    padding: 4px 8px;
    border-radius: 12px;
}

#sound-mute-toggle {
    min-width: 36px;
    min-height: 36px;
    display: inline-grid;
    place-items: center;
    padding: 6px;
    color: #f8e8c8;
}

#sound-mute-toggle .ui-icon {
    width: 18px;
    height: 18px;
    flex-basis: 18px;
}

#sound-volume {
    min-width: 0;
    margin: 0;
}

#haptic-toggle .haptic-toggle-icon {
    width: 19px;
    height: 19px;
}

button,
label,
select,
input[type='range'],
.checker-color-option {
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

/* Profile: one clear identity card, a compact avatar rail, one close action. */
#player-stats-modal,
#how-to-play-modal {
    position: fixed;
    inset: 0;
    padding:
        max(12px, env(safe-area-inset-top))
        max(12px, env(safe-area-inset-right))
        max(12px, env(safe-area-inset-bottom))
        max(12px, env(safe-area-inset-left));
}

#player-stats-card {
    width: min(100%, 760px);
    max-height: min(860px, calc(100dvh - 24px));
    gap: 10px;
    padding: clamp(14px, 2.4vw, 22px);
    border-radius: 20px;
}

#player-stats-title,
#how-to-play-title {
    margin: 0;
}

#local-profile-card {
    gap: 12px;
    padding: clamp(13px, 2.2vw, 18px);
    background:
        radial-gradient(circle at 90% 0, rgba(229, 178, 78, 0.12), transparent 38%),
        rgba(25, 12, 8, 0.68);
}

#profile-section-title {
    font-family: Georgia, 'Times New Roman', serif;
    font-size: 1.15rem !important;
    letter-spacing: 0.01em;
}

#profile-preview-avatar,
.friend-preview-avatar {
    position: relative;
    overflow: hidden;
    font-size: 0;
}

#profile-preview-avatar::before,
.friend-preview-avatar::before {
    content: '';
    width: 56%;
    height: 56%;
    display: block;
    background: #f2cf79;
    -webkit-mask: var(--profile-avatar-image) center / contain no-repeat;
    mask: var(--profile-avatar-image) center / contain no-repeat;
}

.friend-preview-avatar::before {
    width: 58%;
    height: 58%;
    margin: 21%;
}

#profile-display-name {
    font-size: 16px;
}

#profile-local-note {
    padding: 8px 10px;
    border-left: 2px solid rgba(235, 190, 99, 0.62);
    border-radius: 0 8px 8px 0;
    background: rgba(255, 226, 158, 0.05);
}

#profile-avatar-picker {
    padding: 10px 0 2px;
    border: 0;
}

#profile-avatar-picker legend {
    padding: 0;
}

.profile-avatar-option img {
    width: 25px;
    height: 25px;
    filter: invert(88%) sepia(42%) saturate(673%) hue-rotate(347deg) brightness(105%);
}

.profile-avatar-option[data-selected='true'] img {
    filter: brightness(0) saturate(100%);
}

#profile-actions {
    grid-template-columns: minmax(0, 1fr) auto;
}

#profile-reset-button,
#stats-reset-button {
    border-color: rgba(226, 183, 101, 0.22);
    background: transparent;
    color: rgba(239, 216, 174, 0.76);
}

#player-stats-footer {
    display: flex;
    justify-content: flex-end;
}

#stats-reset-button {
    width: auto;
    min-height: 40px;
    padding: 7px 12px;
}

/* How to Play: adjacent navigation and a single close affordance. */
#how-to-play-card {
    width: min(100%, 560px);
    max-height: min(680px, calc(100dvh - 24px));
    gap: 12px;
    padding: clamp(14px, 2.4vw, 20px);
}

#how-to-play-pages {
    min-height: 150px;
    display: grid;
    align-items: center;
    padding: clamp(14px, 3vw, 22px);
}

.guide-page h4 {
    margin: 0 0 10px;
    font-family: Georgia, 'Times New Roman', serif;
    font-size: clamp(1rem, 3.8vw, 1.18rem);
    line-height: 1.25;
}

.guide-page p,
.guide-page li {
    margin: 0;
    font-size: clamp(0.9rem, 3.3vw, 1rem);
    line-height: 1.5;
}

#how-to-play-footer {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 10px;
}

#guide-navigation {
    min-width: 0;
    display: grid;
    grid-template-columns: minmax(76px, 1fr) auto minmax(76px, 1fr);
    align-items: center;
    gap: 8px;
}

#guide-prev-button,
#guide-next-button,
#guide-start-button {
    min-height: 44px;
}

#guide-start-button {
    min-width: 132px;
    padding-inline: 16px;
}

/* Theme bottom sheet: compact cards with no intersecting surfaces. */
@media (max-width: 600px) and (orientation: portrait) {
    #theme-manager-card {
        gap: 11px;
        max-height: calc(100dvh - 16px);
        padding: 16px;
    }

    #theme-manager-header h2 {
        font-size: 1.55rem;
    }

    #theme-manager-description {
        font-size: 0.84rem;
        line-height: 1.4;
    }

    #theme-manager-options {
        gap: 9px;
    }

    .theme-option-card {
        grid-template-columns: 108px minmax(0, 1fr);
        gap: 10px;
        min-height: 104px;
        padding: 9px;
    }

    .theme-option-copy strong {
        font-size: 1rem;
    }

    .theme-option-copy > span {
        font-size: 0.78rem;
        line-height: 1.32;
    }

    .theme-option-selected {
        top: 12px;
        right: 12px;
    }

    #theme-manager-footer {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
        align-items: center;
        gap: 10px;
    }

    #theme-manager-close-footer-button {
        min-width: 96px;
    }
}

/* Portrait game: the board and complete control panel share one visual viewport. */
@media (max-width: 600px) and (orientation: portrait) {
    html,
    body {
        width: 100%;
        min-width: 0;
        height: 100dvh;
        min-height: 100dvh;
        margin: 0;
        padding:
            env(safe-area-inset-top)
            env(safe-area-inset-right)
            env(safe-area-inset-bottom)
            env(safe-area-inset-left) !important;
        box-sizing: border-box;
        overflow: hidden;
        overscroll-behavior: none;
    }

    body {
        display: block;
    }

    #game-container {
        width: 100%;
        height: 100dvh;
        min-height: 0;
        max-height: 100dvh;
        display: grid;
        grid-template-columns: minmax(0, 1fr);
        grid-template-rows: auto minmax(0, 1fr);
        grid-template-areas:
            'board'
            'info';
        align-content: stretch;
        gap: 4px;
        padding: 4px;
        border-width: 0;
        border-radius: 0;
        box-sizing: border-box;
        overflow: hidden;
    }

    #board-wrapper {
        grid-area: board;
        width: min(100%, 56dvh);
        max-width: 100%;
        max-height: 42dvh;
        aspect-ratio: 4 / 3;
        justify-self: center;
        align-self: start;
        border-width: 2px;
        box-sizing: border-box;
        overflow: hidden;
    }

    #game-canvas {
        width: 100%;
        height: auto;
        max-width: 100%;
        max-height: 42dvh;
        object-fit: contain;
    }

    #info-panel {
        grid-area: info;
        width: 100%;
        height: 100%;
        min-width: 0;
        min-height: 0;
        max-height: none;
        display: grid;
        grid-template-rows: auto minmax(0, 1fr) auto;
        align-content: stretch;
        gap: 5px;
        padding: 6px;
        border-radius: 10px;
        box-sizing: border-box;
        overflow: hidden;
    }

    #info-panel > #turn-indicator {
        min-height: 30px;
        height: 30px;
        padding: 5px 9px;
        font-size: 0.82rem;
    }

    #panel-controls,
    #panel-controls:has(#auto-bearoff-help[open]) {
        min-height: 0;
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        grid-template-areas:
            'timer dice'
            'bearoff bearoff'
            'bot language'
            'appearance appearance'
            'display display';
        gap: 5px;
        align-content: start;
        overflow-y: auto;
        overscroll-behavior: contain;
        scrollbar-width: thin;
    }

    #timer-container { grid-area: timer; }
    #dice-container { grid-area: dice; }
    #auto-bearoff-container { grid-area: bearoff; }
    #difficulty-container { grid-area: bot; }
    #language-container { grid-area: language; }
    #appearance-controls { grid-area: appearance; }
    #fullscreen-container { grid-area: display; }

    #timer-container,
    #dice-container,
    #difficulty-container,
    #language-container,
    #auto-bearoff-container,
    #fullscreen-container,
    #appearance-controls #theme-container,
    #appearance-controls #sound-controls {
        width: 100%;
        min-width: 0;
        min-height: 42px;
        padding: 3px 6px;
        box-sizing: border-box;
    }

    #timer-container {
        min-height: 46px;
        padding: 5px 6px;
        gap: 2px;
    }

    #timer-container .timer-title {
        margin: 0;
        font-size: 0.58rem;
        letter-spacing: 0.11em;
    }

    #timer-countdown {
        font-size: 0.96rem;
        line-height: 1;
    }

    #dice-container {
        min-height: 46px;
        margin: 0;
        justify-self: stretch;
        align-self: stretch;
    }

    #dice-display {
        gap: 7px;
    }

    .die {
        width: 32px;
        height: 32px;
        min-width: 32px;
        font-size: 15px;
    }

    #auto-bearoff-container {
        min-height: 42px;
        padding: 4px 7px;
    }

    #auto-bearoff-label,
    #auto-turn-confirm-label {
        font-size: 0.75rem;
    }

    #auto-bearoff-help summary {
        width: 24px;
        height: 24px;
    }

    #difficulty-container,
    #language-container {
        min-height: 42px;
        gap: 4px;
    }

    #difficulty-container label,
    #language-container label {
        font-size: 0.72rem;
    }

    #bot-difficulty,
    #language-select,
    #profile-display-name {
        min-width: 0;
        min-height: 34px;
        max-width: 100%;
        padding: 3px 6px;
        font-size: 16px !important;
    }

    #language-container .ui-icon {
        width: 17px;
        height: 17px;
        flex-basis: 17px;
    }

    #appearance-controls {
        grid-template-columns: minmax(86px, 0.78fr) minmax(0, 1.22fr);
        gap: 5px;
    }

    #theme-manager-button,
    #appearance-controls #sound-controls {
        min-height: 42px;
    }

    #theme-manager-button {
        padding: 5px 8px;
    }

    #fullscreen-container {
        min-height: 42px;
        padding: 3px;
    }

    #fullscreen-toggle,
    #point-numbers-toggle,
    #haptic-toggle {
        min-height: 36px;
        padding: 4px 7px;
    }

    #status-message {
        min-height: 18px;
        max-height: 22px;
        margin: 0;
        padding: 2px 4px;
        overflow: hidden;
        font-size: 0.66rem;
        line-height: 1.25;
    }

    #how-to-play-modal,
    #player-stats-modal {
        padding:
            max(8px, env(safe-area-inset-top))
            max(8px, env(safe-area-inset-right))
            max(8px, env(safe-area-inset-bottom))
            max(8px, env(safe-area-inset-left));
    }

    #how-to-play-card,
    #player-stats-card {
        max-height: calc(100dvh - 16px);
        padding: 13px;
    }

    #how-to-play-footer {
        grid-template-columns: minmax(0, 1fr);
    }

    #guide-start-button {
        width: 100%;
    }

    #profile-summary {
        grid-template-columns: 58px minmax(0, 1fr);
        gap: 10px;
    }

    #profile-preview-avatar {
        width: 56px;
        height: 56px;
    }

    #profile-avatar-grid {
        display: grid;
        grid-auto-flow: column;
        grid-auto-columns: 72px;
        grid-template-columns: none;
        gap: 7px;
        margin-top: 8px;
        padding: 2px 2px 8px;
        overflow-x: auto;
        scroll-snap-type: x proximity;
        scrollbar-width: thin;
    }

    .profile-avatar-option {
        min-height: 66px;
        scroll-snap-align: start;
    }

    #profile-actions {
        grid-template-columns: minmax(0, 1fr) auto;
    }

    #profile-reset-button {
        padding-inline: 10px;
        font-size: 0.76rem;
    }

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

/* Landscape phone: board and every primary control remain adjacent and reachable. */
@media (max-height: 600px) and (orientation: landscape) {
    html,
    body {
        width: 100%;
        height: 100dvh;
        min-height: 100dvh;
        margin: 0;
        padding:
            env(safe-area-inset-top)
            env(safe-area-inset-right)
            env(safe-area-inset-bottom)
            env(safe-area-inset-left) !important;
        box-sizing: border-box;
        overflow: hidden;
    }

    #game-container {
        width: 100%;
        height: 100dvh;
        max-height: 100dvh;
        display: grid;
        grid-template-columns: minmax(0, min(64vw, calc(133.333dvh - 10px))) minmax(238px, 1fr);
        grid-template-rows: minmax(0, 1fr);
        grid-template-areas: 'board info';
        gap: 5px;
        padding: 4px;
        border-width: 0;
        border-radius: 0;
        box-sizing: border-box;
        overflow: hidden;
    }

    #board-wrapper {
        grid-area: board;
        width: 100%;
        max-width: 100%;
        max-height: calc(100dvh - 8px);
        align-self: center;
        justify-self: end;
        overflow: hidden;
    }

    #game-canvas {
        width: 100%;
        height: auto;
        max-width: 100%;
        max-height: calc(100dvh - 8px);
        object-fit: contain;
    }

    #info-panel {
        grid-area: info;
        width: 100%;
        height: 100%;
        min-width: 0;
        min-height: 0;
        display: grid;
        grid-template-rows: auto minmax(0, 1fr) auto;
        gap: 4px;
        padding: 5px;
        overflow: hidden;
        box-sizing: border-box;
    }

    #info-panel > #turn-indicator {
        min-height: 28px;
        height: 28px;
        padding: 4px 8px;
        font-size: 0.76rem;
    }

    #panel-controls,
    #panel-controls:has(#auto-bearoff-help[open]) {
        min-height: 0;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        grid-template-areas:
            'bot language'
            'appearance appearance'
            'bearoff bearoff'
            'display display'
            'timer dice';
        gap: 4px;
        align-content: start;
        overflow-y: auto;
        scrollbar-width: thin;
    }

    #difficulty-container { grid-area: bot; }
    #language-container { grid-area: language; }
    #appearance-controls { grid-area: appearance; }
    #auto-bearoff-container { grid-area: bearoff; }
    #fullscreen-container { grid-area: display; }
    #timer-container { grid-area: timer; }
    #dice-container { grid-area: dice; }

    #difficulty-container,
    #language-container,
    #auto-bearoff-container,
    #fullscreen-container,
    #appearance-controls #theme-container,
    #appearance-controls #sound-controls {
        width: 100%;
        min-width: 0;
        min-height: 44px;
        padding: 2px 5px;
        box-sizing: border-box;
    }

    #timer-container,
    #dice-container {
        width: 100%;
        min-width: 0;
        min-height: 34px;
        padding: 2px 5px;
        box-sizing: border-box;
    }

    #bot-difficulty,
    #language-select {
        width: 100%;
        min-width: 0;
        min-height: 44px;
        max-width: 100%;
        padding: 2px 4px;
        font-size: 16px !important;
    }

    #appearance-controls {
        grid-template-columns: minmax(88px, 0.75fr) minmax(0, 1.25fr);
        gap: 4px;
    }

    #theme-manager-button,
    #sound-controls,
    #sound-mute-toggle,
    #fullscreen-toggle,
    #point-numbers-toggle,
    #haptic-toggle {
        min-height: 44px;
    }

    #theme-manager-button {
        padding: 3px 7px;
    }

    #auto-bearoff-label,
    #auto-turn-confirm-label {
        font-size: 0.68rem;
    }

    #timer-container {
        gap: 1px;
    }

    #timer-container .timer-title {
        margin: 0;
        font-size: 0.52rem;
    }

    #timer-countdown {
        font-size: 0.82rem;
    }

    .die {
        width: 28px;
        height: 28px;
        min-width: 28px;
        font-size: 14px;
    }

    #status-message {
        min-height: 32px;
        max-height: clamp(32px, 12dvh, 54px);
        margin: 0;
        padding: 3px 5px;
        overflow-x: hidden;
        overflow-y: auto;
        overflow-wrap: anywhere;
        font-size: 0.6rem;
        line-height: 1.2;
        scrollbar-gutter: stable;
    }
}

@media (min-width: 901px) {
    #appearance-controls {
        grid-template-columns: minmax(84px, 0.78fr) minmax(0, 1.22fr);
    }
}

@media (prefers-reduced-motion: reduce) {
    #profile-avatar-grid {
        scroll-behavior: auto;
    }
}
