/* ================= Theme tokens ================= */
:root {
  --bg-primary: #0f1115;
  --bg-secondary: #1a1d24;
  --bg-tertiary: #242833;
  --bg-info: #1e3a52;
  --border-primary: #3a3f4b;
  --border-secondary: #2a2e38;
  --border-info: #4a90b8;
  --text-primary: #e8e8e8;
  --text-secondary: #9aa0a6;
  --text-info: #7fb8d8;
  --text-success: #5DCAA5;
  --text-danger: #E24B4A;
  --radius-sm: 4px;
  --radius-md: 6px;
  --radius-lg: 10px;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
               "Hiragino Kaku Gothic ProN", "Hiragino Sans", Meiryo, sans-serif;

  /* レスポンシブスケール: clamp(最小, 推奨, 最大) */
  --fs-xs:   clamp(9px,  2vw, 11px);
  --fs-sm:   clamp(11px, 2.5vw, 13px);
  --fs-base: clamp(12px, 3vw,  14px);
  --fs-lg:   clamp(14px, 3.5vw, 18px);
  --fs-xl:   clamp(16px, 4vw,  22px);
  --gap-sm:  clamp(4px, 1vw, 8px);
  --gap-md:  clamp(6px, 1.5vw, 12px);
  --pad-sm:  clamp(4px, 1.5vw, 10px);
  --pad-md:  clamp(8px, 2vw,  16px);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
}

body {
  margin: 0;
  padding: clamp(8px, 2vw, 20px);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: var(--fs-base);
  line-height: 1.5;
}

.app {
  max-width: min(720px, 100%);
  margin: 0 auto;
}

.app-title {
  margin: 0 0 var(--gap-md);
  font-size: var(--fs-xl);
  font-weight: 500;
  letter-spacing: 0.5px;
}

/* ================= Stats ================= */
.stats-bar {
  display: flex;
  gap: var(--gap-sm);
  margin-bottom: var(--gap-md);
  flex-wrap: wrap;
}
.stat-card {
  flex: 1;
  min-width: clamp(55px, 12vw, 80px);
  background: var(--bg-secondary);
  border-radius: var(--radius-md);
  padding: var(--pad-sm) clamp(6px, 1.5vw, 10px);
}
.stat-label {
  font-size: var(--fs-xs);
  color: var(--text-secondary);
}
.stat-value {
  font-size: var(--fs-lg);
  font-weight: 500;
}

/* ================= Canvas ================= */
.canvas-wrap {
  background: var(--bg-secondary);
  border-radius: var(--radius-lg);
  padding: clamp(6px, 1.5vw, 12px);
  margin-bottom: var(--gap-md);
  position: relative;
}
#lg-canvas {
  display: block;
  width: 100%;
  height: auto;
  background: #1a1a1a;
  border-radius: var(--radius-md);
  cursor: crosshair;
  touch-action: none;
}
.message {
  margin-top: clamp(4px, 1vw, 8px);
  font-size: var(--fs-sm);
  color: var(--text-secondary);
  min-height: 16px;
}
.message.error { color: var(--text-danger); }
.message.success { color: var(--text-success); }
.message.shop { color: var(--text-info); }

/* ================= Shop modal ================= */
.shop-modal {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  border-radius: var(--radius-lg);
  z-index: 10;
  padding: clamp(10px, 3vw, 20px);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow-y: auto;
}
.shop-inner {
  background: var(--bg-primary);
  border-radius: var(--radius-lg);
  padding: clamp(12px, 3vw, 20px);
  width: 100%;
  max-width: 560px;
}
.shop-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: var(--gap-sm);
  flex-wrap: wrap;
  gap: 4px;
}
.shop-title {
  margin: 0;
  font-size: var(--fs-lg);
  font-weight: 500;
}
.shop-money {
  font-size: var(--fs-sm);
  color: var(--text-secondary);
}
.shop-desc {
  margin: 0 0 var(--gap-md);
  font-size: var(--fs-sm);
  color: var(--text-secondary);
}
.shop-items {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(140px, 40vw), 1fr));
  gap: var(--gap-sm);
  margin-bottom: var(--gap-md);
}
.shop-item {
  padding: clamp(8px, 2vw, 12px);
  border-radius: var(--radius-md);
  border: 0.5px solid var(--border-primary);
  background: var(--bg-secondary);
  color: var(--text-primary);
  cursor: pointer;
  text-align: left;
  transition: all 0.15s;
  font-family: inherit;
}
.shop-item:hover:not(:disabled) {
  border-color: var(--border-info);
  background: var(--bg-info);
}
.shop-item:disabled { opacity: 0.4; cursor: not-allowed; }
.shop-item-name { font-size: var(--fs-sm); font-weight: 500; margin-bottom: 4px; }
.shop-item-desc { font-size: var(--fs-xs); color: var(--text-secondary); margin-bottom: 6px; min-height: 28px; }
.shop-item-cost { font-size: var(--fs-xs); font-weight: 500; color: var(--text-info); }
.shop-item-cost.unaffordable { color: var(--text-danger); }

