:root {
  --bg: #f8fafc;
  --card-bg: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --text-muted: #64748b;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --secondary: #64748b;
  --success: #22c55e;
  --warning: #f59e0b;
  --danger: #ef4444;
  --shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
  --shadow-lg: 0 10px 40px rgba(0,0,0,0.15);
}

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

html, body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
}

/* コンテナとヘッダー */
.container {
  max-width: 1024px;
  margin: 0 auto;
  padding: 0 20px;
}

.header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 2rem 0;
  margin-bottom: 2rem;
}

.hero {
  text-align: center;
  max-width: 100%;
}

.hero h1 {
  font-size: 2rem;
  font-weight: 700;
  margin-bottom: 0.5rem;
  word-wrap: break-word;
  line-height: 1.2;
}

.badge {
  font-size: 0.75rem;
  background: rgba(255,255,255,0.2);
  padding: 2px 8px;
  border-radius: 12px;
  margin-left: 8px;
  display: inline-block;
}

.subtitle {
  font-size: 1.125rem;
  opacity: 0.95;
  max-width: 100%;
  word-wrap: break-word;
}

/* カード */
.card {
  background: var(--card-bg);
  border-radius: 12px;
  padding: 1.5rem;
  margin-bottom: 1.5rem;
  box-shadow: var(--shadow);
  transition: box-shadow 0.3s ease;
}

.card:hover {
  box-shadow: var(--shadow-lg);
}

.card h2 {
  font-size: 1.5rem;
  margin-bottom: 1rem;
  color: var(--text);
}

.card h3 {
  font-size: 1.25rem;
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

/* 入力エリア */
.input-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
  flex-wrap: wrap;
  gap: 1rem;
}

.input-header h2 {
  margin: 0;
  flex: 1;
}

.sample-btn {
  font-size: 0.9rem;
  padding: 8px 16px;
  white-space: nowrap;
}

textarea {
  width: 100%;
  padding: 12px;
  border: 2px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  resize: vertical;
  min-height: 120px;
  transition: border-color 0.3s ease;
}

textarea:focus {
  outline: none;
  border-color: var(--primary);
}

textarea.error {
  border-color: var(--danger);
  background-color: #fef2f2;
}

.action-area {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
  margin-top: 1.5rem;
}

select {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.95rem;
  background: white;
}

