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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Noto Sans JP", "Segoe UI", sans-serif;
  font-size: 16px;
  line-height: 1.6;
  color: #1f2937;
  background: #f3f4f6;
}

/* ============ HEADER ============ */
header {
  background: #1e293b;
  color: #f1f5f9;
  padding: 1.25rem 1rem 1.5rem;
  text-align: center;
  position: relative;
}

.back-link {
  display: inline-block;
  color: #94a3b8;
  font-size: 0.85rem;
  text-decoration: none;
  margin-bottom: 0.5rem;
  min-height: 44px;
  line-height: 44px;
}

.back-link:hover {
  color: #cbd5e1;
}

header h1 {
  font-size: 1.35rem;
  font-weight: 700;
  line-height: 1.35;
}

.subtitle {
  font-size: 0.875rem;
  color: #94a3b8;
  margin-top: 0.35rem;
}

/* ============ MAIN ============ */
main {
  max-width: 900px;
  margin: 0 auto;
  padding: 1rem;
}

/* ============ AD UNIT ============ */
.ad-unit {
  margin: 1.25rem 0;
}

/* ============ TOOL SECTION ============ */
.tool-section {
  background: #fff;
  border-radius: 10px;
  padding: 1.25rem;
  box-shadow: 0 1px 4px rgba(0,0,0,0.07);
  margin-bottom: 1rem;
}

.tool-section h2 {
  font-size: 1rem;
  font-weight: 700;
  color: #1e293b;
  margin-bottom: 0.75rem;
}

/* ============ REGEX INPUT ============ */
.input-row {
  display: flex;
  gap: 0.5rem;
  align-items: stretch;
  flex-wrap: wrap;
}

.regex-field-wrap {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  border: 2px solid #e2e8f0;
  border-radius: 8px;
  background: #f8fafc;
  overflow: hidden;
  transition: border-color 0.2s;
}

.regex-field-wrap:focus-within {
  border-color: #6366f1;
  background: #fff;
}

.regex-slash {
  padding: 0 0.5rem;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 1.2rem;
  color: #94a3b8;
  user-select: none;
  flex-shrink: 0;
}

#regex-input {
  flex: 1;
  min-width: 0;
  border: none;
  background: transparent;
  padding: 0.65rem 0.25rem;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.95rem;
  color: #1e293b;
  outline: none;
}

#regex-input::placeholder {
  color: #94a3b8;
  font-size: 0.85rem;
}

.flags-wrap {
  display: flex;
  align-items: center;
  gap: 0.15rem;
  padding: 0 0.5rem;
  flex-shrink: 0;
}

.flag-label {
  display: flex;
  align-items: center;
  cursor: pointer;
  user-select: none;
}

.flag-label input[type="checkbox"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.flag-label span {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 4px;
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 0.85rem;
  font-weight: 700;
  color: #64748b;
  background: transparent;
  transition: background 0.15s, color 0.15s;
  min-height: 44px;
  min-width: 44px;
}

.flag-label input:checked + span {
  background: #6366f1;
  color: #fff;
}

.flag-label span:hover {
  background: #e0e7ff;
  color: #4f46e5;
}

.clear-btn {
  padding: 0 1rem;
  min-height: 44px;
  background: #f1f5f9;
  color: #475569;
  border: 2px solid #e2e8f0;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s;
  flex-shrink: 0;
}

.clear-btn:hover {
  background: #e2e8f0;
  border-color: #cbd5e1;
}

/* ============ ERROR ============ */
.error-msg {
  margin-top: 0.75rem;
  padding: 0.6rem 0.9rem;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 6px;
  color: #dc2626;
  font-size: 0.875rem;
}

/* ============ DIAGRAM ============ */
.diagram-container {
  margin-top: 1rem;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  background: #fafbfc;
  min-height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.diagram-placeholder {
  color: #94a3b8;
  font-size: 0.9rem;
  padding: 2rem;
  text-align: center;
}

.diagram-scroll {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding: 1rem 0.5rem;
}

#regex-svg {
  display: block;
  min-width: 100%;
  overflow: visible;
}

/* SVG node styles */
.svg-node rect {
  stroke-width: 2;
}

.svg-node text {
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 13px;
  dominant-baseline: central;
  text-anchor: middle;
  fill: #fff;
  pointer-events: none;
}

.svg-node .quantifier-text {
  font-size: 10px;
  fill: #f97316;
  font-weight: 700;
  font-family: -apple-system, BlinkMacSystemFont, sans-serif;
}

.svg-node .group-label {
  font-size: 10px;
  fill: #8b5cf6;
  font-family: -apple-system, BlinkMacSystemFont, sans-serif;
}

.svg-connector line,
.svg-connector path {
  stroke: #94a3b8;
  stroke-width: 1.5;
  fill: none;
}

.svg-connector polygon {
  fill: #94a3b8;
}

.start-end-node text {
  fill: #f1f5f9;
  font-family: -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 12px;
  font-weight: 700;
  dominant-baseline: central;
  text-anchor: middle;
}

/* ============ TEST INPUT ============ */
.test-section label {
  display: block;
  font-size: 0.875rem;
  font-weight: 600;
  color: #475569;
  margin-bottom: 0.4rem;
}

#test-input {
  width: 100%;
  padding: 0.65rem 0.75rem;
  border: 2px solid #e2e8f0;
  border-radius: 8px;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.95rem;
  color: #1e293b;
  background: #f8fafc;
  transition: border-color 0.2s;
  min-height: 44px;
}

