/*
 * Custom CSS for Household Table
 *
 * Design principles: Calm, warm, domestic, tactile.
 * Typography: Georgia for serif headers/cards (warm/domestic), system-ui for modern controls.
 */

:root {
  --color-bg: #f5f4f0;       /* Warm linen desktop */
  --color-card: #ffffff;     /* Physical index card */
  --color-text: #2c2b29;     /* Soft dark ink */
  --color-muted: #73706b;    /* Muted ink */
  --color-border: #e0ded9;   /* Soft paper creases */
  --color-accent: #5c6f59;   /* Sage green */
  --color-accent-light: #f0f3ef;
  --color-nope: #b25353;     /* Muted clay red */
  --color-discuss: #d1973f;  /* Warm mustard */
  --font-serif: "Georgia", "Times New Roman", serif;
  --font-sans: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --shadow-card: 0 4px 10px rgba(0, 0, 0, 0.05), 0 1px 3px rgba(0, 0, 0, 0.08);
  --shadow-hover: 0 10px 25px rgba(0, 0, 0, 0.08), 0 3px 6px rgba(0, 0, 0, 0.12);
  --radius-card: 4px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
  background-color: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-sans);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--color-accent);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}

/* App Layout */
.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.4rem 1.5rem; /* Tightened from 1rem 2rem to maximize desktop vertical space */
  border-bottom: 1px solid var(--color-border);
  background-color: rgba(255, 255, 255, 0.5);
  backdrop-filter: blur(10px);
  position: relative; /* Establish relative positioning context */
  z-index: 1000; /* Force the header and its dropdowns to sit above all tabletop contents */
}

.logo-group {
  display: flex;
  align-items: center; /* Horizontally aligns title and presence indicator perfectly on the baseline! */
  flex-wrap: wrap; /* Safe wrapping for ultra-small screens */
  gap: 0.35rem;
}

.logo-group h1 {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 1.25rem; /* Tighter font size */
  font-weight: normal;
  font-style: italic;
  color: var(--color-accent);
}

.logo-group p {
  margin: 0;
  font-size: 0.85rem;
  color: var(--color-muted);
}

.header-actions {
  display: flex;
  gap: 1rem;
  align-items: center;
}

/* Share & Settings Controls */
.share-badge {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: #ffffff;
  border: 1px solid var(--color-border);
  padding: 0.3rem 0.65rem; /* Tightened */
  border-radius: 20px;
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.2s ease;
}
.share-badge:hover {
  border-color: var(--color-accent);
  background: var(--color-accent-light);
}

.btn-secondary {
  background: white;
  border: 1px solid var(--color-border);
  color: var(--color-text);
  padding: 0.35rem 0.75rem; /* Tightened */
  border-radius: 4px;
  font-size: 0.9rem;
  cursor: pointer;
  font-family: var(--font-sans);
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  transition: all 0.2s ease;
}
.btn-secondary:hover {
  border-color: var(--color-text);
  background: #fafafa;
}

.btn-primary {
  background: var(--color-accent);
  color: white;
  border: none;
  padding: 0.6rem 1.2rem;
  border-radius: 4px;
  font-size: 0.95rem;
  cursor: pointer;
  font-weight: 500;
  font-family: var(--font-sans);
  transition: all 0.2s ease;
}
.btn-primary:hover {
  background: #4a5c47;
}

/* Homepage Landing */
.welcome-container {
  max-width: 600px;
  margin: 6rem auto;
  padding: 2.5rem;
  background: white;
  border-radius: 8px;
  border: 1px solid var(--color-border);
  box-shadow: 0 4px 20px rgba(0,0,0,0.02);
  text-align: center;
}

.welcome-container h2 {
  font-family: var(--font-serif);
  font-size: 2.2rem;
  font-weight: normal;
  margin-top: 0;
  margin-bottom: 1rem;
  color: var(--color-accent);
}

.welcome-container p {
  color: var(--color-muted);
  font-size: 1.1rem;
  line-height: 1.6;
  margin-bottom: 2rem;
}

.household-form-group {
  margin-bottom: 1.5rem;
  text-align: left;
}

.household-form-group label {
  display: block;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-muted);
  margin-bottom: 0.4rem;
  font-weight: 600;
}

.form-input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  font-size: 1rem;
  font-family: var(--font-sans);
  background: #fafafa;
  transition: border-color 0.2s;
}
.form-input:focus {
  outline: none;
  border-color: var(--color-accent);
  background: #ffffff;
}

.tabletop-wrapper {
  padding: 2rem;
  max-width: 1400px;
  margin: 0 auto;
}

