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

:root {
    /* SaaS-ish design tokens */
    --bg: #f7f8fb;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --text: #0f172a;
    --muted: #64748b;
    --border: #e5e7eb;
    --border-strong: #d7dde6;

    --accent: #2563eb;
    --accent-strong: #1d4ed8;
    --success: #16a34a;
    --danger: #dc2626;

    --radius-lg: 12px;
    --radius-md: 10px;
    --radius-sm: 8px;

    --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
    --shadow-md: 0 10px 22px rgba(15, 23, 42, 0.08);

    --gap: 16px;
    --card-pad: 18px;
    --control-h: 40px;

    /* 固定フレーム（レイアウトのガタつき防止） */
    --upload-frame: 220px;
}

body {
    font-family: Inter, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: radial-gradient(1200px 600px at 20% -10%, rgba(37, 99, 235, 0.10), rgba(255, 255, 255, 0.0) 60%),
                radial-gradient(900px 480px at 100% 0%, rgba(22, 163, 74, 0.08), rgba(255, 255, 255, 0.0) 55%),
                var(--bg);
    color: var(--text);
    line-height: 1.5;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.container {
    max-width: 1320px;
    margin: 0 auto;
    padding: 20px;
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Header */
.header {
    margin-bottom: var(--gap);
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: 16px 18px;
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border);
    flex-shrink: 0;
}

.title {
    font-size: 1.15rem;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 2px;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 10px;
}

.title i {
    color: var(--accent);
    width: 34px;
    height: 34px;
    border-radius: 9px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: rgba(37, 99, 235, 0.10);
}

.subtitle {
    font-size: 0.85rem;
    color: var(--muted);
    font-weight: 400;
    padding-left: 44px; /* icon width + gap */
}

/* Main Content */
.main-content {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--gap);
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

/* Left Panel */
.left-panel {
    display: flex;
    flex-direction: column;
    gap: var(--gap);
    min-height: 0;
    /* PCではページ全体をスクロールさせず、パネル内だけ必要に応じてスクロールさせる */
    overflow-x: hidden;
    overflow-y: auto;
    scrollbar-gutter: stable;
}

/* Right Panel */
.right-panel {
    display: flex;
    flex-direction: column;
    min-height: 0;
    /* PCではページ全体をスクロールさせず、パネル内だけ必要に応じてスクロールさせる */
    overflow-x: hidden;
    overflow-y: auto;
    scrollbar-gutter: stable;
}

/* Scrollbar (subtle) */
.left-panel::-webkit-scrollbar,
.right-panel::-webkit-scrollbar {
    width: 10px;
}
.left-panel::-webkit-scrollbar-thumb,
.right-panel::-webkit-scrollbar-thumb {
    background: rgba(100, 116, 139, 0.25);
    border-radius: 999px;
    border: 3px solid transparent;
    background-clip: content-box;
}
.left-panel::-webkit-scrollbar-thumb:hover,
.right-panel::-webkit-scrollbar-thumb:hover {
    background: rgba(100, 116, 139, 0.38);
    border: 3px solid transparent;
    background-clip: content-box;
}

/* Upload Section */
.upload-section {
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: var(--card-pad);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border);
    flex-shrink: 0;
}

.upload-area {
    border: 2px dashed #cbd5e0;
    border-radius: var(--radius-md);
    padding: 18px 16px;
    text-align: center;
    transition: all 0.2s ease;
    cursor: pointer;
    position: relative;
    background: linear-gradient(180deg, rgba(15, 23, 42, 0.02), rgba(15, 23, 42, 0.0));
    height: var(--upload-frame);
    display: flex;
    align-items: center;
    justify-content: center;
}

.upload-area:hover {
    border-color: var(--accent);
    background: linear-gradient(180deg, rgba(37, 99, 235, 0.05), rgba(15, 23, 42, 0.0));
}

.upload-area.dragover {
    border-color: var(--accent);
    background: rgba(37, 99, 235, 0.08);
}

.upload-content h3 {
    font-size: 1rem;
    margin: 12px 0 6px;
    color: var(--text);
    font-weight: 600;
}

