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

/* Light theme (default) */
:root {
  --bg: #fafafa;
  --surface: #fff;
  --border: #ddd;
  --text: #1a1a1a;
  --text-secondary: #555;
  --accent: #0066cc;
  --accent-hover: #0052a3;
  --error-bg: #fef2f2;
  --error-text: #b91c1c;
  --match-bg: #fff3cd;
  --match-border: #ffc107;
  --group-bg: #d1ecf1;
  --group-border: #17a2b8;
  --radius: 6px;
}

/* Dark theme values */
[data-theme="dark"] {
  --bg: #111;
  --surface: #1a1a1a;
  --border: #333;
  --text: #e5e5e5;
  --text-secondary: #999;
  --accent: #4da3ff;
  --accent-hover: #7dbdff;
  --error-bg: #2d1515;
  --error-text: #f87171;
  --match-bg: #3d3400;
  --match-border: #ffc107;
  --group-bg: #0d3640;
  --group-border: #17a2b8;
}

/* System preference when no explicit choice */
@media (prefers-color-scheme: dark) {
  [data-theme="system"] {
    --bg: #111;
    --surface: #1a1a1a;
    --border: #333;
    --text: #e5e5e5;
    --text-secondary: #999;
    --accent: #4da3ff;
    --accent-hover: #7dbdff;
    --error-bg: #2d1515;
    --error-text: #f87171;
    --match-bg: #3d3400;
    --match-border: #ffc107;
    --group-bg: #0d3640;
    --group-border: #17a2b8;
  }
}

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

main {
  max-width: 700px;
  margin: 0 auto;
  padding: 3rem 1rem;
}

/* Top bar */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

h1 {
  font-size: 1.8rem;
  font-weight: 700;
  margin-bottom: 0.25rem;
}

#theme-toggle {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 1.1rem;
  width: 2.2rem;
  height: 2.2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  line-height: 1;
}

#theme-toggle:hover {
  color: var(--text);
  border-color: var(--text-secondary);
}

.subtitle {
  color: var(--text-secondary);
  margin-bottom: 2rem;
  font-size: 0.95rem;
}

/* Explainer */
.explainer {
  margin-bottom: 1.5rem;
  font-size: 0.9rem;
  color: var(--text-secondary);
  line-height: 1.6;
}

.explainer p + p {
  margin-top: 0.5rem;
}

/* Regex input row */
.regex-input-row {
  display: flex;
  align-items: center;
  gap: 0;
  margin-bottom: 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 0 0.5rem;
}

.regex-input-row:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(0, 102, 204, 0.2);
}

.regex-delim {
  color: var(--text-secondary);
  font-family: monospace;
  font-size: 1.1rem;
  font-weight: 600;
  user-select: none;
  flex-shrink: 0;
}

#regex-input {
  flex: 1;
  padding: 0.6rem 0.4rem;
  font-size: 1rem;
  font-family: monospace;
  border: none;
  background: transparent;
  color: var(--text);
  outline: none;
  min-width: 0;
}

.flags-group {
  display: flex;
  gap: 2px;
  margin-left: 0.25rem;
  flex-shrink: 0;
}

.flag-btn {
  background: none;
  border: 1px solid transparent;
  border-radius: 3px;
  color: var(--text-secondary);
  cursor: pointer;
  font-family: monospace;
  font-size: 0.9rem;
  font-weight: 600;
  width: 1.6rem;
  height: 1.6rem;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  opacity: 0.4;
  transition: opacity 0.15s, background 0.15s;
}

.flag-btn:hover {
  opacity: 0.7;
}

.flag-btn.active {
  opacity: 1;
  color: var(--accent);
  background: rgba(0, 102, 204, 0.08);
  border-color: var(--accent);
}

/* Error */
#error {
  margin-bottom: 0.75rem;
  padding: 0.6rem 0.8rem;
  background: var(--error-bg);
  color: var(--error-text);
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-family: monospace;
}