/* ================= Buttons & tabs ================= */
.btn {
  padding: clamp(7px, 1.5vw, 10px) clamp(8px, 2vw, 14px);
  border-radius: var(--radius-md);
  border: 0.5px solid var(--border-primary);
  background: var(--bg-tertiary);
  color: var(--text-primary);
  cursor: pointer;
  font-family: inherit;
  font-size: var(--fs-sm);
  transition: background 0.15s;
}
.btn:hover:not(:disabled) { background: var(--bg-secondary); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary {
  border-color: var(--border-info);
  background: var(--bg-info);
  color: var(--text-info);
  font-weight: 500;
}
.btn-primary:hover:not(:disabled) { background: var(--bg-info); filter: brightness(1.15); }
.btn-secondary { background: var(--bg-tertiary); }
.btn-full { width: 100%; }

.mode-tabs {
  display: flex;
  gap: var(--gap-sm);
  margin-bottom: var(--gap-sm);
}
.mode-btn {
  flex: 1;
  padding: clamp(6px, 1.5vw, 8px);
  border-radius: var(--radius-md);
  border: 0.5px solid var(--border-primary);
  background: var(--bg-tertiary);
  color: var(--text-primary);
  cursor: pointer;
  font-size: var(--fs-sm);
  font-family: inherit;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.mode-btn:hover { background: var(--bg-secondary); }
.mode-btn.active {
  border-color: var(--border-info);
  background: var(--bg-info);
}

.tool-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(90px, 28vw), 1fr));
  gap: var(--gap-sm);
  margin-bottom: var(--gap-sm);
}
.tool-btn {
  padding: clamp(5px, 1.5vw, 8px);
  border-radius: var(--radius-md);
  border: 0.5px solid var(--border-primary);
  background: var(--bg-tertiary);
  color: var(--text-primary);
  cursor: pointer;
  text-align: left;
  font-family: inherit;
}
.tool-btn:hover:not(:disabled) { background: var(--bg-secondary); }
.tool-btn.active {
  border-color: var(--border-info);
  background: var(--bg-info);
}
.tool-btn.locked {
  opacity: 0.4;
  cursor: not-allowed;
  border-style: dashed;
}
.attack-card.locked {
  opacity: 0.4;
}
.tool-name { font-size: var(--fs-xs); font-weight: 500; }
.tool-desc { font-size: var(--fs-xs); color: var(--text-secondary); }

.attack-info {
  grid-template-columns: repeat(auto-fit, minmax(min(130px, 40vw), 1fr));
  gap: var(--gap-sm);
  margin-bottom: var(--gap-sm);
}
.attack-card {
  padding: clamp(5px, 1.5vw, 8px);
  border-radius: var(--radius-md);
  background: var(--bg-secondary);
  font-size: var(--fs-xs);
}
.attack-name { font-weight: 500; }
.attack-desc { color: var(--text-secondary); font-size: var(--fs-xs); }

.orient-row {
  display: flex;
  gap: var(--gap-sm);
  margin-bottom: var(--gap-md);
  flex-wrap: wrap;
}
.orient-row .btn { flex: 1; min-width: clamp(70px, 20vw, 100px); font-size: var(--fs-xs); }
.orient-display {
  flex: 1;
  min-width: clamp(70px, 20vw, 100px);
  padding: clamp(5px, 1.5vw, 8px);
  border-radius: var(--radius-md);
  background: var(--bg-secondary);
  font-size: var(--fs-xs);
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}