.upload-content p {
    color: var(--muted);
    margin-bottom: 16px;
    font-size: 0.875rem;
}

.upload-icon {
    font-size: 2.2rem;
    color: var(--accent);
    margin-bottom: 10px;
}

#imageInput {
    position: absolute;
    opacity: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
}

.upload-btn {
    background: var(--accent);
    color: white;
    border: none;
    padding: 0 16px;
    height: var(--control-h);
    border-radius: var(--radius-md);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    box-shadow: 0 1px 0 rgba(255, 255, 255, 0.3) inset;
}

.upload-btn:hover {
    background: var(--accent-strong);
}

/* Preview Area */
.preview-area {
    text-align: center;
    height: var(--upload-frame);
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 10px;
}

.preview-area img {
    width: 100%;
    flex: 1;
    min-height: 0;
    object-fit: contain;
    /* JSのdisplay制御が想定外でも、枠からはみ出さないように保険 */
    max-height: calc(var(--upload-frame) - 44px);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border);
    background: #ffffff;
}

.change-image-btn {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 0 14px;
    height: 34px;
    border-radius: 999px;
    font-size: 0.875rem;
    cursor: pointer;
    transition: all 0.2s ease;
}

.change-image-btn:hover {
    background: var(--surface-2);
}

/* Input Section */
.input-section {
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: var(--card-pad);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border);
    flex-shrink: 0;
}

.input-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-bottom: 20px;
}

.input-group {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.input-group label {
    font-weight: 500;
    color: var(--text);
    font-size: 0.875rem;
}

.input-group input {
    padding: 0 12px;
    height: var(--control-h);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    font-size: 0.875rem;
    transition: all 0.2s ease;
    background: var(--surface);
}

.input-group input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.analyze-btn {
    background: var(--success);
    color: white;
    border: none;
    padding: 0 16px;
    height: 44px;
    border-radius: var(--radius-md);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    box-shadow: 0 1px 0 rgba(255, 255, 255, 0.25) inset;
}

.analyze-btn:hover:not(:disabled) {
    background: #15803d;
}

.analyze-btn:disabled {
    background: #cbd5e1;
    cursor: not-allowed;
}

/* Loading Section */
.loading-section {
    text-align: center;
    padding: 32px 20px;
    flex-shrink: 0;
}

.loading-spinner {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
}

.spinner {
    width: 32px;
    height: 32px;
    border: 2px solid #e2e8f0;
    border-top: 2px solid #3182ce;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.loading-spinner p {
    font-size: 0.875rem;
    color: var(--muted);
    font-weight: 500;
}

/* Results Section */
.results-section {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.results-card {
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: var(--card-pad);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border);
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}

.results-title {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 14px;
    text-align: left;
    flex-shrink: 0;
}

/* Main Results */
.main-results {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-bottom: 24px;
    flex-shrink: 0;
}

.result-card {
    background: var(--surface-2);
    border: 1px solid var(--border);
    padding: 16px;
    border-radius: var(--radius-md);
    text-align: center;
    display: flex;
    align-items: center;
    gap: 12px;
}

.result-icon {
    font-size: 1.3rem;
    color: var(--accent);
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: rgba(37, 99, 235, 0.10);
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.result-content {
    flex: 1;
}

.result-value {
    font-size: 1.55rem;
    font-weight: 600;
    margin-bottom: 4px;
    color: var(--text);
    font-variant-numeric: tabular-nums;
}

.result-label {
    font-size: 0.875rem;
    color: var(--muted);
    font-weight: 500;
}

/* Perimeter breakdown (small reference text) */
.result-submetrics {
    margin-top: 6px;
    display: flex;
    justify-content: center;
    gap: 14px;
    flex-wrap: wrap;
    font-size: 0.75rem;
    color: var(--muted);
    line-height: 1.2;
}

.submetric {
    display: inline-flex;
    align-items: baseline;
    gap: 4px;
}

.submetric-label {
    font-weight: 500;
    color: #475569;
}

.submetric-value {
    font-variant-numeric: tabular-nums;
}

/* Contour Section */
.contour-section {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 14px;
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}

.contour-section h3 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 10px;
    flex-shrink: 0;
}

.contour-legend {
    display: flex;
    gap: 20px;
    margin-bottom: 12px;
    justify-content: center;
    flex-shrink: 0;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 6px;
    font-weight: 500;
    color: #475569;
    font-size: 0.875rem;
}

.legend-color {
    width: 16px;
    height: 16px;
    border-radius: 2px;
}

.legend-color.green {
    background: #38a169;
}

.legend-color.blue {
    background: #3182ce;
}

.contour-image {
    text-align: center;
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 0;
    overflow: hidden;
}

.contour-image img {
    max-width: 100%;
    max-height: 100%;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
    object-fit: contain;
    border: 1px solid var(--border);
    background: #ffffff;
}

/* Image modal (contour preview 확대) */
.image-modal {
    position: fixed;
    inset: 0;
    z-index: 9999;
    display: none;
}

.image-modal.is-open {
    display: block;
}

.image-modal__backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
}

