/**
 * LETTER ARCHIVE APP - Stylesheet
 * 
 * This file contains all the CSS styling for the letter archive application.
 * It uses CSS custom properties (variables) for colors and a CSS Grid layout.
 */

/* ============================================================================
   CSS VARIABLES (Custom Properties)
   ============================================================================
   These define the color scheme and can be easily changed throughout the app.
   Using variables makes it easy to update colors in one place.
*/
:root {
  --bg: #f8f6f2;           /* Background color (light beige) */
  --paper: #fffef9;        /* Card/panel background (off-white) */
  --ink: #1a1814;          /* Main text color (dark brown) */
  --ink-muted: #5c564d;    /* Secondary text color (gray-brown) */
  --accent: #8b6914;        /* Accent color for buttons/links (golden brown) */
  --accent-soft: #e8dfc4;  /* Light accent for highlights (light beige) */
  --border: #e2ddd4;        /* Border color (light gray) */
  --shadow: rgba(26, 24, 20, 0.06); /* Shadow color (semi-transparent dark) */
}

/* ============================================================================
   GLOBAL STYLES
   ============================================================================
*/

/* Make all elements use border-box sizing (includes padding/border in width) */
* {
  box-sizing: border-box;
}

/* Body styles - sets up the page foundation */
body {
  margin: 0; /* Remove default browser margins */
  font-family: "DM Sans", system-ui, sans-serif; /* Default font */
  background: var(--bg); /* Use CSS variable for background */
  color: var(--ink); /* Use CSS variable for text color */
  height: 100vh; /* Full viewport height */
  overflow: hidden; /* Prevent page scrolling (we have independent scroll areas) */
  line-height: 1.5; /* Spacing between lines of text */
  -webkit-font-smoothing: antialiased; /* Better font rendering */
  -moz-osx-font-smoothing: grayscale;
}

/* ============================================================================
   MAIN APP LAYOUT (CSS Grid)
   ============================================================================
   Uses CSS Grid to create a 3-column layout:
   - Left: Filters sidebar (240px fixed width)
   - Middle: Letter list (flexible width)
   - Right: Reader panel (42% of width, minimum 320px)
   
   Grid areas:
   - Row 1: Header spans full width, Reader panel on right
   - Row 2: Filters | Letter List | Reader panel
*/
.app {
  max-width: none; /* No maximum width - uses full screen */
  margin: 0;
  padding: 2rem 1.5rem; /* Spacing around the grid */
  display: grid; /* Enable CSS Grid layout */
  
  /* Define 3 columns: fixed filters, flexible content, flexible reader */
  grid-template-columns: 240px minmax(0, 1fr) minmax(320px, 42%);
  
  /* Define 2 rows: auto-height header, flexible content row */
  grid-template-rows: auto 1fr;
  
  /* Name the grid areas for easy placement */
  grid-template-areas:
    "header header reader"    /* Row 1: header spans 2 cols, reader on right */
    "filters content reader"; /* Row 2: filters | content | reader */
  
  gap: 2rem; /* Space between grid items */
  height: 100vh; /* Full viewport height */
  overflow: hidden; /* Prevent scrolling at app level */
}

/* ============================================================================
   RESPONSIVE DESIGN - Tablet Size (iPad: 768px - 1024px)
   ============================================================================
   On tablet screens, stack reader panel below the main content
   Filters stay on the left, content in the middle
*/
@media (max-width: 1024px) {
  .app {
    padding: 1.5rem 1rem; /* Reduce padding for tablet */
    grid-template-columns: 200px 1fr; /* Narrower filters column */
    grid-template-areas:
      "header header"      /* Header spans both columns */
      "filters content"    /* Filters and content side by side */
      "reader reader";     /* Reader spans full width below */
    gap: 1.5rem; /* Slightly smaller gaps */
    height: 100vh;
    overflow: hidden;
  }
  
  /* Reader panel takes full width on tablet */
  .reader-panel {
    max-height: 50vh; /* Limit height so it doesn't take too much space */
  }
}

