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

:root {
  --bg: #0f1117;
  --bg2: #181c27;
  --bg3: #1e2333;
  --border: rgba(255, 255, 255, 0.08);
  --border2: rgba(255, 255, 255, 0.14);
  --text: #e8eaf0;
  --text2: #8b90a0;
  --text3: #555c70;
  --accent: #4f8ef7;
  --accent2: #2a5cbf;
  --error: #f76b6b;
  --mono: 'IBM Plex Mono', monospace;
  --sans: 'Noto Sans JP', sans-serif;

  --h0: #f7c948;
  --h1: #4f8ef7;
  --h2: #50d9a0;
  --h3: #f7824f;
  --h4: #c97cf7;
  --h5: #f74f9e;

  --h0-bg: rgba(247, 201, 72, 0.18);
  --h1-bg: rgba(79, 142, 247, 0.18);
  --h2-bg: rgba(80, 217, 160, 0.18);
  --h3-bg: rgba(247, 130, 79, 0.18);
  --h4-bg: rgba(201, 124, 247, 0.18);
  --h5-bg: rgba(247, 79, 158, 0.18);
}

body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ===== HEADER ===== */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 52px;
  border-bottom: 1px solid var(--border);
  background: var(--bg2);
  flex-shrink: 0;
}

.logo {
  font-family: var(--mono);
  font-size: 15px;
  font-weight: 500;
  color: var(--accent);
  letter-spacing: -0.02em;
}

.logo span {
  color: var(--text2);
}

.engine-select {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text2);
}

.engine-select select {
  background: var(--bg3);
  border: 1px solid var(--border2);
  color: var(--text);
  font-family: var(--mono);
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 4px;
  cursor: pointer;
  outline: none;
}

/* ===== MAIN LAYOUT ===== */
main {
  display: grid;
  grid-template-columns: 3fr 2fr;
  gap: 16px;
  flex: 1;
  padding: 16px 24px;
  min-height: 0;
  overflow: hidden;
}

/* ===== 左カラム ===== */
.col-left {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
}

.regex-section {
  flex-shrink: 0;
}

.test-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.test-section .test-panel {
  flex: 1;
  min-height: 0;
}

/* ===== 右カラム ===== */
.col-right {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
}

.details-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.details-section .details-panel {
  flex: 1;
  min-height: 0;
}

.explain-section {
  flex-shrink: 0;
  max-height: 200px;
  display: flex;
  flex-direction: column;
}

.explain-section .explain-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}


.section-label {
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text3);
  margin-bottom: 6px;
  font-family: var(--mono);
}

/* ===== F1: 正規表現入力 ===== */
.regex-panel {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.regex-panel.error {
  border-color: var(--error);
}

.regex-row {
  display: flex;
  align-items: center;
  padding: 0 16px;
  height: 52px;
  gap: 8px;
}

.regex-slash {
  font-family: var(--mono);
  font-size: 22px;
  color: var(--text3);
  user-select: none;
  line-height: 1;
}

#pattern-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  font-family: var(--mono);
  font-size: 16px;
  color: var(--text);
  caret-color: var(--accent);
  line-height: 1;
}

#pattern-input::placeholder {
  color: var(--text3);
}

.flags {
  display: flex;
  gap: 4px;
  align-items: center;
}

.flag-btn {
  font-family: var(--mono);
  font-size: 12px;
  padding: 3px 7px;
  border-radius: 4px;
  border: 1px solid var(--border2);
  background: transparent;
  color: var(--text2);
  cursor: pointer;
  transition: all 0.15s;
  line-height: 1.4;
}

.flag-btn.active {
  background: var(--accent2);
  border-color: var(--accent);
  color: #fff;
}

.flag-btn:hover:not(.active) {
  border-color: var(--text2);
  color: var(--text);
}

.error-msg {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--error);
  padding: 6px 16px;
  border-top: 1px solid rgba(247, 107, 107, 0.2);
  background: rgba(247, 107, 107, 0.06);
  display: none;
}

.error-msg.show {
  display: block;
}


/* ===== F2: テスト文字列 ===== */
.test-panel {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.test-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}

.match-count {
  font-family: var(--mono);
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 4px;
  background: var(--h1-bg);
  color: var(--h1);
}

.match-count.none {
  background: var(--bg3);
  color: var(--text3);
}