.image-modal__content {
    position: relative;
    margin: 24px auto;
    width: min(1100px, calc(100vw - 32px));
    height: min(80vh, 760px);
    background: #ffffff;
    border-radius: var(--radius-lg);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
    border: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    overflow: hidden;
    top: 50%;
    transform: translateY(-50%);
}

.image-modal__content img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    border-radius: 6px;
}

.image-modal__close {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 36px;
    height: 36px;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    background: #ffffff;
    color: #475569;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s ease;
}

.image-modal__close:hover {
    background: var(--surface-2);
}

/* Initial State */
.initial-state {
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: 34px 22px;
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 1;
    min-height: 0;
}

.initial-content {
    text-align: center;
    color: var(--muted);
}

.initial-content i {
    font-size: 3rem;
    color: #cbd5e1;
    margin-bottom: 12px;
}

.initial-content h3 {
    font-size: 1.125rem;
    color: #475569;
    margin-bottom: 6px;
    font-weight: 600;
}

.initial-content p {
    font-size: 0.875rem;
    color: #718096;
    line-height: 1.5;
}

/* Error Section */
.error-section {
    margin-top: 12px;
    flex-shrink: 0;
}

.error-card {
    background: var(--surface);
    border-radius: var(--radius-md);
    padding: 16px;
    box-shadow: var(--shadow-sm);
    border: 1px solid #fed7d7;
    text-align: center;
    border-left: 4px solid var(--danger);
}

.error-card i {
    font-size: 1.5rem;
    color: var(--danger);
    margin-bottom: 8px;
}

.error-card h3 {
    font-size: 1rem;
    color: var(--text);
    margin-bottom: 8px;
    font-weight: 600;
}

.retry-btn {
    background: var(--danger);
    color: white;
    border: none;
    padding: 0 14px;
    height: 36px;
    border-radius: var(--radius-md);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    margin-top: 8px;
}

.retry-btn:hover {
    background: #b91c1c;
}

/* Footer */
.footer {
    text-align: center;
    margin-top: 16px;
    padding: 12px;
    color: var(--muted);
    font-size: 0.75rem;
    flex-shrink: 0;
}

/* Responsive Design */
@media (max-width: 1024px) {
    .container {
        height: auto;
        min-height: 100vh;
        overflow: visible;
    }
    
    .main-content {
        grid-template-columns: 1fr;
        gap: 20px;
        overflow: visible;
    }
    
    .left-panel {
        order: 1;
        overflow: visible;
    }
    
    .right-panel {
        order: 2;
        overflow: visible;
    }
    
    .container {
        padding: 16px;
    }
    
    .title {
        font-size: 1.375rem;
    }
    
    .input-row {
        grid-template-columns: 1fr;
        gap: 12px;
    }
    
    .main-results {
        grid-template-columns: 1fr;
        gap: 12px;
    }
    
    .result-card {
        flex-direction: column;
        gap: 8px;
    }
    
    .result-value {
        font-size: 1.5rem;
    }
    
    .contour-legend {
        flex-direction: column;
        align-items: center;
        gap: 8px;
    }
    
    .contour-image img {
        max-height: 300px;
    }
    
    .initial-state {
        padding: 32px 20px;
    }
    
    .initial-content i {
        font-size: 2.5rem;
    }
}

