/* ============================
   Custom Properties
   ============================ */
:root {
  --bg: #ede8e0;
  --surface: rgba(255, 252, 247, 0.86);
  --surface-solid: #fffcf7;
  --ink: #1c1a17;
  --ink-2: #605749;
  --ink-3: #9a8f82;
  --border: #d9cfbf;
  --border-focus: #b8aa96;
  --ring: rgba(30, 74, 67, 0.35);
  --accent: #bf5528;
  --accent-hover: #a64a22;
  --teal: #1e4a43;
  --teal-hover: #163b36;
  --teal-light: rgba(30, 74, 67, 0.07);
  --green: #2d7b5a;
  --red: #b33;
  --radius-lg: 16px;
  --radius: 12px;
  --radius-sm: 8px;
}

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

body {
  margin: 0;
  font-family: "Avenir Next", "Avenir", "Segoe UI", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  background:
    radial-gradient(ellipse at top right, rgba(191, 85, 40, 0.09), transparent 50%),
    radial-gradient(ellipse at bottom left, rgba(30, 74, 67, 0.07), transparent 45%),
    var(--bg);
  color: var(--ink);
  padding-bottom: env(safe-area-inset-bottom);
}

/* ============================
   Typography
   ============================ */
h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-2);
  margin-bottom: 5px;
  user-select: none;
}

p { margin: 0; }

/* ============================
   Focus
   ============================ */
:focus-visible {
  outline: 2px solid var(--teal);
  outline-offset: 2px;
}

/* ============================
   Topbar
   ============================ */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.wordmark {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.04em;
  color: var(--ink);
}

/* ============================
   Layout
   ============================ */
.layout {
  display: grid;
  grid-template-columns: 5fr 7fr;
  gap: 20px;
  padding: 0 20px 24px;
  max-width: 1200px;
  margin: 0 auto;
  align-items: start;
}

/* ============================
   Panels
   ============================ */
.composer {
  display: grid;
  gap: 12px;
}

.results {
  display: grid;
  gap: 12px;
  position: sticky;
  top: 16px;
  max-height: calc(100dvh - 80px);
  overflow-y: auto;
  overscroll-behavior: contain;
}

.results-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
}

/* ============================
   Sections (details/summary)
   ============================ */
details.section {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  backdrop-filter: blur(8px);
  overflow: hidden;
}

details.section > summary {
  padding: 13px 16px;
  font-size: 14px;
  font-weight: 600;
  color: var(--ink-2);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  user-select: none;
  list-style: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

details.section > summary::-webkit-details-marker { display: none; }

details.section > summary::after {
  content: '';
  width: 7px;
  height: 7px;
  border-right: 2px solid var(--ink-3);
  border-bottom: 2px solid var(--ink-3);
  transform: rotate(-45deg);
  transition: transform 0.15s ease;
  flex-shrink: 0;
  margin-left: 8px;
}

details[open].section > summary::after {
  transform: rotate(45deg);
}

.section-body {
  padding: 12px 16px 16px;
  border-top: 1px solid var(--border);
  display: grid;
  gap: 14px;
}

/* ============================
   Form Fields
   ============================ */
.field {
  display: grid;
  gap: 0;
}

.field-row {
  display: grid;
  gap: 12px;
}

textarea,
input[type="number"],
select {
  width: 100%;
  font: inherit;
  font-size: 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: 10px 12px;
  background: var(--surface-solid);
  color: var(--ink);
  transition: border-color 0.15s ease;
}

textarea {
  resize: vertical;
  min-height: 44px;
  line-height: 1.45;
}

textarea::placeholder,
input::placeholder {
  color: var(--ink-3);
}

textarea:focus,
input:focus,
select:focus {
  border-color: var(--border-focus);
  outline: none;
  box-shadow: 0 0 0 3px var(--ring);
}

select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg width='10' height='6' viewBox='0 0 10 6' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%239a8f82' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 32px;
}

input[type="number"] {
  font-variant-numeric: tabular-nums;
}

/* Hide number input spinners */
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
input[type="number"] { -moz-appearance: textfield; }

.field-hint {
  font-size: 12px;
  color: var(--ink-3);
  margin-top: 5px;
  line-height: 1.4;
}

/* ============================
   Segmented Control
   ============================ */
.seg-group {
  display: flex;
  background: var(--bg);
  border-radius: var(--radius);
  padding: 3px;
  gap: 2px;
}

.seg {
  flex: 1;
  padding: 9px 6px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--ink-2);
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  text-align: center;
  min-height: 38px;
}

.seg.active {
  background: var(--surface-solid);
  color: var(--ink);
  box-shadow: 0 1px 3px rgba(0,0,0,0.06), 0 0 0 0.5px rgba(0,0,0,0.04);
}

/* ============================
   Pip Selector
   ============================ */
.pips {
  display: flex;
  gap: 6px;
}

.pip {
  width: 42px;
  height: 42px;
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--border);
  background: var(--surface-solid);
  color: var(--ink-2);
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s ease, background-color 0.15s ease, color 0.15s ease;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  padding: 0;
}

