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

:root {
  --primary: #4F46E5;
  --primary-hover: #4338CA;
  --primary-light: #EEF2FF;
  --bg: #F3F4F6;
  --surface: #FFFFFF;
  --text: #111827;
  --text-secondary: #6B7280;
  --text-tertiary: #9CA3AF;
  --border: #E5E7EB;
  --border-focus: #A5B4FC;
  --error: #EF4444;
  --error-light: #FEF2F2;
  --warning: #F59E0B;
  --warning-light: #FFFBEB;
  --success: #10B981;
  --radius: 8px;
  --radius-lg: 12px;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.1), 0 1px 2px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

/* === Header === */
.header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 1.25rem 1.5rem;
}

.header-content {
  max-width: 1400px;
  margin: 0 auto;
}

.header h1 {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 0.15rem;
}

.header p {
  font-size: 0.85rem;
  color: var(--text-secondary);
}

/* === Main Layout === */
.main {
  max-width: 1400px;
  margin: 0 auto;
  padding: 1.5rem;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.5rem;
  align-items: start;
}

/* === Input Panel === */
.input-panel {
  background: var(--surface);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  padding: 1.5rem;
  box-shadow: var(--shadow-sm);
}

/* === Settings Bar === */
.settings-bar {
  display: flex;
  gap: 1rem;
  margin-bottom: 1.5rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid var(--border);
}

.setting-group {
  flex: 1;
}

.setting-group label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 0.35rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.select-wrapper {
  position: relative;
}

.select-wrapper select {
  width: 100%;
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
  font-family: var(--font);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%236B7280' stroke-width='2.5'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  padding-right: 2rem;
}

.select-wrapper select:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

#custom-model {
  margin-top: 0.5rem;
}

.api-key-wrapper {
  display: flex;
  gap: 0.35rem;
}

.api-key-wrapper input {
  flex: 1;
  min-width: 0;
}

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text-secondary);
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s;
}

.icon-btn:hover {
  background: var(--bg);
  color: var(--text);
}

.hint {
  display: block;
  font-size: 0.75rem;
  color: var(--text-tertiary);
  margin-top: 0.3rem;
}

/* === Form Elements === */
.form-group {
  margin-bottom: 1.25rem;
}

.form-group label,
.field-group label {
  display: block;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 0.35rem;
}

.required {
  color: var(--error);
}

.optional {
  font-weight: 400;
  color: var(--text-tertiary);
  font-size: 0.8rem;
}

input[type="text"],
input[type="url"],
input[type="password"],
textarea {
  width: 100%;
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
  font-family: var(--font);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s;
}

input::placeholder,
textarea::placeholder {
  color: var(--text-tertiary);
}

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

input.invalid,
textarea.invalid {
  border-color: var(--error);
}

input.invalid:focus,
textarea.invalid:focus {
  box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.1);
}

textarea {
  resize: vertical;
  min-height: 80px;
}

.textarea-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 0.3rem;
}

.char-counter {
  font-size: 0.75rem;
  color: var(--text-tertiary);
  flex-shrink: 0;
}

.char-counter.over {
  color: var(--warning);
}

.field-error {
  display: block;
  font-size: 0.75rem;
  color: var(--error);
  min-height: 1.1rem;
  margin-top: 0.2rem;
}

/* === Anchor Pairs === */
.form-section {
  margin-bottom: 1.25rem;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
}

.section-header h2 {
  font-size: 0.875rem;
  font-weight: 600;
}

.pair-count {
  font-size: 0.8rem;
  color: var(--text-secondary);
  background: var(--bg);
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
}

.anchor-pair {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 0.75rem;
  background: var(--bg);
  transition: border-color 0.15s;
}

.anchor-pair:hover {
  border-color: #D1D5DB;
}

.pair-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
}

.pair-label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--primary);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.remove-pair {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-tertiary);
  cursor: pointer;
  font-size: 1.25rem;
  line-height: 1;
  transition: background 0.15s, color 0.15s;
}

.remove-pair:hover {
  background: var(--error-light);
  color: var(--error);
}

.pair-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}

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

.field-group label {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-secondary);
}

/* === Buttons === */
.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.7rem 1.25rem;
  font-size: 0.9rem;
  font-weight: 600;
  font-family: var(--font);
  color: #fff;
  background: var(--primary);
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s, transform 0.1s;
}

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

.btn-primary:active:not(:disabled) {
  transform: scale(0.99);
}

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

.btn-secondary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.5rem 1rem;
  font-size: 0.8rem;
  font-weight: 600;
  font-family: var(--font);
  color: var(--primary);
  background: var(--primary-light);
  border: 1px solid transparent;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.btn-secondary:hover:not(:disabled) {
  background: #E0E7FF;
  border-color: var(--primary);
}

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

#add-anchor:disabled {
  position: relative;
}

/* === Output Panel === */
.output-panel {
  position: sticky;
  top: 1.5rem;
}

#output-content {
  background: var(--surface);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  padding: 1.5rem;
  min-height: 400px;
  box-shadow: var(--shadow-sm);
}

