* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", sans-serif;
    background: linear-gradient(180deg, #87ceeb 0%, #b8e986 100%);
    min-height: 100vh;
    color: #1a1a1a;
}

.container {
    max-width: 480px;
    margin: 0 auto;
    padding: 24px 16px;
}

.room-container {
    padding-bottom: 24px;
}

.title {
    font-size: 32px;
    text-align: center;
    margin-bottom: 8px;
}

.subtitle {
    text-align: center;
    color: #555;
    margin-bottom: 32px;
}

.card {
    background: white;
    border-radius: 16px;
    padding: 20px;
    margin-bottom: 16px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.card h2 { margin-bottom: 12px; font-size: 18px; }

input[type="text"] {
    width: 100%;
    padding: 12px;
    border: 1px solid #ddd;
    border-radius: 8px;
    font-size: 16px;
    margin-bottom: 12px;
}

button {
    width: 100%;
    padding: 12px;
    background: #2c3e50;
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    cursor: pointer;
}

button:hover:not(:disabled) { background: #34495e; }

button:disabled {
    background: #aaa;
    cursor: not-allowed;
}

.error-banner {
    background: #ffe0e0;
    color: #c00;
    padding: 12px;
    border-radius: 8px;
    margin-top: 12px;
    text-align: center;
}

.code-display {
    font-size: 32px;
    font-weight: bold;
    letter-spacing: 4px;
    text-align: center;
    padding: 12px;
    background: #f5f5f5;
    border-radius: 8px;
    margin: 12px 0;
}

.player-list {
    list-style: none;
    padding: 0;
    margin: 6px 0 0 0;
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.player-list li {
    padding: 4px 10px;
    background: #f0f0f0;
    border-radius: 999px;
    margin: 0;
    display: inline-flex;
    align-items: center;
    font-size: 13px;
    line-height: 1.4;
}

.player-list li.host {
    background: #fce4e4;
}
.player-list li.host::before {
    content: "방장";
    font-size: 11px;
    font-weight: 700;
    color: #c0392b;
    margin-right: 6px;
}

.player-list .insect-icon {
    margin-left: 4px;
    font-size: 14px;
}

.player-list .insect-pending {
    margin-left: 4px;
    opacity: 0.4;
    filter: grayscale(100%);
}

.section-heading {
    margin-top: 14px;
    margin-bottom: 6px;
    font-size: 15px;
    display: flex;
    align-items: center;
    gap: 8px;
}
.section-heading .heading-sub {
    font-size: 12px;
    color: #888;
    font-weight: 500;
}

.details-row {
    display: flex;
    gap: 8px;
    margin: 8px 0 4px 0;
}
.details-row > details {
    flex: 1;
    background: #f7f7f7;
    border-radius: 8px;
    padding: 6px 10px;
    font-size: 13px;
}
.details-row > details summary {
    cursor: pointer;
    font-weight: 600;
    color: #555;
    padding: 2px 0;
    list-style: none;
}
.details-row > details summary::-webkit-details-marker { display: none; }
.details-row > details summary::before {
    content: '▸';
    display: inline-block;
    color: #888;
    margin-right: 4px;
    transition: transform 0.15s ease;
}
.details-row > details[open] summary::before {
    transform: rotate(90deg);
}

.game-history-details summary {
    cursor: pointer;
    font-weight: 600;
    color: #555;
    padding: 6px 0;
}

.game-history-list {
    list-style: none;
    padding-left: 0;
    margin: 8px 0 0 0;
    max-height: 240px;
    overflow-y: auto;
}

.game-history-list li {
    padding: 6px 10px;
    background: #f8f8f8;
    border-radius: 4px;
    margin-bottom: 3px;
    font-size: 14px;
    display: flex;
    align-items: center;
}

.game-history-num {
    color: #888;
    min-width: 50px;
    font-weight: 600;
}

.game-history-winner {
    color: #222;
}

.game-history-empty {
    color: #999;
    font-style: italic;
    background: transparent !important;
}

.insect-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    margin: 6px 0 4px 0;
}

.insect-card {
    background: white;
    border: 2px solid #ddd;
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 26px;
    transition: all 0.2s;
    position: relative;
    padding: 8px 4px 6px 4px;
}

.insect-info-btn {
    display: inline-block;
    box-sizing: border-box;
    position: absolute;
    top: 2px;
    right: 2px;
    width: 16px;
    height: 16px;
    padding: 0;
    border: 1px solid #bbb;
    border-radius: 50%;
    background: white;
    color: #666;
    font-size: 10px;
    font-weight: 700;
    line-height: 14px;
    cursor: help;
    text-align: center;
    user-select: none;
}
.insect-info-btn:hover {
    background: #f5f5f5;
    border-color: #888;
    color: #222;
}

.matchup-balloon {
    position: absolute;
    width: 300px;
    background: #fff;
    border: 1px solid #d0d0d0;
    border-radius: 10px;
    padding: 12px 14px;
    box-shadow: 0 6px 18px rgba(0,0,0,0.18);
    z-index: 1000;
    font-size: 13px;
    line-height: 1.55;
}
.matchup-balloon .matchup-title {
    font-size: 15px;
    font-weight: 700;
    margin-bottom: 6px;
}
.matchup-balloon .matchup-row {
    display: flex;
    gap: 6px;
    margin-top: 2px;
}
.matchup-balloon .matchup-key {
    color: #777;
    font-weight: 600;
    min-width: 38px;
    flex-shrink: 0;
}
.matchup-balloon .matchup-val {
    color: #222;
}
.matchup-balloon .matchup-close {
    position: absolute;
    top: 4px;
    right: 6px;
    background: transparent;
    border: none;
    cursor: pointer;
    font-size: 14px;
    color: #999;
    padding: 2px 6px;
}
.matchup-balloon .matchup-close:hover { color: #333; }

.insect-card:hover:not(.taken) { transform: translateY(-2px); border-color: #2c3e50; }
.insect-card.mine { border-color: #27ae60; background: #e8f8e8; }
.insect-card.taken { opacity: 0.4; cursor: not-allowed; }
.insect-card .name { font-size: 11px; margin-top: 2px; }
.insect-card .owner { font-size: 10px; color: #666; }

.player-list .boon-icon {
    margin-left: 4px;
    font-size: 14px;
}

.boon-hint {
    text-align: center;
    color: #555;
    font-size: 12px;
    margin-top: 4px;
}

.boon-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    margin: 6px 0 4px 0;
}

.boon-card {
    background: white;
    border: 2px solid #ddd;
    border-radius: 10px;
    padding: 8px 6px;
    text-align: center;
    cursor: pointer;
    transition: transform 0.15s, border-color 0.15s, background 0.15s;
}
.boon-card:hover { transform: translateY(-2px); border-color: #2c3e50; }
.boon-card.chosen { border-color: #e67e22; background: #fff4e6; }
.boon-card .boon-emoji { font-size: 22px; line-height: 1.1; }
.boon-card .boon-name { font-size: 12px; font-weight: 700; margin-top: 2px; }
.boon-card .boon-desc { font-size: 10px; color: #555; margin-top: 2px; line-height: 1.25; }
.boon-empty {
    grid-column: 1 / -1;
    color: #999;
    text-align: center;
    padding: 14px;
    font-style: italic;
}

.penalty-input {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}
.penalty-input label { min-width: 50px; font-weight: bold; }
.penalty-input input { margin: 0; }

#race-canvas {
    width: 100%;
    height: auto;
    max-height: 70vh;
    aspect-ratio: 5 / 3;
    background: linear-gradient(180deg, #87ceeb 0%, #c8e88a 100%);
    border-radius: 12px;
    display: block;
}

.canvas-wrap {
    position: relative;
    width: 100%;
}
.countdown-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: clamp(96px, 18vmin, 220px);
    font-weight: bold;
    color: white;
    text-shadow: 0 6px 16px rgba(0,0,0,0.55);
    pointer-events: none;
    background: rgba(0,0,0,0.08);
    border-radius: 12px;
}
.countdown-overlay[hidden] { display: none; }

.ranking-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    margin-bottom: 8px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    animation: slideIn 0.5s ease-out;
}

@keyframes slideIn {
    from { transform: translateX(-30px); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}

.ranking-row .rank { font-size: 24px; font-weight: bold; min-width: 60px; }
.ranking-row .insect { font-size: 32px; }
.ranking-row .info { flex: 1; }
.ranking-row .nickname { font-weight: bold; }
.ranking-row .penalty { color: #c0392b; margin-top: 4px; }

.section { display: none; }
.section.active { display: block; }

.host-only { display: none; }
.is-host .host-only { display: block; }

.guest-only, .guest-only-waiting { display: block; }
.is-host .guest-only, .is-host .guest-only-waiting { display: none; }

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}
.btn-compact {
    width: auto;
    padding: 8px 14px;
    font-size: 13px;
}
.btn-mini {
    width: auto;
    padding: 4px 10px;
    font-size: 11px;
    margin-left: 8px;
    background: #888;
}
.btn-secondary { background: #aaa; }
.btn-add {
    width: auto;
    padding: 6px 12px;
    background: #666;
    margin-bottom: 12px;
    font-size: 13px;
}

.room-list { margin-top: 8px; }
.room-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px;
    background: #f5f7fa;
    border-radius: 8px;
    margin-bottom: 8px;
    gap: 12px;
}
.room-row.disabled { opacity: 0.5; }
.room-info { flex: 1; min-width: 0; }
.room-code {
    font-weight: bold;
    font-size: 20px;
    letter-spacing: 2px;
}
.room-name-display {
    font-weight: 700;
    font-size: 18px;
    color: #222;
    margin-bottom: 2px;
    word-break: keep-all;
}
.room-code-inline {
    font-family: monospace;
    color: #777;
    letter-spacing: 1px;
}
.room-name-row {
    display: flex;
    gap: 6px;
    align-items: center;
}
.room-name-row input { flex: 1; }
.room-name-row .btn-mini {
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    padding: 0;
    font-size: 16px;
}
.room-meta {
    font-size: 12px;
    color: #555;
    margin-top: 2px;
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}
.state-badge {
    padding: 2px 8px;
    border-radius: 10px;
    background: #ddd;
    font-weight: bold;
    font-size: 11px;
}
.state-WAITING { background: #d4edda; color: #155724; }
.state-RACE_READY { background: #fff3cd; color: #856404; }
.state-RACING { background: #cce5ff; color: #004085; }
.state-FINISHED { background: #e2e3e5; color: #383d41; }
.join-btn {
    width: auto;
    padding: 8px 14px;
    background: #27ae60;
}
.join-btn:disabled { background: #aaa; }
.empty-msg { color: #999; text-align: center; padding: 20px; }

.modal {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 16px;
}
.modal[hidden] { display: none; }
.modal-content {
    background: white;
    border-radius: 12px;
    padding: 20px;
    max-width: 440px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
}
.modal-content h2 { margin-bottom: 12px; }
.modal-content label {
    display: block;
    font-size: 13px;
    color: #555;
    margin: 10px 0 4px;
    font-weight: bold;
}
.modal-actions {
    display: flex;
    gap: 8px;
    margin-top: 16px;
}
.hint { color: #555; font-size: 13px; margin-bottom: 8px; }

.penalty-section {
    margin-bottom: 12px;
}
.penalty-section-title {
    font-size: 12px;
    color: #666;
    margin-bottom: 4px;
    font-weight: bold;
}
.penalty-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
}
.rank-label {
    min-width: 38px;
    font-weight: bold;
    text-align: center;
    font-size: 13px;
}
.penalty-input {
    flex: 1;
    margin: 0 !important;
}
.remove-btn {
    width: 28px;
    height: 28px;
    background: #e74c3c;
    color: white;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    font-size: 18px;
    line-height: 1;
    padding: 0;
}

.header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 6px;
}
.code-chip {
    background: #34495e;
    color: white;
    padding: 6px 12px;
    border-radius: 6px;
    font-weight: bold;
    letter-spacing: 2px;
}
.host-info { color: #666; margin-bottom: 12px; font-size: 13px; }
.penalty-summary {
    font-size: 13px;
}
.penalty-summary summary { cursor: pointer; font-weight: 600; }
.penalty-summary ul { margin: 6px 0 0 20px; }
.penalty-summary li { margin: 3px 0; }

body.race-mode .container {
    max-width: 1200px;
    padding: 16px;
}
body.race-mode .room-container {
    padding-bottom: 16px;
}
body.race-mode #race-canvas {
    width: 100%;
    height: auto;
    max-height: 80vh;
}
.race-info {
    margin-top: 8px;
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    font-weight: bold;
}

.chat-panel {
    background: white;
    border-radius: 12px;
    padding: 10px 12px;
    margin-top: 12px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.08);
}
.chat-title {
    font-size: 13px;
    color: #2c3e50;
    margin-bottom: 6px;
    font-weight: 700;
}
.chat-messages {
    height: 110px;
    overflow-y: auto;
    padding: 6px 8px;
    background: #f8f9fa;
    border-radius: 6px;
    margin-bottom: 6px;
    font-size: 13px;
    line-height: 1.5;
}
.chat-message {
    padding: 1px 0;
    word-wrap: break-word;
    word-break: break-word;
}
.chat-message .author {
    font-weight: bold;
    color: #2c3e50;
    margin-right: 4px;
}
.chat-message .author.host-author { color: #c0392b; }
.chat-message .author.mine-author { color: #27ae60; }
.chat-message.system { color: #888; font-style: italic; }
.chat-form {
    display: flex;
    gap: 8px;
}
.chat-form input {
    flex: 1;
    margin: 0 !important;
    padding: 8px 10px;
    font-size: 14px;
}
.chat-form button {
    width: auto;
    padding: 8px 16px;
    font-size: 14px;
}
