/* ==========================================================================
   RED DEAD EDITOR — Style Sheet
   A weathered journal from the American frontier, circa 1899.
   ========================================================================== */

/* --------------------------------------------------------------------------
   1. CUSTOM FONTS
   -------------------------------------------------------------------------- */

@font-face {
  font-family: 'RedDead';
  src: url('assets/red.woff') format('woff');
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'RedDeadText';
  src: url('assets/redT.woff') format('woff');
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

/* --------------------------------------------------------------------------
   2. CSS CUSTOM PROPERTIES — The palette of the Old West
   -------------------------------------------------------------------------- */

:root {
  /* === Core Colors === */
  --bg-deep:         #1a1210;
  --bg-deep-rgb:     26, 18, 16;

  --parchment:       #d4c4a0;
  --parchment-dark:  #b8a67e;
  --parchment-light: #e6dcc4;

  --red-deep:        #8b1a1a;
  --red-bright:      #c62828;
  --red-glow:        #e53935;

  --gold:            #c7a44a;
  --gold-light:      #e8d48b;
  --gold-dim:        #a68a3a;

  --ink:             #2c1810;
  --ink-light:       #4a3728;

  --text-light:      #e8dcc8;
  --text-muted:      #9a8b78;

  /* === Error Type Colors === */
  --err-grammar:     #c62828;
  --err-grammar-bg:  rgba(198, 40, 40, 0.18);
  --err-spelling:    #e65100;
  --err-spelling-bg: rgba(230, 81, 0, 0.18);
  --err-style:       #c7a44a;
  --err-style-bg:    rgba(199, 164, 74, 0.18);
  --err-vocabulary:  #1565c0;
  --err-vocab-bg:    rgba(21, 101, 192, 0.18);

  /* === Spacing === */
  --space-xs:  4px;
  --space-sm:  8px;
  --space-md:  16px;
  --space-lg:  24px;
  --space-xl:  32px;
  --space-2xl: 48px;

  /* === Sizing === */
  --header-height: 90px;
  --sidebar-width-left:  280px;
  --sidebar-width-right: 408px;
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-xl: 16px;

  /* === Editor Lines Alignment === */
  --editor-line-height: 38px;
  --editor-line-offset: 18px;

  /* === Transitions === */
  --transition-fast:   0.15s ease;
  --transition-base:   0.25s ease;
  --transition-slow:   0.4s ease;
  --transition-drawer: 0.35s cubic-bezier(0.4, 0, 0.2, 1);

  /* === Shadows === */
  --shadow-sm:    0 1px 3px rgba(0,0,0,0.4);
  --shadow-md:    0 4px 12px rgba(0,0,0,0.5);
  --shadow-lg:    0 8px 30px rgba(0,0,0,0.6);
  --shadow-glow:  0 0 20px rgba(199,164,74,0.15);
  --shadow-inset: inset 0 2px 8px rgba(0,0,0,0.3);
}

/* --------------------------------------------------------------------------
   3. RESET & BASE STYLES
   -------------------------------------------------------------------------- */

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  font-family: 'Lora', 'Georgia', 'Times New Roman', serif;
  font-size: 16px;
  line-height: 1.6;
  color: var(--text-light);
  overflow: hidden;           /* Prevent body scroll; layout handles it */
  height: 100vh;
  height: 100dvh;
  width: 100vw;
}

/* Selection color: gold on deep brown */
::selection {
  background: var(--gold);
  color: var(--ink);
}

::-moz-selection {
  background: var(--gold);
  color: var(--ink);
}

/* Remove tap highlight on mobile */
* {
  -webkit-tap-highlight-color: transparent;
}

img, svg {
  display: inline-block;
  vertical-align: middle;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

a {
  color: var(--gold);
  text-decoration: none;
}

/* --------------------------------------------------------------------------
   4. FULL-VIEWPORT BACKGROUND
   Distressed dark texture covering the entire page
   -------------------------------------------------------------------------- */

body::before {
  content: '';
  position: fixed;
  inset: 0;
  z-index: -2;
  background:
    /* Dark overlay gradient for extra depth - opacity lowered to reveal the background image */
    linear-gradient(
      180deg,
      rgba(var(--bg-deep-rgb), 0.6) 0%,
      rgba(var(--bg-deep-rgb), 0.45) 50%,
      rgba(var(--bg-deep-rgb), 0.65) 100%
    ),
    url('assets/background.jpg') center / cover no-repeat fixed;
  background-color: var(--bg-deep);
}

/* Subtle vignette overlay */
body::after {
  content: '';
  position: fixed;
  inset: 0;
  z-index: -1;
  background: radial-gradient(
    ellipse at center,
    transparent 50%,
    rgba(0, 0, 0, 0.4) 100%
  );
  pointer-events: none;
}

/* --------------------------------------------------------------------------
   5. APP HEADER
   -------------------------------------------------------------------------- */

.app-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  height: var(--header-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-lg);
  background: rgba(var(--bg-deep-rgb), 0.88);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid rgba(199, 164, 74, 0.12);
}

/* Textured red stripe beneath header */
.app-header::after {
  content: '';
  position: absolute;
  bottom: -4px;
  left: 0;
  right: 0;
  height: 4px;
  background: url('assets/texturedRed.png') repeat-x center / auto 100%;
  opacity: 0.7;
}

.header-brand {
  text-align: center;
  flex: 1;
}