.tabletop {
  display: grid;
  grid-template-columns: 1fr auto 1fr; /* auto width lets the unsorted pile collapse dynamically! */
  grid-template-rows: 250px 250px;
  gap: 1.5rem;
  margin-bottom: 2rem;
}

/* Spatial Tabletop Layout (Desktop) */
@media (min-width: 769px) {
  html, body {
    height: 100vh;
    overflow: hidden;
  }
  body {
    display: flex;
    flex-direction: column;
  }
  .app-header {
    flex-shrink: 0;
  }
  .tabletop-wrapper {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding: 1rem 1.5rem;
    max-width: none;
    width: 100%;
    margin: 0;
  }
  .tabletop {
    flex-grow: 1;
    display: grid;
    grid-template-columns: 1fr auto 1fr; /* auto width lets the unsorted pile collapse dynamically! */
    grid-template-rows: 1fr 1fr;
    gap: 1.25rem;
    margin-bottom: 0.5rem;
    min-height: 0; /* crucial to allow grid height to shrink within flexbox container */
  }
  .zone {
    min-height: 0; /* crucial to enable internal scrolling rather than expanding */
  }
  .capture-bar {
    max-width: 900px;
    width: 100%;
    margin: 0.5rem auto 0 auto !important;
    flex-shrink: 0;
  }

  /* Desktop-only card hover animations and overlays */
  .index-card:hover {
    scale: 1.02; /* Scale slightly on hover, but DO NOT lift vertically to avoid overlapping the card above */
    box-shadow: var(--shadow-hover);
    border-color: #cac8c1;
    z-index: 10;
    break-before: avoid !important; /* CRUCIAL: Instructs the column layout engine to freeze and avoid wrapping this card to Column 2 while hovered! */
  }

  /* Fill the zone vertical height on desktop minus the header height */
  .card-stack {
    height: calc(100% - 2.5rem) !important;
  }

  /* Translate all subsequent sibling cards down on hover on desktop to make perfect space for the sliding actions drawer */
  /* This creates the beautiful sliding spacer effect using hardware-accelerated transforms, which DO NOT affect box-model heights! */
  /* Because the box-model remains 100% static, the CSS columns engine never re-balances or wraps cards, completely curing hover flicker! */
  .index-card:hover:not(:has(.card-textarea:focus)) ~ .index-card {
    transform: translateY(1.85rem) !important;
  }

  /* Show hover actions on desktop, but NOT when the user is actively typing/focusing the card textarea */
  .index-card:hover:not(:has(.card-textarea:focus)) .card-actions {
    opacity: 1;
    pointer-events: auto; /* enable clicks when visible */
  }

  /* Shrink the Unsorted Pile stack to fit exactly the active columns rendered, preventing any empty gaps */
  .zone-unsorted .card-stack {
    width: fit-content !important;
    max-width: 100% !important;
    margin: 0 auto; /* Keep the columns centered inside the zone */
  }
}

/* Drop Zones */
.zone {
  background: rgba(255, 255, 255, 0.4);
  border: 1px dashed var(--color-border);
  border-radius: 8px;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  position: relative;
  transition: all 0.2s ease;
  overflow-y: auto;
  scrollbar-gutter: stable; /* CRUCIAL: Keeps layout stable by reserving space for the scrollbar, preventing cards from re-wrapping and shifting/flickering! */
  max-height: 100%;
}