/* ============================================================================
   RESPONSIVE DESIGN - Large Phone / Small Tablet (481px - 767px)
   ============================================================================
   On large phones, filters become a horizontal bar or stack vertically
*/
@media (max-width: 767px) {
  .app {
    padding: 1rem 0.75rem; /* Smaller padding */
    grid-template-columns: 1fr; /* Single column */
    grid-template-rows: auto auto 1fr auto; /* Header, filters, content, reader */
    grid-template-areas:
      "header"     /* Header */
      "filters"    /* Filters bar */
      "content"    /* Letter list */
      "reader";     /* Reader panel */
    gap: 1rem;
    height: 100vh;
    overflow-y: auto; /* Allow page scrolling */
    overflow-x: hidden;
  }
  
  /* Filters become horizontal/compact on mobile */
  .filters {
    position: static; /* Remove sticky positioning */
    max-height: none;
    overflow-y: visible;
    flex-direction: row; /* Horizontal layout */
    flex-wrap: wrap; /* Wrap to multiple rows if needed */
    gap: 0.75rem;
    padding: 0.75rem;
    background: var(--paper);
    border-radius: 8px;
    border: 1px solid var(--border);
  }
  
  .filter-group {
    flex: 1 1 auto; /* Allow filters to grow/shrink */
    min-width: 140px; /* Minimum width for readability */
  }
  
  /* Remove height constraints on mobile for better scrolling */
  .content,
  .reader-panel {
    max-height: none;
    min-height: 300px; /* Ensure readable height */
  }
  
  .reader-panel {
    max-height: 60vh; /* Reader panel takes up to 60% of viewport */
  }
}

/* ============================================================================
   RESPONSIVE DESIGN - Small Phone (≤480px)
   ============================================================================
   Optimized for small phone screens
*/
@media (max-width: 480px) {
  .app {
    padding: 0.75rem 0.5rem; /* Minimal padding */
    gap: 0.75rem;
  }
  
  /* Header adjustments for small screens */
  .header {
    padding-bottom: 0.75rem;
  }
  
  .header h1 {
    font-size: 1.5rem; /* Smaller title */
  }
  
  .tagline {
    font-size: 0.85rem; /* Smaller tagline */
  }
  
  /* Filters stack vertically on very small screens */
  .filters {
    flex-direction: column; /* Stack filters vertically */
  }
  
  .filter-group {
    min-width: 100%; /* Full width on small screens */
  }
  
  /* Smaller filter inputs */
  .filters select,
  .filters input[type="date"],
  .filters input[type="search"] {
    font-size: 0.9rem;
    padding: 0.5rem;
  }
  
  /* Letter cards more compact */
  .letter-card {
    padding: 0.75rem 1rem;
  }
  
  .letter-sender {
    font-size: 1.1rem; /* Slightly smaller */
  }
  
  /* Reader panel adjustments */
  .reader-panel {
    padding: 1rem;
    max-height: 50vh; /* Smaller reader panel on phones */
  }
  
  .reader-sender {
    font-size: 1.2rem; /* Smaller reader title */
  }
  
  .reader-body {
    font-size: 1rem; /* Smaller body text */
    line-height: 1.6;
  }
  
  /* Buttons larger for touch */
  .btn-secondary,
  .letter-open,
  .reader-pdf-link {
    padding: 0.6rem 1rem;
    font-size: 0.95rem;
    min-height: 44px; /* Minimum touch target size */
  }
}

/* ============================================================================
   HEADER SECTION
   ============================================================================
*/
.header {
  grid-area: header;
  border-bottom: 1px solid var(--border);
  padding-bottom: 1rem;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 0.5rem;
}

.header .btn-lock {
  margin-left: auto;
  padding: 0.35rem 0.75rem;
  font-size: 0.85rem;
  color: var(--ink-muted);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  font-family: inherit;
}

.header .btn-lock:hover {
  color: var(--ink);
  border-color: var(--accent);
  background: var(--accent-soft);
}

.header h1 {
  font-family: "Cormorant Garamond", Georgia, serif;
  font-size: 2rem;
  font-weight: 600;
  margin: 0;
  letter-spacing: 0.02em;
}

.tagline {
  margin: 0.25rem 0 0;
  font-size: 0.95rem;
  color: var(--ink-muted);
}

@media (max-width: 767px) {
  .header {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.75rem;
  }
  .header .btn-lock {
    margin-left: 0;
    align-self: flex-end;
  }
}

/* ============================================================================
   FILTERS SIDEBAR
   ============================================================================
   Left column containing all filter controls
*/
.filters {
  grid-area: filters; /* Place in "filters" grid area */
  display: flex;
  flex-direction: column; /* Stack filter groups vertically */
  gap: 1rem; /* Space between filter groups */
  min-height: 0; /* Allow shrinking in grid */
  overflow-y: auto; /* Scroll if filters are too tall */
  align-self: start; /* Align to top of grid cell */
  position: sticky; /* Stays visible when scrolling */
  top: 0; /* Stick to top of viewport */
  max-height: calc(100vh - 2rem); /* Don't exceed viewport height */
}

.filter-group {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.filters label {
  font-size: 0.8rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--ink-muted);
}

.filters select,
.filters input[type="date"],
.filters input[type="search"] {
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--paper);
  font-family: inherit;
  font-size: 0.95rem;
  color: var(--ink);
  /* Better touch targets on mobile */
  min-height: 40px;
  -webkit-appearance: none; /* Remove iOS default styling */
  appearance: none;
}