#test-input:focus {
  outline: none;
  border-color: #6366f1;
  background: #fff;
}

.match-result {
  margin-top: 0.75rem;
}

.match-header {
  font-size: 0.85rem;
  color: #475569;
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.match-highlight {
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.9rem;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  padding: 0.75rem;
  word-break: break-all;
  line-height: 1.8;
}

.match-highlight mark {
  background: #fde68a;
  color: #92400e;
  border-radius: 2px;
  padding: 0 2px;
}

.match-highlight mark.match-first {
  background: #f97316;
  color: #fff;
}

.match-list {
  margin-top: 0.5rem;
  padding-left: 1.25rem;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.85rem;
  color: #374151;
}

.match-list li {
  margin-bottom: 0.2rem;
}

.no-match {
  margin-top: 0.75rem;
  padding: 0.6rem 0.9rem;
  background: #fef9c3;
  border: 1px solid #fde047;
  border-radius: 6px;
  color: #854d0e;
  font-size: 0.875rem;
}

/* ============ COMPONENT TABLE ============ */
.component-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
  margin-top: 0.25rem;
}

.component-table th {
  background: #f1f5f9;
  color: #475569;
  font-weight: 700;
  padding: 0.5rem 0.75rem;
  text-align: left;
  border-bottom: 2px solid #e2e8f0;
}

.component-table td {
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid #f1f5f9;
  vertical-align: middle;
}

.component-table tr:last-child td {
  border-bottom: none;
}

.comp-badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 0.8rem;
  font-weight: 700;
  color: #fff;
  white-space: nowrap;
}

/* ============ SAMPLES ============ */
.samples-desc {
  font-size: 0.875rem;
  color: #64748b;
  margin-bottom: 0.75rem;
}

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

.sample-btn {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.2rem;
  padding: 0.75rem 1rem;
  min-height: 44px;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  cursor: pointer;
  text-align: left;
  width: 100%;
  transition: background 0.15s, border-color 0.15s;
}

.sample-btn:hover {
  background: #eff6ff;
  border-color: #bfdbfe;
}

.sample-label {
  font-size: 0.8rem;
  font-weight: 700;
  color: #6366f1;
}

.sample-btn code {
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.8rem;
  color: #475569;
  word-break: break-all;
}

/* ============ HOWTO ============ */
.howto-section {
  background: #fff;
  border-radius: 10px;
  padding: 1.25rem;
  box-shadow: 0 1px 4px rgba(0,0,0,0.07);
  margin-bottom: 1rem;
}

.howto-section h2 {
  font-size: 1rem;
  font-weight: 700;
  color: #1e293b;
  margin-top: 1.25rem;
  margin-bottom: 0.6rem;
}

.howto-section h2:first-child {
  margin-top: 0;
}

.howto-section ol,
.howto-section p {
  font-size: 0.9rem;
  color: #4b5563;
  margin-bottom: 0.5rem;
}

.howto-section ol {
  padding-left: 1.25rem;
}

.howto-section li {
  margin-bottom: 0.4rem;
}

.howto-section code {
  font-family: "SFMono-Regular", Consolas, monospace;
  background: #f1f5f9;
  padding: 0.1em 0.35em;
  border-radius: 3px;
  font-size: 0.85em;
  color: #6366f1;
}

/* ============ LEGEND ============ */
.legend-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.4rem 1rem;
  margin-top: 0.5rem;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.875rem;
  color: #374151;
}

.legend-dot {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 3px;
  flex-shrink: 0;
}

/* ============ SR-ONLY ============ */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

/* ============ FOOTER ============ */
footer {
  text-align: center;
  padding: 2rem 1rem;
  font-size: 0.85rem;
  color: #9ca3af;
}

footer a {
  color: #6b7280;
  text-decoration: none;
}

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

/* ============ RESPONSIVE ============ */
@media (min-width: 640px) {
  header h1 {
    font-size: 1.75rem;
  }

  main {
    padding: 1.5rem 2rem;
  }

  .tool-section,
  .howto-section {
    padding: 1.5rem;
  }

  .legend-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  .sample-list {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
}

@media (min-width: 900px) {
  .sample-list {
    grid-template-columns: 1fr 1fr 1fr;
  }
}
