:root {
    --bg-color: #f8fafc;
    --canvas-bg: #ffffff;
    --panel-bg: rgba(255, 255, 255, 0.85);
    --panel-border: rgba(0, 0, 0, 0.08);
    --accent-color: #6366f1;
    --accent-glow: rgba(99, 102, 241, 0.4);
    --text-primary: #0f172a;
    --text-secondary: #475569;
    --danger-color: #f43f5e;
    --success-color: #10b981;
    --header-height: auto;
    --footer-height: 120px;
    --radius-lg: 16px;
    --radius-md: 10px;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: 'Outfit', 'Inter', -apple-system, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-primary);
    overflow: hidden;
    height: 100vh;
    height: 100dvh;
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
}

#app {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.glass-panel {
    background: var(--panel-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--panel-border);
    padding: 12px;
}

header {
    height: var(--header-height);
    z-index: 100;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    padding-top: 8px;
}

.app-logo {
    text-align: center;
    font-size: 1.1rem;
    font-weight: 900;
    letter-spacing: 0.05em;
    background: linear-gradient(135deg, var(--accent-color), #818cf8, #c084fc);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 8px;
    filter: drop-shadow(0 2px 4px rgba(99, 102, 241, 0.2));
    font-family: 'Outfit', sans-serif;
}

.header-top,
.header-bottom {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
}

.zoom-controls,
.history-controls {
    display: flex;
    align-items: center;
    gap: 2px;
}

.zoom-controls .label {
    font-size: 0.65rem;
    font-weight: bold;
    color: var(--text-secondary);
    margin-right: 2px;
}

.history-controls button,
.zoom-controls button {
    min-width: 32px;
    width: auto;
    padding: 6px 8px;
}

#header-zoom-reset-btn {
    font-size: 0.6rem;
    padding: 6px 4px;
}

.stats {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--accent-color);
}

.history-controls button,
.util-btn {
    background: rgba(255, 255, 255, 0.5);
    border: 1px solid var(--panel-border);
    color: var(--text-primary);
    padding: 8px 4px;
    border-radius: var(--radius-md);
    font-size: 0.72rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    white-space: nowrap;
    text-align: center;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
}

.history-controls button:active,
.util-btn:active,
.util-btn.active {
    transform: translateY(-1px);
    background: var(--accent-color);
    color: white;
    box-shadow: 0 4px 12px var(--accent-glow);
    border-color: transparent;
}

.util-btn i,
.util-btn svg {
    width: 18px;
    height: 18px;
}

.util-btn.danger {
    color: var(--danger-color);
    border-color: rgba(244, 63, 94, 0.2);
}

.util-btn.danger:active {
    background: var(--danger-color);
    color: white;
    box-shadow: 0 4px 12px rgba(244, 63, 94, 0.3);
}

.header-bottom {
    display: grid;
    grid-template-columns: 80px 1fr;
    gap: 8px;
    margin-top: 8px;
    align-items: center;
}

.mode-btn {
    flex: 0 0 auto;
    min-width: 80px;
    padding: 10px;
    border-radius: var(--radius-lg);
    border: none;
    font-weight: 800;
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
}

.mode-btn:active,
.util-btn:active,
.zoom-btn:active,
.more-btn:active,
.batch-btn:active {
    transform: translateY(1px);
    opacity: 0.8;
}

.draw-mode.active {
    background: linear-gradient(135deg, var(--accent-color), #818cf8);
    color: white;
    box-shadow: 0 8px 20px var(--accent-glow);
}

.move-mode.active {
    background: linear-gradient(135deg, var(--success-color), #34d399);
    color: white;
    box-shadow: 0 8px 20px rgba(16, 185, 129, 0.3);
}

main {
    flex: 1;
    position: relative;
    overflow: hidden;
    background-color: var(--canvas-bg);
    background-image:
        radial-gradient(circle at 1px 1px, rgba(0, 0, 0, 0.05) 1px, transparent 0);
    background-size: 20px 20px;
}

#bead-canvas {
    display: block;
    cursor: crosshair;
    width: 100%;
    height: 100%;
    touch-action: none;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

footer {
    height: var(--footer-height);
    z-index: 100;
    display: flex;
    flex-direction: column;
}

#palette-container {
    display: flex;
    align-items: center;
    gap: 12px;
    height: 60px;
}

#primary-color {
    min-width: 50px;
    height: 50px;
    border: 3px solid white;
    cursor: pointer;
    border-radius: var(--radius-md);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.divider {
    width: 1px;
    height: 30px;
    background: var(--panel-border);
}

#active-colors {
    flex: 1;
    display: flex;
    overflow-x: auto;
    gap: 8px;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

#active-colors::-webkit-scrollbar {
    display: none;
}

