:root {
    color-scheme: light dark;
    --bg-gradient-start: #eef3ff;
    --bg-gradient-end: #e0e7ff;
    --panel-bg: rgba(255, 255, 255, 0.92);
    --panel-border: rgba(84, 110, 182, 0.25);
    --text-primary: #1d2a44;
    --text-secondary: #4d5d7a;
    --accent: #4c6ef5;
    --accent-soft: rgba(76, 110, 245, 0.12);
    --system-msg: #f08c00;
    --answer-msg: #2f9e44;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: "Pretendard", "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
    background: linear-gradient(160deg, var(--bg-gradient-start) 0%, var(--bg-gradient-end) 100%);
    color: var(--text-primary);
    min-height: 100vh;
}

body.canvas-drawing {
    overflow: hidden;
    overscroll-behavior: none;
}

.game-shell {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    padding: clamp(10px, 1.8vw, 20px);
    gap: clamp(8px, 1.6vw, 16px);
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
}

header h1 {
    margin: 0;
    font-size: clamp(1.4rem, 2vw, 1.8rem);
    font-weight: 700;
    color: var(--text-primary);
}

.status-message {
    flex: 1 1 auto;
    font-size: clamp(0.9rem, 1vw, 1rem);
    color: var(--text-secondary);
}

.game-layout {
    flex: 1 1 auto;
    display: grid;
    grid-template-columns: minmax(150px, 220px) minmax(0, 1fr) minmax(150px, 220px);
    gap: clamp(10px, 2vw, 18px);
}

.players-column {
    display: grid;
    grid-template-rows: repeat(4, minmax(96px, 1fr));
    gap: clamp(8px, 1.6vw, 14px);
}

.player-card {
    position: relative;
    padding: 14px 16px 18px;
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    border-radius: 18px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    box-shadow: 0 10px 20px rgba(51, 77, 128, 0.14);
    transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease, border-color 0.2s ease;
    overflow: visible;
}

.player-card[data-empty="true"] {
    align-items: center;
    justify-content: center;
    color: rgba(77, 93, 122, 0.6);
    font-size: 0.95rem;
    font-style: italic;
    box-shadow: none;
}

.player-card::after {
    content: attr(data-order);
    position: absolute;
    top: 10px;
    right: 12px;
    font-size: 0.75rem;
    color: rgba(77, 93, 122, 0.7);
    font-weight: 600;
}

.player-header {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 8px;
}

.player-name {
    font-size: 1.08rem;
    font-weight: 700;
    line-height: 1.3;
}

.player-score {
    font-size: 0.92rem;
    font-weight: 600;
    color: rgba(29, 42, 68, 0.72);
}

.player-meta {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 6px;
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.player-role-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    border-radius: 999px;
    padding: 4px 10px;
    font-size: 0.78rem;
    font-weight: 600;
    background: rgba(76, 110, 245, 0.15);
    color: var(--accent);
}

.player-status {
    margin-top: 10px;
    font-size: 0.85rem;
    color: var(--text-secondary);
    min-height: 1.2em;
}

