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

html {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: 'Segoe UI', 'Meiryo', sans-serif;
    display: flex;
    flex-direction: column;
    height: 100vh;
    min-height: 0;
    overflow: hidden;
    background: #f8fafc;
}

.header {
    flex-shrink: 0;
    padding: 10px 32px;
    background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
    border-bottom: 1px solid rgba(255,255,255,0.08);
    box-shadow: 0 4px 24px rgba(0,0,0,0.12);
}

.header h1 {
    font-family: 'Outfit', 'Segoe UI', sans-serif;
    font-size: 1.5rem;
    font-weight: 500;
    color: #f8fafc;
    letter-spacing: 0.02em;
    display: flex;
    align-items: baseline;
    gap: 0.5em;
}

.header h1 .brand {
    font-weight: 300;
    color: rgba(248,250,252,0.9);
    letter-spacing: 0.08em;
}

.header h1 .accent {
    width: 30px;
    height: 1.1em;
    background: linear-gradient(180deg, #94a3b8 0%, #475569 100%);
    border-radius: 1px;
    flex-shrink: 0;
    opacity: 0.6;
}

.content-wrapper {
    flex: 1;
    min-height: 0;
    position: relative;
    display: flex;
    flex-direction: column;
}

.main-container {
    display: flex;
    flex: 1;
    min-height: 0;
}

.options-panel {
    flex: 2;
    min-width: 200px;
    min-height: 0;
    padding: 24px 12px 24px 24px;
    background: #fff;
    border-right: 1px solid #e2e8f0;
    display: flex;
    flex-direction: column;
}

.options-panel-scroll {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
}

.usage-count {
    font-size: 0.8125rem;
    color: #94a3b8;
    line-height: 1.5;
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid #e2e8f0;
}

.options-panel .sidebar-links {
    margin-top: auto;
    padding-top: 16px;
    display: flex;
    gap: 12px;
}

.options-panel .sidebar-link {
    font-size: 0.875rem;
    color: #94a3b8;
    text-decoration: none;
}

.options-panel .sidebar-link:hover {
    color: #64748b;
    text-decoration: underline;
}

.tray-panel {
    flex: 6;
    min-width: 0;
    padding: 24px;
    background: #f8fafc;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow-y: auto;
}

.usage-panel {
    flex: 2;
    min-width: 180px;
    min-height: 0;
    padding: 24px;
    background: #fff;
    border-left: 1px solid #e2e8f0;
    display: flex;
    flex-direction: column;
}

.usage-panel-scroll {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
}

.usage-item-title {
    font-size: 0.875rem;
    font-weight: 600;
    color: #334155;
    margin: 16px 0 6px 0;
}

.usage-item-title:first-child {
    margin-top: 0;
}

.usage-item-text {
    font-size: 0.8125rem;
    line-height: 1.6;
    color: #475569;
    margin: 0 0 4px 0;
}


.options-panel h2,
.tray-panel h2,
.usage-panel h2 {
    font-size: 1rem;
    font-weight: 600;
    color: #1e293b;
    margin-bottom: 16px;
}

.usage-hints {
    margin-bottom: 16px;
}

.usage-hint {
    padding: 12px 16px;
    background: #f1f5f9;
    border-left: 4px solid #64748b;
    border-radius: 4px;
    margin-bottom: 8px;
}

.usage-hint:last-child {
    margin-bottom: 0;
}

.usage-hint-title {
    font-size: 0.875rem;
    font-weight: 600;
    color: #334155;
    margin-bottom: 6px;
}

.usage-hint-content {
    font-size: 0.8125rem;
    line-height: 1.5;
    color: #475569;
}

.option-category {
    margin-bottom: 12px;
    border: 1px solid #e2e8f0;
    border-radius: 4px;
    overflow: hidden;
}

.option-category-header {
    display: flex;
    align-items: center;
    padding: 10px 12px;
    background: #f1f5f9;
    cursor: pointer;
    user-select: none;
    font-size: 0.9rem;
    font-weight: 600;
    color: #334155;
}

.option-category-header:hover {
    background: #e2e8f0;
}

.option-category-toggle {
    margin-right: 8px;
    font-size: 0.7rem;
    transition: transform 0.2s;
}

.option-category.collapsed .option-category-toggle {
    transform: rotate(-90deg);
}

.option-category-body {
    padding: 12px;
    background: #fff;
}

.option-category.collapsed .option-category-body {
    display: none;
}

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

.option-group:last-child {
    margin-bottom: 0;
}

.option-group label {
    display: block;
    font-size: 0.875rem;
    color: #555;
    margin-bottom: 0px;
}

.option-group input[type="number"],
.option-group select {
    width: 100%;
    padding: 3px 12px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 0.875rem;
}

.option-group input[type="checkbox"] {
    margin-right: 8px;
}

.option-group .option-checkbox-label {
    margin: 0;
    font-weight: normal;
    cursor: pointer;
}

.btn-primary {
    width: 100%;
    padding: 10px 16px;
    background: #0284c7;
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 0.875rem;
    cursor: pointer;
    margin-top: 8px;
}

.btn-primary:hover {
    background: #0369a1;
}

.btn-primary:disabled {
    background: #94a3b8;
    cursor: not-allowed;
}

.btn-primary:disabled:hover {
    background: #94a3b8;
}

.btn-primary.loading {
    position: relative;
}

.btn-primary.loading .btn-loading-spinner {
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid rgba(255,255,255,0.3);
    border-top-color: white;
    border-radius: 50%;
    animation: btn-spin 0.8s linear infinite;
    margin-right: 8px;
    vertical-align: middle;
}

@keyframes btn-spin {
    to { transform: rotate(360deg); }
}

.btn-exit {
    width: 100%;
    padding: 10px 16px;
    background: #64748b;
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 0.875rem;
    cursor: pointer;
    margin-top: 16px;
}

.btn-exit:hover {
    background: #475569;
}

.tray-toolbar {
    margin-bottom: 16px;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
}

.tray-toolbar-label {
    font-size: 0.875rem;
    color: #334155;
    margin: 0;
}

.tray-divisions-select {
    padding: 6px 10px;
    border: 1px solid #e2e8f0;
    border-radius: 4px;
    font-size: 0.875rem;
    background: #fff;
    min-width: 60px;
}

.btn-add-tray {
    padding: 8px 16px;
    background: #0ea5e9;
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 0.875rem;
    cursor: pointer;
}

.btn-add-tray:hover {
    background: #0284c7;
}

.btn-clear {
    padding: 8px 16px;
    background: #64748b;
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 0.875rem;
    cursor: pointer;
}

.btn-clear:hover {
    background: #475569;
}

.grid-wrapper {
    /* container-type: size により cqw / cqh が使えるようになる。 */
    container-type: size;
    flex: 1;
    min-width: 0;
    min-height: 0;
    position: relative;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
}

/* clear-bar の高さ分（52px）を差し引いてグリッド領域の高さを算出 */

.grid-container {
    /*
     * 縦横どちらが狭くても縦横比を維持して縮小する。
     * 100cqh は grid-wrapper の高さ。clear-bar 分を差し引いた高さで幅を算出。
     */
    width: min(100%, calc((100cqh - 52px) * 518 / 362));
    aspect-ratio: 518 / 362;
    flex-shrink: 0;
    position: relative;
    background: #fff;
    border: 2px solid #334155;
    border-radius: 4px;
    padding: 4px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.grid {
    display: grid;
    grid-template-columns: repeat(10, 1fr);
    grid-template-rows: repeat(7, 1fr);
    gap: 2px;
    width: 100%;
    height: 100%;
    min-height: 0;
    background: #c8c8c8;
}

.grid-cell {
    background: #fff;
    min-width: 0;
    min-height: 0;
}

.clear-bar {
    margin-top: 8px;
    flex-shrink: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: min(100%, calc((100cqh - 52px) * 518 / 362));
}

.btn-3mf-right {
    width: auto;
    margin-top: 0;
}

#trays-container {
    position: absolute;
    left: 6px;
    top: 6px;
    pointer-events: none;
}

#trays-container .tray {
    pointer-events: auto;
}

