/* ═══════════════════════════════════════════════
   絵茶室 CSS — 古き良きインターネット風
   ═══════════════════════════════════════════════ */

:root {
    --bg-main: #87D4DA;
    --bg-panel: #c0c0c0;
    --bg-dark: #808080;
    --bg-light: #dfdfdf;
    --bg-white: #ffffff;
    --bg-canvas: #e8e8e8;
    --border-raised: #ffffff #808080 #808080 #ffffff;
    --border-sunken: #808080 #ffffff #ffffff #808080;
    --text-main: #333;
    --text-blue: #000080;
    --text-link: #0000CD;
    --accent-orange: #FF9966;
    --accent-teal: #02A9C0;
    --accent-pink: #D96D9F;
    --font-pixel: 'VT323', 'DotGothic16', monospace;
    --font-body: 'M PLUS 1p', 'MS PGothic', 'ＭＳ Ｐゴシック', sans-serif;
}

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

body {
    font-family: var(--font-body);
    font-size: 13px;
    color: var(--text-main);
    background: var(--bg-main);
    background-image:
        radial-gradient(circle at 20% 50%, rgba(255,255,255,0.1) 0%, transparent 50%),
        radial-gradient(circle at 80% 20%, rgba(255,255,255,0.08) 0%, transparent 40%);
    line-height: 1.4;
    display: flex;
    flex-direction: column;
    height: 100dvh;
    min-height: 100vh;
    overflow: hidden;
}

.hidden { display: none !important; }

