/* --- Design System & Reset --- */
:root {
    --bg: #f8fafc;
    --surface: #ffffff;
    --border: #e2e8f0;
    --text-main: #0f172a;
    --text-muted: #64748b;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --danger: #ef4444;
    --success: #10b981;
    --warning: #f59e0b;
    --footer: #f1f3f5;
    --header-h: 60px;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background-color: var(--bg);
    color: var(--text-main);
    line-height: 1.5;
    overflow-x: hidden;
    min-height: 100vh;

}

h1,
h2,
h3 {
    font-weight: 700;
    margin-bottom: 1rem;
    color: var(--text-main);
}

button {
    cursor: pointer;
    border: none;
    outline: none;
    font-family: inherit;
    transition: all 0.2s;
}

input,
select {
    font-family: inherit;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.5rem;
    color: var(--text-main);
}

input:focus,
select:focus {
    outline: 2px solid var(--primary);
    outline-offset: -1px;
}

input:disabled {
    background-color: #f1f5f9;
    color: var(--text-muted);
    cursor: not-allowed;
}

/* --- Header & Navigation --- */
#app-header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: var(--header-h);
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1.5rem;
    z-index: 100;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.h-left {
    display: flex;
    align-items: center;
    gap: 1rem;
}

#app-title {
    font-size: 1.25rem;
    font-weight: bold;
}

.desktop-nav {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.nav-btn {
    background: transparent;
    color: var(--text-main);
    font-weight: bold;
    padding: 0.5rem 1rem;
    border-radius: 6px;
    font-size: 0.95rem;
}

.nav-btn:hover {
    background: #f1f5f9;
}

.nav-btn.active {
    color: var(--primary);
    background: #eff6ff;
}

/* Dropdown (Desktop) */
.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    right: 0;
    top: 100%;
    background-color: white;
    min-width: 180px;
    box-shadow: 0px 8px 16px rgba(0, 0, 0, 0.1);
    z-index: 101;
    border-radius: 8px;
    border: 1px solid var(--border);
    overflow: hidden;
    padding: 0.5rem 0;
}

.dropdown-content button {
    color: var(--text-main);
    padding: 12px 16px;
    width: 100%;
    text-align: left;
    background: none;
    border-bottom: 1px solid #f1f5f9;
    border-radius: 0;
    font-size: 0.9rem;
}

.dropdown-content button:last-child {
    border-bottom: none;
}

.dropdown-content button:hover {
    background-color: #f8fafc;
    color: var(--primary);
}

.dropdown:hover .dropdown-content {
    display: block;
}

/* Mobile Drawer */
.mobile-only {
    display: none;
}

.mobile-menu-btn {
    background: transparent;
    border: none;
    font-size: 1.5rem;
    color: var(--text-main);
    padding: 0.25rem;
    border-radius: 4px;
}

.mobile-menu-btn:hover {
    background: #f1f5f9;
}

#mobile-drawer {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.6);
    z-index: 1000;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
    backdrop-filter: blur(2px);
}

#mobile-drawer.open {
    opacity: 1;
    pointer-events: auto;
}

.drawer-content {
    position: absolute;
    left: -260px;
    top: 0;
    bottom: 0;
    width: 260px;
    background: white;
    transition: left 0.3s;
    padding: 2rem 1rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
}

#mobile-drawer.open .drawer-content {
    left: 0;
}

.drawer-content .nav-btn {
    text-align: left;
    padding: 1rem;
    font-size: 1.1rem;
    border-bottom: 1px solid #f1f5f9;
    border-radius: 8px;
}

/* Responsive Breakpoint */
@media (max-width: 1023px) {
    .desktop-nav {
        display: none;
    }

    .mobile-only {
        display: block;
    }

    .mobile-menu-btn {
        display: block;
    }
}

@media (min-width: 1024px) {
    .mobile-menu-btn {
        display: none;
    }
}

/* --- Main Content & Layout --- */
#main-content {
    min-height: auto;
    flex: 1;
    margin-top: var(--header-h);
}

.view-section {
    display: none;
    animation: fadeIn 0.3s;
}