.header-title {
  font-family: 'RedDead', 'Georgia', serif;
  font-size: clamp(1.4rem, 3.2vw, 2.1rem);
  font-weight: normal;
  color: var(--gold);
  letter-spacing: 0.12em;
  text-shadow:
    0 0 30px rgba(199, 164, 74, 0.3),
    0 2px 4px rgba(0, 0, 0, 0.6);
  line-height: 1.2;
}

.header-subtitle {
  font-family: 'RedDeadText', 'Lora', serif;
  font-size: clamp(0.7rem, 1.6vw, 0.85rem);
  color: var(--text-muted);
  letter-spacing: 0.25em;
  text-transform: uppercase;
  opacity: 0.7;
  margin-top: 3px;
}

/* Gallery link button in Header */
.gallery-header-btn {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: 8px 16px;
  background: rgba(199, 164, 74, 0.08);
  border: 1px solid rgba(199, 164, 74, 0.2);
  border-radius: var(--radius-md);
  color: var(--gold);
  text-decoration: none;
  font-family: 'RedDeadText', 'Lora', serif;
  font-size: 0.85rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  transition: all var(--transition-fast);
  cursor: pointer;
  z-index: 102;
}

.gallery-header-btn:hover {
  background: rgba(199, 164, 74, 0.18);
  border-color: var(--gold);
  color: var(--gold-light);
  box-shadow: 0 0 12px rgba(199, 164, 74, 0.2);
}

.gallery-btn-icon {
  transition: transform var(--transition-fast);
}

.gallery-header-btn:hover .gallery-btn-icon {
  transform: scale(1.1);
}

@media (max-width: 768px) {
  .gallery-header-btn {
    position: absolute;
    right: 70px;
    padding: var(--space-sm);
  }
  .gallery-header-btn span {
    display: none;
  }
}

/* Sidebar toggle buttons — hidden on desktop, shown on mobile */
.sidebar-toggle {
  display: none;  /* Hidden on desktop */
  width: 44px;
  height: 44px;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
  color: var(--text-muted);
  transition: color var(--transition-fast), background var(--transition-fast);
}

.sidebar-toggle:hover {
  color: var(--gold);
  background: rgba(199, 164, 74, 0.1);
}

/* --------------------------------------------------------------------------
   5b. MARY-BETH AVATAR
   Sits on the right side of the header, overflowing into the main body.
   -------------------------------------------------------------------------- */

.marybeth-avatar {
  position: absolute;
  right: 36px;
  bottom: 8px; /* Sits cleanly inside the header, above the red line */
  z-index: 101; /* Above the header and its ::after stripe */
  width: 60px;   /* Scaled down to fit within a 90px header height */
  height: 80px;
  pointer-events: none;
  filter: drop-shadow(0 3px 8px rgba(0, 0, 0, 0.4))
          drop-shadow(0 0 15px rgba(199, 164, 74, 0.1));
  transition: filter var(--transition-base);
}

.marybeth-sprite {
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: bottom center;
  animation: marybeth-breathe 4s ease-in-out infinite;
  transition: opacity 0.3s ease;
}

/* Gentle breathing / idle bob */
@keyframes marybeth-breathe {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-3px); }
}

/* Pose transition: brief fade */
.marybeth-sprite.pose-swap {
  opacity: 0;
  transform: translateY(2px);
}

/* Active action pose — pause the breathing, subtle glow */
.marybeth-avatar.pose-active .marybeth-sprite {
  animation: none;
}

.marybeth-avatar.pose-active {
  filter: drop-shadow(0 4px 16px rgba(0, 0, 0, 0.6))
          drop-shadow(0 0 24px rgba(199, 164, 74, 0.25));
}

/* --------------------------------------------------------------------------
   6. APP LAYOUT — Three-column CSS Grid
   -------------------------------------------------------------------------- */

.app-layout {
  display: grid;
  grid-template-columns: var(--sidebar-width-left) 1fr var(--sidebar-width-right);
  height: calc(100vh - var(--header-height));
  height: calc(100dvh - var(--header-height));
  margin-top: var(--header-height);
  overflow: hidden;
}

/* --------------------------------------------------------------------------
   7. SIDEBARS — Shared Styles
   -------------------------------------------------------------------------- */

.sidebar {
  display: flex;
  flex-direction: column;
  background: rgba(var(--bg-deep-rgb), 0.65);
  border-right: 1px solid rgba(199, 164, 74, 0.08);
  overflow: hidden;
}

.corrections-sidebar {
  border-right: none;
  border-left: 1px solid rgba(199, 164, 74, 0.08);
}

.sidebar-header {
  padding: var(--space-lg);
  flex-shrink: 0;
}

.sidebar-title {
  font-family: 'RedDeadText', 'Lora', serif;
  font-size: 1rem;
  font-weight: normal;
  color: var(--gold);
  letter-spacing: 0.08em;
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding-bottom: var(--space-md);
  /* Textured yellow underline */
  border-bottom: 3px solid transparent;
  background-image: url('assets/texturedYellow.png');
  background-repeat: no-repeat;
  background-position: bottom left;
  background-size: 100% 3px;
  /* Clip so image only appears on the border area */
}

/* Alternative: use pseudo-element for the underline */
.sidebar-title::after {
  content: none; /* Disabled, using background-image above */
}

.sidebar-icon {
  color: var(--gold-dim);
  flex-shrink: 0;
}

.sidebar-footer {
  padding: var(--space-md) var(--space-lg);
  flex-shrink: 0;
  border-top: 1px solid rgba(199, 164, 74, 0.08);
}