.zone-header {
  font-family: var(--font-serif);
  font-size: 1.1rem;
  font-style: italic;
  font-weight: normal;
  color: var(--color-muted);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.6rem 1rem 0.35rem 1rem;
  border-bottom: 1px solid var(--color-border);
  
  /* Dual-Axis Sticky Freeze: Stays permanently anchored in place horizontally & vertically */
  position: sticky;
  top: -1rem; /* Flush with top padding offset */
  left: -1rem; /* Flush with left padding offset */
  z-index: 20; /* Float above scrolling cards underneath */
  width: calc(100% + 2rem); /* Cover full scrollable width */
  margin-top: -1rem;
  margin-left: -1rem;
  margin-right: -1rem;
  margin-bottom: 0.75rem;

  /* Frosted Glass Aesthetics */
  background: rgba(247, 245, 240, 0.85); /* matching warm tabletop wash */
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

/* Keeps custom emojis perfectly upright and un-slanted inside italicized zone headers */
.zone-emoji {
  font-style: normal !important;
  display: inline-block; /* Forces browser to apply upright rendering layout */
}

.zone-count {
  font-size: 0.8rem;
  font-family: var(--font-sans);
  font-style: normal;
  background: var(--color-border);
  color: var(--color-text);
  padding: 0.1rem 0.5rem;
  border-radius: 10px;
}

/* Position mapping for the tabletop felt */
.zone-participant_one {
  grid-column: 1 / 2;
  grid-row: 1 / 2;
}

.zone-participant_two {
  grid-column: 3 / 4;
  grid-row: 1 / 2;
}

.zone-unsorted {
  grid-column: 2 / 3;
  grid-row: 1 / 3;
  background: rgba(255, 255, 255, 0.6);
  border: 1px solid var(--color-border);
  box-shadow: inset 0 2px 8px rgba(0,0,0,0.01);
  border-radius: 12px;
  max-width: 1000px; /* Allows the Unsorted Pile to expand horizontally up to 3 columns before wrapping, maximizing space */
  width: 100%;
  transition: max-width 0.2s ease, padding 0.2s ease, background 0.2s ease;
}

/* If the Unsorted Pile has no active index-cards inside its stack on desktop, collapse its width to an ultra-thin 50px slot! */
@media (min-width: 769px) {
  /* On desktop, force the Unsorted Zone container itself to shrink snugly to fit its active card columns! */
  .zone-unsorted {
    justify-self: center !important; /* CRUCIAL: Deactivates grid stretching, forcing the grid track to shrink snugly to fit only the active card columns! */
    width: max-content !important; /* Let container expand and contract snugly to match the exact width of the card-stack! */
    min-width: 170px !important; /* give it a clean, visually balanced minimum starting width */
  }

  /* Restores static header layout on desktop unsorted zone to prevent any circular width-dependency stretching bugs! */
  .zone-unsorted .zone-header {
    position: static !important;
    width: 100% !important;
    margin: 0 0 0.75rem 0 !important;
    padding: 0 0 0.35rem 0 !important;
    background: transparent !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
    border-bottom: 1px solid var(--color-border) !important;
  }

  .zone-unsorted:not(:has(.index-card)) {
    max-width: 50px !important; /* ultra-thin slot when empty! */
    padding: 0.5rem 0.25rem !important;
    background: rgba(0, 0, 0, 0.015) !important; /* subtle dark crease on the desk felt */
    border-style: dotted !important;
    box-shadow: none !important;
  }

  /* Hide the text "Unsorted Pile" when collapsed */
  .zone-unsorted:not(:has(.index-card)) .zone-header span:first-child {
    display: none !important;
  }

  /* Center the remaining contents in the header when collapsed */
  .zone-unsorted:not(:has(.index-card)) .zone-header {
    justify-content: center !important;
    padding-bottom: 0 !important;
    border-bottom: none !important;
  }

  /* Hide the text of the empty prompt, leaving only the star icon */
  .zone-unsorted:not(:has(.index-card)) #unsorted-empty-prompt {
    padding: 0 !important;
  }
  .zone-unsorted:not(:has(.index-card)) #unsorted-empty-prompt p {
    display: none !important;
  }
  .zone-unsorted:not(:has(.index-card)) #unsorted-empty-prompt .empty-state-icon {
    font-size: 1.5rem !important;
    margin: 1rem 0 0 0 !important;
  }
}

/* When the Unsorted Pile is maximized, completely override and disable all narrow/collapsed max-width limits! */
.zone-unsorted.maximized {
  max-width: none !important;
  width: calc(100% - 3rem) !important;
  padding: 1.5rem !important;
  background: var(--color-bg) !important;
  border-style: solid !important;
  box-shadow: 0 25px 60px rgba(0, 0, 0, 0.18) !important;
}

/* Ensure the header and empty prompt text show up beautifully when maximized, even if it is empty! */
.zone-unsorted.maximized .zone-header span:first-child {
  display: inline !important;
}
.zone-unsorted.maximized .zone-header {
  justify-content: space-between !important;
  border-bottom: 1px solid var(--color-border) !important;
  padding-bottom: 0.75rem !important;
}
.zone-unsorted.maximized #unsorted-empty-prompt p {
  display: block !important;
}
.zone-unsorted.maximized #unsorted-empty-prompt .empty-state-icon {
  font-size: 2.5rem !important;
  margin: 0 0 1rem 0 !important;
}
.zone-unsorted .zone-header {
  font-size: 1.25rem;
  font-style: normal;
  font-weight: 600;
  color: var(--color-accent);
}

.zone-shared {
  grid-column: 1 / 2;
  grid-row: 2 / 3;
}