/* ボタン */
.btn {
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

.btn.primary {
  background: var(--primary);
  color: white;
}

.btn.primary:hover {
  background: var(--primary-hover);
  transform: translateY(-1px);
}

.btn.primary.large {
  padding: 12px 32px;
  font-size: 1.125rem;
}

.btn.secondary {
  background: white;
  color: var(--secondary);
  border: 1px solid var(--border);
}

.btn.secondary:hover {
  background: var(--bg);
}

.btn:active {
  transform: translateY(0);
}

.btn:disabled,
.btn.disabled {
  opacity: 0.5;
  cursor: not-allowed;
  background: #e2e8f0;
  color: #94a3b8;
  border-color: #e2e8f0;
}

.btn.primary:disabled,
.btn.primary.disabled {
  background: #cbd5e1;
  color: #94a3b8;
}

.btn:disabled:hover,
.btn.disabled:hover {
  transform: none;
  box-shadow: none;
}

/* 結果表示 */
.hidden {
  display: none !important;
}

.result-card {
  background: linear-gradient(135deg, #f6f8fb 0%, #ffffff 100%);
}

.simple-result {
  margin: 1.5rem 0;
}

.winner-cipher {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 1.5rem;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.cipher-icon {
  font-size: 3rem;
}

.cipher-details {
  flex: 1;
}

.cipher-details h3 {
  font-size: 1.5rem;
  margin-bottom: 0.5rem;
  color: var(--primary);
}

.description {
  color: var(--text-muted);
  margin-bottom: 1rem;
}

.confidence-bar {
  position: relative;
  height: 24px;
  background: var(--border);
  border-radius: 12px;
  overflow: hidden;
}

.confidence-fill {
  height: 100%;
  background: linear-gradient(90deg, #22c55e, #16a34a);
  transition: width 0.5s ease;
  border-radius: 12px;
}

.confidence-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-weight: 700;
  font-size: 1rem;
  letter-spacing: 0.5px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  /* 色とシャドウはJavaScriptで動的に設定 */
}

.other-possibilities {
  margin-top: 1.5rem;
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}

.possibility-chip {
  padding: 8px 16px;
  background: white;
  border: 1px solid var(--border);
  border-radius: 20px;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.possibility-chip .percentage {
  font-weight: 600;
  color: var(--text-muted);
}

/* 判定根拠セクション */
.evidence-section {
  margin-top: 1.5rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
}

.evidence-section h3 {
  margin-bottom: 1rem;
  color: var(--text);
}

.evidence-content {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.evidence-group {
  background: var(--bg);
  border-radius: 8px;
  padding: 1rem;
  border-left: 3px solid var(--primary);
}

.evidence-group h4 {
  margin: 0 0 0.75rem 0;
  color: var(--primary);
  font-size: 1rem;
}

.evidence-item {
  margin-bottom: 0.75rem;
  padding: 0.5rem;
  background: white;
  border-radius: 6px;
  border: 1px solid var(--border);
}

.evidence-item:last-child {
  margin-bottom: 0;
}

.evidence-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.25rem;
}

.evidence-metric {
  font-weight: 600;
  color: var(--text);
}

.evidence-value {
  font-family: ui-monospace, Consolas, monospace;
  background: var(--bg);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.9rem;
}

.evidence-description {
  color: var(--text-muted);
  font-size: 0.9rem;
  line-height: 1.4;
}

.confidence-badge {
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
}

.confidence-high {
  background: #dcfce7;
  color: #166534;
}

.confidence-medium {
  background: #fef3c7;
  color: #92400e;
}

.confidence-low {
  background: #fee2e2;
  color: #991b1b;
}

.strength-very_strong {
  background: #10b981;
  color: white;
}

.strength-strong {
  background: #059669;
  color: white;
}

.strength-medium {
  background: #d97706;
  color: white;
}

.strength-weak {
  background: #dc2626;
  color: white;
}

.excluded-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem;
  background: #fef2f2;
  border-radius: 6px;
  margin-bottom: 0.5rem;
}

.excluded-cipher {
  font-weight: 600;
  color: var(--text);
}

.excluded-reason {
  color: var(--text-muted);
  font-size: 0.9rem;
  flex: 1;
  margin: 0 1rem;
}

.excluded-prob {
  font-size: 0.8rem;
  color: #dc2626;
  font-weight: 600;
}

/* 詳細セクション */
.detail-toggle {
  text-align: center;
  margin-top: 1.5rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
}

.detail-card {
  animation: slideIn 0.3s ease;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.stat-item {
  padding: 1rem;
  background: var(--bg);
  border-radius: 8px;
  border-left: 3px solid var(--primary);
}

.stat-label {
  font-size: 0.875rem;
  color: var(--text-muted);
  margin-bottom: 0.25rem;
}

.stat-value {
  font-size: 1.25rem;
  font-weight: 600;
}

/* グラフ */
.freq-chart-container {
  margin-top: 1.5rem;
}

.freq-chart-container h4 {
  margin-bottom: 0.5rem;
}

.help-text {
  font-size: 0.875rem;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}

.freq-chart, .mini-chart {
  width: 100%;
  height: auto;
  background: var(--bg);
  border-radius: 8px;
  padding: 8px;
}

.period-analysis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 1.5rem;
  margin-bottom: 1.5rem;
}

.analysis-item h4 {
  margin-bottom: 0.25rem;
}

/* ヘルプアイコン */
.help-icon {
  color: var(--text-muted);
  cursor: help;
  font-size: 1rem;
}

/* オプション設定 */
.options-detail {
  margin: 1rem 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.5rem;
  background: var(--bg);
}

.options-detail summary {
  cursor: pointer;
  font-weight: 600;
  padding: 0.5rem;
  user-select: none;
  list-style: none;
}

.options-detail summary::-webkit-details-marker {
  display: none;
}

.options-detail summary:hover {
  color: var(--primary);
}

.options-content {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.options-content h4 {
  margin: 1rem 0 0.5rem;
  color: var(--text-muted);
}

.checks {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.checks label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
}

.checks input[type="checkbox"] {
  width: 18px;
  height: 18px;
  cursor: pointer;
}

/* フッター */
footer {
  margin-top: 3rem;
  padding: 2rem 0;
  background: white;
  border-top: 1px solid var(--border);
  text-align: center;
  color: var(--text-muted);
}

footer a {
  color: var(--primary);
  text-decoration: none;
}

footer a:hover {
  text-decoration: underline;
}

/* レスポンシブ */
@media (max-width: 768px) {
  .container {
    padding: 0 15px;
  }
  
  .hero h1 {
    font-size: 1.5rem;
    line-height: 1.3;
  }
  
  .badge {
    display: block;
    margin: 0.5rem auto 0;
    width: fit-content;
  }
  
  .subtitle {
    font-size: 1rem;
  }
  
  .input-header {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
  }
  
  .input-header h2 {
    text-align: center;
  }
  
  .sample-btn {
    align-self: center;
  }
  
  .action-area {
    justify-content: space-between;
  }
  
  .winner-cipher {
    flex-direction: column;
    text-align: center;
  }
}

@media (max-width: 480px) {
  .hero h1 {
    font-size: 1.25rem;
  }
  
  .subtitle {
    font-size: 0.9rem;
  }
}

/* モーダル */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-content {
  background: white;
  border-radius: 12px;
  max-width: 600px;
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1.5rem;
  border-bottom: 1px solid var(--border);
}

.modal-header h3 {
  margin: 0;
  color: var(--text);
}

.modal-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: var(--text-muted);
  padding: 0;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-close:hover {
  background: var(--bg);
  color: var(--text);
}

.modal-body {
  padding: 1.5rem;
}

.modal-description {
  color: var(--text-muted);
  margin-bottom: 1.5rem;
  line-height: 1.5;
}

.sample-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.sample-item {
  border: 2px solid var(--border);
  border-radius: 8px;
  padding: 1rem;
  cursor: pointer;
  transition: all 0.3s ease;
  background: white;
}

.sample-item:hover {
  border-color: var(--primary);
  background: #f8fafc;
}

.sample-item h4 {
  margin: 0 0 0.5rem 0;
  color: var(--primary);
  font-size: 1.1rem;
}

.sample-item p {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.95rem;
  line-height: 1.4;
}

/* 頻度グラフのツールチップ */
.freq-tooltip {
  position: absolute;
  background: white;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  max-width: 280px;
  font-size: 0.9rem;
  pointer-events: none;
}

.freq-tooltip.hidden {
  display: none;
}

/* 周期性分析グラフのツールチップ */
.period-tooltip {
  position: absolute;
  background: white;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  max-width: 320px;
  font-size: 0.9rem;
  pointer-events: none;
  white-space: nowrap;
}

.period-tooltip.hidden {
  display: none;
}

.tooltip-header {
  font-weight: 600;
  color: var(--primary);
  margin-bottom: 0.5rem;
  font-size: 1rem;
}

.tooltip-content {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.tooltip-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.tooltip-label {
  color: var(--text-muted);
  font-size: 0.85rem;
}

.tooltip-value {
  font-weight: 600;
  color: var(--text);
  font-family: ui-monospace, Consolas, monospace;
}

.tooltip-value.difference.positive {
  color: #059669;
}

.tooltip-value.difference.negative {
  color: #dc2626;
}

/* SVGバーのホバー効果 */
.bar-group:hover .observed-bar {
  opacity: 1;
  filter: brightness(1.1);
}

.bar-group:hover .expected-bar {
  opacity: 0.8;
}

/* 周期性分析グラフのバーホバー効果 */
.period-bar {
  cursor: pointer;
  transition: opacity 0.2s ease, filter 0.2s ease;
}

.period-bar:hover {
  opacity: 1;
  filter: brightness(1.1);
}

.period-bar-group:hover .period-bar {
  opacity: 1;
}

/* 入力エラーメッセージ */
.input-error {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.5rem;
  padding: 0.75rem;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 8px;
  color: var(--danger);
  font-size: 0.9rem;
  animation: slideDown 0.3s ease;
}

.input-error.hidden {
  display: none;
}

.error-icon {
  font-size: 1rem;
  flex-shrink: 0;
}

.error-message {
  flex: 1;
  line-height: 1.4;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ヘッダーレイアウト修正 */
.header .container {
  position: relative;
}

.help-icon-btn {
  position: absolute;
  top: 2rem;
  right: 20px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 2px solid rgba(255, 255, 255, 0.3);
  font-size: 1.2rem;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(10px);
}

.help-icon-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  border-color: rgba(255, 255, 255, 0.5);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

/* ヘルプモーダル専用スタイル */
.help-modal-content {
  max-width: 800px;
  max-height: 85vh;
}

.help-modal-body {
  padding: 0;
  max-height: calc(85vh - 80px);
  overflow-y: auto;
}

.help-content {
  padding: 1.5rem;
}

.help-section {
  margin-bottom: 2rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid #f1f5f9;
}

.help-section:last-child {
  border-bottom: none;
  margin-bottom: 0;
}

.help-section h4 {
  color: var(--primary);
  margin-bottom: 1rem;
  font-size: 1.1rem;
  font-weight: 600;
}

.help-section p {
  color: var(--text-muted);
  line-height: 1.6;
  margin-bottom: 1rem;
}

.help-section ol, .help-section ul {
  color: var(--text-muted);
  line-height: 1.6;
  padding-left: 1.5rem;
}

.help-section li {
  margin-bottom: 0.5rem;
}

.help-section li strong {
  color: var(--text);
}

.help-section a {
  color: var(--primary);
  text-decoration: none;
}

.help-section a:hover {
  text-decoration: underline;
}

/* 暗号方式グリッド */
.cipher-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 1rem;
  margin-top: 1rem;
}

.cipher-item {
  padding: 1rem;
  background: #f8fafc;
  border-radius: 8px;
  border-left: 3px solid var(--primary);
}

.cipher-item strong {
  display: block;
  color: var(--text);
  margin-bottom: 0.5rem;
  font-size: 1rem;
}

.cipher-item span {
  color: var(--text-muted);
  font-size: 0.9rem;
  line-height: 1.4;
}

/* 分析説明 */
.analysis-explanation {
  margin-top: 1rem;
}

.analysis-explanation .analysis-item {
  margin-bottom: 1.5rem;
  padding: 1rem;
  background: #f8fafc;
  border-radius: 8px;
}

.analysis-explanation .analysis-item:last-child {
  margin-bottom: 0;
}

.analysis-explanation .analysis-item strong {
  display: block;
  color: var(--text);
  margin-bottom: 0.5rem;
  font-size: 1rem;
}

.analysis-explanation .analysis-item p {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.9rem;
  line-height: 1.5;
}

/* 共通説明ボックス */
.info-box {
  margin-bottom: 1.5rem;
  padding: 1rem;
  border-radius: 8px;
  border-left: 3px solid var(--primary);
}

.info-box-blue {
  background: #f8fafc;
  border-color: #3b82f6;
}

.info-box-orange {
  background: #fff7ed;
  border-color: #f59e0b;
}

.info-box h4 {
  margin: 0 0 0.5rem 0;
  color: var(--text);
}

.info-box-blue h4 {
  color: #1e293b;
}

.info-box-orange h4 {
  color: #92400e;
}

.info-box p {
  margin: 0;
  line-height: 1.5;
}

.info-box-blue p {
  color: #64748b;
}

.info-box-orange p,
.info-box-orange ul {
  color: #92400e;
  font-size: 0.9rem;
}

.info-box ul {
  margin: 0;
  padding-left: 1.2rem;
}

.info-box li {
  margin-bottom: 0.25rem;
}

/* 分析説明テキスト */
.analysis-description {
  margin-bottom: 1rem;
}

.analysis-description .analysis-label {
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.analysis-description .analysis-text {
  font-size: 0.9rem;
  color: #64748b;
  margin-bottom: 0.5rem;
}

.analysis-description .analysis-note {
  font-weight: 600;
}

/* トグルセクション */
.toggle-section {
  margin: 1rem 0;
  text-align: center;
}

.toggle-btn {
  background: var(--card-bg);
  border: 2px solid var(--border);
  border-radius: 8px;
  padding: 1rem 2rem;
  font-size: 1rem;
  color: var(--secondary);
  cursor: pointer;
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  box-shadow: var(--shadow);
}

.toggle-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
}

/* 暗号解読ツールリンク */
.tool-link {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 1.5rem;
  background: #3b82f6;
  color: white;
  text-decoration: none;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 500;
  box-shadow: 0 2px 4px rgba(59, 130, 246, 0.3);
  transition: all 0.2s;
}

.tool-link:hover {
  background: #2563eb;
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(59, 130, 246, 0.4);
}

.tool-link-icon {
  font-size: 0.8rem;
  opacity: 0.8;
}

.tool-header {
  margin-bottom: 0.5rem;
  font-weight: 600;
  color: var(--primary);
}

.tool-description {
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

/* モバイル対応 */
@media (max-width: 768px) {
  .help-icon-btn {
    top: 1.5rem;
    right: 15px;
    width: 36px;
    height: 36px;
    font-size: 1.1rem;
  }
  
  .help-modal-content {
    max-width: 95vw;
    max-height: 90vh;
    margin: 5vh auto;
  }
  
  .help-modal-body {
    max-height: calc(90vh - 80px);
  }
  
  .cipher-grid {
    grid-template-columns: 1fr;
  }
}