.pip.active {
  background: var(--teal);
  border-color: var(--teal);
  color: #eaf2ef;
}

.pip:active {
  transform: scale(0.95);
}

/* ============================
   Toggle Switch
   ============================ */
.toggle-row {
  display: flex;
  flex-wrap: wrap;
  gap: 14px 28px;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  font-size: 14px;
  font-weight: 500;
  color: var(--ink-2);
  user-select: none;
}

.toggle input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
  pointer-events: none;
}

.toggle-track {
  position: relative;
  width: 44px;
  height: 26px;
  background: var(--border);
  border-radius: 13px;
  transition: background-color 0.2s ease;
  flex-shrink: 0;
}

.toggle input:checked + .toggle-track {
  background: var(--teal);
}

.toggle-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 22px;
  height: 22px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s ease;
  box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}

.toggle input:checked + .toggle-track .toggle-thumb {
  transform: translateX(18px);
}

/* ============================
   Buttons
   ============================ */
.btn-primary {
  width: 100%;
  padding: 14px 20px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font: inherit;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  box-shadow: 0 1px 4px rgba(0,0,0,0.12), 0 0 0 0.5px rgba(0,0,0,0.04);
  transition: background-color 0.15s ease, opacity 0.15s ease;
}

.btn-primary:active {
  transform: scale(0.98);
}

.btn-primary:disabled {
  opacity: 0.55;
  cursor: default;
  transform: none;
}

.btn-ghost {
  background: transparent;
  color: var(--ink-2);
  border: none;
  padding: 8px 14px;
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  border-radius: var(--radius-sm);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: color 0.15s ease;
}

.btn-sm {
  font-size: 13px;
  padding: 6px 10px;
}

/* ============================
   Action Bar
   ============================ */
.action-bar {
  display: grid;
  gap: 6px;
  padding-top: 4px;
}

.action-secondary {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 4px;
}

.action-sep {
  color: var(--ink-3);
  font-size: 10px;
  user-select: none;
}

/* ============================
   Results
   ============================ */
.meta {
  color: var(--ink-3);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  min-height: 16px;
}

.empty-hint {
  color: var(--ink-3);
  font-size: 14px;
  text-align: center;
  padding: 32px 16px;
}

.empty-hint.error {
  color: var(--red);
}

/* ============================
   Draft Cards
   ============================ */
.draft-card {
  background: var(--surface-solid);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px 16px;
  transition: border-color 0.15s ease;
}

.draft-card.copied {
  border-color: var(--green);
}

.draft-body {
  display: flex;
  gap: 10px;
  align-items: flex-start;
}

.draft-text {
  flex: 1;
  font-size: 16px;
  line-height: 1.5;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.btn-copy-draft {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--ink-3);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: color 0.15s ease, border-color 0.15s ease;
  padding: 0;
}

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

.draft-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}

.draft-tag {
  font-size: 12px;
  font-weight: 500;
  color: var(--ink-3);
  background: var(--teal-light);
  padding: 3px 8px;
  border-radius: 6px;
}

.draft-rationale {
  font-size: 13px;
  line-height: 1.45;
  color: var(--ink-3);
  margin-top: 8px;
}

/* ============================
   Raw Output
   ============================ */
.raw-actions {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 8px;
}

.output-box {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  min-height: 80px;
  max-height: 400px;
  overflow: auto;
  white-space: pre-wrap;
  font-size: 13px;
  line-height: 1.5;
  color: var(--ink-2);
  margin: 0;
}

/* ============================
   Login
   ============================ */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding: 16px;
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 32px 24px;
  display: grid;
  gap: 24px;
  text-align: center;
}

.login-card .wordmark {
  font-size: 22px;
}

.login-card form {
  display: grid;
  gap: 12px;
  text-align: left;
}

.login-error {
  color: var(--red);
  font-size: 13px;
  min-height: 18px;
  text-align: center;
}

.login-error:empty { display: none; }

/* ============================
   Responsive
   ============================ */
@media (min-width: 421px) {
  .field-row {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 800px) {
  .topbar {
    padding: 12px 16px;
  }

  .layout {
    grid-template-columns: 1fr;
    padding: 0 16px 24px;
    gap: 16px;
  }

  .results {
    position: static;
    max-height: none;
    overflow: visible;
  }
}

/* ============================
   Hover (desktop only)
   ============================ */
@media (hover: hover) and (pointer: fine) {
  .btn-primary:hover:not(:disabled) {
    background: var(--accent-hover);
  }

  .btn-ghost:hover {
    color: var(--ink);
  }

  .pip:hover:not(.active) {
    border-color: var(--border-focus);
    background: var(--teal-light);
  }

  .seg:hover:not(.active) {
    background: var(--teal-light);
  }

  .draft-card:hover {
    border-color: var(--border-focus);
  }

  .btn-copy-draft:hover {
    color: var(--ink-2);
    border-color: var(--border-focus);
  }
}

/* ============================
   Reduced Motion
   ============================ */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    transition-duration: 0.01ms !important;
  }
}