.zone-discuss {
  grid-column: 3 / 4;
  grid-row: 2 / 3;
  border-color: rgba(209, 151, 63, 0.3);
}
.zone-discuss .zone-header {
  color: var(--color-discuss);
}

/* Drag & Drop Hover state */
.zone.drag-over {
  background: var(--color-accent-light);
  border-color: var(--color-accent);
  transform: scale(1.01);
}

/* Card Stack inside zones */
.card-stack {
  display: flex;
  flex-flow: column wrap; /* Stack cards vertically, and wrap them horizontally into new columns! */
  align-content: flex-start; /* Pack columns snugly next to each other from left-to-right */
  justify-content: flex-start;
  gap: 0.5rem; /* vertical gap between cards and columns */
  width: fit-content; /* CRUCIAL: Shrink-to-fit exactly the active, filled columns track-by-step! */
  max-width: 100%;
  min-height: 50px;
  overflow: visible; /* absolutely prevent any scrollbars inside the stack! */
}

/* Tactile Index Card Styling */
.index-card {
  background: var(--color-card);
  color: var(--color-text);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-card);
  padding: 0.4rem 0.65rem; /* Clean default padding */
  width: auto; /* Shrink horizontally to fit the text exactly */
  max-width: 145px; /* Set comfortable maximum width for card columns on desktop */
  margin-bottom: 0.5rem; /* vertical spacing between stacked cards inside columns */
  display: flex; /* Standard flex is perfectly suited for Flexbox columns tracks */
  flex-direction: column;
  justify-content: space-between;
  min-width: 120px; /* Restored original minimum width to allow narrow cards to fit side-by-side and cluster organically */
  min-height: 38px;
  height: auto;
  box-shadow: var(--shadow-card);
  position: relative;
  transition: transform 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease, margin-bottom 0.15s ease, scale 0.15s ease;
  cursor: grab;
  user-select: none;

  /* Force hardware-accelerated GPU layers to completely prevent browser rendering pixel duplication & corner clipping glitches! */
  backface-visibility: hidden;
  transform: translateZ(0); /* Safely triggers GPU compositing */
}

.index-card:active {
  cursor: grabbing;
}

/* Invisible pointer-events capture backplate to completely stabilize slow-entry hovers and transitions */
.index-card::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none; /* Disabled by default when not hovered */
  z-index: -1;
  background: transparent;
}

.index-card:hover::before {
  /* On hover, expand the invisible shield 8px around and 24px below the card to capture the cursor stably, */
  /* preventing any sub-pixel boundary layout rounding flickers or column-wrapping oscillations! */
  top: -8px;
  left: -8px;
  right: -8px;
  bottom: -24px; /* Covers the entire bottom actions drawer area */
  pointer-events: auto; /* Enable capture */
}

/* Subtle staggered rotations and slight physical offsets for an organic, scattered tabletop feel */
.index-card:nth-child(2n) {
  transform: rotate(1.5deg) translate(3px, 1px);
}
.index-card:nth-child(3n) {
  transform: rotate(-1.2deg) translate(-2px, -3px);
}
.index-card:nth-child(5n) {
  transform: rotate(0.8deg) translate(1px, 2px);
}

/* Inline Card Editing Form styling */
.card-editor-form {
  margin: 0;
  width: 100%;
  height: auto;
}

.card-textarea {
  width: 100%;
  height: auto;
  min-height: 1.5em;
  field-sizing: content;
  overflow: hidden;
  border: none;
  background: transparent;
  resize: none;
  font-family: var(--font-serif);
  font-size: 0.9rem;
  line-height: 1.3;
  color: var(--color-text);
  padding: 0;
  margin: 0;
}
.card-textarea:focus {
  outline: none;
}

/* Card Actions Overlays (Positioned absolutely directly underneath the card bottom border) */
.card-actions {
  position: absolute;
  top: 100%; /* Sits directly outside and below the card's bottom border! */
  margin-top: 1px;
  right: 4px;
  left: 4px; /* Constrains the overlay width to the card width */
  display: flex;
  flex-wrap: nowrap; /* Forces all buttons to sit on a single, neat line */
  justify-content: flex-end;
  gap: 0.25rem; /* Spacious clicking gaps */
  opacity: 0;
  pointer-events: none; /* prevent accidental clicks when invisible */
  transition: opacity 0.2s ease;
  background: transparent; /* Floats beautifully on the tabletop felt */
  border-radius: 4px;
  padding: 2px;
  z-index: 15; /* sits above other cards below */
}

