html, body {
    height: 100%;
    overflow: hidden; /* app fits the viewport instead of scrolling */
}

body {
    margin: 0;
    display: flex;
    flex-direction: column;
    align-items: center; /* horizontally centers the toolbar card; canvasArea stretches full width and centers its own content */
    height: 100dvh; /* more reliable on mobile browsers with dynamic chrome; ignored by browsers that don't support it, falling back to 100vh above */
    background: #f0f0f0;
    font-family: sans-serif;
}

#toolbar {
    margin: 15px 0 0;
    flex-shrink: 0;
    background: white;
    padding: 10px 20px;
    border-radius: 8px;
    border: 2px solid #ccc;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
    display: none; /* revealed once a nickname is set */
    align-items: center;
    gap: 20px;
    flex-wrap: wrap;
    position: relative;
    z-index: 1000;
}

#toolbar.visible {
    display: flex;
}

#toolbar label {
    font-weight: bold;
    color: #333;
    font-size: 14px;
}

#colorPicker {
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    background: none;
}

#sizeSlider {
    cursor: pointer;
}

#sizeValue {
    min-width: 20px;
    font-weight: bold;
    color: #666;
}

#roomSwitcher {
    display: flex;
    align-items: center;
    gap: 8px;
    padding-left: 20px;
    border-left: 2px solid #eee;
}

#roomInput {
    border: 1px solid #ccc;
    border-radius: 4px;
    padding: 6px 8px;
    font-size: 14px;
    width: 140px;
}

#joinRoomBtn {
    border: none;
    border-radius: 4px;
    padding: 7px 14px;
    font-size: 14px;
    font-weight: bold;
    background: #333;
    color: white;
    cursor: pointer;
}

#joinRoomBtn:hover {
    background: #555;
}

#currentRoomLabel {
    font-size: 13px;
    color: #888;
}

#nicknameDisplay {
    display: flex;
    align-items: center;
    gap: 6px;
    padding-left: 20px;
    border-left: 2px solid #eee;
    font-size: 14px;
    color: #333;
}

#currentNicknameLabel {
    font-weight: bold;
}

#changeNicknameBtn {
    border: none;
    background: none;
    cursor: pointer;
    font-size: 14px;
    color: #888;
    padding: 2px 4px;
}

#changeNicknameBtn:hover {
    color: #333;
}

#toolSwitcher {
    display: flex;
    align-items: center;
    gap: 4px;
    padding-left: 20px;
    border-left: 2px solid #eee;
}

.tool-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: 1px solid #ccc;
    border-radius: 6px;
    background: white;
    color: #555;
    cursor: pointer;
    padding: 0;
}

.tool-btn svg {
    width: 20px;
    height: 20px;
}

.tool-btn:hover {
    background: #f5f5f5;
}

.tool-btn.active {
    background: #333;
    border-color: #333;
    color: white;
}

#resetViewBtn {
    height: 36px;
    padding: 0 10px;
    border: 1px solid #ccc;
    border-radius: 6px;
    background: white;
    color: #555;
    font-size: 12px;
    font-weight: bold;
    cursor: pointer;
}

#resetViewBtn:hover {
    background: #f5f5f5;
}

#canvasArea {
    flex: 1 1 auto;
    align-self: stretch; /* full width, overriding body's align-items:center for this child */
    width: 100%;
    min-height: 0; /* lets this flex child shrink properly instead of overflowing (classic flexbox gotcha) */
    margin-top: 8px; /* small guaranteed gap under the toolbar, even when the canvas fills all remaining height */
    display: flex;
    justify-content: center;
    align-items: center;
    box-sizing: border-box;
}

#canvasWrapper {
    position: relative;
    display: none; /* revealed once a nickname is set */
    /* Pan/zoom is applied here as a CSS transform (see canvas-sync.js).
       0 0 origin keeps the zoom math simple: scaling grows from the
       top-left corner rather than the center. */
    transform-origin: 0 0;
    will-change: transform;
}

#canvasWrapper.visible {
    display: block;
}

#canvasWrapper.zoom-transition {
    transition: transform 0.15s ease-out;
}

canvas {
    background: white;
    border: 2px solid #ccc;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    display: block;
    /* Border-box so the width/height JS sets to fit the viewport
       is the full rendered size — otherwise the 2px border would
       make getBoundingClientRect() slightly larger than intended,
       throwing off the screen-to-canvas coordinate scaling. */
    box-sizing: border-box;
    /* Stop the browser from treating drawing/pan/zoom gestures as
       scroll/pan/zoom of the page — critical on touch devices. */
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none; /* disable iOS long-press menu */
}

#paintCanvas.tool-draw {
    cursor: crosshair;
}

#paintCanvas.tool-move {
    cursor: grab;
}

#paintCanvas.tool-move.grabbing {
    cursor: grabbing;
}

#paintCanvas.tool-zoom {
    cursor: zoom-in;
}

.stroke-label {
    position: absolute;
    transform: translate(-50%, -100%);
    margin-top: -6px; /* small fixed gap above the point, consistent at any canvas scale */
    background: rgba(0, 0, 0, 0.75);
    color: white;
    font-size: 11px;
    font-weight: bold;
    padding: 2px 6px;
    border-radius: 4px;
    pointer-events: none;
    white-space: nowrap;
    z-index: 10;
}

#status {
    position: absolute;
    top: 10px;
    left: 10px;
    padding: 5px 10px;
    background: rgba(0, 0, 0, 0.7);
    color: white;
    border-radius: 4px;
}

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal-overlay.hidden {
    display: none;
}

.modal-box {
    background: white;
    padding: 30px 40px;
    border-radius: 10px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
    text-align: center;
    display: flex;
    flex-direction: column;
    gap: 12px;
    min-width: 260px;
}

.modal-box h2 {
    margin: 0 0 4px;
    color: #333;
    font-size: 18px;
}

#nicknameInput {
    padding: 10px 12px;
    border: 1px solid #ccc;
    border-radius: 6px;
    font-size: 15px;
}

#nicknameSubmitBtn {
    padding: 10px 16px;
    border: none;
    border-radius: 6px;
    background: #333;
    color: white;
    font-weight: bold;
    font-size: 14px;
    cursor: pointer;
}

#nicknameSubmitBtn:hover {
    background: #555;
}

@media (max-width: 600px) {
    #toolbar {
        gap: 10px;
        padding: 8px 12px;
        font-size: 13px;
    }

    #roomSwitcher,
    #nicknameDisplay,
    #toolSwitcher {
        padding-left: 10px;
    }

    #roomInput {
        width: 100px;
    }

    .tool-btn {
        width: 32px;
        height: 32px;
    }

    .tool-btn svg {
        width: 18px;
        height: 18px;
    }

    #resetViewBtn {
        height: 32px;
        padding: 0 8px;
        font-size: 11px;
    }
}

@media (max-width: 400px) {
    .modal-box {
        min-width: unset;
        width: calc(100% - 48px);
        padding: 24px 20px;
    }
}