/* --------------------------------------------------------------------------
   8. HISTORY LIST & ITEMS
   -------------------------------------------------------------------------- */

.history-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 var(--space-md);
}

.history-item {
  position: relative;
  padding: var(--space-md);
  margin-bottom: var(--space-sm);
  background: rgba(212, 196, 160, 0.06);
  border: 1px solid rgba(212, 196, 160, 0.08);
  border-left: 3px solid transparent;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition:
    background var(--transition-fast),
    border-color var(--transition-fast),
    box-shadow var(--transition-fast),
    transform var(--transition-fast);
}

.history-item:hover {
  background: rgba(212, 196, 160, 0.1);
  border-left-color: var(--red-bright);
  box-shadow: var(--shadow-sm);
  transform: translateY(-1px);
}

.history-item.active {
  background: rgba(199, 164, 74, 0.08);
  border-left-color: var(--gold);
  border-color: rgba(199, 164, 74, 0.2);
}

.history-item-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-sm);
}

.history-item-title {
  font-family: 'RedDeadText', 'Lora', serif;
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--parchment);
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

.history-item-delete {
  opacity: 0;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 1.1rem;
  background: transparent;
  border: none;
  transition: opacity var(--transition-fast), color var(--transition-fast), background var(--transition-fast);
  flex-shrink: 0;
  cursor: pointer;
  padding: 0;
}

.history-item:hover .history-item-delete {
  opacity: 1;
}

.history-item-delete:hover {
  color: var(--red-bright);
  background: rgba(198, 40, 40, 0.15);
}

.history-item-date {
  font-size: 0.72rem;
  color: var(--text-muted);
  letter-spacing: 0.04em;
  margin-top: var(--space-xs);
  display: block;
}

.history-item-preview {
  font-size: 0.78rem;
  color: var(--text-muted);
  opacity: 0.7;
  margin-top: var(--space-xs);
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* New Entry button */
.btn-new-entry {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-sm);
  padding: var(--space-md);
  font-family: 'RedDeadText', 'Lora', serif;
  font-size: 0.85rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--gold);
  background: rgba(199, 164, 74, 0.08);
  border: 1px dashed rgba(199, 164, 74, 0.25);
  border-radius: var(--radius-md);
  transition: background var(--transition-fast), border-color var(--transition-fast);
}

.btn-new-entry:hover {
  background: rgba(199, 164, 74, 0.15);
  border-color: var(--gold);
}

/* --------------------------------------------------------------------------
   9. JOURNAL AREA — The writing surface
   -------------------------------------------------------------------------- */

.journal-area {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: var(--space-md);
  gap: var(--space-sm);
}

/* --- Header with counts --- */
.journal-header {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: var(--space-xs) var(--space-sm);
  flex-shrink: 0;
}

.journal-counts {
  display: flex;
  gap: var(--space-md);
}

.count-badge {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 0.75rem;
  color: var(--text-muted);
  letter-spacing: 0.04em;
}

.count-badge svg {
  opacity: 0.6;
}

.count-value {
  font-weight: 600;
  color: var(--parchment-dark);
}

/* --- Journal container: the transparent writing surface --- */
.journal-container {
  position: relative;
  flex: 1;
  overflow: hidden;
  border-radius: var(--radius-lg);
  box-shadow:
    var(--shadow-lg),
    var(--shadow-inset),
    0 0 0 1px rgba(199, 164, 74, 0.15);

  /* Semi-transparent dark slate background to allow background.jpg to show through */
  background-color: rgba(26, 18, 16, 0.65);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  background-image:
    /* Faint ruled lines in gold/white for dark mode */
    repeating-linear-gradient(
      to bottom,
      transparent,
      transparent calc(var(--editor-line-height) - 1px),
      rgba(199, 164, 74, 0.08) calc(var(--editor-line-height) - 1px),
      rgba(199, 164, 74, 0.08) var(--editor-line-height)
    ),
    /* Subtle grain noise (CSS only) */
    url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.04'/%3E%3C/svg%3E");
  background-position: 0 var(--editor-line-offset), 0 0;
}

/* Aged edge effect on the writing surface */
.journal-container::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: var(--radius-lg);
  box-shadow:
    inset 0 0 60px rgba(0, 0, 0, 0.45),
    inset 0 0 120px rgba(0, 0, 0, 0.3);
  pointer-events: none;
  z-index: 3;
}

/* --- Shared text styling for overlay sync --- */
.highlight-layer,
.editor-textarea {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  font-family: 'RedDead', 'Georgia', serif;
  font-size: 1.3em; /* Increased textarea and highlight-layer font size to 1.3em */
  line-height: var(--editor-line-height);
  padding: var(--space-lg);
  padding-top: 28px; /* align with ruled lines */
  word-wrap: break-word;
  overflow-wrap: break-word;
  white-space: pre-wrap;
  letter-spacing: 0.01em;
  border: none;
  overflow-y: auto;
}

/* Highlight layer: text is invisible, only marks show */
.highlight-layer {
  color: transparent;
  pointer-events: none;
  z-index: 4; /* Overlay on top of textarea to capture events on children */
  user-select: none;
  -webkit-user-select: none;
}

.highlight-layer mark.mark-applied-fix {
  color: transparent;
  background-color: transparent !important;
  border-bottom: 2px solid var(--gold); /* solid golden line */
  border-radius: 0;
  padding: 0;
  position: relative;
  pointer-events: auto !important; /* capture pointer events */
  cursor: pointer;
  transition: background-color var(--transition-fast), border-color var(--transition-fast);
}