/* Minimal tactility for card control buttons */
.card-btn {
  background: rgba(0,0,0,0.03);
  border: none;
  color: var(--color-muted);
  width: 22px; /* tactile, easy to click, completely non-fiddly */
  height: 22px;
  border-radius: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.05rem; /* Emojis are 50% larger, filling the button container beautifully */
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s ease;
}
.card-btn:hover {
  background: var(--color-accent);
  color: white;
}
.card-btn.btn-nope-card:hover {
  background: var(--color-nope);
}

/* Hide the minimal mobile actions toggle button by default on desktop viewports */
.mobile-toggle-btn {
  display: none;
}

/* Fast Capture Bar styling (centered at the bottom) */
.capture-bar {
  max-width: 600px;
  margin: 1rem auto; /* Tightened */
  background: white;
  padding: 0.4rem 0.75rem; /* Tightened */
  border-radius: 40px;
  border: 1px solid var(--color-border);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.04);
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.capture-input {
  flex-grow: 1;
  border: none;
  font-family: var(--font-serif);
  font-size: 1rem; /* Tighter font size */
  padding: 0.25rem 1rem; /* Tightened */
  background: transparent;
}
.capture-input:focus {
  outline: none;
}

.capture-btn {
  width: 32px; /* Tighter dimension */
  height: 32px;
  border-radius: 16px;
  background: var(--color-accent);
  color: white;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem; /* Tighter */
  transition: background 0.2s;
}
.capture-btn:hover {
  background: #4a5c47;
}

/* Settings Overlay / Modal */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(44, 43, 41, 0.4);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}
.modal-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

.modal-content {
  background: white;
  width: 90%;
  max-width: 500px;
  border-radius: 8px;
  padding: 2rem;
  box-shadow: 0 15px 50px rgba(0,0,0,0.15);
  position: relative;
}

.modal-close {
  position: absolute;
  top: 1.25rem;
  right: 1.25rem;
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: var(--color-muted);
}

/* Hidden Cards Drawer / View */
.drawer-container {
  max-width: 800px;
  margin: 3rem auto;
  padding: 2rem;
  background: white;
  border-radius: 8px;
  border: 1px solid var(--color-border);
}

.drawer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
  border-bottom: 1px solid var(--color-border);
  padding-bottom: 1rem;
}

.drawer-header h2 {
  font-family: var(--font-serif);
  font-size: 1.8rem;
  font-weight: normal;
  margin: 0;
}

.hidden-cards-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1.25rem;
}

.hidden-card-item {
  background: #fcfbfa;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-card);
  padding: 1rem;
  min-height: 100px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.hidden-card-text {
  font-family: var(--font-serif);
  font-size: 1rem;
  color: var(--color-text);
  margin-bottom: 1rem;
}

.hidden-card-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* Empty States */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 2rem;
  color: var(--color-muted);
  flex-grow: 1;
}

.empty-state-icon {
  font-size: 2.5rem;
  margin-bottom: 1rem;
  opacity: 0.6;
}

.empty-state-text {
  font-family: var(--font-serif);
  font-size: 1.1rem;
  font-style: italic;
  max-width: 320px;
  margin: 0 auto;
}

