:root {
    --hus-dark: #0d3827;
    --hus-cream: #f5f0d4;
    --hus-yellow: #e4b927;
    --hus-orange: #d67625;
    --hus-player-one: #c67941;
    --hus-player-one-hover: #d88b52;
    --hus-player-two: #4d6c44;
    --hus-nav-height: 62px;
    --hus-safe-top: env(safe-area-inset-top, 0px);
}

body[data-hus-room] {
    --game-ui-accent: var(--hus-yellow);
    --game-ui-header-background: #0d3827f2;
    --game-ui-primary-background: var(--hus-dark);
}

* {
    box-sizing: border-box;
}

body {
    min-height: 100vh;
    margin: 0;
    overflow-x: hidden;
    background: #b9ca83;
    font-family: Arial, Helvetica, sans-serif;
}

body:has(.hus-landing) {
    background: linear-gradient(145deg, #092c1dc7, #132f1d73), url('/public/img/hus/background.png') center / cover fixed;
    background-attachment: fixed;
}

button,
input,
select {
    font: inherit;
}

button {
    cursor: pointer;
    touch-action: manipulation;
}

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

#hus-app {
    min-height: 100vh;
}

.game-container {
    position: fixed;
    left: 0;
    width: 100vw;
    overflow: hidden;
    display: grid;
    place-content: center;
}

#game {
    display: grid;
    grid-template-columns: repeat(8, minmax(0, 1fr));
    grid-template-rows: repeat(4, minmax(0, 1fr));
}

#player1,
#player2 {
    display: contents;
}

.field {
    appearance: none;
    position: relative;
    width: 100%;
    height: 100%;
    padding: 0;
    overflow: hidden;
    border: 0;
    border-radius: 50%;
    color: #fff;
    font-size: 0;
    font-weight: 700;
}

.field::after {
    position: absolute;
    inset: 0;
    z-index: 2;
    display: grid;
    place-items: center;
    color: #fff;
    font-size: 10px;
    pointer-events: none;
    text-shadow: 0 1px 2px #0009;
}

.field .stone {
    position: absolute;
    width: 19%;
    height: 19%;
    border-radius: 50%;
    outline: .5px solid #0008;
    transform: translate(-50%, -50%);
}

#player1-stats-container,
#player2-stats-container {
    position: absolute;
    z-index: 1000;
    display: flex;
    align-items: center;
    gap: 10px;
}

#player1-stats-container {
    bottom: 20px;
    left: 20px;
}

#player2-stats-container {
    top: 20px;
    right: 20px;
}

#player1-count,
#player2-count {
    width: 45px;
    height: 45px;
    display: grid;
    place-items: center;
    border-radius: 50%;
    color: #fff;
    font-family: "Courier New", Courier, monospace;
    font-size: 20px;
    font-weight: 700;
}

#player1-count {
    margin-right: 10px;
}

#player2-count {
    margin-left: 10px;
}

#player1-name,
#player2-name {
    color: #fff;
    font-size: 20px;
    text-shadow: 0 1px 4px #173521b8;
}

.hus-player-meta {
    position: relative;
    min-width: 0;
}