.view-section.active {
    display: block;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(5px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: #94a3b8;
}

.grid-layout {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1.5rem;
}

.input-sidebar {
    padding: 1.5rem;
}

.output-main {
    padding: 1.5rem 1.5rem 4rem 1.5rem;
    display: flex;
    flex-direction: column;
}

#help-view {
    padding: 1.5rem;
}

/* デスクトップ向け独立スクロールレイアウト */
@media (min-width: 1024px) {
    body {
        overflow: hidden;
        height: 100vh;
        /* 画面全体に固定 */
        display: flex;
        flex-direction: column;
    }

    #main-content {
        flex: 1;
        /* 残りの高さを占有 */
        height: calc(100vh - var(--header-h));
        display: flex;
        flex-direction: column;
    }

    .view-section.active {
        height: 100%;
        display: flex;
        flex-direction: column;
    }

    .grid-layout {
        grid-template-columns: 620px 1fr;
        height: 100%;
        gap: 0;
    }

    .input-sidebar {
        padding: 2rem;
        overflow-y: auto;
        border-right: 1px solid var(--border);
        background: var(--surface);
    }

    .output-main {
        padding: 2rem;
        overflow-y: auto;
        background: var(--bg);
    }

    .canvas-scroll-area {
        max-height: calc(100vh - 230px);
        /* デスクトップで2:1の板が見切れないように拡張 スクロールバーが出ないようJSの計算値より余裕を持たせる */
    }

    #help-view {
        overflow-y: auto;
        height: 100%;
        padding: 2rem;
    }
}

/* フッターのスタイル（固定解除） */
footer {
    text-align: center;
    padding: 0.2rem;
    color: var(--text-muted);
    font-size: 0.8rem;
    background: var(--footer);
    /* 背景色をbodyと合わせる */
    width: 100%;
}

/* --- Tool UI --- */
.panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1.5rem;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.02);
    margin-bottom: 1.5rem;
}

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    border-bottom: 2px solid #f1f5f9;
    padding-bottom: 0.5rem;
}

.panel-header h2 {
    margin: 0;
    font-size: 1.1rem;
    color: #1e293b;
}

.settings-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
    gap: 1rem;
}

.input-group {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    margin-bottom: 1rem;
}

.input-group label {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-muted);
}

