* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

/* Global Reset for Solid Design */
*:not(#credit-btn):not(.tool-tooltip) {
    border-radius: 0 !important;
}

html,
body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    touch-action: none;
    background: #fff;
    font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', Arial, 'YuGothic', 'Yu Gothic', sans-serif;
    display: flex;
    flex-direction: column;
}

/* キャンバスエリア (上部・可変) */
#canvas-area {
    flex: 1;
    position: relative;
    background: #999;
    overflow: hidden;
}

#canvas-container {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: auto;
    height: auto;
    max-width: 90%;
    max-height: 90%;
    aspect-ratio: 1 / 1;
    background: #fff;
    border: 1px solid #000;
    z-index: 1;
}

#canvas {
    display: block;
    width: 100%;
    height: 100%;
    touch-action: none;
}

/* 編集パネル (中央・ツールバー) */
#edit-panel {
    background: #000;
    padding: 8px 12px;
    z-index: 11;
    display: flex;
    flex-direction: column;
    gap: 0;
    flex-shrink: 0;
}

/* パネル内部レイアウト */
.panel-row {
    display: flex;
    align-items: center;
    gap: 8px;
    /* Restored "Global don't tighten" */
    justify-content: center;
    /* Center alignment */
}

/* Hide vertical divider in panel row */
.panel-row .divider {
    display: none;
}

/* New Wrapper for Sliders + Flip Button */
.center-controls {
    display: flex;
    align-items: center;
    gap: 0;
    /* Gap 0 between Sliders and Flip */
}

/* ツール切り替え */
.segment-control {
    display: flex;
    flex-direction: column;
    background: #000;
    padding: 2px;
    border: 1px solid #fff;
    gap: 2px;
    flex-shrink: 0;
    filter: grayscale(100%);
}

.segment-btn {
    border: none;
    background: transparent;
    padding: 0 12px;
    height: 34px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 16px;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.segment-btn.active {
    background: #fff;
    color: #000;
}

/* ... */
.preview-container {
    width: 70px;
    height: 70px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #fff;
    border: 1px solid #fff;
    overflow: hidden;
    flex-shrink: 0;
}

/* スライダー群（中央） */
.controls-container {
    flex: 0 1 auto;
    /* 自然幅か縮小 */
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 4px;
}

.control-group {
    display: flex;
    align-items: center;
    gap: 2px;
    /* Set to 2px as requested */
}

.control-group label {
    font-size: 16px;
    color: #fff;
    width: 24px;
    text-align: center;
    margin-right: 4px;
    filter: grayscale(100%);
}

.control-group input[type="range"] {
    width: 100px;
    height: 24px;
    margin: 0;
    cursor: pointer;
    accent-color: #fff;
}

#size-value,
#rotation-value {
    font-size: 11px;
    font-weight: 600;
    color: #fff;
    min-width: 45px;
}

/* アクション群（右側） */
.actions-container {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-shrink: 0;
    /* margin-left: auto; Removed for center alignment */
}

.button-group {
    display: flex;
    gap: 2px;
    /* ボタン間隔狭める */
}

.button-grid {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.button-row {
    display: flex;
    gap: 2px;
}

/* ボタン調整 */
.edit-btn {
    border: 1px solid #fff;
    background: #000;
    color: #fff;
    padding: 0;
    height: 34px;
    width: 34px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.1s;
    filter: grayscale(100%);
    cursor: pointer;
}

.edit-btn.active {
    background: #fff;
    color: #000;
    border-color: #fff;
}

#flip-h {
    height: 70px;
    width: 34px;
    padding: 0;
}

.edit-btn.delete-btn {
    border-color: #ff4444;
    /* Special Red Border */
    /* Neutralize red for grayscale? No, user wants red. */
    /* color: #333; */
    color: #ff4444;
    /* Red icon too */
    margin-left: 4px;
    height: 70px;
    filter: none;
    /* Disable grayscale */
}

/* 絵文字ピッカーコンテナ (下部・固定高さ) */
#emoji-picker-container {
    height: 30vh;
    /* 25vh -> 30vh */
    min-height: 230px;
    /* 180px -> 230px (+50px approx 1 row) */
    background: #000;
    display: flex;
    flex-direction: column;
    z-index: 10;
    padding-bottom: env(safe-area-inset-bottom);
}