.highlight-layer mark.mark-applied-fix:hover {
  background-color: rgba(199, 164, 74, 0.18) !important; /* subtle golden background on hover */
  border-bottom-color: var(--gold-light);
}

/* Textarea: transparent background so highlight shows through */
.editor-textarea {
  background: transparent;
  color: #f5f5f0; /* Chalk white */
  caret-color: var(--gold);
  resize: none;
  outline: none;
  z-index: 2;
  /* Match the highlight layer exactly */
}

.editor-textarea:read-only {
  caret-color: transparent;
  color: #d1d1ca; /* slightly dimmer chalk white */
}

.editor-textarea::placeholder {
  color: var(--parchment-dark);
  opacity: 0.6;
  font-style: italic;
}

/* --- Error highlight marks --- */
.highlight-layer mark {
  color: transparent;
  background-color: transparent !important; /* Remove solid yellow background */
  border-bottom: 2px dashed var(--red-glow);
  border-radius: 2px;
  padding: 0;
  position: relative;
  transition: background-color var(--transition-fast);
  pointer-events: auto; /* Allow clicks/taps on mobile viewports */
}

.highlight-layer mark.mark-hover {
  background-color: rgba(229, 57, 53, 0.25) !important; /* Semi-transparent dark red background on hover */
}

.mark-grammar,
.mark-spelling,
.mark-style,
.mark-vocabulary {
  background: transparent;
}

/* --- Loading overlay --- */
.loading-overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: none;  /* Hidden by default */
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-lg);
  background: rgba(var(--bg-deep-rgb), 0.82);
  backdrop-filter: blur(4px);
  border-radius: var(--radius-lg);
  animation: fadeIn 0.3s ease;
}

.loading-overlay.visible {
  display: flex;
}

.loading-overlay .marybeth-avatar {
  position: relative;
  right: auto;
  bottom: auto;
  z-index: 2;
  width: 140px;  /* fit size - appropriate size to display Mary-Beth inside the text area overlay */
  height: 187px; /* 3:4 aspect ratio */
  display: block !important; /* Ensure it is always shown even if global media queries try to hide it */
  margin-bottom: var(--space-xs);
  pointer-events: none;
  opacity: 0.94; /* Deeper integration with background texture */
}

.loading-overlay .marybeth-sprite {
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: bottom center;
  filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.5))
          sepia(25%) contrast(1.05) brightness(0.88);
  transition: opacity 0.3s ease;
}

/* Custom dynamic waiting/fidgeting state */
.loading-overlay .marybeth-avatar.pose-active .marybeth-sprite {
  animation: marybeth-waiting-fidget 6s ease-in-out infinite;
}

@keyframes marybeth-waiting-fidget {
  0%, 100% {
    transform: rotate(0deg) translateY(0) scale(1);
    filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.5))
            sepia(25%) contrast(1.05) brightness(0.88);
  }
  20% {
    transform: rotate(-1.5deg) translateY(-2px) translateX(-0.5px) scale(0.995);
    filter: drop-shadow(0 6px 15px rgba(0, 0, 0, 0.55))
            sepia(28%) contrast(1.08) brightness(0.92);
  }
  40% {
    transform: rotate(1deg) translateY(-4px) translateX(0.5px) scale(1);
    filter: drop-shadow(0 5px 14px rgba(0, 0, 0, 0.5))
            sepia(25%) contrast(1.05) brightness(0.90);
  }
  60% {
    transform: rotate(-0.5deg) translateY(-1px) translateX(1px) scale(0.995);
    filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.5))
            sepia(28%) contrast(1.08) brightness(0.88);
  }
  80% {
    transform: rotate(1.5deg) translateY(-3px) translateX(-0.5px) scale(1);
    filter: drop-shadow(0 6px 16px rgba(0, 0, 0, 0.55))
            sepia(25%) contrast(1.05) brightness(0.92);
  }
}

.loading-text {
  font-family: 'RedDeadText', 'Lora', serif;
  font-size: 0.95rem;
  color: var(--gold-light);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  text-align: center;
  padding: 0 var(--space-lg);
  animation: pulse 2s ease-in-out infinite;
}

/* --------------------------------------------------------------------------
   10. ACTION BUTTONS — Weathered toolbar at the foot of the journal
   -------------------------------------------------------------------------- */

.journal-footer {
  display: flex;
  flex-shrink: 0;
  background: rgba(var(--bg-deep-rgb), 0.72);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: var(--radius-lg);
  border: 1px solid rgba(199, 164, 74, 0.1);
  box-shadow:
    var(--shadow-md),
    inset 0 1px 0 rgba(255, 255, 255, 0.04);
  padding: 4px;
  gap: 4px;
  position: relative;
}

/* Faint ruled separator between buttons */
.journal-footer .btn + .btn::before {
  content: '';
  position: absolute;
  left: -2px;
  top: 20%;
  bottom: 20%;
  width: 1px;
  background: linear-gradient(
    to bottom,
    transparent,
    rgba(199, 164, 74, 0.22),
    transparent
  );
  pointer-events: none;
}