/* Toast Messages */
.flash-messages {
  position: fixed;
  top: 1.5rem;
  right: 1.5rem;
  z-index: 10000; /* Set above everything on the screen (including maximized zones at z-index 9999) */
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.flash-msg {
  padding: 0.75rem 1.5rem;
  border-radius: 4px;
  color: white;
  font-size: 0.95rem;
  font-weight: 500;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  animation: slideIn 0.3s ease forwards;
}

.flash-notice { background: var(--color-accent); }
.flash-alert { background: var(--color-nope); }

@keyframes slideIn {
  from { transform: translateX(120%); }
  to { transform: translateX(0); }
}

/* --- Responsive & Mobile Deal Mode Layout --- */
@media (max-width: 768px) {
  .app-header {
    padding: 1rem;
  }
  
  .tabletop-wrapper {
    padding: 1rem;
    display: flex;
    flex-direction: column; /* Stacked layout allowing us to visually re-order elements on mobile viewports */
  }

  /* Rearrange grid into stacked mobile view */
  .tabletop {
    grid-template-columns: 1fr;
    grid-template-rows: auto;
    gap: 1rem;
  }

  .zone {
    grid-column: auto !important;
    grid-row: auto !important;
    max-height: 420px; /* Increased to 420px to comfortably accommodate 4-card tall vertical columns with plenty of actions drawer headroom */
    padding-bottom: 2.5rem !important; /* CRUCIAL: Spacious bottom safety buffer so the last card and its sliding drawer never clip off the bottom! */
    overflow-y: auto;
    scrollbar-gutter: stable; /* Keeps mobile layout stable and scroll-shift free */
    background: #ffffff;
    border: 1px solid var(--color-border);
  }

  /* Make Unsorted card stack look like a beautiful "Dealing Pile" */
  .zone-unsorted {
    grid-row: 1 !important; /* Always on top */
    min-height: 220px;
    background: var(--color-accent-light) !important;
    border: 1px dashed var(--color-accent);
  }

  .zone-unsorted .card-stack {
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    min-height: 150px;
  }

  /* Overlap unsorted cards as a stacked pile where only top card is actioned */
  .zone-unsorted .index-card {
    position: absolute !important;
    width: 220px;
    height: 130px;
    margin: 0;
  }
  
  /* Stack effect: Only show text and actions for the active top card */
  .zone-unsorted .index-card:not(:last-child) {
    pointer-events: none;
  }
  
  .zone-unsorted .index-card:not(:last-child) .card-textarea {
    color: transparent;
  }

  .zone-unsorted .index-card:not(:last-child) .card-actions {
    display: none;
  }

  /* Permanently show the action overlay buttons inside the active top unsorted card for rapid-fire single-tap sorting! */
  .zone-unsorted .index-card .card-actions {
    opacity: 1 !important;
    pointer-events: auto !important;
    position: absolute !important;
    top: auto !important;
    bottom: 6px !important; /* sit beautifully at the bottom border inside the 130px card */
    right: 4px !important;
    left: 4px !important;
    background: transparent !important;
    padding: 0 !important;
    box-shadow: none !important;
  }
  
  /* Hide the minimal three-dots toggle button on unsorted cards on mobile */
  .zone-unsorted .index-card .mobile-toggle-btn {
    display: none !important;
  }

  /* Perfect horizontal swipe-scroll masonry columns on mobile viewports using stable Column-Wrap Flexbox! */
  .card-stack {
    display: flex !important;
    flex-flow: column wrap !important; /* Stack cards vertically, and wrap them horizontally into new columns! */
    align-content: flex-start !important; /* Pack columns snugly next to each other from left-to-right */
    justify-content: flex-start !important;
    gap: 0.5rem !important; /* Spacing between cards and columns */
    width: auto !important; /* Allow horizontal expansion without trailing blank gaps */
    height: 365px !important; /* Set to 365px permanently to fit exactly 4 cards vertically stacked per track, with spacious headroom to absorb drawer expansion without column-wrapping! */
    overflow-y: hidden !important; /* Strictly disable vertical scrolling */
    overflow-x: auto !important; /* Smooth horizontal swiping scroll */
    -webkit-overflow-scrolling: touch !important; /* Smooth hardware-accelerated momentum scroll */
    padding: 6px !important; /* CRUCIAL: Spacious 6px padding on all sides inside the scroll container, completely preventing rotated card corners from clipping at the borders! */
  }

  /* Force the masonry columns to expand and fill the complete vertical height of maximized full-screen viewports on mobile! */
  .zone.maximized .card-stack {
    height: calc(100% - 3.5rem) !important; /* Spans the entire height of the expanded screen, packing cards deeply! */
  }

  .index-card {
    width: 145px !important; /* Compact, highly legible fixed column width for mobile columns */
    flex: 0 0 auto !important; /* Strictly prevent vertical stretching, letting each card keep its natural content height! */
    height: auto !important; /* Keep natural height based on text content */
    margin-bottom: 0 !important;
  }

  /* Style card action overlay buttons as large, highly comfortable 28px touch badges on mobile when tapped */
  .card-btn {
    width: 28px !important; /* comfortable, precise mobile touch target size for thumbs */
    height: 28px !important;
    border-radius: 14px !important;
    font-size: 1.15rem !important; /* larger, clearer emojis for mobile screens */
  }

  /* Ensure the actions overlay sits cleanly and scales correctly below the card on mobile */
  .card-actions {
    top: 100% !important;
    bottom: auto !important;
    margin-top: 2px !important;
  }

  /* Reveal the hyper-minimal mobile toggle button and position it in the top-right corner on mobile viewports */
  .mobile-toggle-btn {
    display: flex !important;
    position: absolute;
    top: 4px;
    right: 4px;
    background: transparent !important; /* Removed gray background */
    border: none;
    color: var(--color-muted);
    width: 20px;
    height: 20px;
    border-radius: 10px;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    font-weight: bold;
    cursor: pointer;
    z-index: 10;
  }
  .mobile-toggle-btn:active {
    background: rgba(0, 0, 0, 0.05) !important;
    color: var(--color-text);
  }

  /* Support explicit toggle class for mobile actions drawer when the toggle button is tapped */
  /* This slides open a roomy 2.2rem gap directly below the card to fit the 28px buttons snugly inside the track */
  .index-card.show-actions {
    margin-bottom: 2.2rem !important;
  }
  .index-card.show-actions .card-actions {
    opacity: 1 !important;
    pointer-events: auto !important; /* Reveal the overlay drawer */
  }

  /* Hide text labels in the top nav on mobile to make everything fit perfectly on small screens */
  .hide-mobile-text {
    display: none !important;
  }

  .header-actions {
    gap: 0.7rem !important; /* Tighten spacing between compact icon buttons on mobile screens to prevent crowding */
  }
  
  .share-badge, .btn-secondary {
    padding: 0.4rem 0.5rem !important; /* Compact square padding on mobile */
    font-size: 0.85rem !important;
  }
  
  .logo-group p {
    display: none !important; /* Hide secondary header description on mobile to save vertical space */
  }

  .capture-bar {
    border-radius: 8px;
    margin: 1rem 0;
    order: -1 !important; /* Visually repositions the Add Card input form to the very top of the page on mobile */
    max-width: 100% !important;
    width: 100% !important;
  }
}

/* --- Maximize Area Layout & Toggle Button --- */
.zone-maximize-btn {
  background: rgba(0,0,0,0.03);
  border: none;
  color: var(--color-muted);
  width: 20px;
  height: 20px;
  border-radius: 4px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  cursor: pointer;
  transition: all 0.15s ease;
  margin-left: 0.25rem;
}
.zone-maximize-btn:hover {
  background: var(--color-accent);
  color: white;
}
.zone-maximize-btn.active {
  background: var(--color-nope);
  color: white;
  width: 24px;
  height: 24px;
  border-radius: 12px;
  font-size: 0.8rem;
}

/* Maximized state takes over full viewport */
.zone.maximized {
  position: fixed !important;
  top: 1.5rem !important;
  left: 1.5rem !important;
  right: 1.5rem !important;
  bottom: 1.5rem !important;
  width: calc(100% - 3rem) !important;
  height: calc(100vh - 3rem) !important;
  max-height: none !important;
  z-index: 9999 !important; /* over everything else */
  background: var(--color-bg) !important;
  border: 2px solid var(--color-accent) !important;
  box-shadow: 0 25px 60px rgba(0, 0, 0, 0.18) !important;
  padding: 1.5rem !important;
  display: flex !important;
  flex-direction: column !important;
  border-radius: 12px !important;
  overflow-y: auto !important;
}

.zone.maximized .zone-header {
  font-size: 1.5rem !important;
  padding-bottom: 0.75rem !important;
  margin-bottom: 1.5rem !important;
  border-bottom: 1px solid var(--color-border) !important;
}

.zone.maximized .card-stack {
  gap: 0.75rem !important; /* more breathing room when maximized! */
}

.zone.maximized .index-card {
  /* give maximized cards a slightly roomier max-width! */
  max-width: 250px !important;
}

/* --- Laptop & MacBook Pro Screen Optimization --- */
@media (min-width: 769px) and (max-width: 1550px) {
  .tabletop-wrapper {
    padding: 0.75rem 1rem !important; /* tighter wrapper padding */
  }
  
  .tabletop {
    gap: 0.75rem !important; /* tighter tabletop gap */
  }
  
  .card-stack {
    column-count: 2 !important;
    column-gap: 0.35rem !important; /* tighter column gaps to save space */
  }
  
  .index-card {
    min-width: 100px !important; /* narrower min-width so 2 columns always fit on laptops */
    padding: 0.3rem 0.5rem !important; /* tighter card padding */
  }

  .card-btn {
    width: 20px !important; /* slightly more compact, non-fiddly buttons for narrower cards */
    height: 20px !important;
    border-radius: 10px !important;
    font-size: 0.95rem !important; /* Emojis are 50% larger on laptops */
  }
}

/* --- Search Command Palette (Cmd+K) --- */
.search-palette-overlay {
  z-index: 15000 !important; /* sit above even flash notifications and maximized zones */
}

.search-palette-content {
  max-width: 550px !important;
  width: 90% !important;
  border-radius: 12px !important;
  box-shadow: 0 30px 80px rgba(0,0,0,0.25) !important;
  border: 1px solid var(--color-accent) !important;
  background: white !important;
}

.search-palette-input {
  font-family: var(--font-serif) !important;
  font-size: 1.1rem !important;
  padding: 0.75rem 1rem !important;
  margin-bottom: 1rem !important;
  background: #fafafa !important;
  border: 1px solid var(--color-border) !important;
  border-radius: 6px !important;
}
.search-palette-input:focus {
  background: white !important;
  border-color: var(--color-accent) !important;
  box-shadow: 0 0 0 3px rgba(92, 111, 89, 0.15) !important;
}

.search-results-list {
  max-height: 280px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  padding-right: 2px;
}

/* Custom scrollbar for results list */
.search-results-list::-webkit-scrollbar {
  width: 6px;
}
.search-results-list::-webkit-scrollbar-thumb {
  background-color: var(--color-border);
  border-radius: 3px;
}

.search-result-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.6rem 1rem;
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.1s ease;
  border: 1px solid transparent;
}