/* Improve date input on mobile */
@media (max-width: 767px) {
  .filters input[type="date"] {
    font-size: 0.9rem; /* Prevent zoom on iOS */
  }
  
  .filters input[type="search"] {
    font-size: 16px; /* Prevent zoom on iOS when focusing */
  }
}

.filters input[type="search"]:focus,
.filters select:focus,
.filters input[type="date"]:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

.btn-secondary {
  margin-top: 0.5rem;
  padding: 0.5rem 1rem;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--ink-muted);
  cursor: pointer;
  /* Better touch targets */
  min-height: 40px;
  touch-action: manipulation; /* Prevent double-tap zoom */
}

@media (max-width: 767px) {
  .btn-secondary {
    margin-top: 0;
    width: 100%; /* Full width buttons on mobile */
  }
}

.btn-secondary:hover {
  background: var(--accent-soft);
  color: var(--ink);
  border-color: var(--accent);
}

/* ============================================================================
   CONTENT AREA (Letter List Column)
   ============================================================================
   Middle column showing filtered letter cards.
   Has independent vertical scrolling.
*/
.content {
  grid-area: content; /* Place in "content" grid area */
  min-width: 0; /* Allow shrinking below content size */
  min-height: 0; /* Allow shrinking below content size */
  overflow-y: auto; /* Enable vertical scrolling */
  overflow-x: hidden; /* Prevent horizontal scrolling */
  display: flex;
  flex-direction: column; /* Stack result count and list vertically */
}

.result-count {
  font-size: 0.9rem;
  color: var(--ink-muted);
  margin: 0 0 1rem;
}

@media (max-width: 767px) {
  .result-count {
    font-size: 0.85rem;
    margin: 0 0 0.75rem;
  }
}

.letter-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

/* ============================================================================
   LETTER CARDS
   ============================================================================
   Individual letter cards in the list
*/
.letter-card {
  background: var(--paper);
  border: 1px solid var(--border);
  border-radius: 8px; /* Rounded corners */
  padding: 1rem 1.25rem;
  box-shadow: 0 1px 3px var(--shadow); /* Subtle shadow */
  transition: box-shadow 0.2s, border-color 0.2s; /* Smooth hover effect */
  /* Better touch interaction on mobile */
  cursor: pointer;
  -webkit-tap-highlight-color: transparent; /* Remove tap highlight on mobile */
}

@media (max-width: 767px) {
  .letter-card {
    padding: 0.875rem 1rem; /* Slightly smaller padding */
  }
  
  .letter-card:active {
    transform: scale(0.98); /* Slight press effect on mobile */
  }
}

/* Hover effect - card becomes more prominent */
.letter-card:hover {
  border-color: var(--accent);
  box-shadow: 0 4px 12px var(--shadow); /* Stronger shadow */
}

/* Selected state - highlighted card */
.letter-card.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft); /* Accent-colored border */
  background: var(--accent-soft); /* Light background highlight */
}

.letter-card .letter-card-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.letter-card .letter-info {
  cursor: pointer;
  flex: 1;
  min-width: 0;
}

.letter-card a.letter-open {
  text-decoration: none;
  color: inherit;
}

.letter-info {
  flex: 1;
  min-width: 0;
}

.letter-id {
  font-size: 0.75rem;
  color: var(--ink-muted);
  margin: 0 0 0.2rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.letter-sender {
  font-family: "Cormorant Garamond", Georgia, serif;
  font-size: 1.2rem;
  font-weight: 600;
  margin: 0 0 0.25rem;
}

.letter-recipient {
  font-size: 0.9rem;
  color: var(--ink-muted);
  margin: 0 0 0.25rem;
}

.letter-date {
  font-size: 0.9rem;
  color: var(--ink-muted);
  margin: 0;
}

.letter-preview {
  font-size: 0.85rem;
  color: var(--ink-muted);
  margin: 0.5rem 0 0;
  line-height: 1.4;
}

.letter-keywords {
  font-size: 0.85rem;
  color: var(--ink-muted);
  margin: 0.35rem 0 0;
  font-style: italic;
}

.letter-open {
  flex-shrink: 0;
  padding: 0.4rem 0.9rem;
  background: var(--accent);
  color: var(--paper) !important;
  border-radius: 6px;
  font-size: 0.9rem;
  font-weight: 500;
  /* Better touch targets */
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
}

@media (max-width: 767px) {
  .letter-open {
    padding: 0.5rem 1rem;
    font-size: 0.95rem;
    min-height: 44px; /* Larger touch target */
  }
}

.letter-card:hover .letter-open {
  background: #7a5d12;
}

.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  background: var(--paper);
  border: 1px dashed var(--border);
  border-radius: 8px;
  color: var(--ink-muted);
}