/* --- Base button --- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-sm);
  padding: var(--space-md) var(--space-lg);
  font-family: 'RedDeadText', 'Lora', serif;
  font-size: 0.78rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-muted);
  flex: 1;
  min-width: 0;
  position: relative;
  overflow: hidden;
  white-space: nowrap;
  cursor: pointer;
  transition:
    color var(--transition-fast),
    background var(--transition-fast),
    border-color var(--transition-fast),
    box-shadow var(--transition-fast),
    transform var(--transition-fast);
}

.btn:hover:not(:disabled) {
  color: var(--text-light);
  background: rgba(199, 164, 74, 0.08);
  border-color: rgba(199, 164, 74, 0.15);
}

.btn:active:not(:disabled) {
  transform: scale(0.97);
}

.btn:disabled {
  opacity: 0.4;
  pointer-events: none;
  cursor: not-allowed;
}

/* --- Button icon --- */
.btn-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0; /* collapse emoji font-size fallback */
  line-height: 1;
  opacity: 0.7;
  transition: opacity var(--transition-fast);
}

.btn:hover .btn-icon {
  opacity: 1;
}

.btn-icon svg {
  width: 15px;
  height: 15px;
  stroke-width: 2;
}

/* --- Evaluate — crimson accent --- */
.btn-evaluate {
  color: var(--red-glow);
}

.btn-evaluate:hover:not(:disabled) {
  color: #fff;
  background: var(--red-deep);
  border-color: var(--red-bright);
  box-shadow: 0 0 18px rgba(229, 57, 53, 0.2);
}

.btn-evaluate .btn-icon svg { stroke: currentColor; }

/* --- Rewrite — gold accent --- */
.btn-rewrite {
  color: var(--gold);
}

.btn-rewrite:hover:not(:disabled) {
  color: var(--ink);
  background: var(--gold);
  border-color: var(--gold-light);
  box-shadow: 0 0 18px rgba(199, 164, 74, 0.25);
}

.btn-rewrite .btn-icon svg { stroke: currentColor; }

/* --- Hone — muted gold outline accent --- */
.btn-hone {
  color: var(--gold-dim);
}

.btn-hone:hover:not(:disabled) {
  color: var(--gold-light);
  background: rgba(199, 164, 74, 0.1);
  border-color: var(--gold-dim);
  box-shadow: 0 0 14px rgba(199, 164, 74, 0.12);
}

.btn-hone .btn-icon svg { stroke: currentColor; }

/* --- Save — parchment accent --- */
.btn-save {
  color: var(--parchment-dark);
}

.btn-save:hover:not(:disabled) {
  color: var(--ink);
  background: var(--parchment);
  border-color: var(--parchment-dark);
  box-shadow: 0 0 14px rgba(212, 196, 160, 0.15);
}

.btn-save .btn-icon svg { stroke: currentColor; }

/* --- Loading state --- */
.btn--loading {
  pointer-events: none;
}

.btn--loading::after {
  content: '';
  position: absolute;
  width: 18px;
  height: 18px;
  border: 2px solid transparent;
  border-top-color: currentColor;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

/* --------------------------------------------------------------------------
   11. CORRECTIONS SIDEBAR — Score, Cards, TTS
   -------------------------------------------------------------------------- */

.corrections-sidebar {
  overflow-y: auto;
}



/* --- Summary Text --- */
.summary-text {
  padding: 0 var(--space-lg) var(--space-md);
  font-size: 0.88rem;
  color: var(--text-muted);
  line-height: 1.6;
  border-bottom: 1px solid rgba(199, 164, 74, 0.08);
}

.summary-text strong {
  color: var(--parchment);
}

/* --- Corrections List --- */
.corrections-list {
  padding: var(--space-md) var(--space-lg);
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
}

.correction-card {
  background: rgba(var(--bg-deep-rgb), 0.6);
  border: 1px solid rgba(199, 164, 74, 0.1);
  border-radius: var(--radius-md);
  padding: var(--space-md);
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  transition:
    border-color var(--transition-fast),
    box-shadow var(--transition-fast);
}

.corrections-list.stagger-in .correction-card {
  animation: slideUpFade 0.45s cubic-bezier(0.16, 1, 0.3, 1) both;
}

.correction-card:hover {
  border-color: rgba(199, 164, 74, 0.25);
  box-shadow: var(--shadow-glow);
}

/* Error type badge */
.error-badge {
  display: inline-block;
  padding: 2px 10px;
  font-family: 'RedDeadText', 'Lora', serif;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  border-radius: 99px;
  width: fit-content;
}

.error-grammar    { background: var(--err-grammar-bg); color: var(--err-grammar); border: 1px solid var(--err-grammar); }
.error-spelling   { background: var(--err-spelling-bg); color: var(--err-spelling); border: 1px solid var(--err-spelling); }
.error-style      { background: var(--err-style-bg); color: var(--err-style); border: 1px solid var(--err-style); }
.error-vocabulary { background: var(--err-vocab-bg); color: var(--err-vocabulary); border: 1px solid var(--err-vocabulary); }

.correction-original {
  font-size: 0.88rem;
  color: #c07070;
}

.correction-original del {
  text-decoration: line-through;
  text-decoration-color: var(--red-bright);
}

.correction-suggestion {
  font-size: 0.88rem;
  color: var(--gold-light);
}

.correction-suggestion ins {
  text-decoration: none;
  background: rgba(199, 164, 74, 0.1);
  padding: 1px 4px;
  border-radius: 2px;
}

.correction-explanation {
  font-size: 0.88rem;
  color: var(--parchment-light);
  line-height: 1.6;
  font-style: normal;
  border-left: 2px solid rgba(199, 164, 74, 0.35);
  padding-left: 10px;
  margin: 4px 0;
}

.btn-apply-fix {
  align-self: flex-start;
  padding: var(--space-xs) var(--space-md);
  font-family: 'RedDeadText', 'Lora', serif;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #fff;
  background-color: var(--red-deep);
  background-image: url('assets/texturedRed.png');
  background-repeat: repeat;
  border-radius: var(--radius-sm);
  transition: background-color var(--transition-fast), box-shadow var(--transition-fast);
}

.btn-apply-fix:hover {
  background-color: var(--red-bright);
  box-shadow: 0 0 12px rgba(198, 40, 40, 0.3);
}

/* TTS player styles removed */

/* --------------------------------------------------------------------------
   13. SIDEBAR BACKDROP (Mobile)
   -------------------------------------------------------------------------- */

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 49;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(2px);
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--transition-drawer), visibility var(--transition-drawer);
}

