*, *::before, *::after { box-sizing: border-box; }

:root {
  --bg: #eef2f0;
  --surface: #ffffff;
  --surface-soft: #f5f7f5;
  --border: #d8dfda;
  --border-soft: #e6ebe7;
  --text: #3c4a52;
  --text-mute: #7c8a90;
  --primary: #7a9877;
  --primary-strong: #628560;
  --primary-soft: #e5ede3;
  --primary-tint: #f0f5ef;
  --danger: #cf7f6f;
  --danger-soft: #f5e6e2;
  --accent: #c9a05c;
}

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

body { touch-action: manipulation; }

.app {
  max-width: 720px;
  margin: 0 auto;
  padding: 0 16px env(safe-area-inset-bottom);
}

.app-header {
  padding: 22px 4px 18px;
}

.app-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.app-logo {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  display: inline-block;
}

.app-logo svg { width: 100%; height: 100%; display: block; }

.app-brand-text {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
}

.app-title {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: 0.06em;
  line-height: 1;
}

.app-tagline {
  margin: 0;
  font-size: 11.5px;
  color: var(--text-mute);
  font-weight: 500;
  line-height: 1.4;
}

.main {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding-bottom: 32px;
}

.step {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: 18px;
  padding: 20px 18px;
  box-shadow: 0 1px 2px rgba(60, 74, 82, 0.03);
}

.step.is-locked {
  opacity: 0.55;
  background: var(--surface-soft);
  box-shadow: none;
}

.step-title {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 16px;
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: 0.02em;
}

.step-num {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  background: var(--primary-soft);
  color: var(--primary-strong);
  border-radius: 50%;
  font-size: 11px;
  font-weight: 700;
}

.locked-note {
  margin: 0;
  font-size: 13px;
  color: var(--text-mute);
}

/* ─── フォーム ─── */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
}

.field label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-mute);
}

.field select,
.field input {
  padding: 11px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #ffffff;
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
  width: 100%;
  transition: border-color 0.15s ease;
}

.field select:focus,
.field input:focus {
  outline: none;
  border-color: var(--primary);
}

.field select {
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%237c8a90' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 16px;
  padding-right: 40px;
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.inline-input {
  display: flex;
  align-items: stretch;
  gap: 0;
}

.inline-input input {
  border-radius: 12px 0 0 12px;
  border-right: none;
  text-align: right;
}

.inline-input .unit {
  padding: 11px 14px;
  background: var(--surface-soft);
  border: 1px solid var(--border);
  border-left: none;
  border-radius: 0 12px 12px 0;
  font-size: 13px;
  color: var(--text-mute);
  display: inline-flex;
  align-items: center;
  font-weight: 500;
}

/* ─── ゲージ ─── */
.gauge-panel {
  background: var(--primary-tint);
  border: 1px solid var(--primary-soft);
  border-radius: 14px;
  padding: 14px 16px;
  margin-bottom: 14px;
}

.gauge-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  font-weight: 700;
  color: var(--primary-strong);
  letter-spacing: 0.03em;
}

.gauge-note {
  margin: 6px 0 12px;
  font-size: 11px;
  color: var(--text-mute);
  line-height: 1.5;
}

.gauge-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.gauge-fields label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-mute);
}

.gauge-fields input {
  flex: 1 1 auto;
  min-width: 0;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #ffffff;
  font-size: 14px;
  text-align: right;
  font-family: inherit;
  color: var(--text);
}

.gauge-fields input:focus { outline: none; border-color: var(--primary); }

.gauge-fields .unit {
  font-size: 11px;
  color: var(--text-mute);
  white-space: nowrap;
}

.link-btn {
  background: transparent;
  border: none;
  color: var(--primary-strong);
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  padding: 0;
  text-decoration: none;
  border-bottom: 1px dashed var(--primary);
  font-family: inherit;
}

.link-btn:active { opacity: 0.6; }

/* ─── 編み図サイズ表示 ─── */
.grid-summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: var(--surface-soft);
  border-radius: 12px;
  gap: 12px;
}

.grid-summary-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-mute);
}

.grid-summary-value {
  font-size: 17px;
  font-weight: 700;
  color: var(--primary-strong);
  font-variant-numeric: tabular-nums;
}

/* ─── 画像アップロード ─── */
.upload-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 0 8px;
}

.icon-btn {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 10px 4px 8px;
  min-width: 68px;
  background: #ffffff;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 16px;
  cursor: pointer;
  font-family: inherit;
  transition: transform 0.1s ease, background 0.15s ease;
}

.icon-btn > svg {
  width: 26px;
  height: 26px;
  stroke: currentColor;
}

.icon-btn-label {
  font-size: 11px;
  font-weight: 600;
  color: inherit;
  opacity: 0.85;
  white-space: nowrap;
}

.icon-btn:active { transform: translateY(1px); }
.icon-btn:hover { background: var(--surface-soft); }