.main-controls {
  display: flex;
  gap: var(--gap-sm);
  flex-wrap: wrap;
  margin-bottom: clamp(8px, 2vw, 12px);
}
.main-controls .btn { padding: clamp(9px, 2vw, 12px); }
.main-controls .btn:nth-child(1) { flex: 2; min-width: clamp(110px, 30vw, 140px); font-weight: 500; }
.main-controls .btn:nth-child(2) { flex: 1; min-width: clamp(70px, 20vw, 100px); }
.main-controls .btn:nth-child(3) { flex: 0 0 auto; min-width: 44px; }

.settings-row {
  display: flex;
  gap: clamp(6px, 2vw, 12px);
  align-items: center;
  font-size: var(--fs-sm);
  color: var(--text-secondary);
  margin-bottom: clamp(6px, 1.5vw, 10px);
  flex-wrap: wrap;
}
.settings-row input[type="range"] { flex: 1; min-width: 80px; }
.settings-row #speed-out { min-width: 50px; }
.sound-label { display: flex; align-items: center; gap: 4px; cursor: pointer; }

.help-details {
  font-size: var(--fs-sm);
  color: var(--text-secondary);
}
.help-details summary { cursor: pointer; padding: 4px 0; }
.help-content { margin-top: 8px; line-height: 1.7; }
.help-content ul { margin: 4px 0; padding-left: 20px; }
.help-content strong { color: var(--text-primary); }

/* ================= Tutorial overlay ================= */
#tutorial-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: clamp(12px, 4vw, 24px);
}
#tutorial-modal {
  background: var(--bg-secondary);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-lg);
  padding: clamp(16px, 4vw, 28px) clamp(16px, 5vw, 32px);
  max-width: 520px;
  width: 100%;
  color: var(--text-primary);
  line-height: 1.7;
  overflow-y: auto;
  max-height: 90dvh;
}
#tutorial-modal h2 {
  margin: 0 0 16px;
  font-size: var(--fs-lg);
  color: #fff;
}
#tutorial-modal p { margin: 8px 0; font-size: var(--fs-sm); }
#tutorial-modal ul { margin: 8px 0; padding-left: 20px; font-size: var(--fs-sm); }
#tutorial-modal strong { color: var(--text-success); }
.tut-grid {
  display: grid;
  grid-template-columns: 18px 1fr;
  gap: 6px 10px;
  align-items: center;
  margin-top: 12px;
  font-size: var(--fs-sm);
}
.tut-cell {
  width: 14px;
  height: 14px;
  border-radius: 2px;
  flex-shrink: 0;
}
.tut-ops { display: flex; flex-direction: column; gap: 6px; font-size: var(--fs-sm); }
.tut-op-row { display: flex; align-items: center; gap: 10px; }
.tut-table { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); margin-top: 8px; }
.tut-table th { background: #1e2530; color: #9aa0a6; padding: 4px 8px; text-align: left; }
.tut-table td { padding: 4px 8px; border-bottom: 1px solid #2a2f3a; }
.tut-table tr:last-child td { border-bottom: none; }
.tut-op-row kbd {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-primary);
  border-radius: 4px;
  padding: 2px 8px;
  font-family: monospace;
  font-size: var(--fs-xs);
  min-width: 56px;
  text-align: center;
  flex-shrink: 0;
}
#tutorial-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 24px;
  flex-wrap: wrap;
  gap: 8px;
}
#tutorial-dots { display: flex; gap: 6px; }
.tut-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--border-primary);
}
.tut-dot.active { background: var(--text-success); }
#tutorial-btns { display: flex; gap: 8px; }