/* 検索バー */
#emoji-search-bar {
    padding: 8px;
    background: #000;
}

#emoji-search {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid #fff;
    background: #000;
    color: #fff;
    font-size: 15px;
    border-radius: 0;
    text-align: center;
    transition: all 0.2s;
}

#emoji-search:focus {
    background: #fff;
    color: #000;
    text-align: left;
    outline: none;
}

/* カテゴリタブ */
/* カテゴリタブ */
#emoji-categories {
    display: flex;
    background: #000;
    padding: 4px;
    gap: 4px;
    overflow-x: auto;
    overflow-y: hidden;
    white-space: nowrap;
    -webkit-overflow-scrolling: touch;
    /* Hide scrollbar */
    scrollbar-width: none;
    -ms-overflow-style: none;
}

#emoji-categories::-webkit-scrollbar {
    display: none;
}

.category-tab {
    flex: 0 0 auto;
    /* Don't shrink, allow natural width */
    padding: 8px 16px;
    border: none;
    background: transparent;
    cursor: pointer;
    font-size: 20px;
    /* Increase icon size slightly */
    transition: all 0.2s;
    color: #666;
    filter: grayscale(100%);
    min-width: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.category-tab.active {
    color: #000;
    background: #fff;
    border-radius: 4px;
    /* border: 1px solid #000; Removed border for cleaner scroll look? Or keep it? keeping it match style */
    border: 1px solid #000;
}

/* 絵文字リスト */
#emoji-list-container {
    flex: 1;
    overflow-y: auto;
    padding: 4px;
    background: #000;
}

.emoji-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(40px, 1fr));
    gap: 4px;
    background: #000;
}

.no-recent {
    grid-column: 1 / -1;
    text-align: center;
    padding: 40px 20px;
    color: #888;
    font-size: 14px;
}

.emoji-item {
    width: 100%;
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 26px;
    cursor: pointer;
    background: #fff;
    border: 1px solid #000;
    transition: transform 0.1s, background 0.1s;
}

.emoji-item:hover {
    background: #f8f8fa;
}

.emoji-item:active {
    background: #bcc0c5;
    transform: none;
    /* remove scale */
}

/* 右上ツールバー (キャンバス内に移動したため調整) */
#toolbar-right {
    position: absolute;
    top: 12px;
    right: 12px;
    display: flex;
    gap: 8px;
    z-index: 100;
}

.tool-btn {
    position: relative;
    width: 40px;
    /* Matched to emoji-item size */
    height: 40px;
    border-radius: 0;
    border: 1px solid #000;
    background: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform 0.1s, background 0.1s;
    color: #333;
    overflow: visible;
    filter: grayscale(100%);
}

.tool-btn .emoji-icon {
    font-size: 20px;
    /* Balanced for 40px button */
    line-height: 1;
}

.tool-btn:active {
    transform: scale(0.92);
}

.tool-btn.active {
    border: 1px solid #fff;
    background: #000;
    color: #fff;
}

/* ツールチップ */
.tool-tooltip {
    display: none;
    position: absolute;
    left: 50%;
    top: 60px;
    transform: translateX(-50%);
    background: rgba(255, 255, 255, 0.95);
    color: #333;
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 13px;
    white-space: nowrap;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    border: 2px solid #000;
    z-index: 400;
    pointer-events: none;
}

.tool-tooltip-bottom::before {
    content: '';
    position: absolute;
    left: 50%;
    bottom: 100%;
    transform: translateX(-50%);
    border: 6px solid transparent;
    border-bottom-color: #ddd;
}

.tool-tooltip-bottom::after {
    content: '';
    position: absolute;
    left: 50%;
    bottom: 100%;
    transform: translateX(-50%);
    border: 5px solid transparent;
    border-bottom-color: rgba(255, 255, 255, 0.95);
    margin-bottom: -1px;
}

body.help-mode .tool-tooltip {
    display: block;
}

.tooltip-offset-2 {
    top: 90px;
}

.tooltip-offset-3 {
    top: 120px;
}

/* 保存UI */
#save-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.3);
    z-index: 200;
}

#save-overlay.active {
    display: block;
}

