:root {
    --bg-dark: #0a0a0b;
    --bg-panel: rgba(26, 26, 29, 0.8);
    --accent: #ff4757;
    --text: #e0e0e0;
    --text-dim: #a0a0a0;
    --border: rgba(255, 255, 255, 0.1);
    --glass: rgba(255, 255, 255, 0.05);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html,
body {
    height: 100%;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Inter', 'Noto Sans JP', sans-serif;
    background-color: var(--bg-dark);
    color: var(--text);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.header {
    min-height: 80px;
    background: var(--bg-panel);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    /* Vertical centering via flex and min-height */
    z-index: 100;
    gap: 15px;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 15px;
    flex: 1;
    justify-content: flex-end;
}

.menu-toggle {
    display: none;
    padding: 8px 15px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 0.8rem;
    font-weight: bold;
    cursor: pointer;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
    white-space: nowrap;
}

.logo-area {
    display: flex;
    flex-direction: column;
    padding: 8px 0;
    /* Add padding here to push content down */
}

.logo {
    font-size: 1.3rem;
    font-weight: 700;
    letter-spacing: -0.01em;
    color: var(--text);
    line-height: 1.2;
    padding-top: 5px;
    /* Explicitly push text down from the top edge */
}

/* --- Rest of the file cleanup --- */
@media (max-height: 500px) and (orientation: landscape) {
    .header {
        min-height: 50px;
        padding: 5px 15px;
    }

    .sidebar {
        padding-top: 10px;
    }

    .d-pad-container {
        transform: scale(0.8);
        transform-origin: bottom right;
        bottom: 10px;
        right: 10px;
    }
}

/* Chat UI (Moved outside small landscape media query) */
.chat-toggle-btn {
    position: fixed;
    bottom: 30px;
    right: 170px;
    /* Moved further left to clear D-pad */
    padding: 0 20px;
    height: 50px;
    border-radius: 25px;
    background: var(--accent);
    color: white;
    border: none;
    font-size: 1rem;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    cursor: pointer;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.5);
    z-index: 1000;
    transition: transform 0.2s;
}

.chat-toggle-btn:hover {
    transform: scale(1.1);
}

.chat-unread-badge {
    position: absolute;
    top: -2px;
    right: -2px;
    width: 12px;
    height: 12px;
    background: #ff0000;
    border-radius: 50%;
    border: 2px solid var(--bg-dark);
}

.chat-container {
    position: fixed;
    bottom: 90px;
    right: 30px;
    width: 320px;
    height: 400px;
    background: var(--bg-panel);
    backdrop-filter: blur(15px);
    border: 1px solid var(--border);
    border-radius: 12px;
    display: flex;
    flex-direction: column;
    z-index: 1000;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
    overflow: hidden;
    animation: slide-up 0.3s ease-out;
}

@keyframes slide-up {
    from {
        opacity: 0;
        transform: translateY(20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.chat-header {
    padding: 12px 15px;
    background: rgba(255, 255, 255, 0.05);
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.chat-header h3 {
    margin: 0;
    font-size: 0.9rem;
    color: var(--text);
}

#chat-close-btn {
    background: none;
    border: none;
    color: var(--text-dim);
    font-size: 1.2rem;
    cursor: pointer;
    padding: 0 5px;
}

.chat-messages {
    flex: 1;
    padding: 15px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.chat-message {
    max-width: 85%;
    padding: 8px 12px;
    border-radius: 12px;
    font-size: 0.85rem;
    line-height: 1.4;
    word-break: break-word;
    position: relative;
    animation: fade-in 0.2s ease-out;
}

@keyframes fade-in {
    from {
        opacity: 0;
        transform: translateY(5px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.chat-message.self {
    align-self: flex-end;
    background: var(--accent);
    color: white;
    border-bottom-right-radius: 2px;
}

.chat-message.other {
    align-self: flex-start;
    background: rgba(255, 255, 255, 0.1);
    color: var(--text);
    border-bottom-left-radius: 2px;
}

.chat-message.system {
    align-self: center;
    background: none;
    color: var(--text-dim);
    font-size: 0.7rem;
    text-align: center;
    padding: 5px;
    border: 1px dashed var(--border);
    width: 100%;
}

.chat-input-area {
    padding: 10px;
    border-top: 1px solid var(--border);
    display: flex;
    gap: 8px;
    background: rgba(0, 0, 0, 0.2);
}

#chat-input {
    flex: 1;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid var(--border);
    border-radius: 20px;
    padding: 8px 12px;
    color: white;
    font-family: inherit;
    font-size: 0.9rem;
}

#chat-send-btn {
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 20px;
    padding: 8px 16px;
    font-weight: bold;
    cursor: pointer;
    font-size: 0.8rem;
}

@media (max-width: 768px) {
    .chat-container {
        right: 0;
        bottom: 0;
        width: 100%;
        height: 50vh;
        border-radius: 12px 12px 0 0;
    }

    .chat-toggle-btn {
        right: auto;
        left: 20px;
        bottom: 90px;
    }
}

.d-pad-container.dimmed,
.chat-toggle-btn.dimmed {
    opacity: 0.2;
    pointer-events: none;
    transition: opacity 0.3s;
}

.tagline {
    font-size: 0.7rem;
    color: var(--text-dim);
    font-weight: 300;
}

.ad-banner {
    background: var(--glass);
    height: 40px;
    width: 320px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    color: var(--text-dim);
    border: 1px dashed var(--border);
    border-radius: 4px;
}

.main-container {
    flex: 1;
    display: flex;
    overflow: hidden;
}

.sidebar {
    width: 220px;
    background: var(--bg-panel);
    backdrop-filter: blur(10px);
    border-right: 1px solid var(--border);
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    height: 100%;
    overflow-y: auto;
}

/* Force visibility on desktop even if 'collapsed' class is present */
@media (min-width: 769px) {
    .sidebar.collapsed {
        visibility: visible !important;
        opacity: 1 !important;
        transform: none !important;
        position: static !important;
        width: 220px !important;
        height: 100% !important;
        padding: 10px !important;
        max-height: none !important;
        pointer-events: auto !important;
    }
}


.share-btn {
    font-size: 0.65rem;
}

.tool-section {
    padding-bottom: 5px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.header-meta {
    display: flex;
    align-items: center;
    gap: 12px;
}

.presented-by {
    font-size: 0.65rem;
    opacity: 0.7;
}

.presented-by a {
    color: inherit;
    text-decoration: underline;
}

.presented-by a:hover {
    opacity: 1;
}

.participant-count {
    font-size: 0.9rem;
    font-weight: 700;
    background: #2ed573;
    color: #000;
    padding: 4px 12px;
    border-radius: 20px;
    display: flex;
    align-items: center;
    gap: 6px;
    box-shadow: 0 0 15px rgba(46, 213, 115, 0.4);
    animation: live-pulse 2s infinite;
}

@keyframes live-pulse {
    0% {
        transform: scale(1);
        box-shadow: 0 0 15px rgba(46, 213, 115, 0.4);
    }

    50% {
        transform: scale(1.05);
        box-shadow: 0 0 25px rgba(46, 213, 115, 0.6);
    }

    100% {
        transform: scale(1);
        box-shadow: 0 0 15px rgba(46, 213, 115, 0.4);
    }
}

.tool-section h3 {
    font-size: 0.6rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-dim);
    margin-bottom: 5px;
}

/* Horizontal Grid for Tools */
.tool-group.grid-4 {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 4px;
}

.tool-group.grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4px;
}

.tool-btn,
.action-btn {
    width: 100%;
    padding: 8px 4px;
    background: var(--glass);
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s;
    font-size: 0.65rem;
    text-align: center;
    margin-bottom: 4px;
}

.tool-btn.active,
.tool-btn:hover,
.action-btn:hover {
    background: var(--accent);
    border-color: var(--accent);
}

.tool-btn,
.action-btn,
.board-tab,
.color-swatch,
.logo {
    user-select: none;
    -webkit-user-select: none;
}

.color-control {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

#color-picker {
    width: 100%;
    height: 24px;
    border: none;
    background: none;
    cursor: pointer;
}

.preset-colors {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 4px;
    margin-top: 10px;
}

.color-swatch {
    aspect-ratio: 1;
    border-radius: 4px;
    cursor: pointer;
    border: 1px solid var(--border);
}

#width-slider {
    width: 100%;
    margin-bottom: 5px;
}

#width-value {
    font-size: 0.8rem;
    color: var(--text-dim);
}

.board-tabs {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 6px;
}

.board-tab {
    padding: 6px 4px;
    background: var(--glass);
    border: 1px solid var(--border);
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.75rem;
    text-align: center;
    transition: all 0.2s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.board-tab.active {
    background: rgba(255, 255, 255, 0.15);
    border-color: var(--text);
}

.zoom-controls-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 4px;
    background: var(--glass);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 2px 4px;
    width: 100%;
}

.zoom-btn {
    padding: 2px 6px;
    background: transparent;
    border: none;
    font-size: 1rem;
    cursor: pointer;
    color: var(--text);
    display: flex;
    align-items: center;
    justify-content: center;
}

#zoom-level {
    font-size: 0.7rem;
    color: var(--text-dim);
    min-width: 35px;
    text-align: center;
}

.small-btn {
    font-size: 0.6rem !important;
    padding: 4px 6px !important;
    width: auto !important;
    margin: 0 !important;
}

.canvas-wrapper {
    flex: 1;
    background: #000;
    position: relative;
    overflow: auto;
    display: block;
    /* Use block on PC to avoid flex-stretch issues */
    padding: 40px;
}

.canvas-container {
    background-color: #fff;
    box-shadow: 0 0 40px rgba(0, 0, 0, 0.5);
    position: relative;
    width: 2000px;
    height: 2000px;
    aspect-ratio: 1 / 1;
    /* Force square aspect ratio */
    flex-shrink: 0;
    /* Prevent flexbox from squishing the container */
    transform-origin: 0 0;
    transition: width 0.2s ease-out, height 0.2s ease-out;
    overflow: hidden;
}

.canvas-bg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100% !important;
    height: 100% !important;
    background-size: cover;
    background-position: center;
    z-index: 0;
    pointer-events: none;
}

#main-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100% !important;
    height: 100% !important;
    cursor: crosshair;
    touch-action: none;
    background-color: transparent;
}

