/* カスタムカラーピッカーUIの位置・余白調整 */
#penTypeButtons {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 4px;
    max-width: 100%;
}
#penTypeButtons .pen-type-btn {
    border: 2px solid #bbb;
    background: #fff;
    border-radius: 6px;
    font-size: 16px;
    padding: 4px 0;
    margin: 0;
    width: 100%;
    min-width: 0;
    cursor: pointer;
    transition: all 0.2s;
    aspect-ratio: 1/1;
    display: flex;
    align-items: center;
    justify-content: center;
}
#penTypeButtons .pen-type-btn.active {
    border: 2.5px solid #0066cc;
    background: #e6f2ff;
    color: #0066cc;
    box-shadow: 0 0 0 2px #b3d8ff;
    z-index: 1;
}

#stampShapeSelect {
    border: 2px solid #bbb;
    border-radius: 6px;
    font-size: 16px;
    padding: 6px 12px;
    background: #fff;
    transition: border-color 0.2s, box-shadow 0.2s;
}
#stampShapeSelect.active, #stampShapeSelect:focus {
    border: 2.5px solid #0066cc;
    box-shadow: 0 0 0 2px #b3d8ff;
    outline: none;
}
#customColorPicker {
    position: fixed;
    z-index: 10000;
    background: rgba(0,0,0,0.15);
    /* クリック位置で表示する場合はwidth/height指定しない */
}
#customColorPicker .custom-color-picker-inner {
    background: #fff;
    border: none;
    padding: 12px;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.12);
    width: 320px;
    max-width: 90vw;
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin: 0;
}
/* キャンバスのグレー市松模様背景 */
#drawingCanvas {
    background-color: transparent;
    background-image: none;
}
/* 起動時オーバーレイ */
#startup-overlay {
    position: fixed;
    top: 0; left: 0; width: 100vw; height: 100vh;
    background: rgba(32,32,32,0.85);
    z-index: 99999;
    display: flex;
    align-items: center;
    justify-content: center;
}
.startup-message {
    color: #fff;
    font-size: 2.2em;
    font-weight: bold;
    text-shadow: 0 2px 8px #000;
}
/* 選択ツールボタン群とオプションUI配置 */
#postpicUploadDialog { width: 440px; max-width: calc(100vw - 48px); border: 1px solid #888; border-radius: 8px; }
#postpicUploadDialog::backdrop { background: #0007; }
#postpicUploadForm { display: flex; flex-direction: column; gap: 8px; }
#postpicUploadForm input[type="text"], #postpicUploadForm textarea { box-sizing: border-box; width: 100%; }
.postpic-upload-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }
.selection-tools {
    position: relative;
}
.subject-selection-tool {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}
#subjectSelectTool {
    flex: 1;
    padding: 0 8px;
}
#subjectSelectionSource {
    width: 100%;
    height: 22px;
    box-sizing: border-box;
    font-size: 11px;
    border: 1px solid #ccc;
    border-radius: 4px;
}
.selection-range-modes { display: flex; gap: 6px; margin-bottom: 6px; }
.selection-range-modes button { flex: 1; padding: 6px 4px; }
.selection-range-modes button.active { border-color: #0066cc; background: #e6f2ff; color: #0066cc; }
#selection-tool-options {
    position: absolute;
    top: 100%;
    left: 0;
    width: 100%;
    z-index: 1200;
    background: #fff;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    border-radius: 0 0 8px 8px;
    padding: 10px 8px 8px 8px;
}
/* モード切替ボタンの視認性向上 */
        #development-panel[hidden], #canvas-controls > [hidden] { display: none !important; }
        #canvas-controls.developing {
            bottom: 24px;
            height: auto;
            overflow-y: auto;
        }
        .development-inactive { opacity: 0.45; }
        .development-help { font-size: 12px; line-height: 1.6; color: #666; }
        .development-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
        #development-compare[aria-pressed="true"] { background: #e6f2ff; border-color: #0066cc; }
        #development-groups details { border-top: 1px solid #ddd; padding: 10px 0; }
        #development-groups summary { cursor: pointer; font-size: 13px; font-weight: bold; margin-bottom: 10px; }
        #development-groups label { display: flex; justify-content: space-between; font-size: 12px; margin-top: 12px; }
        #development-groups input { width: 100%; margin: 6px 0; }
        #mode-toolbar {
            display: flex;
            align-items: center;
            flex-shrink: 0;
            gap: 8px;
            padding: 0 8px;
        }
        #mode-toolbar > button {
            background: #f5f5f5;
            color: #333;
            border: 3px solid #bbb;
            border-radius: 6px;
            font-size: 24px;
            font-weight: bold;
            width: 52px;
            height: 48px;
            padding: 4px;
            margin: 0;
            transition: all 0.2s;
            cursor: pointer;
            flex-shrink: 0;
        }
        .paint-sharing-link {
            margin-left: auto;
            color: #0066cc;
            font-size: 13px;
            white-space: nowrap;
        }
        #mode-toolbar > button.active {
            background: #e6f2ff;
            color: #0066cc;
            border: 3px solid #0066cc;
            box-shadow: 0 0 0 2px #b3d8ff;
            z-index: 1;
        }
        #mode-toolbar > button:not(.active) {
            opacity: 0.7;
            border: 3px solid #bbb;
            background: #f5f5f5;
            color: #888;
        }
        .tool-popup {
            position: fixed;
            inset: auto;
            margin: 0;
            padding: 12px;
            width: 344px;
            box-sizing: border-box;
            max-width: calc(100vw - 40px);
            max-height: calc(100vh - 40px);
            overflow: auto;
            color: #333;
            background: #fff;
            border: 1px solid #bbb;
            border-radius: 8px;
            box-shadow: 0 6px 24px rgba(0,0,0,0.25);
        }
        .tool-popup h3 { margin: 0 0 10px; font-size: 14px; }
        #penToolPopup #penTypeButtons {
            display: grid !important;
            grid-template-columns: repeat(3, minmax(0, 1fr));
            gap: 6px;
        }
        #penToolPopup .pen-type-btn {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 5px;
            padding: 8px 4px;
            min-width: 0;
            min-height: 68px;
            aspect-ratio: auto;
            font-size: 12px;
            cursor: pointer;
        }
        .tool-popup .pen-type-btn > span:first-child { font-size: 24px; }
        .pen-tool-icon { width: 26px; height: 26px; display: block; }
        #mode-toolbar .pen-tool-icon { width: 28px; height: 28px; }
        #deselectBtn { display: grid; place-items: center; }
        .tool-popup button[aria-pressed="true"] { background: #e6f2ff; border-color: #0066cc; }
        .tool-popup button:focus-visible, #mode-toolbar > button:focus-visible {
            outline: 2px solid #0066cc;
            outline-offset: 2px;
        }
        /* メニュー無効化用 */
        .menu-disabled {
            background: #444 !important;
            color: #ccc !important;
            cursor: not-allowed !important;
        }
        /* メニューバー */
        .menubar {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 6px 12px;
            background: #222;
            color: white;
            width: 100%;
            box-sizing: border-box;
            position: fixed;
            top: 0;
            left: 0;
            z-index: 20000;
        }
        .menu-item {
            position: relative;
            padding: 6px 10px;
            cursor: default;
        }
        .paint-brand {
            flex-shrink: 0;
            padding: 5px 8px;
            border-radius: 6px;
            background: #4f46e5;
            color: white;
            font-size: 14px;
            font-weight: bold;
            letter-spacing: 1px;
            text-decoration: none;
            white-space: nowrap;
        }
        .paint-brand:hover { background: #6366f1; }
        .paint-title {
            margin-left: auto;
            font-size: 14px;
            font-weight: bold;
            white-space: nowrap;
        }
        @media (max-width: 700px) {
            .menubar { gap: 4px; padding: 6px; }
            .menu-item { padding: 6px 4px; font-size: 12px; white-space: nowrap; }
            .paint-title { font-size: 11px; }
            .paint-brand { font-size: 12px; letter-spacing: 0; padding: 5px; }
        }
        @media (max-width: 480px) {
            .menubar { flex-wrap: wrap; }
            .paint-title { flex-basis: 100%; text-align: right; }
            #mode-toolbar { gap: 4px; padding: 0 4px; }
            #mode-toolbar > button { width: 36px; padding: 0; }
            .paint-sharing-link { font-size: 12px; }
        }
        .menu-dropdown {
            display: none;
            position: absolute;
            top: 100%;
            left: 0;
            min-width: 180px;
            background: #fff;
            color: #000;
            border: 1px solid #ccc;
            box-shadow: 0 4px 8px rgba(0,0,0,0.2);
            z-index: 50;
        }
        .menu-dropdown.menu-closed {
            display: none !important;
        }
        .menu-item:hover .menu-dropdown, .menu-item:focus-within .menu-dropdown { display: block; }
        .menu-dropdown button {
            display: block;
            width: 100%;
            padding: 8px 10px;
            border: none;
            background: transparent;
            text-align: left;
            cursor: pointer;
        }
        .menu-dropdown button:hover { background: #f0f0f0; }
        /* モーダルダイアログ */
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
        }
        .modal-content {
            background-color: #fff;
            margin: 10% auto;
            padding: 20px;
            border: 1px solid #888;
            border-radius: 8px;
            width: 400px;
            box-shadow: 0 4px 16px rgba(0,0,0,0.3);
        }
        .modal-content h2 {
            margin-top: 0;
    #colorSelectTool {
        display: inline-block !important;
        visibility: visible !important;
        opacity: 1 !important;
    }
            display: block;
            margin-top: 12px;
            font-weight: bold;
        }
        .modal-content input, .modal-content select {
            width: 100%;
            padding: 6px;
            margin-top: 4px;
            box-sizing: border-box;
        }
        .modal-buttons {
            margin-top: 20px;
            text-align: right;
        }
        .modal-buttons button {
            margin-left: 8px;
            padding: 8px 16px;
            cursor: pointer;
        }
        /* 画像貼り付けプレビュー */
        #pastePreview {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.7);
            z-index: 2000;
        }
        #pastePreview.active {
            display: block;
        }
        #pastePreview.selection-transform { background: rgba(0,0,0,0.15); }
        #pastePreview.selection-transform #pastePreviewImage { image-rendering: pixelated; }
        #pasteImageWrapper {
            position: absolute;
        }
        #pastePreviewImage {
            position: relative;
            outline: 2px dashed #fff;
            cursor: move;
            box-shadow: 0 0 20px rgba(255,255,255,0.5);
            display: block;
        }
        .resize-handle {
            position: absolute;
            width: 12px;
            height: 12px;
            background: white;
            border: 2px solid #4CAF50;
            border-radius: 50%;
            z-index: 10;
        }
        .resize-handle.nw { top: -6px; left: -6px; cursor: nw-resize; }
        .resize-handle.n { top: -6px; left: calc(50% - 6px); cursor: n-resize; }
        .resize-handle.ne { top: -6px; right: -6px; cursor: ne-resize; }
        .resize-handle.e { top: calc(50% - 6px); right: -6px; cursor: e-resize; }
        .resize-handle.se { bottom: -6px; right: -6px; cursor: se-resize; }
        .resize-handle.s { bottom: -6px; left: calc(50% - 6px); cursor: s-resize; }
        .resize-handle.sw { bottom: -6px; left: -6px; cursor: sw-resize; }
        .resize-handle.w { top: calc(50% - 6px); left: -6px; cursor: w-resize; }
        #pasteControls {
            position: fixed;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
            background: white;
            padding: 15px 20px;
            border-radius: 8px;
            box-shadow: 0 4px 16px rgba(0,0,0,0.3);
            display: flex;
            gap: 15px;
            align-items: center;
        }
        #pasteControls label {
            font-size: 13px;
            margin-right: 5px;
        }
        #pasteControls input[type="number"] {
            width: 80px;
            padding: 4px;
        }
        #pasteControls button {
            padding: 8px 16px;
            cursor: pointer;
            border: 1px solid #ccc;
            border-radius: 4px;
            background: #f9f9f9;
        }
        #pasteControls button:hover {
            background: #e9e9e9;
        }
        #pasteControls .ok-btn {
            background: #4CAF50;
            color: white;
            border-color: #4CAF50;
        }
        #pasteControls .ok-btn:hover {
            background: #45a049;
        }
        /* キャンバスコントロールパネル */
        #canvas-controls {
            position: fixed;
            top: 100px;
            right: 0;
            width: 320px;
            background: white;
            border: 1px solid #ccc;
            border-top-left-radius: 8px;
            border-bottom-left-radius: 8px;
            padding: 10px 12px;
            box-shadow: -2px 0 12px rgba(0,0,0,0.15);
            z-index: 10000;
            font-size: 12px;
            /* 左ペインと同じ高さに揃える: 上部メニューバー(56px) とステータスバー(24px) を考慮 */
            height: calc(200vh - 56px - 24px);
            overflow: hidden;
        }
        #canvas-controls h3 {
            margin: 0 0 8px 0;
            font-size: 13px;
            font-weight: bold;
            border-bottom: 1px solid #eee;
            padding-bottom: 6px;
        }
        #canvas-controls h4 {
            margin: 12px 0 8px 0;
            font-size: 12px;
            font-weight: bold;
            border-bottom: 1px solid #eee;
            padding-bottom: 4px;
            color: #444;
        }
        #canvas-controls .control-row {
            display: flex;
            align-items: center;
            gap: 8px;
            margin-bottom: 6px;
        }
        #canvas-controls .control-row:last-child {
            margin-bottom: 0;
        }
        #canvas-controls .canvas-view-row {
            flex-wrap: wrap;
        }
        #canvas-controls .control-label {
            font-size: 11px;
            color: #666;
            min-width: 40px;
        }
        #canvas-controls .control-value {
            font-size: 11px;
            font-weight: bold;
            min-width: 50px;
            text-align: right;
        }
        #canvas-controls button {
            padding: 3px 8px;
            font-size: 11px;
            border: 1px solid #ccc;
            border-radius: 4px;
            background: #f9f9f9;
            cursor: pointer;
            white-space: nowrap;
        }
        #canvas-controls button:hover {
            background: #e9e9e9;
        }
        #canvas-controls .reset-all-btn {
            flex: 1;
        }
        #canvas-controls .pen-controls {
            margin-top: 12px;
        }
        #canvas-controls .pen-control-item {
            margin-bottom: 8px;
        }
        #canvas-controls .pen-control-item label {
            display: block;
            font-size: 11px;
            color: #666;
            margin-bottom: 4px;
        }
        #canvas-controls .pen-control-item input[type="range"] {
            width: 100%;
        }
        #canvas-controls .pen-control-item select {
            width: 100%;
            padding: 4px;
            border: 1px solid #ccc;
            border-radius: 4px;
            background: white;
            font-size: 11px;
        }
        #canvas-controls .thickness-display {
            font-size: 11px;
            font-weight: bold;
            text-align: center;
            margin-top: 2px;
        }
        #canvas-controls .color-palette {
            display: grid;
            grid-template-columns: repeat(8, 1fr);
            gap: 3px;
            margin-top: 6px;
        }
        #canvas-controls .color-swatch {
            width: 16px;
            height: 16px;
            border: 2px solid #ccc;
            border-radius: 2px;
            cursor: pointer;
            transition: transform 0.1s, border-color 0.1s;
        }
        #canvas-controls .color-swatch:hover {
            transform: scale(1.15);
            border-color: #666;
        }
        #canvas-controls .color-swatch.active {
            border-color: #000;
            border-width: 2px;
            transform: scale(1.1);
            box-shadow: 0 0 0 1px #fff inset;
        }
        #canvas-controls .current-color-display {
            display: flex;
            align-items: center;
            gap: 6px;
            margin-top: 6px;
        }
        #canvas-controls .current-color-box {
            flex: 1;
            height: 24px;
            border: 2px solid #333;
            border-radius: 4px;
        }
        #canvas-controls .custom-color-picker {
            display: none;
        }
        /* カスタムカラーピッカーUI */
        .custom-color-editor {
            display: none;
            margin-top: 10px;
            padding-top: 10px;
            border-top: 1px solid #eee;
        }
        .custom-color-editor.active {
            display: block;
        }
        .custom-color-editor h5 {
            margin: 0 0 8px 0;
            font-size: 11px;
            font-weight: bold;
            color: #444;
        }
        .color-preview {
            width: 100%;
            height: 30px;
            border: 2px solid #333;
            border-radius: 4px;
            margin-bottom: 8px;
        }
        .color-slider-group {
            margin-bottom: 6px;
        }
        .color-slider-group label {
            display: flex;
            justify-content: space-between;
            font-size: 10px;
            margin-bottom: 2px;
            color: #666;
        }
        .color-slider-group input[type="range"] {
            width: 100%;
        }
        .color-picker-buttons {
            display: flex;
            gap: 6px;
            margin-top: 8px;
        }
        .color-picker-buttons button {
            flex: 1;
            padding: 4px;
            font-size: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            cursor: pointer;
            background: #f9f9f9;
        }
        .color-picker-buttons button:hover {
            background: #e9e9e9;
        }
        .color-picker-buttons .ok-btn {
            background: #4CAF50;
            color: white;
            border-color: #4CAF50;
        }
        .color-picker-buttons .ok-btn:hover {
            background: #45a049;
        }
        #canvas-controls .action-buttons {
            display: flex;
            flex-direction: column;
            gap: 6px;
            margin-top: 10px;
        }
        #canvas-controls .action-buttons button {
            width: 100%;
            padding: 6px 8px;
        }
        /* レイヤーリスト */
        .layer-section {
            margin-top: 12px;
            padding-top: 12px;
            border-top: 1px solid #eee;
        }
        .layer-section h4 {
            margin: 0 0 8px 0;
            font-size: 12px;
            font-weight: bold;
            color: #444;
        }
        .layer-list {
            max-height: 200px;
            overflow-y: auto;
            border: 1px solid #ddd;
            border-radius: 4px;
            background: #f9f9f9;
        }
        #selection-brush-cursor {
            position: fixed;
            pointer-events: none;
            z-index: 99999;
            box-sizing: border-box;
            border: 1px solid white;
            border-radius: 50%;
            box-shadow: 0 0 0 1px #333;
            transform: translate(-50%, -50%);
        }
        .layer-item {
            display: flex;
            flex-direction: column;
            align-items: stretch;
            gap: 3px;
            padding: 5px 6px;
            border-bottom: 1px solid #eee;
            cursor: pointer;
            font-size: 10px;
        }
        .layer-item:last-child {
            border-bottom: none;
        }
        .layer-item:hover {
            background: #e9e9e9;
        }
        .layer-item.active {
            background: #d0e8ff;
            font-weight: bold;
        }
        .layer-visibility {
            width: 16px;
            height: 16px;
            border: 1px solid #999;
            border-radius: 3px;
            background: white;
            cursor: pointer;
            flex-shrink: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 10px;
        }
        .layer-visibility.hidden {
            background: #ddd;
        }
        .layer-rename-btn {
            width: 18px;
            height: 18px;
            padding: 0;
            margin: 0;
            border: 1px solid #999;
            border-radius: 3px;
            background: white;
            cursor: pointer;
            flex-shrink: 0;
            font-size: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .layer-rename-btn:hover {
            background: #f0f0f0;
        }
        .layer-name {
            display: block;
            min-width: 0;
            line-height: 15px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .layer-options {
            display: flex;
            align-items: center;
            gap: 5px;
            min-width: 0;
        }
        .layer-opacity-wrap {
            flex: 1;
            min-width: 0;
            gap: 4px;
        }
        .layer-opacity-slider {
            flex: 1;
            min-width: 0;
            width: 0;
        }
        .layer-opacity-label {
            flex-shrink: 0;
            min-width: 28px;
            text-align: right;
            font-size: 9px;
        }
        .layer-clipping {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 18px;
            height: 18px;
            padding: 0;
            margin: 0;
            border: 1px solid #999;
            border-radius: 3px;
            background: white;
            font-size: 10px;
            font-weight: normal;
            cursor: pointer;
            white-space: nowrap;
            flex-shrink: 0;
        }
        .layer-clipping:hover:not(:disabled) {
            background: #f0f0f0;
        }
        .layer-clipping:disabled {
            background: #ddd;
            cursor: default;
        }
        .layer-controls {
            display: flex;
            gap: 4px;
            margin-top: 6px;
        }
        .layer-controls button {
            flex: 1;
            padding: 4px 6px;
            font-size: 10px;
            border: 1px solid #ccc;
            border-radius: 3px;
            background: #f9f9f9;
            cursor: pointer;
        }
        .layer-controls button:hover {
            background: #e9e9e9;
        }
        .selection-box {
            position: fixed;
            pointer-events: none;
            z-index: 50;
            box-sizing: border-box;
        }
        @keyframes marching-ants {
            0% {
                background-position: 0 0, 0 100%, 0 0, 100% 0;
            }
            100% {
                background-position: 16px 0, -16px 100%, 0 -16px, 100% 16px;
            }
        }
        /* ステータスバー */
        .selection-outline {
            position: absolute;
            inset: 0;
            pointer-events: none;
            z-index: 202;
            background-image: linear-gradient(135deg, #fff 25%, #000 25%, #000 50%, #fff 50%, #fff 75%, #000 75%);
            background-size: 8px 8px;
            mask-size: 100% 100%;
            mask-repeat: no-repeat;
            animation: marching-ants 1s linear infinite;
        }
        @media (prefers-reduced-motion: reduce) {
            .selection-outline { animation: none; }
        }
        #status-bar {
            display: grid;
            grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
            gap: 12px;
            align-items: center;
            background-color: #2c2c2c;
            color: #e0e0e0;
            padding: 4px 12px;
            font-size: 12px;
            border-top: 1px solid #444;
            min-height: 36px;
            flex-shrink: 0;
            box-sizing: border-box;
            z-index: 15000;
        }
        #status-bar .status-left {
            display: flex;
            gap: 16px;
            min-width: 0;
            overflow: hidden;
            white-space: nowrap;
        }
        #status-bar .status-left span {
            overflow: hidden;
            text-overflow: ellipsis;
            min-width: 0;
        }
        #status-bar .status-right {
            display: flex;
            gap: 8px;
            align-items: center;
            justify-content: flex-end;
            min-width: 0;
            white-space: nowrap;
            overflow: hidden;
        }
        #autosaveStatus { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
        #paintHelpDialog {
            width: 780px;
            max-width: calc(100vw - 32px);
            max-height: min(85vh, 760px);
            box-sizing: border-box;
            padding: 20px;
            border: 1px solid #888;
            border-radius: 10px;
            background: #f7f7f7;
            color: #222;
        }
        #paintHelpDialog[open] { display: flex; flex-direction: column; gap: 12px; }
        #paintHelpDialog::backdrop { background: #0008; }
        #paintAboutDialog {
            width: 360px;
            max-width: calc(100vw - 32px);
            box-sizing: border-box;
            padding: 20px;
            border: 1px solid #888;
            border-radius: 10px;
            background: #f7f7f7;
            color: #222;
        }
        #paintAboutDialog::backdrop { background: #0008; }
        .paint-about-header { display: flex; align-items: center; gap: 12px; }
        .paint-about-header h2 { margin: 0 auto 0 0; font-size: 20px; }
        .paint-about-brand { display: inline-flex; align-items: center; }
        .paint-about-content { display: flex; align-items: center; gap: 18px; margin-top: 18px; }
        .paint-about-content p { margin: 4px 0; }
        .paint-help-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
        .paint-help-header h2 { margin: 0; font-size: 20px; }
        .paint-help-intro { margin: 0; font-size: 13px; }
        .paint-help-nav { display: flex; flex-wrap: wrap; gap: 6px; }
        #paintHelpDialog button { padding: 6px 10px; cursor: pointer; }
        #paintHelpDialog button:disabled { cursor: default; opacity: 0.45; }
        .paint-help-nav button { border: 1px solid #aaa; border-radius: 5px; background: #fff; color: #333; }
        .paint-help-nav button[aria-pressed="true"] { border-color: #1765a9; background: #1765a9; color: #fff; }
        .paint-help-pages { overflow: auto; min-height: 0; }
        .paint-help-page h3 { margin: 0 0 10px; font-size: 17px; }
        .paint-help-page p { font-size: 13px; line-height: 1.6; }
        .paint-help-page table { width: 100%; border-collapse: collapse; font-size: 13px; table-layout: fixed; }
        .paint-help-page th, .paint-help-page td { border: 1px solid #ccc; padding: 10px; text-align: left; vertical-align: top; overflow-wrap: anywhere; }
        .paint-help-page thead { background: #e4edf5; }
        .paint-help-page tbody th { width: 26%; background: #eee; font-weight: normal; }
        .paint-help-page th:nth-child(2) { width: 40%; }
        #paintHelpDialog kbd { display: inline-block; border: 1px solid #bbb; border-bottom-width: 2px; border-radius: 4px; padding: 1px 4px; background: #fff; color: #222; font: 12px monospace; white-space: nowrap; }
        .paint-help-note { background: #e8eff5; border-left: 3px solid #1765a9; padding: 10px; }
        .paint-help-footer { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
        .paint-help-key-hint { text-align: center; color: #555; }
        @media (max-width: 600px) {
            #paintHelpDialog { padding: 12px; max-height: 90vh; }
            .paint-help-page th, .paint-help-page td { padding: 6px; font-size: 12px; }
            #paintHelpDialog button { font-size: 12px; }
        }
        #status-colors {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 6px;
            white-space: nowrap;
        }
        #currentColorBox, .status-bg-swatch {
            width: 26px;
            height: 24px;
            padding: 0;
            border: 1px solid #aaa;
            border-radius: 3px;
            box-sizing: border-box;
            cursor: pointer;
        }
        .status-bg-swatch { position: relative; display: inline-block; }
        #canvasEyedropper {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            min-width: 36px;
            min-height: 36px;
            padding: 4px;
            border: 1px solid #aaa;
            border-radius: 3px;
            cursor: pointer;
        }
        #canvasEyedropper[aria-pressed="true"] { background: #1765a9; color: #fff; }
        #status-colors #bgColorPicker {
            position: absolute;
            inset: 0;
            width: 100%;
            height: 100%;
            padding: 0;
            border: 0;
            background: transparent;
            cursor: pointer;
        }
        .status-bg-swatch:focus-within { outline: 2px solid #8ac7ff; outline-offset: 2px; }
        #status-colors input[type="range"] { width: 76px; margin: 0; }
        #bgAlphaValue { min-width: 34px; text-align: right; }
        #currentColorBox[aria-expanded="true"], .status-bg-swatch:has([aria-expanded="true"]) {
            outline: 2px solid #8ac7ff;
            outline-offset: 2px;
        }
        #hsvPaletteContainer {
            position: fixed;
            margin: 0;
            padding: 12px;
            width: 272px;
            max-width: calc(100vw - 40px);
            overflow: auto;
            border: 1px solid #888;
            border-radius: 8px;
            background: #fff;
            color: #333;
            box-shadow: 0 4px 18px #0004;
        }
        .hsv-popup-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; font-size: 12px; }
        .hsv-popup-actions { display: flex; gap: 4px; }
        #canvas-container.palette-eyedropper, #canvas-container.palette-eyedropper * { cursor: crosshair !important; }
        #paletteEyedropperPreview {
            position: fixed;
            width: 32px;
            height: 32px;
            box-sizing: border-box;
            border: 2px solid #fff;
            outline: 1px solid #333;
            border-radius: 4px;
            box-shadow: 2px 2px 6px #0006;
            pointer-events: none;
            z-index: 10002;
        }
        .hsv-popup-palettes { display: grid; grid-template-columns: minmax(0, 1fr) 50px; gap: 12px; }
        #hsvPaletteMain { min-width: 0; }
        #customColorPalette { display: grid; grid-template-columns: repeat(2, 1fr); grid-auto-rows: 20px; gap: 5px; align-content: start; }
        #paletteStorageStatus { font-size: 12px; color: #a00; margin: 8px 0 0; }
        #hsvPaletteContainer .color-palette { display: grid; grid-template-columns: repeat(8, 1fr); gap: 5px; margin-top: 12px; }
        #hsvPaletteContainer .color-swatch { height: 20px; border: 1px solid #aaa; border-radius: 3px; cursor: pointer; }
        #hsvPaletteContainer .color-swatch.active { outline: 2px solid #0066cc; outline-offset: 1px; }
        @media (max-width: 850px) {
            #status-bar { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 4px 8px; }
            #status-colors { grid-column: 1 / -1; grid-row: 1; }
            #status-bar .status-left, #status-bar .status-right { grid-row: 2; }
        }
        #status-filename {
            cursor: pointer;
            color: #4CAF50;
            position: relative;
        }
        #status-filename:hover {
            text-decoration: underline;
        }
        #status-filename[title]:hover::after {
            content: attr(title);
            position: absolute;
            bottom: 100%;
            left: 0;
            background: #333;
            color: #fff;
            padding: 4px 8px;
            border-radius: 4px;
            white-space: nowrap;
            font-size: 11px;
            margin-bottom: 4px;
            z-index: 1000;
        }
        body {
            display: flex;
            flex-direction: column;
            height: 100vh;
            margin: 0;
            padding: 0;
            background-color: #f0f0f0;
            font-family: Arial, sans-serif;
            overflow: hidden;
        }
        #main-container {
            display: flex;
            flex: 1;
            overflow: hidden;
            /* 右ペイン（固定）分の余白を確保 */
            padding-right: 360px;
            box-sizing: border-box;
        }
        /* 左ペイン */
        #left-pane {
            width: 220px;
            background: #f9f9f9;
            border-right: 1px solid #ccc;
            overflow-y: auto;
            padding: 12px;
            box-sizing: border-box;
        }
        #left-pane [role="status"]:empty {
            margin: 0;
        }
        #mobile-pane-buttons, .pane-popup-header {
            display: none;
        }
        @media (max-width: 1000px) {
            body {
                height: 100dvh;
            }
            #main-container {
                padding-right: 0;
                min-height: 0;
            }
            #canvas-container {
                min-width: 0;
            }
            #left-pane, #canvas-controls {
                display: none;
            }
            #left-pane:popover-open, #canvas-controls:popover-open {
                display: block;
                position: fixed;
                inset: 8px;
                top: 8px !important;
                margin: auto;
                width: min(360px, calc(100% - 16px));
                height: fit-content;
                max-height: calc(100dvh - 16px);
                box-sizing: border-box;
                overflow: auto;
                overscroll-behavior: contain;
                border: 1px solid #ccc;
                border-radius: 8px;
                padding: 12px;
                background: #fff;
                color: #333;
                box-shadow: 0 6px 24px #0006;
            }
            .pane-popup-header {
                display: flex;
                justify-content: space-between;
                align-items: center;
                gap: 8px;
                margin-bottom: 12px;
            }
            #mobile-pane-buttons {
                display: flex;
                flex-shrink: 0;
                gap: 8px;
                padding: 6px 8px max(6px, env(safe-area-inset-bottom));
                background: #2c2c2c;
                border-top: 1px solid #444;
            }
            #mobile-pane-buttons button, .pane-popup-header button {
                min-height: 44px;
                border: 1px solid #bbb;
                border-radius: 6px;
                padding: 8px;
                background: #f9f9f9;
                color: #333;
                cursor: pointer;
            }
            #mobile-pane-buttons button {
                flex: 1;
                min-width: 0;
            }
            #mobile-pane-buttons button[aria-expanded="true"] {
                background: #e6f2ff;
                border-color: #0066cc;
                color: #0066cc;
            }
            #mobile-pane-buttons button:disabled {
                opacity: 0.45;
                cursor: not-allowed;
            }
        }
        #left-pane h3 {
            font-size: 13px;
            margin: 0 0 10px 0;
            padding-bottom: 4px;
            border-bottom: 1px solid #ddd;
            color: #444;
        }
        #left-pane h4 {
            font-size: 12px;
            margin: 16px 0 8px 0;
            color: #555;
        }
        #left-pane .pen-control-item {
            margin-bottom: 12px;
        }
        #left-pane .pen-control-item label {
            display: block;
            font-size: 11px;
            color: #666;
            margin-bottom: 4px;
        }
        #left-pane .pen-control-item input[type="range"] {
            width: 100%;
        }
        #left-pane .pen-control-item select {
            width: 100%;
            padding: 4px;
            border: 1px solid #ccc;
            border-radius: 4px;
            background: white;
            font-size: 11px;
        }
        #left-pane .color-palette {
            display: grid;
            grid-template-columns: repeat(8, 1fr);
            gap: 3px;
            margin-top: 6px;
        }
        /* HSV palette styles */
        #left-pane .hsv-palette {
            display: grid;
            grid-template-columns: repeat(12, 1fr);
            gap: 3px;
            margin-top: 4px;
        }
        #left-pane .hsv-swatch {
            width: 18px;
            height: 18px;
            border: 2px solid #ddd;
            border-radius: 2px;
            cursor: pointer;
            transition: transform 0.08s, border-color 0.08s;
        }
        #left-pane .hsv-swatch:hover { transform: scale(1.12); border-color: #666; }
        #left-pane .hsv-swatch.active { box-shadow: 0 0 0 1px #fff inset; border-color: #000; }
        /* カラーホイールキャンバス */
        .color-wheel-canvas {
            border: 1px solid #ccc;
            border-radius: 6px;
            box-shadow: inset 0 0 0 1px rgba(255,255,255,0.02);
            background: #fff;
            display: block;
        }
                #hsvPaletteContainer input[type="range"] {
                    margin-top: 4px;
                }
                #hsvPaletteContainer div[id$="Label"] {
                    margin-bottom: 4px;
                }
        #left-pane .color-swatch {
            width: 18px;
            height: 18px;
            border: 2px solid #ccc;
            border-radius: 2px;
            cursor: pointer;
            transition: transform 0.1s, border-color 0.1s;
        }
        #left-pane .color-swatch:hover {
            transform: scale(1.15);
            border-color: #666;
        }
        #left-pane .color-swatch.active {
            border-color: #000;
            border-width: 2px;
            transform: scale(1.1);
            box-shadow: 0 0 0 1px #fff inset;
        }
        #left-pane .current-color-display {
            display: flex;
            align-items: center;
            gap: 6px;
            margin-top: 8px;
        }
        #left-pane .current-color-box {
            flex: 1;
            height: 28px;
            border: 2px solid #333;
            border-radius: 4px;
            cursor: pointer;
        }
        /* Canvas container (中央エリア) を右ペインを避けて伸縮させる */
        #canvas-container {
            position: relative;
            flex: 1 1 auto;
            touch-action: none;
            /* スクロールバーを表示しない */
            overflow: hidden;
            padding: 32px 16px 16px 32px;
            box-sizing: border-box;
        }
        /* 選択ツールスタイル */
        .selection-tools {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 8px;
            margin-bottom: 16px;
        }
        .tool-button {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 12px 8px;
            border: 2px solid #ccc;
            border-radius: 6px;
            background: white;
            cursor: pointer;
            transition: all 0.2s;
            font-size: 10px;
            font-weight: bold;
            color: #555;
        }
        .tool-button:hover {
            border-color: #999;
            background: #f5f5f5;
        }
        .tool-button.active {
            border-color: #0066cc;
            background: #e6f2ff;
            color: #0066cc;
        }
        .selection-actions {
            display: flex;
            flex-direction: column;
            gap: 6px;
        }
        .action-button {
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
            background: white;
            cursor: pointer;
            font-size: 11px;
            transition: background 0.2s;
        }
        .action-button:hover {
            background: #f0f0f0;
        }
        #controls {
            display: none;
        }
        #canvas-container {
            flex: 1;
            overflow: hidden;
            position: relative;
            background-color: #999;
            padding: 32px 16px 16px 32px;
            box-sizing: border-box;
        }
        .canvas-ruler, #ruler-corner {
            position: absolute;
            z-index: 500;
            background: #2d333b;
            pointer-events: none;
            user-select: none;
        }
        #horizontal-ruler {
            top: 0;
            left: 24px;
            width: calc(100% - 24px);
            height: 24px;
            border-bottom: 1px solid #111;
        }
        #vertical-ruler {
            top: 24px;
            left: 0;
            width: 24px;
            height: calc(100% - 24px);
            border-right: 1px solid #111;
        }
        #ruler-corner {
            top: 0;
            left: 0;
            z-index: 501;
            width: max-content;
            min-width: 64px;
            height: 24px;
            box-sizing: border-box;
            padding: 0 4px;
            color: #f0f3f6;
            font: 10px/11px monospace;
            white-space: nowrap;
            border-right: 1px solid #111;
            border-bottom: 1px solid #111;
        }
        #ruler-corner > span {
            display: block;
        }
        .canvas-scrollbar {
            position: absolute;
            z-index: 600;
            background: #ddd;
            scrollbar-color: #888 #ddd;
        }
        .canvas-scrollbar[hidden] {
            display: none;
        }
        .canvas-scrollbar::-webkit-scrollbar {
            width: 16px;
            height: 16px;
        }
        .canvas-scrollbar::-webkit-scrollbar-thumb {
            background: #888;
            border: 3px solid #ddd;
            border-radius: 8px;
        }
        .canvas-scrollbar:focus-visible {
            outline: 2px solid #0066cc;
            outline-offset: -2px;
        }
        #canvas-scroll-x {
            left: 24px;
            right: 0;
            bottom: 0;
            height: 16px;
            overflow-x: scroll;
            overflow-y: hidden;
        }
        #canvas-scroll-x > div {
            height: 1px;
        }
        #canvas-scroll-y {
            top: 24px;
            right: 0;
            bottom: 0;
            width: 16px;
            overflow-x: hidden;
            overflow-y: scroll;
        }
        #canvas-scroll-y > div {
            width: 1px;
        }
        #canvas-container.rulers-hidden {
            padding: 16px;
        }
        #canvas-container.rulers-hidden .canvas-ruler,
        #canvas-container.rulers-hidden #ruler-corner {
            display: none;
        }
        #drawingCanvas {
            position: absolute;
            top: 16px;
            left: 16px;
            background-color: transparent;               /* もう一つの単色を入れる */
            cursor: crosshair;
            display: block;
        }

    /* Modal for filter preview */
    #filterPreviewModal {
        position: fixed;
        left: 0; top: 0; right: 0; bottom: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        z-index: 200000; /* bring preview modal above virtually all UI */
        pointer-events: auto;
        background: transparent;
    }
    #filterPreviewModal .modal-backdrop {
        position: absolute; left:0; top:0; right:0; bottom:0; background: transparent;
        pointer-events: none;
        z-index: 200000;
    }
    #filterPreviewModal .modal-dialog {
        position: fixed; background: transparent; padding: 0; border-radius: 0; width: auto; max-width: 100%; box-shadow: none; z-index: 200001;
        overflow: hidden;
    }
    #filterPreviewModal .modal-dialog > * {
        z-index: 200002; /* ensure controls and canvas are above backdrop */
    }
    /* Force preview canvas and controls to top layer and ensure absolute positioning */
    #filterPreviewModal #filterPreviewCanvas {
        position: absolute !important;
        left: 0; top: 0;
        z-index: 40003 !important;
        border: none !important;
        box-shadow: none;
    }
    #filterPreviewModal #filterPreviewControls {
        position: fixed !important;
        right: 12px; top: 12px;
        z-index: 40004 !important;
        box-sizing: border-box;
        max-width: calc(100vw - 24px);
        max-height: calc(100vh - 24px);
        overflow: auto;
    }
    #filterPreviewModal h3 { margin: 0 0 8px 0; font-size: 16px; }
    #filterPreviewModal .modal-body { padding: 4px 0; }
    #filterPreviewModal .modal-footer { margin-top: 8px; }

/* --- 回転ハンドル用スタイル --- */
.rotate-handle {
    position: absolute;
    width: 14px;
    height: 14px;
    background: #ffe066;
    border: 2px solid #d7a700;
    border-radius: 50%;
    cursor: grab;
    box-shadow: 0 0 4px #d7a70055;
    z-index: 11;
    transition: box-shadow 0.2s;
}
.rotate-handle:hover {
    box-shadow: 0 0 8px #d7a700cc;
    background: #fffbe6;
}