.icon-btn.primary {
  background: linear-gradient(180deg, #94b291 0%, var(--primary) 100%);
  border-color: transparent;
  color: #ffffff;
  box-shadow: 0 2px 6px rgba(122, 152, 119, 0.22);
  padding: 14px 8px 12px;
  min-width: 88px;
}

.icon-btn.primary > svg { width: 30px; height: 30px; }
.icon-btn.primary .icon-btn-label { opacity: 1; font-size: 12px; }
.icon-btn.primary:hover { background: linear-gradient(180deg, #94b291 0%, var(--primary) 100%); }

.icon-btn.danger { color: var(--danger); }
.icon-btn.danger:hover { background: var(--danger-soft); }
.icon-btn.danger:active { background: var(--danger-soft); }

.editor-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}

/* 背景色ピッカー */
.bg-color-btn {
  position: relative;
  cursor: pointer;
}

.bg-color-btn input[type="color"] {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  border: 0;
  padding: 0;
  cursor: pointer;
}

.bg-color-chip {
  display: block;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid #ffffff;
  box-shadow: 0 0 0 1px var(--border), inset 0 0 0 1px rgba(60, 74, 82, 0.05);
  background: #ffffff;
}

/* ─── 画像エディタ ─── */
.editor-wrap {
  margin-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.editor-wrap[hidden],
.upload-empty[hidden],
.icon-btn[hidden] { display: none; }

.editor-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  background-color: #ffffff;
  border: 1.5px solid var(--primary-soft);
  border-radius: 14px;
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.editor-frame canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  pointer-events: none;
}

.image-box {
  position: absolute;
  border: 1.5px dashed var(--primary);
  cursor: move;
  touch-action: none;
}

.handle {
  position: absolute;
  width: 18px;
  height: 18px;
  background: #ffffff;
  border: 2px solid var(--primary);
  border-radius: 50%;
  cursor: pointer;
}

.handle-tl { top: -10px; left: -10px; cursor: nwse-resize; }
.handle-tr { top: -10px; right: -10px; cursor: nesw-resize; }
.handle-bl { bottom: -10px; left: -10px; cursor: nesw-resize; }
.handle-br { bottom: -10px; right: -10px; cursor: nwse-resize; }

.unit { font-variant-numeric: tabular-nums; }

/* ─── セグメントボタン（色数） ─── */
.segment-buttons {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 4px;
  background: var(--surface-soft);
  padding: 4px;
  border-radius: 12px;
}

.segment-buttons button {
  padding: 8px 4px;
  background: transparent;
  border: none;
  border-radius: 9px;
  font-size: 13px;
  font-weight: 700;
  color: var(--text-mute);
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s ease, color 0.15s ease;
}

.segment-buttons button.is-active {
  background: #ffffff;
  color: var(--primary-strong);
  box-shadow: 0 1px 2px rgba(60, 74, 82, 0.08);
}

/* ─── パレット & プレビュー ─── */
.sub-section { margin-top: 14px; }

.sub-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 8px;
}

.sub-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--text);
}

.sub-note {
  font-size: 11px;
  color: var(--text-mute);
  font-weight: 500;
}

.palette-swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.palette-swatch {
  position: relative;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 2px solid #ffffff;
  box-shadow: 0 0 0 1px var(--border), 0 1px 2px rgba(60, 74, 82, 0.05);
  cursor: pointer;
  padding: 0;
  overflow: hidden;
}

.palette-swatch input[type="color"] {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  border: 0;
  cursor: pointer;
  padding: 0;
}

.dot-preview-wrap {
  background: var(--surface-soft);
  border-radius: 12px;
  padding: 14px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.dot-preview {
  display: block;
  max-width: 100%;
  image-rendering: pixelated;
  image-rendering: -moz-crisp-edges;
  background: #ffffff;
  border-radius: 4px;
  box-shadow: 0 1px 3px rgba(60, 74, 82, 0.08);
}

.palette-content[hidden] { display: none; }

/* ─── 編み図（P4） ─── */
.chart-info {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 14px 16px;
  background: var(--primary-tint);
  border: 1px solid var(--primary-soft);
  border-radius: 14px;
}

.chart-info-row {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-mute);
}

.chart-info-row strong {
  color: var(--primary-strong);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.chart-toggles {
  display: flex;
  gap: 6px;
  margin-top: 14px;
  flex-wrap: wrap;
}

.chip-btn {
  padding: 6px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-mute);
  cursor: pointer;
  font-family: inherit;
}

.chip-btn.is-active {
  background: var(--primary-soft);
  border-color: var(--primary-soft);
  color: var(--primary-strong);
}

.chart-canvas-wrap {
  margin-top: 12px;
  padding: 12px;
  background: var(--surface-soft);
  border-radius: 12px;
  overflow: auto;
  max-height: 70vh;
  -webkit-overflow-scrolling: touch;
}

.chart-canvas {
  display: block;
  background: #ffffff;
  border-radius: 4px;
  box-shadow: 0 1px 3px rgba(60, 74, 82, 0.06);
  image-rendering: -webkit-optimize-contrast;
}

