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

:root {
  --primary: #4a6fa5;
  --primary-light: #5b82b8;
  --primary-dark: #3a5a8a;
  --gray-50: #f8f9fb;
  --gray-100: #eef1f5;
  --gray-200: #dce2ea;
  --gray-300: #c5ced9;
  --gray-400: #8e99a8;
  --gray-600: #5a6577;
  --gray-800: #2d3748;
  --white: #ffffff;
  --radius: 10px;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.1);
  --transition-fast: 0.15s ease;
  --transition-normal: 0.2s ease;
  --ring-color: rgba(74, 111, 165, 0.35);
  --ring-width: 3px;
}

html {
  font-size: 16px;
  scroll-behavior: smooth;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, "Noto Sans SC", sans-serif;
  background: var(--gray-50);
  color: var(--gray-800);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ===== Skip Link ===== */
.skip-link {
  position: absolute;
  top: -100%;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  padding: 0.5rem 1rem;
  background: var(--primary-dark);
  color: var(--white);
  border-radius: 0 0 6px 6px;
  font-size: 0.85rem;
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
}

.skip-link:focus {
  top: 0;
}

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

/* ===== Header ===== */
.header {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: var(--white);
  padding: 1.25rem 1.5rem;
  padding-left: calc(1.5rem + env(safe-area-inset-left, 0px));
  padding-right: calc(1.5rem + env(safe-area-inset-right, 0px));
  box-shadow: var(--shadow-md);
  position: sticky;
  top: 0;
  z-index: 100;
}

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

.logo {
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.3px;
  text-wrap: balance;
}

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

/* ===== Container / Layout ===== */
.container {
  flex: 1;
  max-width: 1400px;
  margin: 1.5rem auto;
  padding: 0 1.5rem;
  padding-left: calc(1.5rem + env(safe-area-inset-left, 0px));
  padding-right: calc(1.5rem + env(safe-area-inset-right, 0px));
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.5rem;
  align-items: start;
}

/* ===== Panels ===== */
.input-panel,
.output-panel {
  background: var(--white);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--gray-200);
  overflow: hidden;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--gray-100);
  background: var(--gray-50);
}

.panel-header h2 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--gray-800);
  text-wrap: balance;
}

/* ===== Input Panel ===== */
.work-input {
  width: 100%;
  min-height: 220px;
  padding: 1rem 1.25rem;
  border: none;
  outline: none;
  resize: vertical;
  font-size: 0.9rem;
  line-height: 1.7;
  color: var(--gray-800);
  font-family: inherit;
}

.work-input::placeholder {
  color: var(--gray-400);
}

.work-input:focus {
  background: var(--gray-50);
}

.work-input:focus-visible {
  box-shadow: inset 0 0 0 var(--ring-width) var(--ring-color);
}

/* ===== Options ===== */
.options-group {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--gray-100);
}

.option-item {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.option-label {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--gray-600);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  cursor: pointer;
}

.option-select {
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--gray-200);
  border-radius: 6px;
  font-size: 0.88rem;
  color: var(--gray-800);
  background: var(--white);
  cursor: pointer;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
  appearance: auto;
}

.option-select:focus-visible {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 var(--ring-width) var(--ring-color);
}