.replay-controls {
    position: fixed;
    bottom: 30px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--bg-panel);
    backdrop-filter: blur(15px);
    border: 1px solid var(--border);
    padding: 15px 25px;
    border-radius: 50px;
    display: flex;
    align-items: center;
    gap: 20px;
    z-index: 1000;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}

#replay-close {
    position: absolute;
    top: -15px;
    right: -15px;
    width: 30px;
    height: 30px;
    background: var(--accent);
    color: white;
    border: 2px solid white;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    font-weight: bold;
    z-index: 1001;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
}

.hidden {
    display: none !important;
}

/* Ad Overlay */
.ad-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(5px);
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
}

.ad-modal {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    padding: 40px;
    border-radius: 12px;
    max-width: 500px;
    width: 90%;
    text-align: center;
}

.ad-modal h3 {
    margin-bottom: 20px;
}

.ad-modal-content {
    background: var(--glass);
    height: 200px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 20px;
    border-radius: 8px;
}

#close-ad {
    padding: 10px 30px;
    background: var(--accent);
    border: none;
    color: #fff;
    border-radius: 6px;
    cursor: pointer;
}

/* D-Pad Controls */
.d-pad-container {
    position: fixed;
    bottom: 30px;
    right: 30px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
    z-index: 2000;
}