.player-card[data-role="drawer"] {
    background: linear-gradient(140deg, #ffe066 0%, #ffd43b 100%);
    border-color: #f08c00;
    box-shadow: 0 16px 40px rgba(240, 140, 0, 0.35);
    color: #3c2f00;
}

.player-card[data-role="drawer"] .player-role-badge {
    background: rgba(240, 140, 0, 0.16);
    color: #c87900;
}

.player-card[data-role="guesser"] {
    background: linear-gradient(140deg, #d3f9d8 0%, #8ce99a 100%);
    border-color: #2f9e44;
    box-shadow: 0 16px 40px rgba(47, 158, 68, 0.28);
    color: #0b3b19;
}

.player-card[data-role="guesser"] .player-role-badge {
    background: rgba(47, 158, 68, 0.18);
    color: #2b8a3e;
}

.player-card[data-role="spectator"] {
    background: linear-gradient(140deg, rgba(76, 110, 245, 0.08) 0%, rgba(76, 110, 245, 0.16) 100%);
    border-color: rgba(76, 110, 245, 0.35);
}

.player-bubble {
    position: absolute;
    inset-inline: 14px;
    top: -14px;
    padding: 10px 14px;
    border-radius: 14px;
    font-size: 0.85rem;
    font-weight: 600;
    background: rgba(76, 110, 245, 0.92);
    color: #fff;
    box-shadow: 0 12px 32px rgba(22, 36, 84, 0.32);
    opacity: 0;
    transform: translateY(-6px);
    pointer-events: none;
    transition: opacity 0.25s ease, transform 0.25s ease;
    display: none;
    word-break: break-word;
}

.player-bubble::after {
    content: '';
    position: absolute;
    bottom: -8px;
    left: 28px;
    border-width: 8px 8px 0 8px;
    border-style: solid;
    border-color: rgba(76, 110, 245, 0.92) transparent transparent transparent;
}

.player-bubble.answer {
    background: rgba(47, 158, 68, 0.92);
    box-shadow: 0 12px 30px rgba(47, 158, 68, 0.3);
}

.player-bubble.answer::after {
    border-color: rgba(47, 158, 68, 0.92) transparent transparent transparent;
}

.player-bubble.drawer {
    background: rgba(240, 140, 0, 0.95);
    box-shadow: 0 12px 30px rgba(240, 140, 0, 0.32);
}

.player-bubble.drawer::after {
    border-color: rgba(240, 140, 0, 0.95) transparent transparent transparent;
}

.player-card[data-bubble-visible="true"] .player-bubble {
    opacity: 1;
    transform: translateY(-2px);
}

@media (min-width: 960px) {
    .player-bubble {
        display: block;
    }
}

.board-area {
    display: flex;
    flex-direction: column;
    gap: 14px;
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    border-radius: 22px;
    padding: clamp(12px, 1.8vw, 20px);
    box-shadow: 0 16px 32px rgba(54, 72, 110, 0.18);
}

.keyword-banner {
    text-align: center;
    font-size: 1.1rem;
    font-weight: 600;
    padding: 12px 18px;
    border-radius: 16px;
    background: rgba(76, 110, 245, 0.12);
    color: var(--accent);
    min-height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.keyword-banner.revealed {
    background: rgba(47, 158, 68, 0.12);
    color: #2f9e44;
}

#gameCanvas {
    width: 100%;
    aspect-ratio: 3 / 2;
    max-height: min(440px, 52vh);
    border-radius: 16px;
    border: 2px solid rgba(76, 110, 245, 0.26);
    background: #ffffff;
    box-shadow: inset 0 0 0 1px rgba(76, 110, 245, 0.08), 0 9px 22px rgba(33, 51, 93, 0.15);
    touch-action: none;
    cursor: crosshair;
}

#gameCanvas.disabled {
    cursor: not-allowed;
    filter: grayscale(0.1) brightness(0.98);
}

.toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    align-items: center;
    justify-content: space-between;
    border: 1px solid rgba(76, 110, 245, 0.16);
    border-radius: 16px;
    padding: 12px 16px;
    background: rgba(76, 110, 245, 0.08);
}