.search-result-item .result-text {
  font-family: var(--font-serif);
  font-size: 0.95rem;
  color: var(--color-text);
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 70%;
}

.search-result-item .result-placement {
  font-size: 0.75rem;
  color: var(--color-muted);
  background: rgba(0, 0, 0, 0.03);
  padding: 0.1rem 0.5rem;
  border-radius: 10px;
}

.search-result-item:hover, 
.search-result-item.highlighted {
  background: var(--color-accent-light) !important;
  border-color: rgba(92, 111, 89, 0.15) !important;
}
.search-result-item:hover .result-text, 
.search-result-item.highlighted .result-text {
  color: var(--color-accent) !important;
}
.search-result-item:hover .result-placement, 
.search-result-item.highlighted .result-placement {
  background: var(--color-accent) !important;
  color: white !important;
}

/* High-Impact Selected Card Flash Animation with IMMENSE GUSTO! */
.index-card.card-flash {
  animation: cardFlashPulse 1.2s cubic-bezier(0.25, 1.25, 0.5, 1.25) !important;
  z-index: 10000 !important; /* Float way above everything else during animation */
}

@keyframes cardFlashPulse {
  0% {
    border-color: var(--color-accent);
    box-shadow: 0 0 0 12px rgba(92, 111, 89, 0.6), 0 10px 40px rgba(0,0,0,0.3);
    transform: scale(1.18) rotate(-6deg);
  }
  15% {
    transform: scale(1.18) rotate(5deg);
    box-shadow: 0 0 0 12px rgba(209, 151, 63, 0.7), 0 15px 45px rgba(0,0,0,0.35);
  }
  30% {
    transform: scale(1.18) rotate(-4deg);
    box-shadow: 0 0 0 12px rgba(92, 111, 89, 0.6), 0 15px 45px rgba(0,0,0,0.35);
  }
  45% {
    transform: scale(1.18) rotate(3deg);
    box-shadow: 0 0 0 12px rgba(209, 151, 63, 0.7), 0 12px 40px rgba(0,0,0,0.3);
  }
  60% {
    transform: scale(1.1) rotate(-1deg);
    box-shadow: 0 0 0 6px rgba(92, 111, 89, 0.4), 0 8px 30px rgba(0,0,0,0.2);
  }
  100% {
    border-color: var(--color-border);
    box-shadow: var(--shadow-card);
    transform: scale(1) rotate(0deg);
  }
}