/* ═══ Win95 パネル ═══ */
.win-panel {
    background: var(--bg-panel);
    border-width: 2px;
    border-style: solid;
    border-color: var(--border-raised);
    padding: 3px;
}
.win-panel-inner {
    background: var(--bg-panel);
    border: 1px solid #999;
    padding: 6px;
}
.win-title-bar {
    background: linear-gradient(90deg, #000080, #1084d0);
    color: #fff;
    font-weight: bold;
    font-size: 12px;
    padding: 2px 6px;
    margin-bottom: 4px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-family: var(--font-body);
}
.win-close-btn {
    background: var(--bg-panel);
    border-width: 2px;
    border-style: solid;
    border-color: var(--border-raised);
    color: #000;
    font-size: 10px;
    width: 18px;
    height: 18px;
    cursor: pointer;
    line-height: 1;
    padding: 0;
}
.win-close-btn:active { border-color: var(--border-sunken); }
.win-input {
    border: 2px solid;
    border-color: var(--border-sunken);
    background: #fff;
    padding: 3px 5px;
    font-family: var(--font-body);
    font-size: 13px;
    outline: none;
    width: 100%;
}
.win-input:focus { outline: 1px dotted #000; }
.win-btn {
    background: var(--bg-panel);
    border-width: 2px;
    border-style: solid;
    border-color: var(--border-raised);
    padding: 4px 14px;
    font-family: var(--font-body);
    font-size: 13px;
    font-weight: bold;
    cursor: pointer;
    color: #000;
}
.win-btn:active { border-color: var(--border-sunken); }
.win-btn:disabled { color: #888; }
.win-btn-sm {
    background: var(--bg-panel);
    border-width: 1px;
    border-style: solid;
    border-color: var(--border-raised);
    padding: 2px 8px;
    font-size: 12px;
    cursor: pointer;
    font-family: var(--font-body);
}
.win-btn-sm:active { border-color: var(--border-sunken); }

/* ═══ タイトルバー ═══ */
.title-bar {
    background: linear-gradient(180deg, #1a5276 0%, #0d3b5e 100%);
    background-image:
        linear-gradient(180deg, #1a5276 0%, #0d3b5e 100%),
        repeating-linear-gradient(
            0deg,
            transparent,
            transparent 2px,
            rgba(255,255,255,0.03) 2px,
            rgba(255,255,255,0.03) 4px
        );
    color: #fff;
    text-align: center;
    padding: 8px 10px 6px;
    border-bottom: 3px solid;
    border-color: #fff #333 #333 #fff;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}
.title-bar-inner {
    font-family: var(--font-pixel);
    font-size: 28px;
    letter-spacing: 3px;
}
.title-icon { font-size: 22px; vertical-align: middle; }
.title-text { vertical-align: middle; }
.title-subtitle {
    font-size: 12px;
    color: #aaddff;
    margin-top: 2px;
    letter-spacing: 1px;
    font-family: var(--font-body);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
}
.lang-switcher {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 11px;
    color: #aaddff;
    margin-left: 8px;
}
.lang-btn {
    background: transparent;
    border: 1px solid rgba(255,255,255,0.3);
    color: #aaddff;
    font-size: 11px;
    padding: 1px 8px;
    cursor: pointer;
    font-family: var(--font-body);
    border-radius: 2px;
    transition: all 0.15s;
}
.lang-btn:hover { background: rgba(255,255,255,0.15); color: #fff; }
.lang-btn.active {
    background: rgba(255,255,255,0.25);
    color: #fff;
    border-color: rgba(255,255,255,0.6);
    font-weight: bold;
}
.lang-sep { color: rgba(255,255,255,0.3); font-size: 10px; }
.admin-link {
    color: #aaddff;
    font-size: 11px;
    text-decoration: none;
    border: 1px solid rgba(255,255,255,0.3);
    padding: 1px 8px;
    border-radius: 2px;
    transition: all 0.15s;
}
.admin-link:hover { background: rgba(255,255,255,0.15); color: #fff; }

/* ═══ 入室画面 ═══ */
.entry-screen {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: calc(100vh - 80px);
    padding: 20px;
}
.entry-box {
    width: 340px;
}
.entry-body {
    padding: 15px;
    text-align: center;
}
.entry-mascot {
    font-family: 'MS Gothic', 'ＭＳ ゴシック', 'Osaka-Mono', 'IPAGothic', monospace;
    font-size: 15px;
    line-height: 1.3;
    margin-bottom: 15px;
    color: #555;
}
.entry-label {
    display: block;
    text-align: left;
    font-weight: bold;
    font-size: 12px;
    margin-bottom: 3px;
    color: #333;
}
.entry-color-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 12px;
}
.entry-color-input {
    width: 36px;
    height: 26px;
    border: 2px solid;
    border-color: var(--border-sunken);
    padding: 0;
    cursor: pointer;
    background: none;
}
.entry-color-preview { font-weight: bold; font-size: 13px; }
.entry-submit-btn {
    margin-top: 10px;
    width: 100%;
    padding: 8px;
    font-size: 15px;
    background: #ddeeff;
}
.entry-submit-btn:hover { background: #cce0ff; }
.entry-error {
    margin-top: 8px;
    font-size: 12px;
    color: #c00;
    line-height: 1.4;
}
.entry-error.hidden { display: none !important; }
.entry-notice {
    margin-top: 12px;
    font-size: 11px;
    color: #888;
    line-height: 1.5;
}

/* ═══ メインルーム ═══ */
.main-room {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}
.room-layout {
    display: flex;
    flex: 1;
    min-height: 0;
    gap: 4px;
    padding: 4px;
}

/* ── キャンバスエリア ── */
.canvas-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
}

/* ツールバー */
.toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 3px 6px;
    margin-bottom: 3px;
    flex-shrink: 0;
    flex-wrap: wrap;
    gap: 2px;
}
.toolbar-group {
    display: flex;
    align-items: center;
    gap: 2px;
    flex-wrap: wrap;
}
.toolbar-right { margin-left: auto; }
.toolbar-sep {
    color: #999;
    margin: 0 2px;
    font-size: 14px;
    user-select: none;
}
.tool-btn {
    background: var(--bg-panel);
    border-width: 2px;
    border-style: solid;
    border-color: var(--border-raised);
    padding: 3px 8px;
    font-size: 12px;
    cursor: pointer;
    font-family: var(--font-body);
    white-space: nowrap;
    color: #000;
}
.tool-btn:active { border-color: var(--border-sunken); }
.tool-btn.active {
    border-color: var(--border-sunken);
    background: #a0a0a0;
}
.tool-btn:disabled { color: #aaa; cursor: default; }
.zoom-btn { font-size: 14px; font-weight: bold; padding: 3px 10px; }
.zoom-label {
    font-family: var(--font-pixel);
    font-size: 16px;
    min-width: 48px;
    text-align: center;
    color: #333;
}

/* キャンバス行 */
.canvas-row {
    display: flex;
    flex: 1;
    min-height: 0;
    gap: 3px;
}

/* パレット */
.palette-panel {
    width: 140px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 6px;
    overflow-y: auto;
    overflow-x: hidden;
}
.color-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 2px;
}
.color-swatch {
    width: 100%;
    aspect-ratio: 1;
    border: 2px solid;
    border-color: var(--border-sunken);
    cursor: pointer;
    transition: transform 0.1s;
}
.color-swatch:hover { transform: scale(1.15); z-index: 2; }
.color-swatch.active {
    border-color: #ff0000 !important;
    box-shadow: 0 0 0 1px #fff, 0 0 0 2px #ff0000;
    z-index: 3;
}
.color-picker-input {
    width: 100%;
    height: 28px;
    border: 2px solid;
    border-color: var(--border-sunken);
    padding: 0;
    cursor: default;
    background: none;
    /* 不再使用浏览器内置取色器：仅作为颜色预览条显示 */
    pointer-events: none;
}

/* 自制调色板（昭和/Win95 风） */
.custom-palette {
    margin-top: 6px;
    padding: 4px;
    border: 1px solid #999;
    background: #d8d8d8;
}
.custom-palette-label {
    font-size: 11px;
    font-weight: bold;
    margin-bottom: 3px;
    color: #333;
}
.custom-palette-sv-wrap {
    position: relative;
    width: 100%;
    max-width: 120px;
    margin: 0 auto 4px;
    border: 2px solid;
    border-color: var(--border-sunken);
    background: #fff;
}
#custom-sv-canvas {
    display: block;
    width: 100%;
    height: auto;
}
.custom-palette-h-row {
    position: relative;
    width: 100%;
    max-width: 120px;
    margin: 0 auto 4px;
    border: 2px solid;
    border-color: var(--border-sunken);
    background: #fff;
    height: 16px;
}
#custom-h-canvas {
    display: block;
    width: 100%;
    height: 100%;
}
.custom-palette-cursor {
    position: absolute;
    width: 10px;
    height: 10px;
    border: 2px solid #000;
    box-shadow: 0 0 0 1px #fff;
    border-radius: 50%;
    pointer-events: none;
    box-sizing: border-box;
}
.custom-palette-cursor-h {
    top: 50%;
    transform: translate(-50%, -50%);
}


.slider-group { padding: 2px 0; }
.slider-label {
    font-size: 11px;
    font-weight: bold;
    display: block;
    margin-bottom: 2px;
    color: #333;
}
.retro-slider {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 16px;
    background: #fff;
    border: 2px solid;
    border-color: var(--border-sunken);
    outline: none;
}
.retro-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 12px;
    height: 20px;
    background: var(--bg-panel);
    border: 2px solid;
    border-color: var(--border-raised);
    cursor: pointer;
}
.retro-slider::-moz-range-thumb {
    width: 12px;
    height: 20px;
    background: var(--bg-panel);
    border: 2px solid;
    border-color: var(--border-raised);
    cursor: pointer;
}

/* レイヤーパネル */
.layer-panel {
    border: 1px solid #999;
    padding: 4px;
    background: #d8d8d8;
}
.layer-title {
    font-size: 11px;
    font-weight: bold;
    margin-bottom: 3px;
    color: #333;
}
.layer-list {
    max-height: 140px;
    overflow-y: auto;
    background: #fff;
    border: 1px solid;
    border-color: var(--border-sunken);
    margin-bottom: 3px;
}
.layer-item {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 2px 4px;
    font-size: 11px;
    cursor: pointer;
    border-bottom: 1px solid #eee;
    user-select: none;
}
.layer-item:last-child { border-bottom: none; }
.layer-item:hover { background: #e8e8ff; }
.layer-item.active { background: #cce0ff; font-weight: bold; }
.layer-eye {
    width: 16px;
    text-align: center;
    font-size: 12px;
    cursor: pointer;
    flex-shrink: 0;
}
.layer-eye.hidden-eye { opacity: 0.3; }
.layer-name { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.layer-btns {
    display: flex;
    gap: 2px;
}
.layer-btns .win-btn-sm { flex: 1; text-align: center; font-size: 11px; padding: 2px 4px; }

.palette-actions {
    display: flex;
    gap: 3px;
    margin-top: auto;
}
.palette-action-btn { flex: 1; text-align: center; font-size: 11px; padding: 4px 2px; }

/* キャンバスビューポート */
.canvas-viewport {
    flex: 1;
    min-width: 0;
    min-height: 0;
    overflow: auto;
    background: var(--bg-canvas);
    border: 2px solid;
    border-color: var(--border-sunken);
    position: relative;
}
.canvas-stage {
    display: inline-block;
    position: relative;
    min-width: 100%;
    min-height: 100%;
}
#main-canvas {
    display: block;
    background: #ffffff;
    image-rendering: pixelated;
    box-shadow: 2px 2px 8px rgba(0,0,0,0.2);
    cursor: crosshair;
}

/* ── 右サイドパネル ── */
.side-panel {
    width: 300px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

/* ── Mobile top tabs (single-screen switching) ── */
.mobile-top-tabs {
    display: none;
    align-items: center;
    justify-content: space-between;
    gap: 4px;
    padding: 4px;
    margin: 4px;
    flex-shrink: 0;
}
.mobile-tab-btn {
    flex: 1;
    background: var(--bg-panel);
    border-width: 2px;
    border-style: solid;
    border-color: var(--border-raised);
    padding: 6px 4px;
    font-family: var(--font-body);
    font-size: 13px;
    font-weight: bold;
    cursor: pointer;
    color: #000;
    user-select: none;
}
.mobile-tab-btn:active { border-color: var(--border-sunken); }
.mobile-tab-btn.active {
    border-color: var(--border-sunken);
    background: #a0a0a0;
}

/* 真机强制单视图：由 JS 添加 body.layout-mobile，不依赖媒体查询 */
body.layout-mobile .mobile-top-tabs { display: flex !important; }
body.layout-mobile .room-layout { flex-direction: column; flex: 1; min-height: 0; }
body.layout-mobile .main-room { flex: 1; min-height: 0; overflow: hidden; }
body.layout-mobile .side-panel { width: 100%; min-height: 0; flex: 1; display: flex; flex-direction: column; overflow: hidden; }
body.layout-mobile #main-room.mobile-view-canvas .side-panel { display: none !important; }
body.layout-mobile #main-room.mobile-view-chat .canvas-area { display: none !important; }
body.layout-mobile #main-room.mobile-view-gallery .canvas-area { display: none !important; }
body.layout-mobile #main-room.mobile-view-canvas .canvas-area { height: 100%; overflow: hidden; }
body.layout-mobile #main-room.mobile-view-canvas .canvas-row { flex: 1; min-height: 0; display: flex; flex-direction: column; }
body.layout-mobile #main-room.mobile-view-canvas .canvas-viewport { flex: 0 0 auto; overflow: hidden; }
body.layout-mobile #main-room.mobile-view-chat .side-panel .tab-bar,
body.layout-mobile #main-room.mobile-view-gallery .side-panel .tab-bar { display: none !important; }
body.layout-mobile { overflow-x: hidden; max-width: 100vw; }
body.layout-mobile .title-bar { flex-direction: column; padding: 4px 6px; font-size: 12px; }
body.layout-mobile .title-bar-inner { font-size: 12px; }
body.layout-mobile .title-subtitle { font-size: 10px; }

.tab-bar {
    display: flex;
    background: var(--bg-panel);
    border: 2px solid;
    border-color: var(--border-raised);
    border-bottom: none;
    flex-shrink: 0;
}
.tab-btn {
    flex: 1;
    padding: 5px 8px;
    font-size: 12px;
    font-weight: bold;
    font-family: var(--font-body);
    background: #b0b0b0;
    border: none;
    border-right: 1px solid #999;
    cursor: pointer;
    color: #555;
}
.tab-btn:last-child { border-right: none; }
.tab-btn.active {
    background: var(--bg-panel);
    color: #000;
    border-bottom: 2px solid var(--bg-panel);
    margin-bottom: -1px;
    position: relative;
    z-index: 1;
}
.tab-content {
    display: none;
    flex: 1;
    min-height: 0;
    border: 2px solid;
    border-color: var(--border-raised);
}
.tab-content.active { display: flex; flex-direction: column; }

/* チャット */
.chat-panel {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}
.online-indicator {
    font-size: 11px;
    padding: 3px 6px;
    color: #006600;
    border-bottom: 1px solid #aaa;
    flex-shrink: 0;
}
.blink-dot {
    animation: blink 1.2s ease-in-out infinite;
    color: #00cc00;
    font-size: 10px;
}
@keyframes blink { 50% { opacity: 0.2; } }
.online-count { color: #888; margin-left: 6px; }

.chat-messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    background: #fff;
    border: 2px solid;
    border-color: var(--border-sunken);
    padding: 6px;
    margin: 4px 0;
    font-size: 12px;
    line-height: 1.5;
}
.chat-msg { margin-bottom: 4px; word-break: break-word; }
.chat-msg-user { font-weight: bold; }
.chat-msg-time { color: #999; font-size: 10px; margin-left: 4px; }
.chat-delete-btn { margin-left: 6px; font-size: 10px; padding: 1px 4px; }
.chat-msg-system {
    color: #888;
    font-style: italic;
    font-size: 11px;
    text-align: center;
    padding: 2px 0;
}
.chat-input-area {
    display: flex;
    gap: 3px;
    flex-shrink: 0;
}
.chat-text-input { flex: 1; }

/* ギャラリー */
.gallery-panel {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}
.gallery-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12px;
    font-weight: bold;
    padding-bottom: 4px;
    border-bottom: 1px solid #aaa;
    margin-bottom: 4px;
    flex-shrink: 0;
}
.gallery-header-btns {
    display: flex;
    align-items: center;
    gap: 6px;
}
.gallery-panel .gallery-delete-btn {
    display: none;
}
.gallery-panel.gallery-delete-mode .gallery-delete-btn {
    display: inline-block;
}
.gallery-scroll {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    background: #fff;
    border: 2px solid;
    border-color: var(--border-sunken);
    padding: 4px;
}
.gallery-item {
    display: flex;
    gap: 8px;
    padding: 6px 4px;
    border-bottom: 1px dotted #ccc;
    cursor: pointer;
    transition: background 0.15s;
}
.gallery-item:hover { background: #f0f0ff; }
.gallery-item:last-child { border-bottom: none; }
.gallery-thumb {
    width: 80px;
    height: 60px;
    object-fit: contain;
    background: #f5f5f5;
    border: 1px solid #ddd;
    flex-shrink: 0;
}
.gallery-meta {
    flex: 1;
    min-width: 0;
    font-size: 11px;
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: 4px;
}
.gallery-meta-title {
    font-weight: bold;
    color: var(--text-blue);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.gallery-meta-artist { color: #666; margin-top: 2px; }
.gallery-meta-date { color: #999; font-size: 10px; margin-top: 2px; }
.gallery-empty {
    text-align: center;
    color: #999;
    padding: 30px 10px;
    font-size: 12px;
}
.gallery-load-more {
    text-align: center;
    padding: 8px;
}

/* ステータスバー */
.status-bar {
    background: var(--bg-panel);
    border: 2px solid;
    border-color: var(--border-raised);
    padding: 2px 8px;
    font-size: 11px;
    display: flex;
    justify-content: space-between;
    flex-shrink: 0;
    color: #333;
}
.status-pos { font-family: var(--font-pixel); font-size: 14px; color: #666; }

/* ═══ モーダル ═══ */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}
.modal-box {
    width: 380px;
    max-width: 95vw;
    max-height: 90vh;
    overflow-y: auto;
}
.modal-body { padding: 12px; }
.upload-preview-wrap {
    text-align: center;
    margin-bottom: 10px;
    background: #fff;
    border: 2px solid;
    border-color: var(--border-sunken);
    padding: 4px;
}
.upload-preview {
    max-width: 100%;
    max-height: 200px;
    image-rendering: pixelated;
}
.modal-btns {
    display: flex;
    gap: 8px;
    margin-top: 10px;
    justify-content: center;
}
.upload-status {
    margin-top: 8px;
    font-size: 12px;
    text-align: center;
    color: #006600;
}

/* ライトボックス */
.lightbox-inner {
    text-align: center;
    max-width: 90vw;
    max-height: 90vh;
}
.lightbox-inner img {
    max-width: 90vw;
    max-height: 80vh;
    image-rendering: pixelated;
    background: #fff;
    border: 3px solid;
    border-color: var(--border-raised);
    box-shadow: 4px 4px 16px rgba(0,0,0,0.4);
}
.lightbox-info {
    background: rgba(0,0,0,0.7);
    color: #fff;
    padding: 6px 12px;
    font-size: 12px;
    margin-top: 4px;
    border-radius: 3px;
    display: inline-block;
}

/* フッターマーキー：1 本の文案が右から左へ流れ、完全に画面外に出たタイミングで右側へ跳ぶ（跳ぶ瞬間は非表示） */
.footer-marquee {
    background: var(--bg-panel);
    border-top: 2px solid;
    border-color: var(--border-raised);
    padding: 2px 0;
    font-size: 12px;
    color: #666;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 100;
    overflow: hidden;
    white-space: nowrap;
}
.footer-marquee-inner {
    display: inline-block;
    white-space: nowrap;
    animation: footer-marquee-scroll 50s linear infinite;
}
.footer-marquee-text {
    display: inline-block;
    margin: 0;
    padding: 0;
}
@keyframes footer-marquee-scroll {
    0%   { transform: translateX(var(--marquee-start, 100%)); }
    100% { transform: translateX(var(--marquee-end, -100%)); }
}

/* ═══ スクロールバー（レトロ風） ═══ */
::-webkit-scrollbar { width: 16px; height: 16px; }
::-webkit-scrollbar-track {
    background: var(--bg-light);
    border: 1px solid #aaa;
}
::-webkit-scrollbar-thumb {
    background: var(--bg-panel);
    border: 2px solid;
    border-color: var(--border-raised);
}
::-webkit-scrollbar-thumb:hover { background: #b0b0b0; }
::-webkit-scrollbar-corner { background: var(--bg-panel); }

/* ═══ レスポンシブ ═══ */

/* 宽屏（≥1025px）：强制桌面布局，避免 body.layout-mobile 在宽屏下仍把画板压到调色盘下方 */
@media (min-width: 1025px) {
    body.layout-mobile .room-layout {
        flex-direction: row !important;
        flex: 1;
        min-height: 0;
    }
    body.layout-mobile .main-room {
        flex: 1;
        min-height: 0;
        overflow: hidden;
    }
    body.layout-mobile .side-panel {
        display: flex !important;
        width: auto !important;
        max-width: 360px;
        flex: 0 1 360px;
        min-width: 280px;
        min-height: 0;
        flex-direction: column;
        overflow: hidden;
    }
    body.layout-mobile #main-room.mobile-view-canvas .side-panel {
        display: flex !important;
    }
    body.layout-mobile #main-room.mobile-view-canvas .canvas-area,
    body.layout-mobile #main-room.mobile-view-chat .canvas-area,
    body.layout-mobile #main-room.mobile-view-gallery .canvas-area {
        display: flex !important;
        flex: 1;
        min-height: 0;
        overflow: hidden;
    }
    body.layout-mobile #main-room.mobile-view-canvas .canvas-area .canvas-row {
        flex-direction: row !important;
        flex: 1;
        min-height: 0;
        display: flex;
    }
    body.layout-mobile #main-room.mobile-view-canvas .canvas-area .canvas-viewport {
        flex: 1 1 0 !important;
        min-width: 0;
        min-height: 0;
        overflow: hidden;
    }
    body.layout-mobile #main-room.mobile-view-canvas .canvas-area .palette-panel {
        flex-shrink: 0;
    }
    body.layout-mobile .mobile-top-tabs {
        display: none !important;
    }
    body.layout-mobile #main-room.mobile-view-chat .side-panel .tab-bar,
    body.layout-mobile #main-room.mobile-view-gallery .side-panel .tab-bar {
        display: flex !important;
    }
}

/* 竖屏（宽 < 高）：采用纵向布局，画板区在上、聊天区在下，避免横向挤成两条窄条 */
@media (min-width: 1025px) and (orientation: portrait) {
    body.layout-mobile .room-layout {
        flex-direction: column !important;
    }
    body.layout-mobile .main-room {
        flex: 1 1 0;
        min-height: 0;
    }
    body.layout-mobile .side-panel {
        max-width: none !important;
        width: 100% !important;
        flex: 1 1 0;
        min-width: 0;
        min-height: 0;
    }
}

@media (max-width: 1024px) {
    .room-layout {
        flex-direction: column;
        flex: 1;
        min-height: 0;
    }
    .main-room {
        flex: 1;
        min-height: 0;
        overflow: hidden;
    }
    .side-panel {
        width: 100%;
        min-height: 0;
        flex: 1;
        display: flex;
        flex-direction: column;
        overflow: hidden;
    }
    .canvas-area {
        min-height: 0;
        flex: 1;
    }
    .palette-panel {
        width: 120px;
    }
    .toolbar { font-size: 11px; }
    .tool-btn { padding: 2px 5px; font-size: 11px; }

    /* Mobile view switching: only show one section at a time */
    #main-room.mobile-view-canvas .side-panel { display: none; }
    #main-room.mobile-view-chat .canvas-area { display: none; }
    #main-room.mobile-view-gallery .canvas-area { display: none; }

    /* Canvas view: keep canvas at top, tools scroll below */
    #main-room.mobile-view-canvas .canvas-area { height: 100%; overflow: hidden; }
    #main-room.mobile-view-canvas .canvas-row {
        flex: 1;
        min-height: 0;
        display: flex;
        flex-direction: column;
    }
    #main-room.mobile-view-canvas .canvas-viewport {
        flex: 0 0 auto;
        overflow: hidden; /* no scroll; use pan tool when zoomed */
    }

    /* Hide side-panel internal tabs on mobile (avoid duplicate tabs) */
    #main-room.mobile-view-chat .side-panel .tab-bar,
    #main-room.mobile-view-gallery .side-panel .tab-bar { display: none; }
}

/* Show mobile top tabs (width-based)；窄屏下始终隐藏侧栏内「聊天/大家的画」切换，避免与顶栏冲突 */
@media (max-width: 1024px) {
    html, body { overflow-x: hidden; max-width: 100vw; }
    .mobile-top-tabs { display: flex; }
    .room-layout .side-panel .tab-bar { display: none !important; }
    /* Mobile: title bar 垂直两行，防止溢出 */
    .title-bar {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 2px;
        padding: 4px 6px;
        text-align: center;
        box-sizing: border-box;
        width: 100%;
        max-width: 100vw;
        overflow: hidden;
    }
    .title-bar-inner {
        font-size: 12px;
        letter-spacing: 0.5px;
        display: inline-flex;
        align-items: center;
        gap: 2px;
        white-space: nowrap;
        max-width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .title-icon { font-size: 10px; }
    .title-text { font-size: 12px; max-width: 85vw; overflow: hidden; text-overflow: ellipsis; }
    .title-subtitle {
        font-size: 10px;
        margin-top: 0;
        gap: 4px;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-wrap: wrap;
        max-width: 100%;
        overflow: hidden;
    }
    .title-subtitle span:first-child {
        font-size: 10px;
        max-width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    .lang-switcher { font-size: 9px; margin-left: 0; gap: 2px; flex-shrink: 0; }
    .lang-btn { font-size: 9px; padding: 0 4px; }
    /* Mobile creation view: hide title bar to maximize canvas area */
    body.mobile-canvas-view .title-bar { display: none; }
    /* 画布区域禁止触摸滚动/缩放，避免笔迹被打断 */
    #main-room.mobile-view-canvas .canvas-viewport,
    #main-room.mobile-view-canvas #main-canvas {
        touch-action: none;
        -webkit-user-select: none;
        user-select: none;
    }
}

/* 中等宽度（平板）：避免 canvas 与侧栏挤压、工具栏换行错乱；画布保持在调色板右侧并占满剩余区域 */
@media (min-width: 601px) and (max-width: 1024px) {
    .main-room { min-height: 0; overflow: hidden; }
    .room-layout { min-height: 0; overflow: hidden; }
    .canvas-area { min-width: 0; min-height: 0; overflow: hidden; }
    .canvas-row { min-width: 0; flex-wrap: wrap; }
    .toolbar { flex-wrap: wrap; min-width: 0; }
    .toolbar-group { min-width: 0; }
    .side-panel { min-width: 0; min-height: 0; overflow: auto; }
    .gallery-scroll { min-height: 120px; }
    /* 不依赖 body.layout-mobile：中等宽度下创作区始终为 调色板左 + 画布右，避免画板跑到调色盘下方 */
    .main-room .canvas-area .canvas-row {
        flex-direction: row !important;
        flex-wrap: nowrap;
        flex: 1;
        min-height: 0;
    }
    .main-room .canvas-area .canvas-viewport {
        flex: 1 1 0 !important;
        min-width: 0;
        min-height: 0;
    }
    .main-room .canvas-area .palette-panel {
        flex-shrink: 0;
    }
    /* 覆盖 body.layout-mobile 与 #main-room.mobile-view-canvas 的 column 规则（更高优先级 + !important） */
    body.layout-mobile #main-room.mobile-view-canvas .canvas-area .canvas-row {
        flex-direction: row !important;
        flex-wrap: nowrap;
        flex: 1;
        min-height: 0;
    }
    body.layout-mobile #main-room.mobile-view-canvas .canvas-area .canvas-viewport {
        flex: 1 1 0 !important;
        min-width: 0;
        min-height: 0;
    }
}

/* 极窄屏：标题再缩小，避免溢出 */
@media (max-width: 400px) {
    .title-bar-inner { font-size: 11px; }
    .title-icon { font-size: 9px; }
    .title-text { font-size: 11px; }
    .title-subtitle span:first-child { font-size: 9px; }
    .lang-switcher { font-size: 8px; }
    .lang-btn { font-size: 8px; }
}

@media (max-width: 600px) {
    .canvas-row {
        flex-direction: column;
    }
    .palette-panel {
        width: 100%;
        flex-direction: column;
        flex-wrap: nowrap;
        gap: 6px;
        max-height: none;
        overflow: auto;
        flex: 1;
        min-height: 0;
    }
    /* Put canvas above tools on very small screens */
    .canvas-viewport { order: 0; }
    .palette-panel { order: 1; }
    .color-grid {
        grid-template-columns: repeat(8, 1fr);
    }
    .custom-palette {
        width: 100%;
    }
    .layer-panel { min-width: 120px; }
}