@media (max-width: 767px) {
  .empty-state {
    padding: 2rem 1rem; /* Less padding on mobile */
  }
}

/* ============================================================================
   UTILITY CLASSES
   ============================================================================
*/

.hidden {
  display: none !important;
}

/* ============================================================================
   PASSWORD GATE
   ============================================================================
*/
.password-gate {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
}

.password-gate-card {
  background: var(--paper);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2rem;
  width: 100%;
  max-width: 360px;
  box-shadow: 0 4px 20px var(--shadow);
}

.password-gate-card h2 {
  font-family: "Cormorant Garamond", Georgia, serif;
  font-size: 1.75rem;
  font-weight: 600;
  margin: 0 0 0.5rem;
}

.password-gate-hint {
  font-size: 0.95rem;
  color: var(--ink-muted);
  margin: 0 0 1.25rem;
}

.password-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.password-form input {
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-family: inherit;
  font-size: 1rem;
  background: var(--paper);
  color: var(--ink);
}

.password-form input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

.password-form button {
  padding: 0.6rem 1rem;
  background: var(--accent);
  color: var(--paper);
  border: none;
  border-radius: 6px;
  font-family: inherit;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
}

.password-form button:hover {
  background: #7a5d12;
}

.password-error {
  margin: 1rem 0 0;
  font-size: 0.9rem;
  color: #b91c1c;
}

.password-error.hidden {
  display: none !important;
}

@media (max-width: 480px) {
  .password-gate {
    padding: 1rem;
  }
  .password-gate-card {
    padding: 1.5rem;
    max-width: 100%;
  }
  .password-gate-card h2 {
    font-size: 1.5rem;
  }
}

.empty-state.hidden {
  display: none;
}

/* ============================================================================
   READER PANEL (Right Column)
   ============================================================================
   Displays the full content of the selected letter.
   Has independent vertical scrolling for the letter body.
*/
.reader-panel {
  grid-area: reader; /* Place in "reader" grid area */
  background: var(--paper);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1.5rem;
  overflow: hidden; /* Hide overflow at panel level */
  display: flex;
  flex-direction: column; /* Stack content vertically */
  min-height: 0; /* Allow shrinking in grid */
  box-shadow: 0 1px 3px var(--shadow);
}

.reader-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
  min-height: 200px;
  color: var(--ink-muted);
  text-align: center;
  padding: 2rem;
}

.reader-placeholder.hidden {
  display: none !important;
}

/* Reader panel text: easy-to-read cursive + navy blue */
.reader-content {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  font-family: "Cormorant Garamond", Georgia, serif;
  color: #0d2137; /* Navy blue - main text */
}

.reader-content .reader-meta {
  flex-shrink: 0;
  margin-bottom: 1rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}

.reader-id {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #1e3a5f; /* Slightly lighter navy for metadata */
  margin: 0 0 0.25rem;
}

.reader-sender {
  font-family: "Cormorant Garamond", Georgia, serif;
  font-size: 1.35rem;
  font-weight: 600;
  margin: 0 0 0.2rem;
  color: #0d2137;
}

.reader-recipient {
  font-size: 1.05rem;
  color: #1e3a5f;
  margin: 0 0 0.5rem;
}

.reader-date {
  font-size: 1rem;
  color: #1e3a5f;
  margin: 0;
}

.reader-pdf-link {
  flex-shrink: 0;
  display: inline-block;
  margin-bottom: 1rem;
  padding: 0.4rem 0.9rem;
  background: var(--accent);
  color: var(--paper) !important;
  border-radius: 6px;
  font-size: 0.9rem;
  font-weight: 500;
  text-decoration: none;
  width: fit-content;
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
}

@media (max-width: 767px) {
  .reader-pdf-link {
    width: 100%; /* Full width button on mobile */
    padding: 0.6rem 1rem;
    min-height: 44px;
  }
}

.reader-pdf-link:hover {
  background: #7a5d12;
}

/* Letter body text - scrollable area, cursive + navy */
.reader-body {
  flex: 1; /* Take up remaining space in reader panel */
  min-height: 0; /* Allow shrinking */
  overflow-y: auto; /* Enable vertical scrolling for long letters */
  overflow-x: hidden; /* Prevent horizontal scrolling */
  font-family: "Cormorant Garamond", Georgia, serif;
  font-size: 1.1rem;
  line-height: 1.65;
  color: #0d2137; /* Navy blue */
  white-space: pre-wrap; /* Preserve line breaks and spaces from original text */
  word-wrap: break-word; /* Break long words if needed */
}

.empty-state p {
  margin: 0 0 1rem;
}
