:root {
  --bg: #0f1115;
  --fg: #e7e9ee;
  --muted: #aab1bd;
  --card: #171a21;
  --border: #2a2f39;
  --accent: #61d095;
  --accent-weak: #2f815c;
  --danger: #ff6b6b;
  --shadow: 0 6px 24px rgba(0,0,0,0.18);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  --sans: Inter, system-ui, -apple-system, Segoe UI, Roboto, "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic", Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--sans);
  background: var(--bg);
  color: var(--fg);
  line-height: 1.6;
}

.app-header {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  padding: 18px 20px;
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  background: color-mix(in oklab, var(--bg) 92%, transparent);
  z-index: 10;
}
.header-title { text-align: center; }
.app-header h1 { margin: 0; font-size: 22px; }
.app-header .subtitle { 
  margin: 4px 0 0 0; 
  font-size: 13px; 
  color: var(--muted); 
  font-weight: 400;
}
.header-actions { 
  display: flex; 
  gap: 10px; 
  align-items: center; 
  position: absolute;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
}
.toggle { display: inline-flex; gap: 6px; align-items: center; font-size: 14px; color: var(--muted); }
.toggle input { transform: translateY(1px); }

.tabs {
  display: flex; 
  flex-wrap: wrap; 
  gap: 8px;
  justify-content: center;
  padding: 12px 16px; 
  border-bottom: 1px solid var(--border);
}
.tab {
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--fg);
  padding: 8px 12px;
  border-radius: 10px;
  cursor: pointer;
}
.tab.active { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent-weak); }

main { padding: 16px; max-width: 1200px; margin: 0 auto; }
.panel { display: none; }
.panel.active { display: block; }