/* ================= Share panel ================= */
.share-panel {
  position: absolute;
  bottom: 48px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-primary);
  border: 1px solid var(--border-info);
  border-radius: var(--radius-lg);
  padding: clamp(10px, 3vw, 16px) clamp(12px, 4vw, 24px);
  z-index: 50;
  text-align: center;
  width: min(380px, 90%);
  box-shadow: 0 4px 32px rgba(0,0,0,0.6);
  animation: shareAppear 0.3s ease-out;
}
@keyframes shareAppear {
  from { opacity: 0; transform: translateX(-50%) translateY(12px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}
.share-score {
  font-size: var(--fs-lg);
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 12px;
}
.share-btns {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}
.share-btn {
  display: inline-block;
  padding: 8px 16px;
  border-radius: var(--radius-md);
  font-size: var(--fs-sm);
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  border: none;
  font-family: inherit;
  transition: filter 0.15s;
}
.share-btn:hover { filter: brightness(1.15); }
.share-btn-x    { background: #000; color: #fff; }
.share-btn-line { background: #06c755; color: #fff; }
.share-btn-copy { background: var(--bg-tertiary); color: var(--text-primary); border: 0.5px solid var(--border-primary); }

/* ================= Grand Chain spike ================= */
.screen-shake {
  animation: shake 0.4s ease-in-out;
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%       { transform: translateX(-6px); }
  40%       { transform: translateX(6px); }
  60%       { transform: translateX(-4px); }
  80%       { transform: translateX(4px); }
}
.last-stand {
  animation: lastStandPulse 1s ease-in-out infinite;
  box-shadow: 0 0 0 3px #E24B4A, 0 0 20px #E24B4A44;
}
@keyframes lastStandPulse {
  0%, 100% { box-shadow: 0 0 0 3px #E24B4A, 0 0 20px #E24B4A44; }
  50%       { box-shadow: 0 0 0 5px #FF6B35, 0 0 40px #FF6B3566; }
}

.spike-text {
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: clamp(1.4rem, 5vw, 2.2rem);
  font-weight: bold;
  text-shadow: 0 0 20px currentColor, 0 0 40px currentColor;
  pointer-events: none;
  z-index: 100;
  white-space: nowrap;
}
.spike-anim {
  animation: spikeAppear 1.5s ease-out forwards;
}
@keyframes spikeAppear {
  0%   { opacity: 0; transform: translate(-50%, -50%) scale(0.5); }
  20%  { opacity: 1; transform: translate(-50%, -50%) scale(1.3); }
  60%  { opacity: 1; transform: translate(-50%, -50%) scale(1.0); }
  100% { opacity: 0; transform: translate(-50%, -80%) scale(0.9); }
}

/* ================= Debug panel ================= */
#debug-panel {
  font-family: monospace;
  font-size: var(--fs-xs);
  background: rgba(0,0,0,0.85);
  border: 1px solid #444;
  border-radius: var(--radius-md);
  padding: 10px 14px;
  color: #ccc;
  margin-top: 10px;
  line-height: 1.8;
}
#debug-panel h3 {
  margin: 0 0 6px;
  font-size: var(--fs-sm);
  color: #fff;
  letter-spacing: 0.05em;
}
.debug-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 2px 16px;
}
.debug-row { display: contents; }
.debug-key  { color: #888; }
.debug-val  { color: #5DCAA5; font-weight: bold; }
#debug-history {
  margin-top: 8px;
  border-top: 1px solid #333;
  padding-top: 6px;
}
#debug-history table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--fs-xs);
}
#debug-history th {
  color: #888;
  text-align: right;
  padding: 1px 6px;
  border-bottom: 1px solid #333;
}
#debug-history th:first-child { text-align: left; }
#debug-history td {
  text-align: right;
  padding: 1px 6px;
  color: #ccc;
}
#debug-history td:first-child { text-align: left; color: #888; }
#debug-history tr:last-child td { color: #fff; }

/* ================= Responsive breakpoints ================= */
@media (max-width: 480px) {
  .app-title { display: none; }

  .stats-bar { gap: 4px; }
  .stat-card { padding: 5px 7px; min-width: 48px; }

  /* モードタブはアイコンのみ */
  .mode-btn { font-size: 11px; padding: 6px 4px; }

  /* 操作ボタンを縦2列 */
  .orient-row { gap: 4px; }
  .orient-display { display: none; }

  .main-controls { gap: 4px; }
  .main-controls .btn:nth-child(1) { flex: 3; }

  .settings-row { gap: 6px; font-size: 11px; }

  .help-details { font-size: 11px; }
}

@media (min-width: 900px) {
  /* 横幅が十分ある場合はサイドパネル的なレイアウト */
  .app {
    display: grid;
    grid-template-columns: 1fr 260px;
    grid-template-rows: auto;
    column-gap: 16px;
    align-items: start;
  }
  .app-title   { grid-column: 1 / -1; }
  .stats-bar   { grid-column: 1 / -1; }
  .canvas-wrap { grid-column: 1; grid-row: 3 / 10; }
  .mode-tabs         { grid-column: 2; }
  #pattern-row       { grid-column: 2; }
  #attack-row        { grid-column: 2; }
  .orient-row        { grid-column: 2; }
  .main-controls     { grid-column: 2; }
  .settings-row      { grid-column: 2; }
  .help-details      { grid-column: 2; }
}