/* Field labels */
.field-label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-bottom: 0.35rem;
  margin-top: 1rem;
}

/* Test string textarea */
#test-input {
  width: 100%;
  padding: 0.8rem;
  font-size: 0.95rem;
  font-family: monospace;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  outline: none;
  resize: vertical;
  line-height: 1.5;
  tab-size: 2;
}

#test-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(0, 102, 204, 0.2);
}

/* Highlight area */
.highlight-area {
  margin-top: 0.75rem;
  padding: 0.8rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: monospace;
  font-size: 0.95rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-all;
  overflow-x: auto;
}

.highlight-area mark {
  background: var(--match-bg);
  border-bottom: 2px solid var(--match-border);
  border-radius: 2px;
  padding: 0 1px;
}

/* Matches card */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.2rem;
  margin-top: 1rem;
}

.card h2 {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 0.75rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.match-item {
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
  font-family: monospace;
  font-size: 0.85rem;
}

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

.match-header {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.match-index {
  color: var(--text-secondary);
  font-size: 0.75rem;
  white-space: nowrap;
}

.match-value {
  font-weight: 600;
  background: var(--match-bg);
  border-radius: 2px;
  padding: 0 3px;
}

.match-groups {
  margin-top: 0.3rem;
  padding-left: 1rem;
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.match-groups span {
  display: inline-block;
  margin-right: 0.75rem;
}

.group-name {
  color: var(--accent);
}

.group-value {
  background: var(--group-bg);
  border-radius: 2px;
  padding: 0 2px;
}

.match-count {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-bottom: 0.5rem;
}

/* Mode toggle */
.mode-toggle-row {
  margin-top: 1rem;
}

.toggle-label {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
  color: var(--text-secondary);
  cursor: pointer;
}

.toggle-label input[type="checkbox"] {
  accent-color: var(--accent);
}

/* Replace section */
#replace-section {
  margin-top: 0.25rem;
}

#replace-input {
  width: 100%;
  padding: 0.6rem 0.8rem;
  font-size: 0.95rem;
  font-family: monospace;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  outline: none;
}

#replace-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(0, 102, 204, 0.2);
}

.replace-output {
  padding: 0.8rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: monospace;
  font-size: 0.95rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-all;
  overflow-x: auto;
  min-height: 2.5rem;
  margin-top: 0.35rem;
}

/* Reference panel */
.reference-panel {
  margin-top: 1.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.reference-panel summary {
  padding: 0.8rem 1rem;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-secondary);
  cursor: pointer;
  user-select: none;
}

.reference-panel summary:hover {
  color: var(--text);
}

.reference-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.3rem 1rem;
  padding: 0 1rem 1rem;
  font-size: 0.85rem;
}

.ref-pattern {
  font-family: monospace;
  font-weight: 600;
  color: var(--accent);
  white-space: nowrap;
  cursor: pointer;
}

.ref-pattern:hover {
  text-decoration: underline;
}

.ref-desc {
  color: var(--text-secondary);
}

/* Footer */
footer {
  max-width: 700px;
  margin: 2rem auto 0;
  padding: 1rem 1rem;
  text-align: center;
  color: var(--text-secondary);
  font-size: 0.8rem;
  border-top: 1px solid var(--border);
}

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

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

/* Utility */
.hidden {
  display: none !important;
}

@media (max-width: 500px) {
  .regex-input-row {
    flex-wrap: wrap;
    padding: 0.4rem 0.5rem;
  }

  #regex-input {
    width: 100%;
    order: 1;
  }

  .regex-delim:first-child {
    order: 0;
  }

  .regex-delim:nth-of-type(2) {
    display: none;
  }

  .flags-group {
    order: 2;
    margin-left: 0;
    margin-top: 0.4rem;
    width: 100%;
    justify-content: flex-start;
  }

  .reference-grid {
    grid-template-columns: 1fr;
  }

  .ref-desc {
    margin-bottom: 0.4rem;
  }
}