.chart-legend {
  margin: 14px 0 0;
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  font-size: 11px;
  color: var(--text-mute);
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.legend-sym {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border-radius: 4px;
  background: #ffffff;
  border: 1px solid var(--border);
  font-size: 13px;
  color: var(--text);
  font-weight: 600;
}

.chart-content[hidden] { display: none; }

/* ─── 編み手順ガイド（P5） ─── */
.guide-content[hidden] { display: none; }

.row-selector {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--surface-soft);
  border-radius: 999px;
  justify-content: center;
}

.row-nav-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.row-nav-btn svg { width: 16px; height: 16px; }
.row-nav-btn:active { background: var(--surface-soft); }
.row-nav-btn:disabled { opacity: 0.35; cursor: not-allowed; }

.row-select-mid {
  display: flex;
  align-items: center;
  gap: 6px;
}

.row-select-label {
  font-size: 12px;
  color: var(--text-mute);
  font-weight: 600;
}

#row-input {
  width: 68px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 15px;
  font-weight: 700;
  text-align: right;
  color: var(--text);
  font-family: inherit;
  background: #ffffff;
  -moz-appearance: textfield;
}

#row-input::-webkit-outer-spin-button,
#row-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

#row-input:focus { outline: none; border-color: var(--primary); }

.row-select-max {
  font-size: 12px;
  color: var(--text-mute);
  font-variant-numeric: tabular-nums;
}

.row-summary {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 20px;
  padding: 14px 16px;
  background: var(--primary-tint);
  border: 1px solid var(--primary-soft);
  border-radius: 14px;
  margin-top: 12px;
}

.row-summary-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.row-summary-label {
  font-size: 11px;
  color: var(--text-mute);
  font-weight: 600;
}

.row-summary-item strong {
  font-size: 13px;
  font-weight: 700;
  color: var(--primary-strong);
  font-variant-numeric: tabular-nums;
}

.row-colors {
  display: inline-flex;
  gap: 4px;
  flex-wrap: wrap;
}

.row-color-item {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  padding: 2px 6px 2px 2px;
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.row-color-chip {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 1px solid #ffffff;
  box-shadow: 0 0 0 1px var(--border);
}

.row-steps {
  list-style: none;
  padding: 0;
  margin: 14px 0 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.step-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: 12px;
  font-size: 13px;
  color: var(--text);
  line-height: 1.5;
}

.step-item.step-turn {
  background: var(--surface-soft);
  color: var(--text-mute);
  font-size: 12px;
}

.step-num-mini {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  background: var(--primary-soft);
  color: var(--primary-strong);
  border-radius: 50%;
  font-size: 10px;
  font-weight: 700;
  flex-shrink: 0;
}

.step-color-chip {
  display: inline-flex;
  width: 16px;
  height: 16px;
  border-radius: 4px;
  border: 1.5px solid #ffffff;
  box-shadow: 0 0 0 1px var(--border);
  flex-shrink: 0;
}

.step-text {
  flex: 1 1 auto;
}

.step-range {
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.step-note {
  padding: 6px 12px 6px 40px;
  font-size: 11px;
  color: var(--text-mute);
  background: transparent;
  display: flex;
  align-items: center;
  gap: 6px;
  line-height: 1.5;
}

.step-note::before {
  content: '↳';
  color: var(--primary);
  font-weight: 700;
}

.step-note .step-carry-chip {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 3px;
  border: 1px solid var(--border);
  vertical-align: middle;
  margin: 0 2px;
}

.guide-note {
  margin: 16px 0 0;
  font-size: 11px;
  color: var(--text-mute);
  line-height: 1.65;
  padding: 10px 12px;
  background: var(--surface-soft);
  border-radius: 10px;
}

.save-row {
  margin-top: 16px;
  display: flex;
  justify-content: center;
}

.save-row .icon-btn {
  flex-direction: row;
  gap: 8px;
  min-width: auto;
  padding: 10px 18px;
}

.save-row .icon-btn > svg { width: 20px; height: 20px; }

.save-row .icon-btn.primary {
  padding: 12px 22px;
  min-width: auto;
}

.save-row .icon-btn.primary > svg { width: 22px; height: 22px; }
.save-row .icon-btn.primary .icon-btn-label { font-size: 13px; }

/* ─── 確認モーダル ─── */
.confirm-dialog {
  border: none;
  padding: 0;
  margin: auto;
  background: transparent;
  max-width: 320px;
  width: calc(100% - 32px);
}

.confirm-dialog::backdrop {
  background: rgba(60, 74, 82, 0.42);
  backdrop-filter: blur(3px);
}

.confirm-body {
  background: var(--surface);
  border-radius: 18px;
  padding: 22px 22px 18px;
  box-shadow: 0 20px 40px rgba(60, 74, 82, 0.25);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.confirm-title {
  margin: 0;
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
}

.confirm-message {
  margin: 0 0 8px;
  font-size: 13px;
  color: var(--text-mute);
  line-height: 1.55;
}

.confirm-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 4px;
}

.danger-btn {
  padding: 9px 18px;
  background: var(--danger);
  color: #ffffff;
  border: none;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  box-shadow: 0 1px 3px rgba(207, 127, 111, 0.25);
}

.danger-btn:active { transform: translateY(1px); box-shadow: none; }