#usage-more-container {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
}

.grid-palette {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(44px, 1fr));
    gap: 10px;
    max-height: 60vh;
    overflow-y: auto;
    padding: 10px;
    margin-bottom: 20px;
}

.palette-group-header {
    grid-column: 1 / -1;
    text-align: left;
    font-size: 0.8rem;
    font-weight: 800;
    color: var(--accent-color);
    padding: 12px 0 4px 4px;
    border-bottom: 1px solid var(--panel-border);
    margin-top: 10px;
    background: transparent;
}

.palette-group-header:first-child {
    margin-top: 0;
}

.full-height {
    max-width: 90%;
    width: 90%;
}

.palette-scroll {
    display: flex;
    overflow-x: auto;
    gap: 8px;
    padding-bottom: 10px;
    scrollbar-width: none;
}

.palette-scroll::-webkit-scrollbar {
    display: none;
}

.color-item {
    width: 40px;
    height: 40px;
    border-radius: var(--radius-md);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    font-weight: bold;
    color: white;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    flex-shrink: 0;
}

.color-item:active {
    transform: scale(0.9);
    opacity: 0.8;
}

.color-item.selected {
    border: 3px solid var(--accent-color);
    transform: scale(1.15) translateY(-2px);
    box-shadow: 0 4px 12px var(--accent-glow);
}

#selected-color-info {
    font-size: 0.8rem;
    color: var(--text-secondary);
    text-align: center;
    margin-top: 5px;
}

.symmetry-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin: 20px 0;
}

.symmetry-grid .util-btn {
    width: 100%;
    height: 50px;
    font-size: 0.85rem;
    font-weight: 800;
}

.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(15, 23, 42, 0.4);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
}

.modal.selection-active {
    pointer-events: none;
    background: transparent !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
    align-items: flex-end !important;
    justify-content: center !important;
}

.modal.selection-active .modal-content {
    pointer-events: auto;
    width: 95%;
    max-width: 500px;
    padding: 10px;
    margin-bottom: 20px;
    background: rgba(255, 255, 255, 0.95);
    border: 2px solid var(--accent-color);
    box-shadow: 0 8px 32px rgba(99, 102, 241, 0.3);
    border-radius: 16px;
}

/* 選択中はボタン類を横並びにして高さを抑える */
.modal.selection-active .symmetry-grid {
    display: flex;
    overflow-x: auto;
    gap: 8px;
    margin: 8px 0;
    padding-bottom: 4px;
    scrollbar-width: none;
}

.modal.selection-active .symmetry-grid::-webkit-scrollbar {
    display: none;
}

.modal.selection-active .symmetry-grid .util-btn {
    min-width: 80px;
    height: 36px;
    font-size: 0.7rem;
    padding: 4px;
}

.modal.selection-active .modal-content h3,
.modal.selection-active .modal-content p:not(#selection-hint),
.modal.selection-active #close-symmetry {
    display: none !important;
}

.util-buttons-group {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 4px;
    width: 100%;
}

.hidden {
    display: none;
}