.editor-wrapper {
  display: flex;
  flex: 1;
  position: relative;
  min-height: 0;
}

.line-numbers {
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.6;
  color: var(--text3);
  padding: 12px 10px 12px 16px;
  text-align: right;
  user-select: none;
  min-width: 40px;
  max-width: 1em;
  border-right: 1px solid var(--border);
}

/* ===== F2+F3: contenteditable エディタ ===== */
/* textarea + オーバーレイ方式を廃止し、contenteditable div に
   テキストと <mark> を同一 DOM で描画することで全角文字のズレを解消する */

.test-editor {
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.6;
  color: var(--text);
  caret-color: var(--accent);
  padding: 12px 14px;
  flex: 1;
  outline: none;
  white-space: pre-wrap;
  word-break: break-all;
  overflow-y: auto;
}

/* プレースホルダー */
.test-editor:empty::before {
  content: attr(data-placeholder);
  color: var(--text3);
  pointer-events: none;
}

/* ハイライト <mark> の共通スタイル */
.test-editor mark {
  border-radius: 2px;
}

/* ===== ハイライトカラー ===== */
.hl-match {
  background: var(--h1-bg);
  color: var(--h1);
}

/* ===== F5: マッチ詳細パネル ===== */
.details-panel {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.details-panel-header {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}

.details-body {
  overflow-y: auto;
  flex: 1;
  padding: 8px 0;
}

.match-item {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}

.match-item:last-child {
  border-bottom: none;
}

.match-item:hover {
  background: var(--bg3);
}

.match-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.match-badge {
  font-family: var(--mono);
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 3px;
  font-weight: 500;
}

.match-string {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text2);
  word-break: break-all;
}

.match-index {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--text3);
}

.group-row {
  display: flex;
  align-items: baseline;
  gap: 6px;
  margin-top: 4px;
  padding-left: 4px;
}

.group-label {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--text3);
  min-width: 52px;
  flex-shrink: 0;
}

.group-value {
  font-family: var(--mono);
  font-size: 11px;
  word-break: break-all;
}

.no-match {
  padding: 24px 16px;
  text-align: center;
  color: var(--text3);
  font-size: 13px;
}

/* ===== バッジカラー ===== */
.badge-match {
  background: var(--h1-bg);
  color: var(--h1);
}

/* ===== F4: トークン解説 ===== */
.explain-panel {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.explain-panel-header {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.explain-body {
  padding: 12px 16px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: flex-start;
  overflow-y: auto;
}

.token-chip {
  display: inline-flex;
  flex-direction: column;
  gap: 2px;
  padding: 6px 10px;
  border-radius: 6px;
  border: 1px solid transparent;
}

.token-pattern {
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 500;
  line-height: 1;
}

.token-desc {
  font-size: 10px;
  line-height: 1.2;
  opacity: 0.8;
}

/* ===== チップカラー ===== */
.chip-0 {
  background: var(--h0-bg);
  border-color: rgba(247, 201, 72, 0.25);
  color: var(--h0);
}

.chip-1 {
  background: var(--h1-bg);
  border-color: rgba(79, 142, 247, 0.25);
  color: var(--h1);
}

.chip-2 {
  background: var(--h2-bg);
  border-color: rgba(80, 217, 160, 0.25);
  color: var(--h2);
}

.chip-3 {
  background: var(--h3-bg);
  border-color: rgba(247, 130, 79, 0.25);
  color: var(--h3);
}

.chip-4 {
  background: var(--h4-bg);
  border-color: rgba(201, 124, 247, 0.25);
  color: var(--h4);
}

.chip-5 {
  background: var(--h5-bg);
  border-color: rgba(247, 79, 158, 0.25);
  color: var(--h5);
}

.explain-empty {
  color: var(--text3);
  font-size: 13px;
  padding: 4px 0;
}

/* ===== レスポンシブ ===== */
@media (max-width: 768px) {
  main {
    grid-template-columns: 1fr;
    padding: 12px 16px;
    overflow-y: auto;
    height: auto;
  }

  body {
    height: auto;
    overflow: auto;
  }

  .details-section,
  .test-section {
    min-height: 240px;
  }

  .explain-section {
    max-height: none;
  }
}
/* リテラルチップ（色なし） */
.chip-literal {
  background: var(--bg3);
  border-color: var(--border2);
  color: var(--text2);
}