.sidebar-backdrop.visible {
  opacity: 1;
  visibility: visible;
}

/* --------------------------------------------------------------------------
   14. TOAST NOTIFICATIONS
   -------------------------------------------------------------------------- */

.toast-container {
  position: fixed;
  bottom: var(--space-lg);
  right: var(--space-lg);
  z-index: 200;
  display: flex;
  flex-direction: column-reverse;
  gap: var(--space-sm);
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  min-width: 280px;
  max-width: 400px;
  padding: var(--space-md) var(--space-lg);
  border-radius: var(--radius-md);
  background: rgba(var(--bg-deep-rgb), 0.95);
  backdrop-filter: blur(8px);
  font-family: 'RedDeadText', 'Lora', serif;
  font-size: 0.85rem;
  color: var(--text-light);
  box-shadow: var(--shadow-lg);
  animation: toastIn 0.4s ease forwards;
  display: flex;
  align-items: center;
  gap: var(--space-md);
}

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

.toast-success {
  border-left: 3px solid var(--gold);
}

.toast-error {
  border-left: 3px solid var(--red-bright);
}

.toast-info {
  border-left: 3px solid var(--parchment-dark);
}

.toast-icon {
  font-size: 1.2rem;
  flex-shrink: 0;
}

/* --------------------------------------------------------------------------
   15. CUSTOM SCROLLBARS
   -------------------------------------------------------------------------- */

/* Webkit (Chrome, Safari, Edge) */
::-webkit-scrollbar {
  width: 7px;
  height: 7px;
}

::-webkit-scrollbar-track {
  background: rgba(var(--bg-deep-rgb), 0.3);
}

::-webkit-scrollbar-thumb {
  background: var(--parchment-dark);
  border-radius: 99px;
  border: 1px solid transparent;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--gold-dim);
}

/* Journal textarea scrollbar on parchment */
.editor-textarea::-webkit-scrollbar-track,
.highlight-layer::-webkit-scrollbar-track {
  background: rgba(44, 24, 16, 0.06);
}

.editor-textarea::-webkit-scrollbar-thumb,
.highlight-layer::-webkit-scrollbar-thumb {
  background: var(--ink-light);
  border-radius: 99px;
}

/* Firefox */
* {
  scrollbar-width: thin;
  scrollbar-color: var(--parchment-dark) rgba(var(--bg-deep-rgb), 0.3);
}

/* --------------------------------------------------------------------------
   16. KEYFRAME ANIMATIONS
   -------------------------------------------------------------------------- */

@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes slideUpFade {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeOut {
  from { opacity: 1; }
  to   { opacity: 0; }
}

@keyframes slideInLeft {
  from { transform: translateX(-100%); }
  to   { transform: translateX(0); }
}

@keyframes slideInRight {
  from { transform: translateX(100%); }
  to   { transform: translateX(0); }
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.5; }
}

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