.modal-content {
    width: 85%;
    max-width: 400px;
    padding: 24px;
    border-radius: 24px;
    text-align: center;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.grid-controls-container {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin: 20px 0;
}

.all-controls {
    background: rgba(99, 102, 241, 0.05);
    padding: 12px;
    margin: -4px -12px 12px -12px;
    border-radius: var(--radius-md) var(--radius-md) 0 0;
    border-bottom: 2px solid var(--panel-border);
}

.all-controls .ctrl-label {
    color: var(--accent-color);
}

.control-row {
    display: grid;
    grid-template-columns: 100px 1fr;
    align-items: center;
    gap: 10px;
}

.ctrl-label {
    text-align: left;
    font-size: 0.85rem;
    font-weight: 800;
    color: var(--text-secondary);
}

.ctrl-btns {
    display: flex;
    gap: 6px;
    justify-content: flex-end;
}

.ctrl-btns button {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    border: none;
    background: var(--text-primary);
    color: white;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.ctrl-btns .batch-btn {
    background: var(--accent-color);
    font-size: 0.65rem;
    font-weight: 800;
}

.ctrl-btns button:active {
    transform: scale(0.9);
}

.setting-summary {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 15px;
    margin-top: 15px;
    padding-top: 15px;
    border-top: 1px solid var(--panel-border);
    font-weight: 800;
    font-size: 1rem;
    color: var(--accent-color);
}

.setting-summary .separator {
    color: var(--text-secondary);
    font-weight: 400;
}

#close-settings,
#close-help {
    margin-top: 15px;
    padding: 10px 20px;
    background: var(--text-primary);
    color: white;
    border: none;
    border-radius: var(--radius-md);
    cursor: pointer;
}

/* 使い方ガイド専用スタイル */
.guide-modal {
    max-width: 450px !important;
    max-height: 85vh;
    display: flex;
    flex-direction: column;
}

.guide-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    border-bottom: 1px solid var(--panel-border);
    padding-bottom: 10px;
}

.close-icon-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: var(--text-secondary);
    cursor: pointer;
}

.guide-scroll-area {
    flex: 1;
    overflow-y: auto;
    text-align: left;
    padding-right: 5px;
}

.guide-section {
    margin-bottom: 20px;
}

.guide-section h4 {
    color: var(--accent-color);
    font-size: 0.95rem;
    margin-bottom: 10px;
    border-left: 3px solid var(--accent-color);
    padding-left: 8px;
}

.guide-item {
    display: flex;
    align-items: center;
    gap: 12px;
    background: rgba(255, 255, 255, 0.4);
    padding: 10px 12px;
    border-radius: 12px;
    margin-bottom: 8px;
    border: 1px solid var(--panel-border);
}

.guide-icon {
    font-size: 1.25rem;
    min-width: 30px;
    text-align: center;
}

.guide-text {
    font-size: 0.85rem;
    color: var(--text-primary);
    line-height: 1.4;
}

.guide-text strong {
    display: block;
    color: var(--text-primary);
    font-weight: 800;
}

.primary-btn {
    background: var(--accent-color) !important;
    box-shadow: 0 4px 12px var(--accent-glow);
}

#help-btn {
    background: rgba(99, 102, 241, 0.1);
    color: var(--accent-color);
    width: 34px;
    height: 34px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

#help-btn:active {
    transform: scale(0.9);
}

#help-btn svg {
    width: 20px;
    height: 20px;
}

.usage-list {
    max-height: 48vh;
    /* ちょうど数件で収まらないように少し高さを調整 */
    overflow-y: scroll;
    /* スクロールが必要なことを示すため常にスクロールバー領域を確保 */
    margin-top: 15px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding-right: 8px;
}

/* スクロールバーのカスタマイズ（常に見える、太くする） */
.usage-list::-webkit-scrollbar {
    width: 10px;
    display: block;
}

.usage-list::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.05);
    border-radius: 10px;
}

.usage-list::-webkit-scrollbar-thumb {
    background: var(--accent-color);
    border-radius: 10px;
    border: 2px solid rgba(255, 255, 255, 0.2);
}

.usage-item {
    display: flex;
    align-items: center;
    gap: 15px;
    padding: 10px 16px;
    background: white;
    border: 1px solid var(--panel-border);
    border-radius: var(--radius-md);
    font-size: 0.9rem;
    transition: all 0.2s;
}

.usage-item:active {
    transform: scale(0.98);
    background: var(--bg-color);
}

.usage-item .color-sample {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 1px solid rgba(0, 0, 0, 0.05);
}

.usage-item .bead-id {
    font-weight: 800;
    min-width: 40px;
    text-align: left;
}

.usage-item .bead-count {
    margin-left: auto;
    color: var(--accent-color);
    font-weight: 800;
}

.more-btn {
    min-width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--accent-color);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.65rem;
    font-weight: 800;
    cursor: pointer;
    box-shadow: 0 4px 12px var(--accent-glow);
    transition: all 0.3s;
}

#select-range-btn.active {
    background: var(--accent-color) !important;
    color: white !important;
    box-shadow: 0 0 15px var(--accent-glow);
}

#select-range-btn.active svg {
    stroke: white;
}