.tray {
    position: absolute;
    background: rgba(210, 240, 255, 0.85);
    border: 2px solid #0ea5e9;
    border-radius: 4px;
    cursor: move;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    z-index: 1;
}

.tray.dragging {
    z-index: 10;
    opacity: 0.9;
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}

.tray-header {
    position: absolute;
    top: 4px;
    left: 4px;
    z-index: 2;
}

.tray-delete {
    width: 16px;
    height: 16px;
    background: #64748b;
    color: white;
    border: none;
    border-radius: 50%;
    font-size: 12px;
    line-height: 1;
    cursor: pointer;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

.tray-delete:hover {
    background: #475569;
}

.tray-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 4px;
    position: relative;
    min-height: 0;
}

.tray-inner {
    flex: 1;
    display: grid;
    padding: 2px;
    min-height: 0;
    gap: 0;
}

.tray-inner .divider-cell {
    min-width: 0;
    min-height: 0;
    background: transparent;
}

.tray-inner .divider-line {
    min-width: 16px;
    min-height: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    background: transparent;
}

.tray-inner .divider-line::before {
    content: '';
    position: absolute;
    background: #0ea5e9;
}

.tray-inner .divider-line:not(.horizontal)::before {
    width: 3px;
    top: 0;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
}

.tray-inner .divider-line.horizontal {
    min-height: 16px;
}