.toolbar-section {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.color-button {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 2px solid transparent;
    cursor: pointer;
    transition: transform 0.2s ease, border-color 0.2s ease;
}

.color-button.active {
    border-color: #fff;
    box-shadow: 0 0 0 3px rgba(76, 110, 245, 0.6);
    transform: scale(1.05);
}

.tool-button {
    padding: 8px 14px;
    border-radius: 12px;
    border: 1px solid rgba(76, 110, 245, 0.2);
    background: #fff;
    color: var(--text-primary);
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s ease, color 0.2s ease, transform 0.18s ease;
}

.tool-button.primary {
    background: var(--accent);
    color: #fff;
    border-color: rgba(76, 110, 245, 0.8);
}

.tool-button:hover {
    transform: translateY(-1px);
    box-shadow: 0 8px 18px rgba(76, 110, 245, 0.18);
}

.tool-button[disabled] {
    opacity: 0.55;
    cursor: not-allowed;
    box-shadow: none;
}

.size-slider {
    width: 140px;
}

.status-line {
    font-size: 0.95rem;
    color: var(--text-secondary);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
}

.timer-bar {
    position: relative;
    height: 12px;
    border-radius: 999px;
    background: rgba(76, 110, 245, 0.08);
    overflow: hidden;
    flex: 1 1 auto;
}

.timer-bar span {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 0;
    background: linear-gradient(90deg, rgba(76, 110, 245, 0.8), rgba(129, 201, 149, 0.85));
    border-radius: inherit;
    transition: width 0.3s linear;
}

.timer-text {
    min-width: 64px;
    text-align: right;
    font-weight: 600;
    color: var(--text-primary);
}

.chat-panel {
    display: flex;
    flex-direction: column;
    border: 1px solid rgba(76, 110, 245, 0.16);
    border-radius: 18px;
    background: #fff;
    padding: 12px;
    gap: 10px;
    max-height: 280px;
}

.chat-log {
    list-style: none;
    margin: 0;
    padding: 0;
    overflow-y: auto;
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    gap: 6px;
    max-height: 180px;
    min-height: 120px;
}

.chat-entry {
    font-size: 0.92rem;
    display: flex;
    flex-direction: column;
    padding: 6px 8px;
    border-radius: 10px;
    background: rgba(76, 110, 245, 0.05);
}

.chat-entry.system {
    background: rgba(240, 140, 0, 0.1);
    color: var(--system-msg);
    font-weight: 600;
}

.chat-entry.answer {
    background: rgba(47, 158, 68, 0.12);
    color: var(--answer-msg);
    font-weight: 600;
}

.chat-meta {
    font-size: 0.75rem;
    color: rgba(77, 93, 122, 0.8);
}

.chat-content {
    margin-top: 2px;
    word-break: break-word;
}

.chat-form {
    display: flex;
    gap: 8px;
}

.chat-form input[type="text"] {
    flex: 1 1 auto;
    border-radius: 12px;
    border: 1px solid rgba(76, 110, 245, 0.24);
    padding: 11px 14px;
    font-size: 0.95rem;
}

.chat-form button {
    padding: 10px 18px;
    border-radius: 12px;
    border: none;
    background: var(--accent);
    color: #fff;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.chat-form button:hover {
    transform: translateY(-1px);
    box-shadow: 0 10px 20px rgba(76, 110, 245, 0.24);
}

.join-overlay {
    position: fixed;
    inset: 0;
    background: rgba(20, 33, 61, 0.45);
    backdrop-filter: blur(6px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    z-index: 999;
}

.join-card {
    background: #fff;
    padding: 28px 24px;
    border-radius: 20px;
    width: min(420px, 90vw);
    box-shadow: 0 26px 48px rgba(26, 42, 92, 0.28);
    display: flex;
    flex-direction: column;
    gap: 18px;
}

.join-card h2 {
    margin: 0;
    font-size: 1.6rem;
    text-align: center;
}

.join-card input[type="text"] {
    padding: 12px 14px;
    border-radius: 14px;
    border: 1px solid rgba(76, 110, 245, 0.3);
    font-size: 1rem;
}

.join-card button {
    padding: 12px 16px;
    border-radius: 14px;
    border: none;
    background: var(--accent);
    color: #fff;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.join-card button:hover {
    transform: translateY(-1px);
    box-shadow: 0 12px 22px rgba(76, 110, 245, 0.26);
}

.error-message {
    color: #d6336c;
    font-size: 0.9rem;
    text-align: center;
    min-height: 20px;
}

@media (max-width: 1080px) {
    .game-layout {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        grid-template-rows: auto auto;
    }

    .players-column:first-of-type {
        order: 2;
    }

    .players-column:last-of-type {
        order: 3;
    }

    .board-area {
        order: 1;
    }
}

@media (max-width: 720px) {
    body {
        padding-bottom: 96px;
    }

    .game-layout {
        display: flex;
        flex-direction: column;
    }

    .players-column {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        grid-auto-rows: minmax(92px, 1fr);
    }

    .player-card::after {
        top: 8px;
        right: 10px;
    }

    .player-bubble {
        display: none !important;
    }

    .chat-panel {
        max-height: none;
    }

    .chat-log {
        max-height: 220px;
        min-height: 160px;
    }
}
