/* Chapter Context Display Styles */

/* ============================================
   Base Styles
   ============================================ */

/* Context container */
.chapter-context {
  background-color: #f5f5f5;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 24px;
  max-height: 200px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch; /* Smooth scroll on iOS */
  border-left: 3px solid #171717;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

/* Context header */
.chapter-context__header {
  margin-bottom: 12px;
}

/* Context title - normal case, not uppercase */
.chapter-context__title {
  font-size: 14px;
  font-weight: 600;
  color: #5a5a5a;
  text-transform: none; /* Removed uppercase for better readability */
  letter-spacing: 0;
  margin: 0;
}

/* Context body */
.chapter-context__body {
  font-size: 16px;
  line-height: 1.6;
  color: #4a4a4a;
  margin-bottom: 12px;
}

/* Context metadata footer */
.chapter-context__meta {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: #7a7a7a;
}

/* Context author wrapper */
.chapter-context__author {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* Context author label */
.chapter-context__author-label {
  font-weight: 500;
}

/* Context author name */
.chapter-context__author-name {
  font-weight: 400;
}

/* Context separator */
.chapter-context__separator {
  margin: 0 4px;
}

/* Context timestamp */
.chapter-context__timestamp {
  font-size: 14px;
}

/* Content divider */
.content-divider {
  border: none;
  height: 1px;
  background-color: #e5e5e5;
  margin: 24px 0;
}

/* Skip link (accessibility) */
.skip-link {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.skip-link:focus {
  position: fixed;
  top: 16px;
  left: 16px;
  width: auto;
  height: auto;
  padding: 12px 16px;
  background-color: #4a4a4a;
  color: #ffffff;
  clip: auto;
  z-index: 1000;
  border-radius: 4px;
  text-decoration: none;
  font-weight: 500;
  cursor: pointer;
}

/* ============================================
   Interactive Elements
   ============================================ */

/* All interactive elements get pointer cursor */
.pos-button,
.pos-form a,
.skip-link,
.pos-form button[type="submit"] {
  cursor: pointer;
  transition: color 0.2s ease, background-color 0.2s ease, opacity 0.2s ease;
}

/* Hover states - use color/opacity only, no scale */
.pos-button:hover,
.pos-form a:hover {
  opacity: 0.9;
}

/* ============================================
    Enhanced Textarea for Writing Experience
    ============================================ */

.chapter-writing-form__textarea,
.pos-form textarea[name*="chapters[body]"],
.pos-form textarea[name*="alternative[chapter_content]"],
.pos-form textarea {
  min-height: 300px !important;
  max-height: 600px !important;
  width: 100% !important;
  min-width: 0 !important;
  max-width: 1000px !important;
  box-sizing: border-box !important;
  padding: 20px !important;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif !important;
  font-size: 18px !important; /* Optimal for long-form writing */
  line-height: 1.75 !important; /* Improved readability */
  resize: vertical !important;
  border-radius: 8px !important;
  transition: border-color 0.2s ease, box-shadow 0.2s ease, color 0.2s ease;
}

/* Strong focus states - 2px outline + 4px shadow */
.chapter-writing-form__textarea:focus,
.pos-form textarea:focus,
.pos-form input:focus {
  border-color: #4a90e2;
  outline: 2px solid #4a90e2;
  outline-offset: 0;
  box-shadow: 0 0 0 4px rgba(74, 144, 226, 0.15);
}

/* focus-visible pattern - keyboard-only focus */
.chapter-writing-form__textarea:focus-visible,
.pos-form textarea:focus-visible,
.pos-form input:focus-visible {
  border-color: #4a90e2;
  outline: 2px solid #4a90e2;
  outline-offset: 0;
  box-shadow: 0 0 0 4px rgba(74, 144, 226, 0.15);
}

/* Focus but not focus-visible - no outline (mouse/touch) */
.chapter-writing-form__textarea:focus:not(:focus-visible),
.pos-form textarea:focus:not(:focus-visible),
.pos-form input:focus:not(:focus-visible) {
  outline: none;
}

/* Valid fields - green border indication */
.pos-form input:valid:not(:placeholder-shown),
.pos-form textarea:valid:not(:placeholder-shown) {
  border-color: #22c55e;
}

/* ============================================
   Validation Error Styling
   ============================================ */

/* Error messages with icon, background, and border */
.pos-form-error {
  background-color: #fef2f2;
  border: 1px solid #fecaca;
  border-left: 4px solid #ef4444;
  color: #991b1b;
  padding: 12px 16px;
  margin-top: 8px;
  margin-bottom: 16px;
  border-radius: 4px;
  font-size: 14px;
  line-height: 1.5;
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

/* Error icon indicator - inline SVG warning icon from Heroicons */
.pos-form-error::before {
  content: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="%23ef4444" class="w-5 h-5"><path fill-rule="evenodd" d="M8.485 2.495c.673-1.167 2.357-1.167 3.03 0l6.28 10.875c.673 1.167-.17 2.625-1.516 2.625H3.72c-1.347 0-2.189-1.458-1.515-2.625L8.485 2.495ZM10 5a.75.75 0 0 1 .75.75v3.5a.75.75 0 0 1-1.5 0v-3.5A.75.75 0 0 1 10 5Zm0 9a1 1 0 1 0 0-2 1 1 0 0 0 0 2Z" clip-rule="evenodd" /></svg>');
  display: inline-block;
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

/* Invalid fields */
.pos-form input:invalid:not(:placeholder-shown),
.pos-form textarea:invalid:not(:placeholder-shown) {
  border-color: #ef4444;
}

/* ============================================
   Loading State with Spinner Animation
   ============================================ */

/* Loading state on submit button */
.pos-button.is-loading {
  position: relative;
  color: transparent !important;
  pointer-events: none;
}

/* Spinner */
.pos-button.is-loading::after {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  top: 50%;
  left: 50%;
  margin-left: -10px;
  margin-top: -10px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top-color: #ffffff;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

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

/* ============================================
   Form Actions (Mobile Stacked)
   ============================================ */

/* Desktop: side-by-side actions */
@media (min-width: 640px) {
  .pos-form-actions {
    display: flex;
    gap: 12px;
    align-items: center;
  }

  .pos-form-actions .pos-button,
  .pos-form-actions a.pos-button {
    flex: 0 0 auto;
  }
}

/* Mobile: stacked actions */
@media (max-width: 639px) {
  .pos-form-actions {
    display: flex;
    flex-direction: column;
    gap: 12px;
  }

  .pos-form-actions .pos-button,
  .pos-form-actions a.pos-button {
    width: 100%;
  }
}

/* ============================================
   Responsive Breakpoints
   ============================================ */

/* 375px - Small mobile */
@media (min-width: 375px) {
  .chapter-context {
    max-height: 180px; /* Reduced on mobile */
    padding: 12px;
  }

  .chapter-writing-form__textarea,
  .pos-form textarea[name*="chapters[body]"],
  .pos-form textarea[name*="alternative[chapter_content]"],
  .pos-form textarea {
    min-height: 250px !important; /* Smaller on mobile */
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
    padding: 16px !important;
  }
}

/* 640px - Mobile landscape */
@media (min-width: 640px) {
  .chapter-context {
    max-height: 200px;
    padding: 16px;
  }

  .content-divider {
    margin: 32px 0;
  }

  .chapter-writing-form__textarea,
  .pos-form textarea[name*="chapters[body]"],
  .pos-form textarea[name*="alternative[chapter_content]"],
  .pos-form textarea {
    min-height: 300px !important;
    min-width: 0 !important;
    width: 100% !important;
    max-width: 1000px !important;
    box-sizing: border-box !important;
    padding: 20px !important;
  }
}

/* 768px - Tablet */
@media (min-width: 768px) {
  .chapter-context {
    max-height: 250px;
    padding: 20px;
  }

  .chapter-writing-form__textarea,
  .pos-form textarea[name*="chapters[body]"],
  .pos-form textarea[name*="alternative[chapter_content]"],
  .pos-form textarea {
    min-height: 350px !important;
    min-width: 0 !important;
    width: 100% !important;
    max-width: 1000px !important;
    box-sizing: border-box !important;
  }
}

/* 1024px - Desktop */
@media (min-width: 1024px) {
  .chapter-context {
    max-height: 300px;
    padding: 24px;
  }

  .chapter-writing-form__textarea,
  .pos-form textarea[name*="chapters[body]"],
  .pos-form textarea[name*="alternative[chapter_content]"],
  .pos-form textarea {
    min-height: 400px !important;
    min-width: 0 !important;
    width: 100% !important;
    max-width: 1000px !important;
    box-sizing: border-box !important;
  }

  .chapter-context-wrapper {
    display: grid !important;
    grid-template-columns: calc(35% - 16px) calc(65% - 16px) !important;
    gap: 32px !important;
    align-items: start !important;
    width: 100% !important;
    max-width: none !important;
  }

  .chapter-context-wrapper .chapter-context {
    position: sticky;
    top: 24px;
  }

  .chapter-context-wrapper .pos-form {
    width: 100% !important;
    max-width: none !important;
  }
}

/* 1440px - Large desktop */
@media (min-width: 1440px) {
  .chapter-context {
    max-height: 350px;
    padding: 24px;
  }

  .chapter-writing-form__textarea,
  .pos-form textarea[name*="chapters[body]"],
  .pos-form textarea[name*="alternative[chapter_content]"],
  .pos-form textarea {
    min-height: 450px !important;
    min-width: 0 !important;
    width: 100% !important;
    max-width: 1000px !important;
    box-sizing: border-box !important;
  }
}

/* ============================================
   Dark Mode Support
   ============================================ */

@media (prefers-color-scheme: dark) {
  /* Context panel in dark mode */
  .chapter-context {
    background-color: #1e1e1e;
    border-left-color: #ffffff;
    color: #e5e5e5;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  }

  .chapter-context__title {
    color: #d4d4d4;
  }

  .chapter-context__body {
    color: #c5c5c5;
  }

  .chapter-context__meta {
    color: #a3a3a3;
  }

  /* Content divider in dark mode */
  .content-divider {
    background-color: #3e3e3e;
  }

  /* Skip link in dark mode */
  .skip-link:focus {
    background-color: #4a4a4a;
    color: #ffffff;
  }

  /* Textarea in dark mode */
  .chapter-writing-form__textarea,
  .pos-form textarea[name*="chapters[body]"],
  .pos-form textarea[name*="alternative[chapter_content]"],
  .pos-form textarea,
  .pos-form input {
    background-color: #1e1e1e;
    border-color: #4a4a4a;
    color: #e5e5e5;
  }

  /* Focus states in dark mode */
  .chapter-writing-form__textarea:focus,
  .pos-form textarea:focus,
  .pos-form input:focus,
  .chapter-writing-form__textarea:focus-visible,
  .pos-form textarea:focus-visible,
  .pos-form input:focus-visible {
    border-color: #60a5fa;
    outline-color: #60a5fa;
    box-shadow: 0 0 0 4px rgba(96, 165, 250, 0.15);
  }

  /* Valid fields in dark mode */
  .pos-form input:valid:not(:placeholder-shown),
  .pos-form textarea:valid:not(:placeholder-shown) {
    border-color: #4ade80;
  }

  /* Invalid fields in dark mode */
  .pos-form input:invalid:not(:placeholder-shown),
  .pos-form textarea:invalid:not(:placeholder-shown) {
    border-color: #f87171;
  }

  /* Error messages in dark mode */
  .pos-form-error {
    background-color: #3f1515;
    border-color: #7c2d2d;
    border-left-color: #ef4444;
    color: #fecaca;
  }

  /* Form labels in dark mode */
  .pos-form label {
    color: #d4d4d4;
  }

  /* Placeholder text in dark mode */
  .pos-form textarea::placeholder,
  .pos-form input::placeholder {
    color: #7a7a7a;
  }
}

/* ============================================
   Class-based Dark Mode Support
   Follows platformOS common-styling pattern
   ============================================ */

.pos-theme-dark .chapter-context {
  background-color: #1e1e1e;
  border-left-color: #ffffff;
  color: #e5e5e5;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.pos-theme-dark .chapter-context__title {
  color: #d4d4d4;
}

.pos-theme-dark .chapter-context__body {
  color: #c5c5c5;
}

.pos-theme-dark .chapter-context__meta {
  color: #a3a3a3;
}

.pos-theme-dark .content-divider {
  background-color: #3e3e3e;
}

.pos-theme-dark .skip-link:focus {
  background-color: #4a4a4a;
  color: #ffffff;
}

.pos-theme-dark .chapter-writing-form__textarea,
.pos-theme-dark .pos-form textarea[name*="chapters[body]"],
.pos-theme-dark .pos-form textarea[name*="alternative[chapter_content]"],
.pos-theme-dark .pos-form textarea,
.pos-theme-dark .pos-form input {
  background-color: #1e1e1e;
  border-color: #4a4a4a;
  color: #e5e5e5;
}

.pos-theme-dark .chapter-writing-form__textarea:focus,
.pos-theme-dark .pos-form textarea:focus,
.pos-theme-dark .pos-form input:focus,
.pos-theme-dark .chapter-writing-form__textarea:focus-visible,
.pos-theme-dark .pos-form textarea:focus-visible,
.pos-theme-dark .pos-form input:focus-visible {
  border-color: #60a5fa;
  outline-color: #60a5fa;
  box-shadow: 0 0 0 4px rgba(96, 165, 250, 0.15);
}

.pos-theme-dark .pos-form input:valid:not(:placeholder-shown),
.pos-theme-dark .pos-form textarea:valid:not(:placeholder-shown) {
  border-color: #4ade80;
}

.pos-theme-dark .pos-form input:invalid:not(:placeholder-shown),
.pos-theme-dark .pos-form textarea:invalid:not(:placeholder-shown) {
  border-color: #f87171;
}

.pos-theme-dark .pos-form-error {
  background-color: #3f1515;
  border-color: #7c2d2d;
  border-left-color: #ef4444;
  color: #fecaca;
}

.pos-theme-dark .pos-form label {
  color: #d4d4d4;
}

.pos-theme-dark .pos-form textarea::placeholder,
.pos-theme-dark .pos-form input::placeholder {
  color: #7a7a7a;
}

/* ============================================
   Visually Hidden Class (for screen readers)
   ============================================ */

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Ensure visually-hidden elements can receive focus when needed */
.visually-hidden.focusable:focus {
  position: fixed;
  width: auto;
  height: auto;
  padding: 12px 16px;
  clip: auto;
  z-index: 1000;
  background-color: #f5f5f5;
  border: 2px solid #4a90e2;
  color: #4a4a4a;
}

@media (prefers-color-scheme: dark) {
  .visually-hidden.focusable:focus {
    background-color: #2d2d2d;
    border-color: #60a5fa;
    color: #e5e5e5;
  }
}