/* ===== API Key ===== */
.api-key-group {
  padding: 0.75rem 1.25rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.api-key-input-wrap {
  display: flex;
  gap: 0.5rem;
}

.api-key-input {
  flex: 1;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--gray-200);
  border-radius: 6px;
  font-size: 0.85rem;
  color: var(--gray-800);
  font-family: "Consolas", "Monaco", monospace;
  background: var(--white);
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.api-key-input:focus-visible {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 var(--ring-width) var(--ring-color);
}

.toggle-key-btn {
  padding: 0.45rem 0.65rem;
  border: 1px solid var(--gray-200);
  border-radius: 6px;
  background: var(--gray-50);
  cursor: pointer;
  font-size: 0.9rem;
  transition: background var(--transition-fast), border-color var(--transition-fast);
  flex-shrink: 0;
}

.toggle-key-btn:hover {
  background: var(--gray-200);
}

.toggle-key-btn:focus-visible {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 var(--ring-width) var(--ring-color);
}

/* ===== Generate Button ===== */
.generate-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: calc(100% - 2.5rem);
  margin: 0 1.25rem 1.25rem;
  padding: 0.75rem 1.5rem;
  background: linear-gradient(135deg, var(--primary), var(--primary-light));
  color: var(--white);
  border: none;
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity var(--transition-fast), transform var(--transition-fast), box-shadow var(--transition-fast);
  letter-spacing: 0.3px;
  position: relative;
  overflow: hidden;
}

.generate-btn:hover:not(:disabled) {
  opacity: 0.92;
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

.generate-btn:active:not(:disabled) {
  transform: translateY(0);
}

.generate-btn:focus-visible {
  outline: 2px solid var(--white);
  outline-offset: 2px;
}

.generate-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none;
}

.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top-color: var(--white);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  margin-right: 0.5rem;
  vertical-align: middle;
}

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

/* ===== Usage Badge ===== */
.usage-badge {
  font-size: 0.75rem;
  padding: 0.25rem 0.6rem;
  border-radius: 20px;
  background: rgba(74, 111, 165, 0.1);
  color: var(--primary);
  font-weight: 600;
  white-space: nowrap;
}

.usage-badge.warning {
  background: rgba(234, 130, 58, 0.12);
  color: #c0792e;
}

.usage-badge.exhausted {
  background: rgba(211, 67, 67, 0.1);
  color: #c0392b;
}

/* ===== Output Panel ===== */
.copy-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.4rem 0.75rem;
  border: 1px solid var(--gray-200);
  border-radius: 6px;
  background: var(--white);
  font-size: 0.8rem;
  color: var(--gray-600);
  cursor: pointer;
  transition: background var(--transition-fast), color var(--transition-fast), border-color var(--transition-fast);
  white-space: nowrap;
}

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

.copy-btn:focus-visible {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 var(--ring-width) var(--ring-color);
}

.copy-btn.copied {
  background: #27ae60;
  color: var(--white);
  border-color: #27ae60;
}

.output-content {
  padding: 1.25rem;
  min-height: 350px;
  max-height: 70vh;
  overflow-y: auto;
  line-height: 1.8;
  font-size: 0.9rem;
}

/* ===== Placeholder ===== */
.placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 280px;
  color: var(--gray-400);
  text-align: center;
}

.placeholder-icon {
  font-size: 3rem;
  margin-bottom: 1rem;
  opacity: 0.6;
}

.placeholder p {
  font-size: 0.95rem;
}

.placeholder-hint {
  font-size: 0.82rem !important;
  margin-top: 0.5rem;
  opacity: 0.7;
}

/* ===== Markdown Rendered Content ===== */
.output-content h1 {
  font-size: 1.4rem;
  margin: 1rem 0 0.6rem;
  color: var(--primary-dark);
  border-bottom: 2px solid var(--gray-100);
  padding-bottom: 0.4rem;
  text-wrap: balance;
}

.output-content h2 {
  font-size: 1.15rem;
  margin: 1rem 0 0.5rem;
  color: var(--gray-800);
  text-wrap: balance;
}

.output-content h3 {
  font-size: 1rem;
  margin: 0.8rem 0 0.4rem;
  color: var(--gray-600);
  text-wrap: balance;
}

.output-content ul,
.output-content ol {
  padding-left: 1.5rem;
  margin: 0.5rem 0;
}

.output-content li {
  margin: 0.3rem 0;
}

.output-content p {
  margin: 0.5rem 0;
}

.output-content strong {
  color: var(--gray-800);
}

.output-content code {
  background: var(--gray-100);
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
  font-size: 0.85em;
}

