/* ======================================
   RESET & BASE STYLES
   ====================================== */
* {
	box-sizing: border-box;
}

body {
	margin: 0;
	font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
	color: #222;
	background: #f5f6f8;
	line-height: 1.5;
}

/* ======================================
   DESIGN TOKENS (COLOR & SPACING)
   ====================================== */
:root {
	/* Brand Colors */
	--color-brand: #c40000;
	--color-brand-hover: #a60000;
	--color-brand-soft: #ffe5e5;

	/* Background Colors */
	--color-bg-page: #f5f6f8;
	--color-bg-panel: #ffffff;
	--color-bg-soft: #f0f2f5;

	/* Border Colors */
	--color-border: #dfe3e8;
	--color-border-strong: #c2c8d0;

	/* Text Colors */
	--color-text: #222;
	--color-text-sec: #555;
	--color-text-faint: #777;
	--color-text-inverse: #ffffff;

	/* Status Colors */
	--color-success: #0d7a3c;
	--color-warn: #c37c00;
	--color-danger: #c40000;

	/* Shadows & Effects */
	--shadow-panel: 0 1px 2px rgba(0,0,0,.06), 0 3px 8px rgba(0,0,0,.08);
	--radius-panel: 4px;

	/* Spacing */
	--space-2: 4px;
	--space-3: 8px;
	--space-4: 12px;
	--space-5: 16px;
	--space-6: 20px;
}

/* ======================================
   ACCESSIBILITY
   ====================================== */
.visually-hidden {
	position: absolute !important;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip: rect(0 0 0 0);
	white-space: nowrap;
	border: 0;
}

/* focus-visible for keyboard navigation */
*:focus-visible {
	outline: 2px solid var(--color-brand);
	outline-offset: 2px;
}

/* ======================================
   HEADER & FOOTER
   ====================================== */
header.app-header,
footer.app-footer {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 10px 24px;
	background: #1c1d1f;
	border-bottom: 4px solid var(--color-brand);
}

footer.app-footer {
	border-top: 1px solid #2c313a;
	border-bottom: none;
	font-size: 12px;
	gap: 16px;
}

h1 {
	font-size: 20px;
	margin: 0;
	font-weight: 600;
	color: var(--color-text-inverse);
	letter-spacing: 0.5px;
}

h1 small {
	font-weight: 400;
	font-size: 12px;
	color: #bbb;
	margin-left: 4px;
}

.brand {
	display: flex;
	align-items: center;
	gap: 16px;
}

.brand-logo {
	height: 40px;
	width: auto;
	display: block;
}

.toolbar {
	display: flex;
	gap: 12px;
	font-size: 13px;
	align-items: center;
	color: var(--color-text-inverse);
}

/* ======================================
   FORM CONTROLS (GLOBAL)
   ====================================== */
select,
input[type=number],
button {
	background: #fff;
	color: var(--color-text);
	border: 1px solid var(--color-border-strong);
	border-radius: 4px;
	padding: 6px 8px;
	font-size: 13px;
	transition: background 0.15s, border-color 0.15s, box-shadow 0.15s;
}

select:focus,
input[type=number]:focus,
button:focus {
	outline: none;
	border-color: var(--color-brand);
	box-shadow: 0 0 0 2px rgba(196, 0, 0, 0.25);
}

input[disabled],
select[disabled] {
	opacity: 0.55;
	cursor: not-allowed;
}

input.field-error,
select.field-error {
	border-color: var(--color-danger);
	background: #ffecec;
}

button {
	cursor: pointer;
}

button:hover {
	background: #f3f4f6;
}

button.primary {
	background: var(--color-brand);
	border-color: var(--color-brand);
	font-weight: 600;
	color: var(--color-text-inverse);
}

button.primary:hover {
	background: var(--color-brand-hover);
}

button.ghost {
	background: #f2f3f5;
	border-color: var(--color-border-strong);
}

button.ghost:hover {
	background: #e7eaef;
}

button.export-pdf-inline {
	background: var(--color-brand);
	border-color: var(--color-brand);
	font-weight: 700;
	letter-spacing: 0.5px;
	color: #fff;
	position: relative;
}

button.export-pdf-inline:hover {
	background: var(--color-brand-hover);
}

button.export-pdf-inline:before {
	display: none;
}

/* ======================================
   MAIN LAYOUT & PANELS
   ====================================== */
main.layout {
	display: grid;
	grid-template-columns: minmax(0, 1fr);
	grid-auto-rows: minmax(100px, auto);
	gap: 24px;
	padding: 24px 40px 56px;
	max-width: 1320px;
	margin: 0 auto;
}

.panel {
	background: var(--color-bg-panel);
	border: 1px solid var(--color-border);
	padding: 18px 20px 22px;
	border-radius: var(--radius-panel);
	display: flex;
	flex-direction: column;
	min-height: 200px;
	position: relative;
	box-shadow: var(--shadow-panel);
}

.panel h2 {
	margin: 0 0 14px;
	font-size: 16px;
	color: #111;
	font-weight: 600;
	letter-spacing: 0;
	display: flex;
	align-items: center;
	gap: 8px;
	padding-bottom: 6px;
	border-bottom: 1px solid var(--color-border);
	position: relative;
	min-height: 1.9em;
}