#save-ui {
    display: none;
    position: fixed;
    top: max(20px, env(safe-area-inset-top));
    left: 20px;
    background: #fff;
    padding: 16px 24px;
    border: 1px solid #000;
    z-index: 401;
    /* UIを最前面に (キャンバスより上) */
    text-align: center;
    transition: opacity 0.2s;
    max-width: 90%;
}

#save-ui.active {
    display: block;
}

#save-ui h3 {
    margin-bottom: 12px;
    font-size: 14px;
    font-weight: normal;
    color: #666;
}

.option-group {
    margin-bottom: 12px;
}

.option-group-label {
    font-size: 12px;
    margin-bottom: 6px;
    color: #666;
}

.option-buttons {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.option-btn {
    padding: 4px 10px;
    border: 1px solid #ccc;
    background: #fff;
    border-radius: 6px;
    cursor: pointer;
    font-size: 12px;
    transition: all 0.2s;
}

.option-btn:hover {
    background: #f0f0f0;
}

.option-btn.active {
    background: #333;
    color: #fff;
    border-color: #333;
}

.save-btn {
    padding: 8px 16px;
    margin: 4px;
    border: 1px solid #000;
    background: #fff;
    border-radius: 0;
    cursor: pointer;
    font-size: 12px;
    transition: all 0.2s;
}

.save-btn:hover {
    background: #f0f0f0;
}

.save-btn.primary {
    background: #333;
    color: #fff;
}

.save-btn.primary:hover {
    background: #555;
}

#save-ui label {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 12px;
    cursor: pointer;
    font-size: 12px;
}

#save-ui input[type="checkbox"] {
    width: 16px;
    height: 16px;
    cursor: pointer;
}

#selection-canvas {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    /* pointer-events: none; <== Removed to allow interaction */
    z-index: 300;
    /* Ensure it's above save-ui */
}

/* クレジットボタン */
#credit-btn {
    position: fixed;
    top: max(12px, env(safe-area-inset-top));
    left: 12px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 1px solid rgba(0, 0, 0, 0.3);
    background: rgba(0, 0, 0, 0.1);
    color: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
    z-index: 100;
    transition: all 0.2s;
}

#credit-btn:hover {
    background: rgba(0, 0, 0, 0.2);
    color: rgba(0, 0, 0, 0.8);
    border-color: rgba(0, 0, 0, 0.5);
}

/* クレジットモーダル */
#credit-modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #fff;
    z-index: 300;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

#credit-modal.visible {
    display: flex;
}

#credit-content {
    background: #fff;
    border: 1px solid rgba(0, 0, 0, 0.6);
    padding: 20px 24px;
    font-family: monospace;
    width: 300px;
    max-width: calc(100vw - 40px);
}

.credit-row {
    display: flex;
    gap: 12px;
    line-height: 1.9;
}

.credit-key {
    color: #888;
    font-size: 11px;
    min-width: 4em;
    flex-shrink: 0;
}

.credit-val {
    font-size: 11px;
    color: #111;
    text-decoration: none;
}

a.credit-val {
    text-decoration: underline;
}

.credit-divider {
    border: none;
    border-top: 1px solid #ddd;
    margin: 12px 0;
}

.credit-manual-link {
    font-size: 11px;
    color: #111;
    text-decoration: underline;
    margin-right: 16px;
}

/* モバイル対応 */
@media (max-width: 600px) {
    #edit-panel {
        padding: 8px;
    }

    .panel-row {
        gap: 8px;
    }
}

/* 無効状態 (レイアウト崩れ防止用) */
.disabled {
    opacity: 0.4;
    pointer-events: none;
}

/* History Controls */
.history-controls {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.divider {
    width: 1px;
    height: 32px;
    background: #000;
    margin: 0 8px;
}

/* Panel Row Layout */
/* .panel-row.top-row removed to prevent space-between override */

/* レスポンシブ対応 */
@media (max-width: 768px) {
    #canvas-container {
        max-width: 85vw;
        max-height: 40vh;
    }

    #emoji-picker-container {
        height: 30vh;
    }

    #edit-panel {
        bottom: 40vh;
    }

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