.output-content pre {
  background: var(--gray-800);
  color: var(--gray-100);
  padding: 1rem;
  border-radius: 8px;
  overflow-x: auto;
  margin: 0.75rem 0;
}

.output-content pre code {
  background: none;
  padding: 0;
}

.output-content blockquote {
  border-left: 3px solid var(--primary);
  padding: 0.5rem 1rem;
  margin: 0.75rem 0;
  background: var(--gray-50);
  border-radius: 0 6px 6px 0;
}

.output-content table {
  width: 100%;
  border-collapse: collapse;
  margin: 0.75rem 0;
}

.output-content th,
.output-content td {
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--gray-200);
  text-align: left;
}

.output-content th {
  background: var(--gray-100);
  font-weight: 600;
}

.output-content hr {
  border: none;
  border-top: 1px solid var(--gray-200);
  margin: 1rem 0;
}

/* ===== Toast ===== */
.toast-container {
  position: fixed;
  top: 1.5rem;
  right: 1.5rem;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  pointer-events: none;
  max-width: 360px;
}

.toast {
  padding: 0.75rem 1.25rem;
  border-radius: 8px;
  font-size: 0.88rem;
  font-weight: 500;
  color: var(--white);
  box-shadow: var(--shadow-lg);
  animation: toastIn 0.3s ease;
  pointer-events: auto;
  word-break: break-word;
}

.toast.success { background: #27ae60; }
.toast.error   { background: #e74c3c; }
.toast.warning { background: #e67e22; }

.toast.hiding {
  animation: toastOut 0.3s ease forwards;
}

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

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

/* ===== Footer ===== */
.footer {
  text-align: center;
  padding: 1.25rem;
  padding-left: calc(1.25rem + env(safe-area-inset-left, 0px));
  padding-right: calc(1.25rem + env(safe-area-inset-right, 0px));
  padding-bottom: calc(1.25rem + env(safe-area-inset-bottom, 0px));
  font-size: 0.78rem;
  color: var(--gray-400);
  border-top: 1px solid var(--gray-200);
  background: var(--white);
}

/* ===== Reduced Motion ===== */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }

  .spinner {
    animation: none;
    border-color: rgba(255, 255, 255, 0.5);
  }

  .toast {
    animation: none;
  }

  .toast.hiding {
    animation: none;
    opacity: 0;
  }
}

/* ===== Responsive ===== */
@media (max-width: 900px) {
  .container {
    grid-template-columns: 1fr;
    gap: 1rem;
    margin: 1rem auto;
    padding: 0 1rem;
  }

  .header {
    padding: 1rem;
  }

  .logo {
    font-size: 1.15rem;
  }

  .subtitle {
    font-size: 0.78rem;
  }

  .options-group {
    grid-template-columns: 1fr 1fr;
  }

  .output-content {
    min-height: 250px;
    max-height: 60vh;
  }

  .generate-btn {
    width: calc(100% - 2rem);
  }

  .toast-container {
    top: 1rem;
    right: 1rem;
    left: 1rem;
    max-width: none;
  }
}

@media (max-width: 480px) {
  .options-group {
    grid-template-columns: 1fr;
  }

  .panel-header {
    padding: 0.75rem 1rem;
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .work-input {
    min-height: 160px;
    font-size: 0.85rem;
  }

  .output-content {
    padding: 1rem;
    font-size: 0.85rem;
  }

  .header-inner {
    text-align: center;
  }
}

/* ===== Scrollbar ===== */
.output-content::-webkit-scrollbar {
  width: 6px;
}

.output-content::-webkit-scrollbar-track {
  background: var(--gray-100);
  border-radius: 3px;
}

.output-content::-webkit-scrollbar-thumb {
  background: var(--gray-300);
  border-radius: 3px;
}

.output-content::-webkit-scrollbar-thumb:hover {
  background: var(--gray-400);
}