.panel h2:after {
	content: "";
	position: absolute;
	left: 0;
	bottom: -1px;
	width: 56px;
	height: 2px;
	background: var(--color-brand);
}

.panel-sub {
	font-size: 11px;
	font-weight: 400;
	color: var(--color-text-sec);
	letter-spacing: 0.5px;
	text-transform: uppercase;
}

/* Panel spacing adjustments */
#panelHeadSelector {
	margin-bottom: 0;
}

#panelInputs {
	margin-top: 0;
}

#panelPreview {
    min-height: 590px; /* 内部の描画が終わる前でも、パネル自体の高さを最低限確保 */
    contain: layout; /* パネル自体のレイアウト計算を独立させ、外側（footer等）への影響を遮断 */
}


/* ======================================
   FORM FIELDS
   ====================================== */
.field {
	display: flex;
	flex-direction: column;
	margin-bottom: 10px;
	gap: 4px;
	min-height: 46px;
}

.field.series-selector-field {
    min-height: 160px; 
    contain: layout; /* レイアウトのズレを外側に波及させない */
}

.field label {
	font-size: 12px;
	font-weight: 600;
	letter-spacing: 0.3px;
}

.field input,
.field select {
	margin-top: 4px;
}

.hint {
	font-size: 11px;
	color: var(--color-text-faint);
	margin-top: 2px;
    display: block;
}

.actions {
	display: flex;
	gap: 10px;
	margin: 12px 0 4px;
}

.field label, .hint {
    line-height: 1.5;      /* 倍率で指定 */
    min-height: 1.5em;     /* 1行分の高さを常に確保 */
}

/* ======================================
   PRODUCT SELECTOR (HEAD CARDS)
   ====================================== */
.head-selector {
	margin-bottom: 14px;
	background: var(--color-bg-soft);
	border: 1px solid var(--color-border);
	border-radius: 10px;
	padding: 12px 12px 10px;
	position: relative;
}

.head-selector-track {
	display: flex;
	gap: 12px;
	overflow-x: auto;
	padding: 4px 2px 6px;
	scrollbar-width: thin;
	min-height: 157px;
}

.head-selector-track::-webkit-scrollbar {
	height: 8px;
}

.head-selector-track::-webkit-scrollbar-track {
	background: var(--color-bg-panel);
}

.head-selector-track::-webkit-scrollbar-thumb {
	background: var(--color-border-strong);
	border-radius: 4px;
}

.head-card {
	flex: 0 0 140px;
	background: #fff;
	border: 2px solid transparent;
	border-radius: 8px;
	padding: 8px 8px 10px;
	display: flex;
	flex-direction: column;
	gap: 6px;
	position: relative;
	cursor: pointer;
	box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08), 0 2px 4px rgba(0, 0, 0, 0.06);
	transition: box-shadow 0.15s, border-color 0.15s, transform 0.15s;
}

.head-card img {
	width: 100%;
	object-fit: contain;
	background: #fafbfc;
	border: 1px solid var(--color-border);
	border-radius: 6px;
}

.head-card .head-img-fallback {
	width: 100%;
	height: 88px;
	display: flex;
	align-items: center;
	justify-content: center;
	background: repeating-linear-gradient(45deg, #f3f4f6, #f3f4f6 10px, #e5e7eb 10px, #e5e7eb 20px);
	border: 1px solid var(--color-border-strong);
	border-radius: 6px;
	font-size: 18px;
	font-weight: 700;
	color: #555;
	letter-spacing: 1px;
}

.head-card .meta {
	display: flex;
	flex-direction: column;
	gap: 2px;
	font-size: 11px;
	line-height: 1.25;
}

.head-card .h-name {
	font-weight: 700;
	font-size: 12px;
	letter-spacing: 0.5px;
}

.head-card .h-feature {
	color: var(--color-text-sec);
	font-size: 11px;
}

.head-card .h-range {
	color: var(--color-text-faint);
	font-size: 10px;
}

.head-card:hover {
	box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 5px 14px rgba(0, 0, 0, 0.10);
}

.head-card:focus-visible {
	outline: none;
	border-color: var(--color-brand);
}

.head-card.current {
	border-color: var(--color-brand);
	box-shadow: 0 0 0 2px rgba(196, 0, 0, 0.25);
}

.head-card.provisional {
	border-color: #ffa726;
	box-shadow: 0 0 0 2px rgba(255, 167, 38, 0.35);
}

.head-selector-actions {
	display: flex;
	justify-content: flex-end;
	margin-top: 8px;
}

.head-selector-actions button.small {
	padding: 4px 10px;
	font-size: 12px;
}

.head-selector-actions button[disabled] {
	opacity: 0.5;
	cursor: not-allowed;
}

.status-line {
	margin-top: 6px;
	font-size: 11px;
	color: #8a929c;
	letter-spacing: 0.5px;
	text-transform: uppercase;
}
.visually-hidden {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    padding: 0 !important;
    margin: -1px !important;
    overflow: hidden !important;
    clip: rect(0, 0, 0, 0) !important;
    white-space: nowrap !important;
    border: 0 !important;
}

/* ======================================
   CANVAS & DRAWING
   ====================================== */
.canvas-wrapper {
	background: #fff;
	border: 1px solid #999;
	border-radius: 4px;
	width: 100%;
	max-width: 100%;
	position: relative;
	overflow: auto;
}

.canvas-wrapper.a4-landscape {
	aspect-ratio: 297 / 210;
	/*追加*/
	width: 100%;
    min-height: 250px; /* デバイスに合わせて調整：初期表示時の「潰れ」を防止 */
    background-color: #fff;
    /*display: flex;*/
	display: block;
    align-items: center;
    justify-content: center;
}

.drawing-sheet {
	width: 100%;
	height: 100%;
}


.Tube-visual {
	position: relative;
	width: 100%;
	height: 100%;
}

#preview-img {
	pointer-events: none;
	position: absolute;
	z-index: 1;
}