.hus-player-name {
    max-width: 180px;
    padding: 0;
    overflow: hidden;
    border: 0;
    border-radius: 0;
    background: transparent;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.hus-player-name:disabled {
    cursor: default;
    opacity: .65;
}

.hus-reaction-bubble {
    position: absolute;
    z-index: 2200;
    max-width: min(220px, calc(100vw - 32px));
    min-width: 38px;
    padding: 7px 10px;
    border: 0;
    border-radius: 15px;
    background: #fff;
    color: #243728;
    font-size: 1.4rem;
    font-weight: 400;
    line-height: 1;
    box-shadow: 0 5px 18px #17352170;
}

.hus-reaction-own {
    bottom: calc(100% + 8px);
    left: 0;
}

.hus-reaction-opponent {
    top: calc(100% + 8px);
    right: 0;
}

.hus-reaction-message {
    width: max-content;
    max-width: min(360px, calc(100vw - 110px));
    overflow-wrap: anywhere;
    font-size: .82rem;
    line-height: 1.25;
    text-align: left;
}

.hus-reaction-picker,
.hus-reaction-composer {
    position: fixed;
    z-index: 2300;
    left: 50%;
    bottom: max(84px, calc(env(safe-area-inset-bottom, 0px) + 70px));
    transform: translateX(-50%);
    border: 1px solid #ffffff38;
    border-radius: 13px;
    background: #173521f2;
    box-shadow: 0 8px 28px #1735218c;
}

.hus-reaction-picker {
    padding: 7px;
    display: flex;
    align-items: center;
    gap: 5px;
}

.hus-reaction-grid {
    display: grid;
    grid-template-columns: repeat(7, 34px);
    gap: 3px;
}

.hus-reaction-picker button {
    width: 34px;
    height: 34px;
    padding: 0;
    border: 0;
    border-radius: 8px;
    background: transparent;
    font-size: 1.15rem;
}

.hus-reaction-picker button:hover,
.hus-reaction-picker button:focus-visible {
    background: #ffffff24;
    outline: none;
}

.hus-reaction-composer {
    width: min(360px, calc(100vw - 24px));
    padding: 7px;
    display: flex;
    gap: 6px;
}

.hus-reaction-composer input {
    min-width: 0;
    flex: 1;
    padding: 9px 10px;
    border: 1px solid #ffffff4a;
    border-radius: 8px;
    background: #fffdf3;
    color: #173521;
}

.hus-reaction-composer button {
    padding: 8px 11px;
    border: 0;
    border-radius: 8px;
    background: var(--hus-player-one);
    color: #fff;
    font-weight: 700;
}

#confetti-canvas {
    position: fixed;
    inset: 0;
    z-index: 999999;
}

.hus-nav {
    position: fixed;
    z-index: 2500;
    top: 0;
    right: 0;
    left: 0;
    height: calc(var(--hus-nav-height) + var(--hus-safe-top));
    padding: var(--hus-safe-top) max(18px, env(safe-area-inset-right, 0px)) 0 max(18px, env(safe-area-inset-left, 0px));
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    border-bottom: 1px solid #ffffff2e;
    background: #0d3827ed;
    box-shadow: 0 6px 24px #061b1270;
    color: #fff;
    backdrop-filter: blur(10px);
}

.hus-nav-back {
    width: max-content;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    color: inherit;
    font-size: .88rem;
    font-weight: 700;
    text-decoration: none;
}

.hus-nav-back svg {
    width: 22px;
    height: 22px;
    fill: none;
    stroke: currentColor;
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke-width: 2;
}