/* Empty State */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  min-height: 350px;
  padding: 2rem;
}

.empty-state p {
  margin-top: 1rem;
  font-size: 0.95rem;
  color: var(--text-secondary);
}

.empty-state .hint {
  margin-top: 0.5rem;
  max-width: 360px;
}

/* Loading State */
.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 350px;
  gap: 1.25rem;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

.loading-message {
  font-size: 0.9rem;
  color: var(--text-secondary);
  animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* Error State */
.error-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 350px;
  text-align: center;
  gap: 0.75rem;
  padding: 2rem;
}

.error-title {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text);
}

.error-message {
  font-size: 0.875rem;
  color: var(--text-secondary);
  max-width: 400px;
}

/* Results */
.domain-analysis {
  background: var(--primary-light);
  border: 1px solid #C7D2FE;
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  margin-bottom: 1rem;
  font-size: 0.85rem;
  color: #3730A3;
  line-height: 1.5;
}

.domain-analysis strong {
  font-weight: 600;
}

.warnings {
  margin-bottom: 1rem;
}

.warning-item {
  background: var(--warning-light);
  border: 1px solid #FDE68A;
  border-radius: var(--radius);
  padding: 0.6rem 0.85rem;
  margin-bottom: 0.5rem;
  font-size: 0.8rem;
  color: #92400E;
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
}

.warning-icon {
  flex-shrink: 0;
  margin-top: 0.1rem;
}

/* Topic Cards */
.topic-card {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.25rem;
  margin-bottom: 1rem;
  background: var(--surface);
  transition: box-shadow 0.15s;
}

.topic-card:hover {
  box-shadow: var(--shadow-md);
}

.topic-card.denial {
  border-color: #FDE68A;
  background: var(--warning-light);
}

.topic-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.topic-number {
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--primary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  background: var(--primary-light);
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  flex-shrink: 0;
}

.topic-card.denial .topic-number {
  background: #FEF3C7;
  color: #92400E;
}

.copy-btn {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.3rem 0.6rem;
  font-size: 0.75rem;
  font-weight: 500;
  font-family: var(--font);
  color: var(--text-secondary);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  flex-shrink: 0;
}

.copy-btn:hover {
  background: var(--border);
  color: var(--text);
}

.copy-btn.copied {
  color: var(--success);
  border-color: var(--success);
}

.topic-title {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text);
  line-height: 1.4;
  margin-bottom: 0.5rem;
}

.topic-summary {
  font-size: 0.875rem;
  color: var(--text-secondary);
  line-height: 1.6;
  margin-bottom: 1rem;
}

/* Anchor Placement */
.placement {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem;
  margin-bottom: 0.75rem;
  background: var(--bg);
}

.placement-anchor {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--primary);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-bottom: 0.5rem;
}

.placement-section {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-bottom: 0.4rem;
}

.placement-section strong {
  font-weight: 600;
  color: var(--text);
}

.placement-sample {
  background: var(--surface);
  border-left: 3px solid var(--primary);
  padding: 0.6rem 0.85rem;
  margin-top: 0.5rem;
  font-size: 0.85rem;
  color: var(--text);
  line-height: 1.6;
  font-style: italic;
  border-radius: 0 var(--radius) var(--radius) 0;
}

.placement-sample .anchor-highlight {
  color: var(--primary);
  font-weight: 600;
  text-decoration: underline;
  text-decoration-color: rgba(79, 70, 229, 0.3);
  text-underline-offset: 2px;
}

/* Naturalness Rationale */
.rationale {
  font-size: 0.8rem;
  color: var(--text-secondary);
  line-height: 1.5;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
  margin-top: 0.75rem;
}

.rationale strong {
  color: var(--text);
  font-weight: 600;
}

/* Denial card */
.denial-reason {
  font-size: 0.875rem;
  color: #92400E;
  line-height: 1.6;
  margin-bottom: 0.75rem;
}

.denial-alternative {
  font-size: 0.85rem;
  color: #78716C;
  line-height: 1.5;
  padding: 0.6rem 0.85rem;
  background: #FEFCE8;
  border-radius: var(--radius);
}

/* Regenerate Button */
.btn-regenerate {
  margin-top: 0.5rem;
}

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

/* === Tooltip === */
[data-tooltip] {
  position: relative;
}

[data-tooltip]::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  padding: 0.35rem 0.6rem;
  font-size: 0.7rem;
  color: #fff;
  background: var(--text);
  border-radius: var(--radius);
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
}

[data-tooltip]:hover::after {
  opacity: 1;
}

/* === Responsive === */
@media (max-width: 900px) {
  .main {
    grid-template-columns: 1fr;
  }

  .output-panel {
    position: static;
  }

  .settings-bar {
    flex-direction: column;
  }

  .pair-fields {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 480px) {
  .header {
    padding: 1rem;
  }

  .main {
    padding: 1rem;
  }

  .input-panel,
  #output-content {
    padding: 1rem;
  }

  .topic-card {
    padding: 1rem;
  }
}