@keyframes toastIn {
  from {
    opacity: 0;
    transform: translateX(80px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateX(0) scale(1);
  }
}

@keyframes toastOut {
  from {
    opacity: 1;
    transform: translateX(0) scale(1);
  }
  to {
    opacity: 0;
    transform: translateX(80px) scale(0.95);
  }
}

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

/* --------------------------------------------------------------------------
   17. RESPONSIVE — TABLET (≤ 1024px)
   Sidebars collapse into slide-over drawers
   -------------------------------------------------------------------------- */

@media (max-width: 1024px) {
  body {
    position: fixed;
    inset: 0;
    overflow: hidden;
  }

  .sidebar-backdrop {
    display: block;
  }

  .history-item-delete {
    opacity: 1 !important;
  }

  .sidebar-toggle {
    display: flex;   /* Show mobile toggles */
  }

  .app-layout {
    grid-template-columns: 1fr;  /* Single column */
  }

  /* --- Convert sidebars to fixed overlay drawers --- */
  .sidebar {
    position: fixed;
    top: var(--header-height);
    bottom: 0;
    z-index: 50;
    width: 300px;
    background: rgba(var(--bg-deep-rgb), 0.96);
    backdrop-filter: blur(12px);
    transition: transform var(--transition-drawer);
    box-shadow: var(--shadow-lg);
  }

  .history-sidebar {
    left: 0;
    border-right: 1px solid rgba(199, 164, 74, 0.1);
    transform: translateX(-100%);
  }

  .corrections-sidebar {
    right: 0;
    border-left: 1px solid rgba(199, 164, 74, 0.1);
    transform: translateX(100%);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  /* Journal area takes full width */
  .journal-area {
    padding: var(--space-sm);
    gap: var(--space-xs);
  }
}

/* --------------------------------------------------------------------------
   18. RESPONSIVE — PHONE (≤ 768px)
   -------------------------------------------------------------------------- */

@media (max-width: 768px) {
  :root {
    --header-height: 60px;
  }

  /* Hide Mary-Beth avatar on mobile */
  .marybeth-avatar {
    display: none;
  }

  .header-title {
    font-size: 1.2rem;
    letter-spacing: 0.08em;
  }

  .header-subtitle {
    font-size: 0.6rem;
    letter-spacing: 0.15em;
  }

  .app-header {
    padding: 0 var(--space-md);
  }

  :root {
    --editor-line-height: 29px;
    --editor-line-offset: 13px;
  }

  /* Slightly smaller editor text */
  .highlight-layer,
  .editor-textarea {
    font-size: 16px;
    padding: var(--space-md);
    padding-top: 22px;
  }

  /* Prevent stacking, keep horizontal row */
  .journal-footer {
    flex-wrap: nowrap;
  }

  .journal-footer .btn {
    flex: 1;
    min-width: 0;
    padding: var(--space-md) var(--space-sm);
    font-size: 0.75rem;
    min-height: 44px;    /* Touch target */
  }



  /* Compact correction cards */
  .correction-card {
    padding: var(--space-sm) var(--space-md);
  }

  .corrections-list {
    padding: var(--space-sm) var(--space-md);
  }

  /* Sidebar width */
  .sidebar {
    width: 280px;
  }

  /* Toast positioning */
  .toast-container {
    left: var(--space-md);
    right: var(--space-md);
    bottom: var(--space-md);
  }

  .toast {
    min-width: 0;
    max-width: none;
    width: 100%;
  }
}

/* --------------------------------------------------------------------------
   19. RESPONSIVE — SMALL PHONE (≤ 480px)
   -------------------------------------------------------------------------- */

@media (max-width: 480px) {
  :root {
    --header-height: 54px;
  }

  .marybeth-avatar {
    display: none;
  }

  .header-title {
    font-size: 1rem;
  }

  .header-subtitle {
    display: none;   /* Hide subtitle on very small screens */
  }

  /* Style buttons gracefully on tiny screens (icon on top, text below) */
  .journal-footer .btn {
    flex-direction: column;
    gap: 4px;
    padding: 6px var(--space-xs);
    min-height: 48px;
  }

  .btn-label {
    display: inline-block;
    font-size: 0.62rem;
    letter-spacing: 0.04em;
  }

  .btn-icon {
    font-size: 0;
  }

  .btn-icon svg {
    width: 16px;
    height: 16px;
  }

  .sidebar {
    width: 100%;  /* Full screen drawer */
  }

  :root {
    --editor-line-height: 27px;
    --editor-line-offset: 9px;
  }

  /* Even smaller editor text */
  .highlight-layer,
  .editor-textarea {
    font-size: 16px;
    padding: var(--space-sm) var(--space-md);
    padding-top: 18px;
  }

  .journal-header {
    padding: 0;
  }

  .count-badge {
    font-size: 0.68rem;
  }
}

/* --------------------------------------------------------------------------
   20. MOBILE POPOVER — Floating error card above text marks on tap
   -------------------------------------------------------------------------- */

.highlight-layer.has-popover,
.editor-textarea.has-popover {
  padding-bottom: 260px !important;
}

.mobile-popover {
  position: fixed;
  z-index: 10000;
  width: 290px;
  max-width: calc(100vw - 24px);
  background: rgba(30, 22, 20, 0.98);
  border: 1px solid var(--gold);
  border-radius: var(--radius-md);
  padding: var(--space-md);
  box-shadow: 
    0 10px 30px rgba(0, 0, 0, 0.7),
    var(--shadow-glow);
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  animation: popoverIn 0.25s cubic-bezier(0.16, 1, 0.3, 1) forwards;
  pointer-events: auto; /* enable interactions inside popover */
}

.mobile-popover > * {
  flex-shrink: 0;
}

@keyframes popoverIn {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.mobile-popover::after {
  content: '';
  position: absolute;
  bottom: -6px;
  left: var(--arrow-left, 50%);
  transform: translateX(-50%);
  border-width: 6px 6px 0;
  border-style: solid;
  border-color: var(--gold) transparent;
  display: block;
  width: 0;
}

.mobile-popover.popover-below::after {
  bottom: auto;
  top: -6px;
  border-width: 0 6px 6px;
}

.popover-close-btn {
  position: absolute;
  top: 8px;
  right: 12px;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  padding: 4px;
  transition: color var(--transition-fast);
}

.popover-close-btn:hover {
  color: var(--red-glow);
}

/* --------------------------------------------------------------------------
   20.1 FULL-TEXT REWRITE INTERLEAVED VIEW
   -------------------------------------------------------------------------- */

.rewrite-banner {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(26, 18, 16, 0.96);
  border-bottom: 1px solid var(--gold);
  padding: 8px 16px;
  font-family: 'RedDeadText', 'Lora', serif;
  font-size: 0.85rem;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
}

.rewrite-banner-text {
  color: var(--gold-light);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 600;
}

/* Container view overrides */
.journal-container.view-rewrite .editor-textarea {
  display: none !important;
}

.journal-container.view-rewrite .highlight-layer {
  color: var(--text-light) !important;
  pointer-events: auto !important;
  user-select: text !important;
  -webkit-user-select: text !important;
  padding-top: 28px !important; /* Align to the first ruled line (Ruled Line 1) under the banner on desktop */
}

@media (max-width: 1024px) {
  .journal-container.view-rewrite .highlight-layer {
    padding-top: calc(22px + var(--editor-line-height)) !important; /* Keep offset on mobile/tablet to avoid overlapping banner */
  }
}

@media (max-width: 480px) {
  .journal-container.view-rewrite .highlight-layer {
    padding-top: calc(18px + var(--editor-line-height)) !important; /* Keep offset on mobile/tablet to avoid overlapping banner */
  }
}

.journal-container.view-rewrite .highlight-layer mark {
  color: inherit !important;
}

/* Interleaved elements styling */
.interleaved-block {
  display: flex;
  flex-direction: column;
  gap: 0 !important; /* No gap to align with vertical grid lines */
  margin-bottom: 0 !important;
  padding: 0 !important;
  border-radius: var(--radius-md);
  transition: background var(--transition-fast);
}

.interleaved-original {
  font-size: 1.05em;
  line-height: var(--editor-line-height) !important;
  padding: 0 var(--space-sm) !important;
  color: var(--text-light);
}

.interleaved-rewritten {
  font-size: 0.98em;
  line-height: var(--editor-line-height) !important;
  color: var(--gold-light);
  font-style: italic;
  padding: 0 14px !important; /* Keep horizontal padding, remove vertical padding */
  background: rgba(199, 164, 74, 0.05);
  border-left: 3px solid var(--gold-dim);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  cursor: pointer;
  outline: none;
  transition: all var(--transition-fast);
  margin: 0 !important; /* Remove margins to maintain grid alignment */
}

.interleaved-rewritten:hover,
.interleaved-rewritten:focus {
  background: rgba(199, 164, 74, 0.1);
  border-left-color: var(--gold);
}

.interleaved-block.active-block .interleaved-rewritten {
  background: rgba(199, 164, 74, 0.16);
  border-left-color: var(--gold);
  box-shadow: 0 0 12px rgba(199, 164, 74, 0.1);
}

.interleaved-separator {
  padding: 0 var(--space-sm) !important;
  margin: 0 !important;
  line-height: var(--editor-line-height) !important;
  opacity: 0.85;
  white-space: pre-wrap;
}

/* --- TTS Visual Feedback --- */
.interleaved-rewritten.audio-loading {
  background: rgba(199, 164, 74, 0.12) !important;
  border-left-color: var(--gold) !important;
  cursor: wait;
}
.interleaved-rewritten.audio-loading::after {
  content: " ⌛";
  font-style: normal;
  display: inline-block;
  margin-left: 6px;
  animation: spinner-pulse 1.2s infinite;
}

.interleaved-rewritten.audio-playing {
  background: rgba(199, 164, 74, 0.22) !important;
  border-left-color: var(--gold) !important;
  box-shadow: 0 0 12px rgba(199, 164, 74, 0.25);
}
.interleaved-rewritten.audio-playing::after {
  content: " 🔊";
  font-style: normal;
  display: inline-block;
  margin-left: 6px;
  animation: audio-pulse 1.2s ease-in-out infinite alternate;
}

@keyframes spinner-pulse {
  0% { opacity: 0.3; }
  50% { opacity: 1; }
  100% { opacity: 0.3; }
}

@keyframes audio-pulse {
  0% { transform: scale(0.9); opacity: 0.7; }
  100% { transform: scale(1.1); opacity: 1; }
}

/* --- Hold to Fulfill Progress Animation --- */
.btn-hold-progress {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: currentColor;
  opacity: 0.18;
  pointer-events: none;
  transform: scaleX(var(--hold-progress, 0));
  transform-origin: left;
  transition: transform 0.05s linear;
  z-index: 1;
}

/* Ensure button content stays above progress overlay */
.btn-icon,
.btn-label {
  position: relative;
  z-index: 2;
}

/* Button scaling and active borders during active hold */
.btn.btn--holding {
  transform: scale(0.97);
  border-color: currentColor;
  box-shadow: 0 0 15px rgba(199, 164, 74, 0.2);
}

/* Pulse/flash effect on successful hold completion */
.btn.btn--fulfilled {
  animation: btn-flash 0.3s ease-out;
}

@keyframes btn-flash {
  0% {
    background: currentColor;
    color: var(--ink, #2c1810);
    box-shadow: 0 0 25px currentColor;
  }
  100% {
    background: transparent;
  }
}

/* --------------------------------------------------------------------------
   21. PRINT STYLES
   -------------------------------------------------------------------------- */

@media print {
  .app-header,
  .sidebar,
  .journal-header,
  .journal-footer,
  .loading-overlay,
  .sidebar-backdrop,
  .toast-container,
  .sidebar-toggle {
    display: none !important;
  }

  body {
    overflow: visible;
    height: auto;
    background: #fff;
    color: #000;
  }

  body::before,
  body::after {
    display: none;
  }

  .app-layout {
    display: block;
    height: auto;
    margin-top: 0;
  }

  .journal-area {
    padding: 0;
  }

  .journal-container {
    box-shadow: none;
    border-radius: 0;
    background: #fff;
    position: static;
    height: auto;
  }

  .editor-textarea {
    position: static;
    height: auto;
    color: #000;
    font-size: 12pt;
    padding: 0;
  }

  .highlight-layer {
    display: none;
  }
}
