/* === Reset & Base === */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --success: #16a34a;
  --warning: #f59e0b;
  --bg: #f8fafc;
  --surface: #ffffff;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 4px 12px rgba(0, 0, 0, 0.1);
}

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

/* === Header === */
header {
  background: var(--primary);
  color: white;
  padding: 1rem 2rem;
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 900px;
  margin: 0 auto;
}

header h1 {
  font-size: 1.5rem;
  font-weight: 700;
}

.subtitle {
  font-size: 0.875rem;
  opacity: 0.85;
  margin-top: 0.15rem;
}

.btn-header {
  background: rgba(255, 255, 255, 0.15);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
  padding: 0.5rem 1rem;
  border-radius: var(--radius);
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
  white-space: nowrap;
}

.btn-header:hover {
  background: rgba(255, 255, 255, 0.25);
}

/* === Layout === */
main {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  max-width: 900px;
  margin: 1.5rem auto;
  padding: 0 1.5rem;
}

/* === Modal === */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 2rem;
  z-index: 100;
  overflow-y: auto;
}

.modal-overlay.hidden {
  display: none;
}

.modal {
  background: var(--surface);
  border-radius: 12px;
  width: 100%;
  max-width: 560px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
  animation: modal-in 0.2s ease;
}

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

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

.modal-header h2 {
  font-size: 1.15rem;
  color: var(--text);
}

.btn-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.25rem;
  line-height: 1;
}

.btn-close:hover {
  color: var(--text);
}

.modal-body {
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

/* === Config Sections (inside modal) === */
.config-section {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
}

.config-section h3 {
  font-size: 0.95rem;
  margin-bottom: 0.75rem;
  color: var(--primary);
}

.config-section label {
  display: block;
  font-size: 0.8125rem;
  font-weight: 600;
  margin-bottom: 0.25rem;
  margin-top: 0.75rem;
}

.config-section label:first-of-type {
  margin-top: 0;
}

.config-section small {
  display: block;
  color: var(--text-muted);
  font-size: 0.75rem;
  margin-top: 0.25rem;
}

/* === Form Controls === */
input[type="text"],
input[type="password"],
input[type="number"],
input[type="email"],
input[type="tel"],
input[type="date"],
select,
textarea {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.875rem;
  font-family: inherit;
  background: var(--surface);
  color: var(--text);
  transition: border-color 0.2s;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.15);
}

textarea {
  resize: vertical;
}

/* === Buttons === */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  border: none;
  border-radius: var(--radius);
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.2s, opacity 0.2s;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

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

.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

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

.btn-secondary:hover:not(:disabled) {
  opacity: 0.9;
}

.btn-small {
  padding: 0.35rem 0.75rem;
  font-size: 0.8rem;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
}

.btn-small:hover:not(:disabled) {
  background: var(--border);
}

/* === Record/Stop Buttons === */
.btn-record {
  background: var(--danger);
  color: white;
  font-size: 1rem;
  padding: 0.75rem 1.5rem;
}

.btn-record:hover:not(:disabled) {
  background: var(--danger-hover);
}

.btn-record.recording {
  animation: pulse-record 1.5s infinite;
}

.record-icon {
  display: inline-block;
  width: 12px;
  height: 12px;
  background: white;
  border-radius: 50%;
}

.btn-record.recording .record-icon {
  background: white;
  animation: blink 1s infinite;
}

@keyframes pulse-record {
  0%, 100% { box-shadow: 0 0 0 0 rgba(220, 38, 38, 0.5); }
  50% { box-shadow: 0 0 0 12px rgba(220, 38, 38, 0); }
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

.btn-stop {
  background: var(--text-muted);
  color: white;
  font-size: 1rem;
  padding: 0.75rem 1.5rem;
}

.btn-stop:hover:not(:disabled) {
  background: var(--text);
}

/* === Status Badge === */
.status-badge {
  display: inline-block;
  padding: 0.25rem 0.75rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--border);
  color: var(--text-muted);
}

.status-badge.active {
  background: #fee2e2;
  color: var(--danger);
}

/* === Cards === */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
}

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

/* === Recording Section === */
.recording-controls {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

#transcript-area label {
  display: block;
  font-size: 0.8125rem;
  font-weight: 600;
  margin-bottom: 0.35rem;
}

.interim {
  font-size: 0.8125rem;
  color: var(--text-muted);
  font-style: italic;
  min-height: 1.25rem;
  margin-top: 0.35rem;
}

/* === Field Builder === */
.field-add-form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

#fields-list {
  list-style: none;
  margin-top: 0.75rem;
}

#fields-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.5rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.8125rem;
}

#fields-list li:last-child {
  border-bottom: none;
}

.field-info {
  display: flex;
  flex-direction: column;
}

.field-info .field-label {
  font-weight: 600;
}

.field-info .field-meta {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.btn-remove-field {
  background: none;
  border: none;
  color: var(--danger);
  cursor: pointer;
  font-size: 1.1rem;
  padding: 0.25rem;
  line-height: 1;
}

.btn-remove-field:hover {
  opacity: 0.7;
}

.field-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.75rem;
  flex-wrap: wrap;
}

/* === Form Fields Container === */
#form-fields-container {
  display: grid;
  gap: 1rem;
}

.form-field-group {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.form-field-group label {
  font-size: 0.8125rem;
  font-weight: 600;
}

.form-field-group small {
  color: var(--text-muted);
  font-size: 0.75rem;
}

.placeholder-text {
  color: var(--text-muted);
  font-style: italic;
  font-size: 0.875rem;
  text-align: center;
  padding: 2rem 0;
}

/* === Extract Controls === */
.extract-controls {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-top: 1.25rem;
}

#extract-status {
  font-size: 0.8125rem;
  color: var(--text-muted);
}

#extract-status.error {
  color: var(--danger);
}

#extract-status.success {
  color: var(--success);
}

/* === Export Section === */
.export-controls {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.json-preview {
  background: #1e293b;
  color: #e2e8f0;
  padding: 1rem;
  border-radius: var(--radius);
  font-size: 0.8rem;
  overflow-x: auto;
  max-height: 200px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

/* === Footer === */
footer {
  text-align: center;
  padding: 1.5rem;
  color: var(--text-muted);
  font-size: 0.8rem;
}

/* === Responsive === */
@media (max-width: 600px) {
  .header-inner {
    flex-direction: column;
    text-align: center;
    gap: 0.75rem;
  }

  main {
    padding: 0 0.75rem;
  }

  .card {
    padding: 1rem;
  }

  .recording-controls {
    flex-direction: column;
    align-items: stretch;
  }

  .modal {
    margin: 0.5rem;
  }
}

/* === TTS Controls === */
.tts-controls {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-top: 0.75rem;
  flex-wrap: wrap;
}

.btn-tts {
  background: var(--success);
  color: white;
  padding: 0.5rem 1rem;
}

.btn-tts:hover:not(:disabled) {
  background: #15803d;
}

.tts-audio-player {
  width: 100%;
  margin-top: 0.5rem;
  border-radius: var(--radius);
}

#tts-status {
  font-size: 0.8125rem;
  color: var(--text-muted);
}

#tts-status.error {
  color: var(--danger);
}

#tts-status.success {
  color: var(--success);
}

/* === Loading spinner === */
.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: middle;
  margin-right: 0.5rem;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Highlight newly filled fields */
.field-filled {
  animation: highlight 1.5s ease;
}

@keyframes highlight {
  0% { background-color: #dbeafe; }
  100% { background-color: transparent; }
}