#preview-svg {
	position: absolute;
	inset: 0;
	z-index: 2;
}

#preview-svg text {
	-webkit-user-select: none; /* Safari, Chrome, iOS Safari 向け */
	user-select: none;	/* 標準プロパティ（最後に記述） */
}

/* ======================================
   SCROLLBAR STYLING
   ====================================== */
ul.reasons::-webkit-scrollbar,
.debug-output::-webkit-scrollbar {
	width: 8px;
}

ul.reasons::-webkit-scrollbar-track,
.debug-output::-webkit-scrollbar-track {
	background: #1c2025;
}

ul.reasons::-webkit-scrollbar-thumb,
.debug-output::-webkit-scrollbar-thumb {
	background: #3a414d;
	border-radius: 4px;
}

/* ======================================
   RESPONSIVE DESIGN
   ====================================== */
@media (max-width: 1200px) {
	main.layout {
		grid-template-columns: minmax(0, 1fr);
		padding: 20px 16px 40px;
		max-width: 100%;
	}

	.panel {
		order: 0;
		min-width: 0;
	}

	#panelPreview {
		order: 1;
	}

	#panelVerdict {
		order: 2;
	}

	#panelDebug {
		order: 3;
	}
}

@media (max-width: 600px) {
	html,
	body {
		max-width: 100%;
		overflow-x: hidden;
	}

	header.app-header {
		width: 100%;
		max-width: 100%;
		padding-left: max(12px, env(safe-area-inset-left));
		padding-right: max(12px, env(safe-area-inset-right));
		border-radius: 0;
	}

	.brand,
	.toolbar {
		flex-wrap: wrap;
	}

	.toolbar {
		gap: 8px;
		row-gap: 6px;
	}

	.toolbar .rule-version {
		display: none;
	}

	.toolbar label {
		white-space: normal;
	}

	.toolbar select {
		max-width: 45vw;
	}

	.brand-logo {
		height: 32px;
		max-width: 40vw;
	}

	h1 {
		font-size: 18px;
	}

	main.layout {
		padding-left: 12px;
		padding-right: 12px;
	}

	.panel {
		padding: 14px 12px 16px;
		width: 100%;
		max-width: 100%;
	}

	.field {
		min-width: 0;
	}

	.field label {
		white-space: normal;
	}

	.hint,
	.status-line {
		white-space: normal;
		word-break: break-word;
		overflow-wrap: anywhere;
	}

	.field input,
	.field select {
		width: 100%;
		max-width: 100%;
		min-width: 0;
	}

	#panelInputs,
	#panelInputs form {
		width: 100%;
		max-width: 100%;
	}

	#panelInputs * {
		max-width: 100%;
		box-sizing: border-box;
		overflow-wrap: anywhere;
		word-break: break-word;
	}

	select {
		-webkit-appearance: none;
		appearance: none;
		background-position: right 8px center;
		background-repeat: no-repeat;
	}

	input[type=number] {
		-moz-appearance: textfield;
		appearance: textfield;
	}

	input[type=number]::-webkit-outer-spin-button,
	input[type=number]::-webkit-inner-spin-button {
		-webkit-appearance: none;
		margin: 0;
	}

	.actions {
		display: grid;
		grid-template-columns: 1fr;
		gap: 8px;
	}

	.actions button {
		width: 100%;
		max-width: 100%;
		font-size: 14px;
		padding: 10px 12px;
		letter-spacing: 0;
	}

	#stdCatalogLinkBox {
		max-width: 100%;
	}

	#stdCatalogLink {
		display: block;
		max-width: 100%;
		white-space: normal;
		word-break: break-word;
		overflow-wrap: anywhere;
	}

	.canvas-wrapper {
		overflow: auto;
	}

	.canvas-wrapper.a4-landscape {
		aspect-ratio: 297 / 210;
		height: auto;
	}

	.head-visual {
		width: 960px;
		height: calc(960px * 297 / 210);
	}

	.Tube-visual {
		width: 960px;
		height: calc(960px * 210 / 297);
	}

	#panelInputs {
		overflow-x: hidden;
	}
}