.hus-nav-logo {
    width: 82px;
    height: auto;
    display: block;
    filter: drop-shadow(0 2px 3px #0005);
}

.hus-nav-logo-link {
    display: block;
    justify-self: center;
    line-height: 0;
}

.hus-nav-room {
    justify-self: end;
    color: #f5f0d4;
    font-family: "Courier New", Courier, monospace;
    font-size: .74rem;
    font-weight: 700;
    letter-spacing: .06em;
}

.hus-landing {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: calc(var(--hus-nav-height) + var(--hus-safe-top) + 28px) 20px 32px;
    position: relative;
    isolation: isolate;
    background: transparent;
}

.hus-landing::before {
    content: '';
    position: absolute;
    z-index: -1;
    inset: 0;
    display: none;
}

.hus-panel {
    width: min(430px, 100%);
    padding: 32px;
    border: 1px solid #fff5;
    border-radius: 24px;
    background: #f5f0d4ed;
    color: #173521;
    box-shadow: 0 24px 70px #07180f88;
    text-align: center;
    backdrop-filter: blur(8px);
}

.hus-logo {
    width: min(260px, 82%);
    height: auto;
    display: block;
    margin: 0 auto 24px;
}

.hus-panel h1,
.hus-panel h2 {
    margin: 0 0 8px;
}

.hus-panel p {
    margin: 0 0 22px;
    color: #425847;
    line-height: 1.5;
}

.hus-form {
    display: grid;
    gap: 14px;
    text-align: left;
}

.hus-form label {
    display: grid;
    gap: 6px;
    color: #425847;
    font-size: .78rem;
    font-weight: 700;
}

.hus-form input,
.hus-form select {
    width: 100%;
    padding: 12px 13px;
    border: 1px solid #17352142;
    border-radius: 10px;
    outline: none;
    background: #fffdf3;
    color: #173521;
}

.hus-form input:focus,
.hus-form select:focus {
    border-color: var(--hus-dark);
    box-shadow: 0 0 0 3px #0d382720;
}

.hus-primary,
.hus-secondary,
.hus-link-button {
    min-height: 44px;
    padding: 11px 16px;
    border: 0;
    border-radius: 10px;
    font-weight: 700;
}

.hus-primary {
    background: var(--hus-dark);
    color: #fff;
}

.hus-primary:disabled {
    cursor: not-allowed;
    opacity: .45;
}

.hus-secondary {
    border: 1px solid #17352142;
    background: transparent;
    color: #173521;
}

.hus-link-button {
    overflow-wrap: anywhere;
    background: #e6dfbd;
    color: #173521;
    font-size: .75rem;
}

.hus-players {
    margin: 22px 0;
    padding: 0;
    display: grid;
    gap: 8px;
    list-style: none;
    text-align: left;
}

.hus-players li {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 11px 13px;
    border-radius: 10px;
    background: #fff9;
}

.hus-players small {
    color: #677266;
}

.hus-lobby-actions {
    display: grid;
    gap: 10px;
}

.hus-waiting {
    margin: 18px 0 !important;
    font-size: .9rem;
}

.hus-recent {
    margin-top: 24px;
    padding-top: 18px;
    text-align: left;
}

.hus-recent:empty {
    display: none;
}

.hus-recent h2 {
    margin: 0 0 9px;
    font-size: .92rem;
}

.hus-recent-list {
    display: grid;
    gap: 7px;
}

.hus-recent-session {
    padding: 10px 11px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    border-radius: 10px;
    background: #fff9;
}

.hus-recent-details {
    min-width: 0;
}

.hus-recent-details strong,
.hus-recent-details small {
    display: block;
}

.hus-recent-details strong {
    overflow: hidden;
    font-size: .84rem;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.hus-recent-details small {
    margin-top: 3px;
    color: #5c6a5e;
    font-size: .68rem;
}

.hus-recent-players {
    margin-top: 7px;
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
}

.hus-recent-players button {
    padding: 5px 8px;
    border: 0;
    border-radius: 7px;
    background: var(--hus-player-two);
    color: #fff;
    font-size: .7rem;
    font-weight: 700;
}

.hus-delete-session {
    flex: 0 0 auto;
    width: 34px;
    height: 34px;
    padding: 7px;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: #805141;
}

.hus-delete-session svg {
    width: 100%;
    height: 100%;
    fill: none;
    stroke: currentColor;
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke-width: 1.8;
}

#hus-status {
    position: fixed;
    z-index: 3000;
    right: 16px;
    bottom: 16px;
    max-width: min(360px, calc(100vw - 32px));
    padding: 10px 14px;
    border-radius: 9px;
    background: #102319ed;
    color: #fff;
    box-shadow: 0 8px 24px #0006;
    opacity: 0;
    pointer-events: none;
    transform: translateY(8px);
    transition: opacity .16s ease, transform .16s ease;
}

#hus-status.visible {
    opacity: 1;
    transform: none;
}

.game-container {
    top: calc(var(--hus-nav-height) + var(--hus-safe-top));
    height: calc(100vh - var(--hus-nav-height) - var(--hus-safe-top));
    height: calc(100dvh - var(--hus-nav-height) - var(--hus-safe-top));
    background: url('/public/img/hus/bg-portrait.png') center / cover no-repeat;
}

#game,
#game.player1,
#game.player2 {
    width: min(800px, calc(100vw - 24px));
    height: auto;
    aspect-ratio: 2 / 1;
    gap: clamp(3px, 1.2vw, 18px);
    transform: none;
    transition: none;
}

#game .field,
#game .field::after,
#game.player1 #player1 .field,
#game.player1 #player1 .field::after,
#game.player2 #player2 .field,
#game.player2 #player1 .field::after {
    transform: none;
    transition: none;
}

.field:disabled {
    cursor: default;
    opacity: 1;
}

#player1 .field {
    background-color: var(--hus-player-one);
}

#player2 .field {
    background-color: var(--hus-player-two);
}