.grid.two {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
/* Mobile Responsive Design */
@media (max-width: 980px) {
  .grid.two { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  /* Header improvements */
  .app-header {
    padding: 12px 16px;
    flex-direction: column;
    gap: 12px;
  }
  
  .header-title h1 {
    font-size: 20px;
  }
  
  .header-title .subtitle {
    font-size: 12px;
    line-height: 1.4;
  }
  
  /* Tab navigation */
  .tabs {
    padding: 8px 12px;
    gap: 4px;
    justify-content: flex-start;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  
  .tab {
    padding: 8px 10px;
    font-size: 13px;
    white-space: nowrap;
    flex-shrink: 0;
  }
  
  /* Main content */
  main {
    padding: 12px;
  }
  
  .panel h2 {
    font-size: 18px;
    margin-bottom: 12px;
  }
  
  .panel h3 {
    font-size: 16px;
  }
}

@media (max-width: 480px) {
  /* Small mobile screens */
  .app-header {
    padding: 10px 12px;
  }
  
  .header-title h1 {
    font-size: 18px;
  }
  
  .header-title .subtitle {
    font-size: 11px;
  }
  
  .tabs {
    padding: 6px 8px;
    gap: 2px;
  }
  
  .tab {
    padding: 6px 8px;
    font-size: 12px;
  }
  
  main {
    padding: 8px;
  }
  
  /* Form elements */
  .row {
    flex-direction: column;
    gap: 8px;
  }
  
  .row label {
    width: 100%;
  }
  
  /* Field stats positioning for mobile */
  .field-stats {
    position: static !important;
    display: block;
    text-align: right;
    margin-top: -4px;
    margin-bottom: 4px;
    background: transparent !important;
    padding: 0;
  }
  
  .output-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
  
  .output-header .field-stats {
    align-self: flex-end;
  }
  
  /* Actions */
  .actions {
    flex-direction: column;
    gap: 8px;
  }
  
  .actions .btn {
    width: 100%;
  }
  
  /* Matrix table */
  .matrix {
    gap: 4px;
    grid-template-columns: repeat(auto-fill, minmax(60px, 1fr));
  }
  
  .matrix .cell {
    padding: 8px 4px;
    font-size: 12px;
  }
  
  .matrix .letter {
    font-size: 16px;
  }
  
  /* Toast positioning */
  .toast {
    bottom: 12px;
    right: 12px;
    left: 12px;
    width: auto;
    text-align: center;
  }
}

.field { display: grid; gap: 6px; margin-block: 8px 14px; }
.field span { font-size: 14px; color: var(--muted); }

/* Field with stats positioning */
.field-with-stats {
  position: relative;
}
.field-stats {
  position: absolute !important;
  top: 0;
  right: 0;
  background: var(--bg);
  color: var(--muted) !important;
  font-size: 12px !important;
  font-weight: 400 !important;
  padding: 2px 8px;
  border-radius: 4px;
  z-index: 2;
  margin: 0 !important;
}

/* Output header with stats */
.output-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.output-header h3 {
  margin: 0;
}
.output-header .field-stats {
  position: static !important;
  background: transparent !important;
}
textarea, input, select {
  width: 100%;
  background: var(--card);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-family: inherit;
}

/* Touch improvements */
@media (max-width: 768px) {
  textarea, input, select {
    padding: 12px 14px;
    font-size: 16px; /* Prevent zoom on iOS */
    border-radius: 8px;
  }
  
  .btn {
    padding: 12px 16px;
    min-height: 44px; /* iOS touch target size */
    font-size: 14px;
  }
  
  .tab {
    min-height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  
  /* Improve touch targets for matrix */
  .matrix .cell {
    min-height: 44px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    cursor: pointer;
  }
  
  /* Better select styling on mobile */
  select {
    appearance: none;
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='m6 8 4 4 4-4'/%3e%3c/svg%3e");
    background-position: right 12px center;
    background-repeat: no-repeat;
    background-size: 16px;
    padding-right: 40px;
  }
}
textarea[readonly] { opacity: 0.95; }
textarea { resize: vertical; }
.row { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; }
.actions { display: flex; flex-wrap: wrap; gap: 10px; margin-block: 10px 6px; }

.btn {
  background: var(--accent);
  color: #042016;
  border: none;
  padding: 8px 14px;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 600;
  box-shadow: var(--shadow);
}
.btn:hover { filter: brightness(1.05); }
.btn.ghost {
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--border);
  box-shadow: none;
}
.btn.tiny { padding: 6px 10px; font-size: 12px; }

.stats { color: var(--muted); font-size: 13px; margin: 6px 0 0; }
.hint.small { color: var(--muted); font-size: 12px; }

.sidecard {
  margin-top: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  background: var(--card);
}
.mono { font-family: var(--mono); }
.small { font-size: 12px; line-height: 1.4; white-space: pre-wrap; }

.preview {
  min-height: 180px;
  border: 1px dashed var(--border);
  border-radius: 12px;
  padding: 12px;
  background: color-mix(in oklab, var(--card) 92%, transparent);
  overflow-wrap: anywhere;
}

.matrix {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 10px;
  margin-top: 12px;
}
.matrix .cell {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  background: var(--card);
}
.matrix .bits { font-family: var(--mono); color: var(--muted); }
.matrix .letter { font-size: 22px; font-weight: 700; }

.sep { border: none; border-top: 1px solid var(--border); margin: 20px 0; }

footer {
  text-align: center;
  width: 100%;
}
.footer {
  text-align: center;
  padding: 18px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  margin: 0 auto;
}
.footer a { color: var(--fg); text-decoration: underline; }

.toast {
  position: fixed;
  bottom: 16px;
  right: 16px;
  background: var(--accent);
  color: var(--bg);
  border: 2px solid var(--accent);
  border-radius: 10px;
  padding: 12px 18px;
  box-shadow: 0 8px 32px rgba(97, 208, 149, 0.4);
  display: none;
  z-index: 1000;
  font-size: 14px;
  font-weight: 500;
}
.toast.show { 
  display: block;
  animation: slideIn 0.3s ease-out;
}
@keyframes slideIn {
  from { 
    transform: translateX(100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

/* Rich embed styles */
.bacon-bold { font-weight: 700; }
.bacon-italic { font-style: italic; }

/* Light mode */
@media (prefers-color-scheme: light) {
  :root {
    --bg: #fbfbfd;
    --fg: #0f141c;
    --muted: #5b6575;
    --card: #ffffff;
    --border: #e5e8ef;
    --accent: #0dbd6e;
    --accent-weak: #0c9d5d;
    --danger: #d94848;
  }
}