/* --- Live Multiplayer Presence Indicator Badge --- */
.presence-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.15rem 0.5rem;
  border-radius: 20px;
  font-size: 0.72rem;
  font-weight: 500;
  transition: all 0.2s ease;
  border: 1px solid var(--color-border);
  margin-left: 0.6rem;
  vertical-align: middle;
  line-height: 1;
}

.presence-badge.alone {
  background: rgba(0, 0, 0, 0.02);
  color: var(--color-muted);
}

.presence-badge.online {
  background: rgba(92, 111, 89, 0.08); /* sage green wash */
  color: var(--color-accent);
  border-color: rgba(92, 111, 89, 0.2);
}

.presence-dot {
  width: 6px;
  height: 6px;
  border-radius: 3px;
  display: inline-block;
}

.presence-dot.alone {
  background: var(--color-muted);
}

.presence-dot.online {
  background: var(--color-accent);
  box-shadow: 0 0 0 rgba(92, 111, 89, 0.4);
  animation: presencePulse 2s infinite;
}

@keyframes presencePulse {
  0% {
    transform: scale(0.95);
    box-shadow: 0 0 0 0 rgba(92, 111, 89, 0.7);
  }
  70% {
    transform: scale(1);
    box-shadow: 0 0 0 5px rgba(92, 111, 89, 0);
  }
  100% {
    transform: scale(0.95);
    box-shadow: 0 0 0 0 rgba(92, 111, 89, 0);
  }
}