#player1-stats-container #player1-count {
    background-color: var(--hus-player-one);
}

#player2-stats-container #player2-count {
    background-color: var(--hus-player-two);
}

#player1 .field:disabled:hover {
    background-color: var(--hus-player-one);
}

#player2 .field:disabled:hover {
    background-color: var(--hus-player-two);
}

#player1 .field.playable {
    /*outline: 3px solid #fff9;*/
    /*outline-offset: 2px;*/
}

#player1 .field.playable:hover,
#player1 .field.playable:focus-visible {
    background-color: var(--hus-player-one-hover);
    /*outline-color: #fff;*/
}

.field[data-show-count='true']::after {
    content: attr(data-count);
}

.hus-turn-status {
    position: absolute;
    z-index: 1001;
    left: 50%;
    max-width: calc(100vw - 24px);
    overflow: hidden;
    padding: 7px 12px;
    border-radius: 999px;
    background: #075b6bd9;
    bottom: 16px;
    color: #fff;
    font-size: .78rem;
    text-overflow: ellipsis;
    transform: translateX(-50%);
    white-space: nowrap;
}

.hus-hand-shell {
    position: absolute;
    z-index: 1000;
}

.hus-hand-opponent {
    top: 16px;
    left: max(20px, env(safe-area-inset-left, 0px));
}

.hus-hand-own {
    right: max(20px, env(safe-area-inset-right, 0px));
    bottom: max(16px, env(safe-area-inset-bottom, 0px));
    justify-items: end;
}

.hus-hand.field {
    width: clamp(62px, 9vw, 88px);
    height: clamp(62px, 9vw, 88px);
    border: 2px solid #ffffffa8;
    background: #ffffff63;
    box-shadow: inset 0 0 20px #ffffff24, 0 4px 14px #00394459;
    pointer-events: none;
}

.hus-hand-opponent .hus-hand {
    border-color: var(--hus-player-two);
}

.hus-hand-own .hus-hand {
    border-color: var(--hus-player-one);
}

.hus-result {
    position: absolute;
    z-index: 2100;
    inset: 0;
    display: grid;
    place-items: center;
    padding: 20px;
    background: #001e25a8;
}

.hus-result-card {
    width: min(330px, 100%);
    padding: 28px;
    border-radius: 20px;
    background: var(--hus-cream);
    color: var(--hus-dark);
    text-align: center;
    box-shadow: 0 20px 60px #00171d99;
}

.hus-result-card h2 {
    margin: 0 0 8px;
    font-size: 2rem;
}

.hus-result-card p {
    margin: 0 0 20px;
}

.hus-result-card .hus-logo {
    width: 160px;
    margin-bottom: 20px;
}

@media (orientation: landscape) {
    .game-container {
        background-image: url('/public/img/hus/bg-landscape.png');
    }
}

@media (max-width: 620px) {
    :root {
        --hus-nav-height: 54px;
    }

    .hus-nav {
        padding-right: max(12px, env(safe-area-inset-right, 0px));
        padding-left: max(12px, env(safe-area-inset-left, 0px));
    }

    .hus-nav-logo {
        width: 68px;
    }

    .hus-nav-room {
        font-size: .64rem;
    }

    .hus-panel {
        padding: 26px 20px;
        border-radius: 19px;
    }

    #game,
    #game.player1,
    #game.player2 {
        width: calc(100vw - 12px);
        gap: clamp(3px, 1vw, 6px);
    }

    #player1-stats-container,
    #player2-stats-container {
        gap: 5px;
    }

    #player1-name,
    #player2-name {
        max-width: 100px;
        overflow: hidden;
        font-size: 14px;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .hus-reaction-bubble {
        max-width: 170px;
    }

    .hus-reaction-bubble.hus-reaction-message {
        max-width: min(300px, calc(100vw - 90px));
    }

    .hus-reaction-picker {
        gap: 3px;
    }

    .hus-reaction-grid {
        grid-template-columns: repeat(7, 31px);
    }

    .hus-reaction-picker button {
        width: 31px;
        height: 31px;
        font-size: 1rem;
    }

    #player1-stats-container #player1-count,
    #player2-stats-container #player2-count {
        width: 36px;
        height: 36px;
        font-size: 15px;
    }

    .hus-hand-opponent {
        top: 12px;
        left: max(12px, env(safe-area-inset-left, 0px));
    }

    .hus-reaction-own {
        right: 0;
        left: auto;
    }

    .hus-hand-own {
        right: max(12px, env(safe-area-inset-right, 0px));
        bottom: max(12px, env(safe-area-inset-bottom, 0px));
    }

    .hus-hand.field {
        width: 54px;
        height: 54px;
    }

}

