:root {
  --font-sans: 'Inter', sans-serif;
  --background: #f8f9fa;
  --surface: #ffffff;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --text-primary: #111827;
  --text-secondary: #6b7280;
  --border-color: #e5e7eb;
  --focus-ring: rgba(79, 70, 229, 0.3);
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
  --radius: 0.75rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --background: #f8f9fa;
    --surface: #111827;
    --primary: #6366f1;
    --primary-hover: #818cf8;
    --text-primary: #f9fafb;
    --text-secondary: #9ca3af;
    --border-color: #374151;
    --focus-ring: rgba(99, 102, 241, 0.4);
  }
}

/* Base */
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }

body {
  font-family: var(--font-sans);
  background-color: var(--background);
  color: var(--text-primary);
  line-height: 1.6;
  padding: 1rem;
}

.header { text-align: center; margin-bottom: 2rem; }
.brand { display: inline-flex; align-items: center; gap: 0.75rem; justify-content: center; margin-bottom: 0.25rem; }
.logo-mark { width: 40px; height: 40px; border-radius: 10px; box-shadow: var(--shadow-sm); }
.logo { font-size: 2.5rem; font-weight: 800; letter-spacing: -0.03em; background: linear-gradient(90deg, #111827, #4f46e5 65%); -webkit-background-clip: text; background-clip: text; color: transparent; }
.tagline { font-size: 1.25rem; color: var(--text-secondary); margin-top: 0.25rem; }
.how-it-works { margin-top: 0.5rem; color: var(--text-secondary); font-size: 0.98rem; max-width: 880px; margin-left: auto; margin-right: auto; }

.main-content {
  max-width: 1280px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

.quad-grid {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-areas:
    "input"
    "settings"
    "summary"
    "keywords";
  gap: 1.5rem;
}
@media (min-width: 900px) {
  .quad-grid {
    grid-template-columns: 1fr 1fr;
    grid-template-areas:
      "input summary"
      "settings keywords";
    grid-template-rows: minmax(280px, auto) minmax(460px, auto);
  }
  #box-settings, #box-keywords { min-height: 460px; }
}
.quad-grid > .card { height: 100%; }

#box-input { grid-area: input; }
#box-summary { grid-area: summary; }
#box-settings { grid-area: settings; }
#box-keywords { grid-area: keywords; }

.quad-grid > #image-display { grid-column: 1 / -1; }
@media (min-width: 900px) {
  .quad-grid > #image-display.in-grid { grid-row: 2; }
}

#settings-inline-slot, #keywords-inline-slot { display: none; }

.card {
  background: #ffffff;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow-md);
  display: flex;
  flex-direction: column;
}
.card-title {
  font-size: 1rem; font-weight: 600; margin-bottom: 0.75rem; color: #000;
  text-align: center; background: linear-gradient(180deg, rgba(255,255,255,0.0), rgba(255,255,255,0.6));
  padding: 0.5rem; border-radius: 8px; border: 1px solid var(--border-color);
}
.panel-content { display: flex; flex-direction: column; gap: 0.75rem; }
.panel-content > .quick-menu, .panel-content > .keywords-output { margin-top: 0; }
.center-actions { display: flex; justify-content: center; align-items: center; margin-top: 0.75rem; }

.collapse { overflow: hidden; max-height: 0; transition: max-height 220ms ease; }
.collapse.open { max-height: 1200px; }

.input-wrapper, .output-wrapper { display: flex; flex-direction: column; }

textarea {
  width: 100%;
  height: 300px;
  background-color: #ffffff;
  color: #111827;
  caret-color: #111827;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 1rem;
  font-size: 1rem;
  resize: vertical;
  transition: border-color 0.2s, box-shadow 0.2s;
  box-shadow: var(--shadow-sm);
}
textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--focus-ring); }
textarea::placeholder { color: var(--text-secondary); }

.action-bar {
  display: flex; flex-wrap: wrap; gap: 0.75rem; margin-top: 1rem; justify-content: center;
}
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 0.75rem 1.25rem; border-radius: var(--radius);
  font-size: 1rem; font-weight: 500; cursor: pointer; border: 1px solid transparent;
  transition: background-color 0.2s, transform 0.1s, box-shadow 0.2s; box-shadow: var(--shadow-sm);
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--focus-ring); }
.btn.primary { background-color: var(--primary); color: #ffffff; border-color: var(--primary); }
.btn.primary:hover:not(:disabled) { background-color: var(--primary-hover); border-color: var(--primary-hover); }
.btn:not(.primary) { background-color: var(--surface); color: var(--text-primary); border-color: var(--border-color); }
.btn:not(.primary):hover:not(:disabled) { background-color: var(--background); border-color: #d1d5db; }
.btn.icon { width: 40px; height: 40px; padding: 0; font-weight: 700; }
.btn.subtle { background-color: var(--surface); color: var(--text-primary); border-color: var(--border-color); }

@media (prefers-color-scheme: dark) {
  .btn:not(.primary):hover:not(:disabled) { border-color: #4b5563; }
}

.keywords-output {
  margin-top: 1rem;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(56px, auto));
  gap: 0.75rem;
  background: transparent;
  border: none;
  padding: 0;
}
.keyword-tag {
  background: #ffffff;
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 0.6rem 0.8rem;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  box-shadow: var(--shadow-sm);
  font-size: 0.98rem;
  color: #000;
  font-weight: 500;
}
@media (max-width: 640px) {
  .keywords-output {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: repeat(4, minmax(48px, auto));
  }
}

.image-display {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
  background-color: var(--surface);
  padding: 1.25rem;
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
}

.image-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  background: #f8fafc;
  border: 1px solid #e5e7eb;
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.image-frame img { max-width: 100%; max-height: 100%; object-fit: contain; }

/* Watermark */
.watermark {
  position: absolute;
  bottom: 0.75rem;
  right: 0.75rem;
  background: rgba(255, 255, 255, 0.7);
  color: #000;
  padding: 0.25rem 0.5rem;
  border-radius: 0.25rem;
  font-size: 0.75rem;
  font-weight: 500;
}
@media (prefers-color-scheme: dark) {
  .watermark { background: rgba(0, 0, 0, 0.5); color: #fff; }
}

.image-info { display: flex; flex-direction: column; }
.info-tabs { display: flex; border-bottom: 1px solid var(--border-color); justify-content: center; }
.tab-btn {
  padding: 0.75rem 1rem; background: none; border: none; cursor: pointer;
  font-size: 1rem; color: var(--text-secondary); border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tab-btn.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }
.info-panel { padding: 1rem 0; font-size: 0.9rem; white-space: pre-wrap; word-break: break-word; color: var(--text-secondary); }

.quick-menu {
  background-color: #ffffff;
  color: #111827;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 1rem;
  margin-top: 0.5rem;
  position: relative;
  z-index: 2;
}
.quick-menu .option-group { margin-bottom: 0.75rem; }
.quick-menu .option-group:last-child { margin-bottom: 0; }
.quick-menu .option-group label { display: block; font-weight: 500; margin-bottom: 0.5rem; color: #111827; }
.quick-menu .chip-group { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.quick-menu .chip {
  padding: 0.4rem 0.9rem; border-radius: 999px; font-size: 0.85rem; cursor: pointer;
  border: 1px solid var(--border-color);
  background-color: #ffffff; color: #111827; transition: all 0.2s;
}
.quick-menu .chip:hover { border-color: var(--primary); }
.quick-menu .chip.selected { background-color: var(--primary); color: #fff; border-color: var(--primary); }

#toast-container {
  position: fixed; top: 1rem; right: 1rem; z-index: 200;
  display: flex; flex-direction: column; gap: 0.5rem;
}
.toast {
  background-color: var(--surface); color: var(--text-primary);
  padding: 1rem 1.5rem; border-radius: var(--radius); box-shadow: var(--shadow-lg);
  animation: toast-in 0.3s ease;
}
@keyframes toast-in {
  from { opacity: 0; transform: translateX(100%); }
  to   { opacity: 1; transform: translateX(0); }
}

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border-width: 0;
}
[hidden] { display: none !important; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}