.list-container {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.list-item {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    overflow-x: auto;
}

.b-dims,
.b-qty,
.b-price {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.part-item {
    position: relative;
    padding-left: 2.2rem;
}

.part-id-badge {
    position: absolute;
    left: 0.5rem;
    top: 50%;
    transform: translateY(-50%);
    background: var(--primary);
    color: white;
    width: 22px;
    height: 22px;
    border-radius: 11px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    font-weight: bold;
}

.p-shape,
.p-dims,
.p-qty {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.p-opts,
.b-opts {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-left: auto;
}

.dim-input {
    width: 65px;
    text-align: center;
    font-weight: 500;
    padding: 0.4rem;
    border: 1px solid var(--border);
    border-radius: 4px;
}

.icon-btn {
    background: #e2e8f0;
    border-radius: 4px;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9rem;
    flex-shrink: 0;
}

.icon-btn:hover:not(:disabled) {
    background: #cbd5e1;
}

.delete-btn {
    color: var(--danger);
    background: transparent;
    padding: 0.4rem;
    font-size: 1rem;
    flex-shrink: 0;
}

.delete-btn:hover {
    background: #fee2e2;
    border-radius: 4px;
}

.add-btn {
    width: 100%;
    padding: 0.6rem;
    background: transparent;
    border: 1px dashed var(--primary);
    color: var(--primary);
    font-weight: bold;
    border-radius: 8px;
    margin-top: 0.5rem;
}

.add-btn:hover {
    background: #eff6ff;
}

.exec-btn {
    width: 100%;
    padding: 1rem;
    background: var(--primary);
    color: white;
    font-size: 1.1rem;
    font-weight: bold;
    border-radius: 8px;
    margin-top: 2rem;
    box-shadow: 0 4px 6px -1px rgba(37, 99, 235, 0.2);
}

.exec-btn:hover {
    background: var(--primary-hover);
    transform: translateY(-1px);
}

/* --- Output & Rendering --- */
.proposal-box {
    background: #fffbeb;
    border: 1px solid #fcd34d;
    padding: 15px;
    border-radius: 6px;
    margin-bottom: 20px;
    display: none;
}

.proposal-box h3 {
    margin: 0 0 10px 0;
    color: #b45309;
    font-size: 1rem;
}

.proposal-box button {
    background: var(--warning);
    margin-top: 10px;
    padding: 10px;
    color: white;
    border-radius: 6px;
    font-weight: bold;
    width: auto;
}

.proposal-box button:hover {
    background: #d97706;
}

.summary-box {
    background: #ecfdf5;
    border: 1px solid #34d399;
    padding: 0.75rem 1rem;
    border-radius: 8px;
    margin-bottom: 1rem;
    display: none;
}

.summary-box.active {
    display: block;
}

.summary-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 0.5rem;
    margin-top: 0.5rem;
}

.s-item {
    background: white;
    padding: 0.5rem;
    border-radius: 6px;
    text-align: center;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
    border: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.s-val {
    font-size: 1.25rem;
    font-weight: bold;
    color: var(--text-main);
    margin: 0;
    line-height: 1.2;
}

.display-options {
    display: none;
    background: white;
    padding: 0.75rem 1.25rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    margin-bottom: 1rem;
    flex-wrap: wrap;
    gap: 1.5rem;
    align-items: center;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.display-options label {
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
    font-weight: bold;
    color: var(--text-main);
    user-select: none;
}

.display-options input[type="checkbox"] {
    transform: scale(1.2);
}

/* Canvas Zooming UI */
.board-container {
    margin-bottom: 2rem;
    border: 1px solid var(--border);
    border-radius: 12px;
    overflow: hidden;
    background: white;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
}

.board-header {
    background: #f8fafc;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.canvas-controls-bar {
    display: flex;
    gap: 0.5rem;
    justify-content: flex-end;
    padding: 0.5rem 1rem;
    background: #f1f5f9;
    border-bottom: 1px solid var(--border);
    align-items: center;
}

.zoom-btn {
    background: white;
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 4px 10px;
    font-weight: bold;
    font-size: 0.85rem;
    color: var(--text-main);
}

.zoom-btn:hover {
    background: #e2e8f0;
}

.canvas-scroll-area {
    overflow: auto;
    max-height: 60vh;
    padding: 1rem;
    background: #f8fafc;
    position: relative;
}

canvas {
    background: #e2e8f0;
    display: block;
    margin: 0 auto;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.player-controls {
    display: flex;
    gap: 1rem;
    padding: 1rem 1.5rem;
    background: #fff;
    border-top: 1px solid var(--border);
    align-items: center;
}

.player-controls button {
    background: var(--text-main);
    color: white;
    padding: 0.5rem 1rem;
    border-radius: 6px;
    font-weight: bold;
    width: 100px;
}

.player-controls input[type="range"] {
    flex: 1;
    cursor: pointer;
}

/* --- Modal (Calculator) --- */
.modal-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(15, 23, 42, 0.6);
    z-index: 1000;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(2px);
}

.modal {
    background: white;
    padding: 2rem;
    border-radius: 12px;
    width: 620px;
    max-width: 95%;
    max-height: 90vh;
    overflow-y: auto;
    overflow-x: hidden;
}

.calc-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1rem;
    background: #f8fafc;
    padding: 1.5rem;
    border-radius: 8px;
    margin: 1.5rem 0;
    border: 1px solid var(--border);
}

@media (max-width: 550px) {
    .calc-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 0.75rem;
        padding: 1rem;
    }
}

.calc-cell {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
}

.calc-label {
    font-size: 0.8rem;
    color: var(--text-muted);
    font-weight: bold;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.calc-input {
    padding: 0.6rem;
    border: 2px solid var(--primary);
    border-radius: 6px;
    font-size: 1rem;
    font-weight: bold;
    color: var(--text-main);
    width: 100%;
    box-sizing: border-box;
}

.calc-readonly {
    background: #e2e8f0;
    padding: 0.6rem;
    border-radius: 6px;
    font-size: 1rem;
    color: #475569;
    display: flex;
    align-items: center;
    min-height: 38px;
    width: 100%;
    box-sizing: border-box;
    overflow: hidden;
}

.modal-actions {
    display: flex;
    gap: 1rem;
    margin-top: 1.5rem;
}

.modal-actions button {
    flex: 1;
    padding: 1rem;
    border-radius: 8px;
    font-weight: bold;
    color: white;
    font-size: 1.05rem;
}

/* --- Article (Help Page) --- */
.article-content {
    max-width: 800px;
    margin: 0 auto;
    background: white;
    padding: 3rem;
    border-radius: 12px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    border: 1px solid var(--border);
}

.article-content h2 {
    border-bottom: 2px solid #e2e8f0;
    padding-bottom: 0.5rem;
    margin-top: 2.5rem;
}

.article-content h3 {
    margin-top: 1.5rem;
    color: var(--primary);
}

.article-content p,
.article-content ul {
    margin-bottom: 1rem;
    color: #334155;
}

.article-content li {
    margin-left: 1.5rem;
    margin-bottom: 0.5rem;
}

/* --- スマートフォン向け：画面はみ出し解消用の追加設定 --- */
@media (max-width: 1023px) {

    /* 1. 画面左右の余白を少し狭めて、コンテンツの表示領域を広げる */
    .input-sidebar,
    .output-main {
        padding: 1rem 0.5rem;
    }

    .panel {
        padding: 1rem 0.5rem;
    }

    /* 2. リストは常に1行を維持し、幅が足りない時は枠内横スクロールにする */
    .list-item {
        flex-wrap: nowrap;
        padding: 0.5rem 0.5rem 0.5rem 2.2rem;
        overflow-x: auto;
        /* 横スクロール許可 */
        -webkit-overflow-scrolling: touch;
        /* スマホでのスクロールを滑らかに */
    }

    .p-shape,
    .p-dims,
    .p-qty,
    .b-dims,
    .b-qty,
    .b-price {
        flex: 0 0 auto;
        /* 要素が潰れるのを防ぐ */
        margin-bottom: 0;
    }

    /* 3. 折り返し設定を解除 */
    .p-opts {
        width: auto;
        margin-top: 0;
        padding-top: 0;
        border-top: none;
        flex: 0 0 auto;
    }

    /* 4. キャンバス下のプレイヤー（再生ボタン・シークバー）の折り返し許可 */
    .player-controls {
        flex-wrap: wrap;
        padding: 1rem 0.5rem;
    }

    .player-controls input[type="range"] {
        min-width: 100%;
        /* スマホではスライダーを独立した行にして操作しやすくする */
        margin-top: 0.5rem;
    }

    /* 5. 実行結果エリアがキャンバスの幅に引っ張られて画面外へ広がるのを防ぐ */
    .output-main {
        min-width: 0;
    }

    /* 6. 「🔍100% / ➕拡大 / ➖縮小」のコントロールバーも折り返しを許可する */
    .canvas-controls-bar {
        flex-wrap: wrap;
    }
}

/* --- 横画面（ランドスケープモード）向けの高さ調整 --- */
@media (orientation: landscape) and (max-height: 600px) {
    .canvas-scroll-area {
        max-height: 75vh;
        /* 画面の高さが低い時は、キャンバスエリアを限界まで広げる */
    }
}

/* --- 横画面（ランドスケープ）時の超圧縮UIモード --- */
@media (max-width: 1023px) and (orientation: landscape) {

    /* 1. アプリの大見出しの固定を解除（スクロールで上に逃がす） */
    #app-header {
        position: absolute;
    }

    #main-content {
        margin-top: 0;
        padding-top: var(--header-h);
        /* 初期位置の確保のみ */
    }

    /* 2. パネルや余白を極限まで削る */
    .output-main {
        padding: 0.25rem;
    }

    .board-container {
        margin-bottom: 1rem;
    }

    /* 3. キャンバス高さを画面限界（90%）まで広げる */
    .canvas-scroll-area {
        max-height: 90vh;
    }
}

/* --- スマホ画面のはみ出し（Grid/Flex Blowout）を完全に封じ込める --- */
@media (max-width: 1023px) {

    /* 親となるすべてのコンテナに対して、内側の要素に引っ張られないよう「縮小許可(min-width: 0)」を与える */
    .grid-layout,
    .input-sidebar,
    .output-main,
    .panel,
    .list-container {
        min-width: 0;
        width: 100%;
        max-width: 100vw;
    }
}