@media (orientation: portrait) and (max-width: 620px) {
    .hus-turn-status {
        bottom: max(88px, calc(env(safe-area-inset-bottom, 0px) + 76px));
    }

    .hus-result {
        padding: 14px;
    }

    .hus-result-card {
        padding: 24px 20px;
    }
}

@media (orientation: landscape) and (max-height: 600px) {
    :root {
        --hus-nav-height: 46px;
    }

    .hus-nav-logo {
        width: 58px;
    }

    .hus-nav-back {
        font-size: .78rem;
    }

    .hus-nav-room {
        font-size: .61rem;
    }

    .game-container {
        background-position: center;
    }

    #game,
    #game.player1,
    #game.player2 {
        width: min(
            800px,
            calc(100vw - clamp(140px, 30vw, 220px)),
            calc(200vh - var(--hus-nav-height) - var(--hus-nav-height) - var(--hus-safe-top) - var(--hus-safe-top) - 156px)
        );
        width: min(
            800px,
            calc(100vw - clamp(140px, 30vw, 220px)),
            calc(200dvh - var(--hus-nav-height) - var(--hus-nav-height) - var(--hus-safe-top) - var(--hus-safe-top) - 156px)
        );
        gap: clamp(3px, .8vw, 10px);
    }

    .hus-hand-opponent {
        top: 10px;
        left: max(12px, env(safe-area-inset-left, 0px));
    }

    .hus-reaction-own {
        right: 0;
        left: auto;
    }

    .hus-hand-own {
        right: auto;
        bottom: max(10px, env(safe-area-inset-bottom, 0px));
        left: max(12px, env(safe-area-inset-left, 0px));
    }

    .hus-hand.field {
        width: 48px;
        height: 48px;
    }

    #player1-stats-container,
    #player2-stats-container {
        right: max(12px, env(safe-area-inset-right, 0px));
        left: auto;
        flex-direction: column;
        align-items: flex-end;
        gap: 3px;
    }

    #player1-stats-container {
        bottom: max(10px, env(safe-area-inset-bottom, 0px));
    }

    #player2-stats-container {
        top: 10px;
    }

    #player1-name,
    #player2-name {
        max-width: 86px;
        overflow: hidden;
        font-size: 12px;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    #player1-stats-container #player1-count,
    #player2-stats-container #player2-count {
        width: 38px;
        height: 38px;
        margin: 0;
        font-size: 14px;
    }

    .hus-turn-status {
        max-width: calc(100vw - clamp(180px, 32vw, 240px));
        bottom: max(8px, env(safe-area-inset-bottom, 0px));
        padding: 5px 10px;
        font-size: .68rem;
    }

    .hus-landing {
        min-height: 100dvh;
        padding: calc(var(--hus-nav-height) + var(--hus-safe-top) + 12px) 12px 16px;
        place-items: start center;
    }

    .hus-panel {
        width: min(520px, 100%);
        padding: 18px 24px;
        border-radius: 18px;
    }

    .hus-panel .hus-logo {
        width: 150px;
        margin-bottom: 10px;
    }

    .hus-panel p {
        margin-bottom: 12px;
    }

    .hus-players {
        margin: 10px 0;
    }

    .hus-result-card {
        width: min(440px, 100%);
        padding: 14px 22px;
    }

    .hus-result-card .hus-logo {
        width: 100px;
        margin-bottom: 8px;
    }

    .hus-result-card h2 {
        margin-bottom: 4px;
        font-size: 1.45rem;
    }

    .hus-result-card p {
        margin-bottom: 10px;
    }
}

@media (hover: none) {
    #player1 .field.playable:hover {
        background-color: var(--hus-player-one);
        outline-color: #fff9;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        transition-duration: .01ms !important;
    }
}