.tray-inner .divider-line.horizontal::before {
    width: 100%;
    height: 3px;
    left: 0;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
}

.resize-handle {
    position: absolute;
    background: #66cffd;
    border: 1px solid #0288D1;
    border-radius: 2px;
    z-index: 5;
}

.resize-handle:hover {
    background: #29B6F6;
}

.resize-handle.edge {
    min-width: 10px;
    min-height: 10px;
}

.resize-handle.corner {
    width: 28px;
    height: 28px;
    background: transparent;
    border: none;
    border-radius: 50%;
}

.resize-handle.corner::before {
    content: '';
    position: absolute;
    width: 12px;
    height: 12px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #66cffd;
    border: 1px solid #0288D1;
    border-radius: 50%;
    pointer-events: none;
}

.resize-handle.corner:hover::before {
    background: #29B6F6;
}

.resize-handle.top {
    top: -4px;
    left: 50%;
    transform: translateX(-50%);
    width: var(--handle-h-length, 78px);
    height: 8px;
    cursor: n-resize;
}
.resize-handle.bottom {
    bottom: -4px;
    left: 50%;
    transform: translateX(-50%);
    width: var(--handle-h-length, 78px);
    height: 8px;
    cursor: s-resize;
}
.resize-handle.left {
    left: -4px;
    top: 50%;
    transform: translateY(-50%);
    width: 8px;
    height: var(--handle-v-length, 78px);
    cursor: w-resize;
}
.resize-handle.right {
    right: -4px;
    top: 50%;
    transform: translateY(-50%);
    width: 8px;
    height: var(--handle-v-length, 78px);
    cursor: e-resize;
}
.resize-handle.top-left { top: -12px; left: -12px; cursor: nw-resize; }
.resize-handle.top-right { top: -12px; right: -12px; cursor: ne-resize; }
.resize-handle.bottom-left { bottom: -12px; left: -12px; cursor: sw-resize; }
.resize-handle.bottom-right { bottom: -12px; right: -12px; cursor: se-resize; }

.divider-controls {
    display: flex;
    flex-direction: column;
    gap: 2px;
    position: absolute;
    z-index: 3;
}

.divider-controls.right {
    right: 2px;
    top: 50%;
    transform: translateY(-50%);
}

.divider-controls.bottom {
    bottom: 2px;
    left: 50%;
    transform: translateX(-50%);
    flex-direction: row;
}

.divider-btn {
    width: 16px;
    height: 16px;
    background: #0ea5e9;
    color: white;
    border: none;
    border-radius: 50%;
    font-size: 12px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.divider-btn:hover {
    background: #0284c7;
}

.divider-btn.minus {
    background: #f44336;
}

.divider-btn.minus:hover {
    background: #d32f2f;
}

.divider-minus-btn {
    width: 14px;
    height: 14px;
    background: #64748b;
    position: relative;
    z-index: 1;
    color: white;
    border: none;
    border-radius: 50%;
    font-size: 10px;
    cursor: pointer;
    flex-shrink: 0;
}

.divider-minus-btn:hover {
    background: #475569;
}

.footer {
    flex-shrink: 0;
    padding: 4px 32px;
    background: #1e293b;
    color: rgba(248, 250, 252, 0.7);
    font-size: 0.875rem;
    text-align: center;
}

.footer p {
    margin: 0;
}

.terms-overlay {
    display: none;
    position: absolute;
    inset: 0;
    z-index: 100;
    background: rgba(15, 23, 42, 0.6);
    align-items: center;
    justify-content: center;
    padding: 24px;
}

.terms-overlay.is-open {
    display: flex;
}

.terms-modal {
    position: relative;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    max-width: 560px;
    max-height: 80vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.terms-close {
    position: absolute;
    top: 12px;
    right: 12px;
    width: 32px;
    height: 32px;
    font-size: 24px;
    line-height: 1;
    color: #64748b;
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

.terms-close:hover {
    color: #1e293b;
}

.terms-content {
    padding: 32px 24px 24px;
    overflow-y: auto;
}

.terms-content h2 {
    font-size: 1.25rem;
    margin-bottom: 16px;
    color: #1e293b;
}

.terms-content p {
    font-size: 0.875rem;
    line-height: 1.6;
    color: #475569;
    margin-bottom: 12px;
}

.terms-content p:last-child {
    margin-bottom: 0;
}