/* 背景スタイル */
body.bg-transparent #canvas-container {
    background:
        linear-gradient(45deg, #ddd 25%, transparent 25%),
        linear-gradient(-45deg, #ddd 25%, transparent 25%),
        linear-gradient(45deg, transparent 75%, #ddd 75%),
        linear-gradient(-45deg, transparent 75%, #ddd 75%);
    background-size: 20px 20px;
    background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
}

body.bg-white #canvas-container {
    background: #ffffff;
}

/* キャンバス上の絵文字スタイル */
.canvas-emoji {
    position: absolute;
    cursor: move;
    user-select: none;
    transition: opacity 0.1s;
}

.canvas-emoji.selected {
    outline: 1px solid #333;
    outline-offset: 4px;
}

.canvas-emoji:hover {
    opacity: 0.8;
}

/* ============================================
   New Toolbar Left (Draw Style)
   ============================================ */
#toolbar-left {
    position: absolute;
    left: 12px;
    /* top: 50%; transform: translateY(-50%); <== Removed center alignment */
    bottom: 12px;
    /* <== Moved to bottom-left */
    display: flex;
    flex-direction: column;
    gap: 8px;
    /* グループ間の隙間 */
    z-index: 100;
}

.tool-group {
    display: flex;
    flex-direction: column;
    gap: 4px;
    /* ボタン間の隙間 */
}

/* 分割線 (線は消して空きだけ残す) */
.divider-h {
    height: 1px;
    background: transparent;
    /* <== Transparent */
    margin: 4px 0;
    width: 100%;
}

/* Tool Buttons Style Update */
.tool-btn {
    width: 48px;
    height: 48px;
    border: 1px solid #999;
    /* Gray inactive */
    background: rgba(255, 255, 255, 0.9);
    color: #333;
    font-size: 24px;
    transition: transform 0.1s, background 0.1s, border-width 0.1s;
    filter: none;
    /* remove grayscale filter if present */
    overflow: visible;
    /* ::beforeを表示するために必要 */
}

.tool-btn .emoji-icon {
    font-size: 24px;
    line-height: 1;
}

.tool-btn:active {
    transform: scale(0.92);
}

/* Active State (Draw Style) */
.tool-btn.active {
    border: 1px solid #000;
    background: #fff;
    color: #000;
    transform: translateX(-5px);
    /* 左へ移動 */
    z-index: 10;
}

.tool-btn.active::before {
    content: '';
    position: absolute;
    top: -1px;
    right: 100%;
    bottom: -1px;
    width: 12px;
    background: #000;
    /* border-radius removed to match Emojie solid design */
}

/* Clear Button Special Red Style */
#clearBtn {
    border: 1px solid #ff4444;
    color: #ff4444;
}

#clearBtn:active {
    background: #ff4444;
    color: #fff;
}

/* History Buttons specific */
.history-btn {
    font-size: 20px;
}

/* Generic Disabled State */
.disabled {
    opacity: 0.3;
    pointer-events: none;
    filter: grayscale(100%);
    /* Optional: force gray */
}

.history-btn.disabled {
    /* history-btn specific overrides if needed, essentially same */
    background: rgba(255, 255, 255, 0.5);
    border-color: #ddd;
}

/* モバイル対応: ツールバーの位置調整 */
@media (max-width: 768px) {
    #toolbar-left {
        left: 8px;
    }

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

    /* 編集パネルの高さ調整 */
    #edit-panel {
        padding: 4px 8px;
    }

    .preview-container {
        width: 50px;
        height: 50px;
        font-size: 24px;
    }

    #edit-panel .controls-container {
        gap: 0;
    }

    .control-group label {
        font-size: 14px;
    }

    /* ツールチップ位置調整 (左サイドバー用) */
    .tool-tooltip-right {
        left: 100%;
        top: 50%;
        transform: translateY(-50%);
        margin-left: 10px;
    }

    .tool-tooltip-right::before {
        left: -6px;
        top: 50%;
        transform: translateY(-50%);
        border: 6px solid transparent;
        border-right-color: #ddd;
        border-bottom-color: transparent;
    }

    .tool-tooltip-right::after {
        left: -5px;
        top: 50%;
        transform: translateY(-50%);
        border: 5px solid transparent;
        border-right-color: rgba(255, 255, 255, 0.95);
        border-bottom-color: transparent;
        margin-bottom: 0;
    }
}