.d-pad-row {
    display: flex;
    gap: 5px;
}

.d-pad-btn {
    width: 60px;
    height: 60px;
    background: var(--accent);
    color: white;
    border: 3px solid rgba(255, 255, 255, 0.2);
    border-radius: 12px;
    font-size: 1.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.5);
    user-select: none;
    transition: transform 0.1s, background 0.1s;
}

.d-pad-btn:active {
    background: #00d2ff;
    transform: scale(0.95);
}

/* Welcome Modal Specifics */
.welcome-modal {
    max-width: 600px;
    padding: 50px 40px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.8);
}

/* Mobile Responsive Overhaul */
@media (max-width: 768px) {
    body {
        height: 100vh;
        overflow: hidden;
        display: flex;
        flex-direction: column;
        height: -webkit-fill-available;
    }

    .header {
        flex-shrink: 0;
        height: auto;
        padding: 10px 15px;
        flex-direction: row;
        /* Keep header items in a row to allow toggle beside logo */
        justify-content: space-between;
        align-items: center;
        gap: 8px;
        background: var(--bg-panel);
    }

    .header-actions {
        width: auto;
        flex: 0;
    }

    .menu-toggle {
        display: block;
        /* Show on mobile */
    }

    .ad-banner.top-ad {
        display: none;
        /* Hide ad when menu toggle is visible to save space */
    }

    .logo-area {
        align-items: flex-start;
    }

    .tagline {
        display: none;
    }

    .ad-banner.top-ad {
        width: 100%;
        height: 30px;
        font-size: 0.6rem;
    }

    .main-container {
        flex: 1;
        flex-direction: column;
        height: 0;
        min-height: 0;
        overflow: hidden;
        display: flex;
    }

    .sidebar {
        position: fixed;
        /* Overlay on mobile */
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        max-height: 100vh;
        z-index: 5000;
        background: var(--bg-panel);
        backdrop-filter: blur(10px);
        /* Reduced from 20px for performance */
        -webkit-backdrop-filter: blur(10px);
        flex-direction: column;
        padding: 30px;
        padding-bottom: 100px;
        /* Space for sticky bottom button */
        transition: visibility 0.3s, opacity 0.3s, transform 0.3s ease;
        overflow-y: auto;
        visibility: visible;
        opacity: 1;
        transform: translateY(0);
        border: none;
    }

    .sidebar.collapsed {
        visibility: hidden;
        opacity: 0;
        transform: translateY(-20px);
        pointer-events: none;
        max-height: 0 !important;
        padding: 0 !important;
    }

    .mobile-close-container {
        display: block;
        width: 100%;
        padding: 40px 0 20px;
        margin-top: auto;
        padding-bottom: env(safe-area-inset-bottom, 20px);
        /* Safe area */
    }

    .menu-toggle.full-width {
        width: 100%;
        padding: 15px;
        font-size: 1rem;
        min-height: 44px;
        /* Touch target */
    }

    .tool-section {
        border-bottom: 1px solid rgba(255, 255, 255, 0.05);
        padding-bottom: 15px;
        display: flex;
        flex-direction: column;
        width: 100%;
    }

    .tool-section:last-child {
        border-bottom: none;
    }

    .tool-section h3 {
        margin-bottom: 8px;
        font-size: 0.7rem;
        text-align: left;
    }

    .tool-group.grid-4 {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 8px;
    }

    .tool-btn,
    .action-btn {
        width: 100%;
        min-width: unset;
        padding: 12px 10px;
        /* Larger padding */
        font-size: 0.8rem;
        /* Slightly larger text */
        margin-bottom: 6px;
        min-height: 44px;
        /* Minimum touch target */
    }

    .preset-colors {
        display: grid;
        grid-template-columns: repeat(5, 1fr);
        margin-top: 5px;
        gap: 10px;
    }

    .color-swatch {
        width: 100%;
        aspect-ratio: 1;
        height: auto;
    }

    .board-tabs {
        display: grid;
        grid-template-columns: repeat(5, 1fr);
        gap: 8px;
    }

    .board-tab {
        padding: 8px 4px;
        width: 100%;
        font-size: 0.75rem;
    }

    .action-row {
        display: flex;
        gap: 10px;
        width: 100%;
    }

    .small-btn {
        flex: 1;
        width: 100% !important;
    }

    .canvas-wrapper {
        padding: 0;
        flex: 1;
        min-height: 0;
        overflow: auto !important;
        background: #111;
        position: relative;
        z-index: 50;
        display: block;
        -webkit-overflow-scrolling: touch;
        width: 100vw;
    }

    .d-pad-container {
        bottom: 20px;
        right: 20px;
    }

    .mobile-zoom-controls {
        display: flex;
    }

    .d-pad-btn {
        width: 60px;
        /* Consistent size */
        height: 60px;
        font-size: 1.4rem;
        touch-action: none;
        /* Essential for reliable touch events */
    }

    /* Width-based zoom for better scroll reliability */
    .canvas-container {
        margin: 0;
        transform: none !important;
        /* Use width/height instead of transform */
        width: 1600px;
        height: 1600px;
        display: block;
        overflow: visible;
        position: relative;
    }

    #main-canvas,
    .canvas-bg {
        width: 100%;
        height: 100%;
        display: block;
    }

    .replay-controls {
        width: 90%;
        bottom: 20px;
        padding: 12px 20px;
        gap: 10px;
    }
}

/* Landscape Mode Optimization */
@media (max-height: 500px) and (orientation: landscape) {
    .header {
        height: 40px;
        padding: 5px 15px;
    }

    .sidebar {
        padding-top: 10px;
    }

    .d-pad-container {
        transform: scale(0.8);
        transform-origin: bottom right;
        bottom: 10px;
        right: 10px;
    }
}

.tool-btn,
.action-btn {
    padding: 8px 10px;
    min-height: 36px;
}