/* =========================================
   1. Global Reset & Variables
   ========================================= */
:root {
  /* Color Palette - Professional & Academic */
  --primary-color: #2c3e50; /* Deep Slate Blue */
  --accent-color: #3498db; /* Educational Blue */
  --accent-hover: #2980b9; /* Darker Blue for Hover */
  --bg-light: #f4f7f6; /* Soft Light Gray for Background */
  --bg-white: #ffffff; /* Pure White for cards */
  --text-main: #333333; /* Dark Gray for readability */
  --text-light: #666666; /* Lighter Gray for subtitles */
  --border-radius: 8px;
  --shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  --transition: all 0.3s ease;
}

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

html {
  /* Prevent font zooming on mobile when inputs are focused */
  text-size-adjust: 100%;
  scroll-behavior: smooth;
}

body {
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
  line-height: 1.6;
  color: var(--text-main);
  background-color: var(--bg-light);
  font-size: clamp(1rem, 1.2vw, 1.1rem); /* Fluid typography */
}

/* =========================================
   2. Layout Utilities
   ========================================= */
.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1.5rem; /* Fluid padding */
}

.content-section {
  padding: 3rem 0;
}

h1,
h2,
h3 {
  color: var(--primary-color);
  margin-bottom: 1rem;
  line-height: 1.3;
}

h1 {
  font-size: clamp(2rem, 5vw, 3rem);
}

h2 {
  font-size: clamp(1.5rem, 4vw, 2rem);
  text-align: center;
  margin-bottom: 2rem;
  border-bottom: 2px solid #e0e0e0;
  display: inline-block;
  padding-bottom: 10px;
  /* Center the heading relative to parent text-align */
  position: relative;
  left: 50%;
  transform: translateX(-50%);
}

/* =========================================
   3. Global Components (Forms & Cards)
   ========================================= */

/* Reusable Tool Container */
.tool-container {
  background: var(--bg-white);
  padding: clamp(1rem, 3vw, 2rem);
  border-radius: var(--border-radius);
  box-shadow: var(--shadow);
  max-width: 800px;
  margin: 2rem auto;
  border-top: 4px solid var(--accent-color);
  width: 100%;
}

/* Form Elements */
.input-group {
  margin-bottom: 1.5rem;
}

.input-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 600;
  color: var(--primary-color);
}

.input-group input,
.input-group textarea {
  width: 100%;
  padding: 0.8rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-family: inherit;
  font-size: 1rem; /* 16px min to prevent iOS zoom */
  min-height: 48px; /* Touch target size */
}

.btn-group {
  display: flex;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.btn-group button {
  flex: 1;
  min-height: 48px; /* Touch target size */
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Step Lists (Instructional) */
.step-list {
  list-style: none;
  counter-reset: step-counter;
}

.step-list li {
  position: relative;
  padding-left: 3rem; /* Increased for mobile touch clearance */
  margin-bottom: 1rem;
}

.step-list li::before {
  counter-increment: step-counter;
  content: counter(step-counter);
  position: absolute;
  left: 0;
  top: 0;
  width: 2rem;
  height: 2rem;
  background: var(--accent-color);
  color: white;
  border-radius: 50%;
  text-align: center;
  line-height: 2rem;
  font-weight: bold;
}

/* Result Output Box (consolidated) */
#cipherResult,
#result,
#output {
  background: var(--bg-light);
  padding: 1rem;
  border-left: 4px solid var(--primary-color);
  min-height: 3rem;
  font-family: "Courier New", monospace;
  font-weight: 600;
  word-break: break-all; /* Prevents overflow on mobile */
}

/* =========================================
   4. Buttons
   ========================================= */
.btn-primary,
.btn-secondary {
  display: inline-block;
  padding: 0.8rem 1.5rem;
  border-radius: var(--border-radius);
  text-decoration: none;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: var(--transition);
  font-size: 1rem;
}

/* Hero Button */
.btn-primary {
  background-color: var(--accent-color);
  color: white;
}

.btn-primary:hover {
  background-color: var(--accent-hover);
  transform: translateY(-2px);
}

/* Card Buttons */
.btn-secondary {
  background-color: transparent;
  border: 2px solid var(--accent-color);
  color: var(--accent-color);
  width: 100%;
  margin-top: auto;
}

.btn-secondary:hover {
  background-color: var(--accent-color);
  color: white;
}

/* =========================================
   5. Hero Section
   ========================================= */
.hero-section {
  background-color: var(--primary-color);
  color: var(--bg-white);
  padding: clamp(3rem, 8vw, 5rem) 0;
  text-align: center;
}

.hero-section h1 {
  color: var(--bg-white);
  margin-bottom: 0.5rem;
}

.subtitle {
  font-size: clamp(1rem, 2vw, 1.25rem);
  color: #ecf0f1;
  margin-bottom: 2rem;
  font-weight: 300;
}

/* =========================================
   6. Ciphers Grid (Cards)
   ========================================= */
.cipher-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 2rem;
}

.cipher-card {
  background: var(--bg-white);
  padding: 2rem;
  border-radius: var(--border-radius);
  box-shadow: var(--shadow);
  transition: var(--transition);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  border-top: 4px solid var(--accent-color);
}

.cipher-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 15px rgba(0, 0, 0, 0.15);
}

.cipher-card h3 {
  font-size: 1.5rem;
  margin-bottom: 0.5rem;
}

.cipher-card p {
  color: var(--text-light);
  margin-bottom: 1.5rem;
  flex-grow: 1;
}

#introduction p {
  max-width: 800px;
  margin: 0 auto;
  text-align: center;
  font-size: 1.1rem;
  color: var(--text-light);
}

/* =========================================
   7. Footer
   ========================================= */
footer {
  background-color: var(--primary-color);
  color: rgba(255, 255, 255, 0.8);
  text-align: center;
  padding: 2rem 0;
  margin-top: 4rem;
}

.disclaimer {
  font-size: 0.85rem;
  margin-top: 0.5rem;
  opacity: 0.7;
}

.portfolio:visited {
  color: black;
}
.portfolio:hover {
  color: white;
}

/* =========================================
   8. Page Specific Styles (Scoped)
   ========================================= */

/* --- Playfair Page --- */
.playfair-page .matrix-table {
  margin: 1rem auto;
  border-collapse: collapse;
  font-family: "Courier New", monospace;
}

.playfair-page .matrix-table td {
  width: 40px;
  height: 40px;
  border: 2px solid var(--primary-color);
  text-align: center;
  font-weight: bold;
  font-size: 1.2rem;
  background: #fff;
  transition: all 0.2s ease;
}

.playfair-page #matrix {
  text-align: center;
  margin-bottom: 1.5rem;
}

.playfair-page .cell-plain {
  background-color: #3498db !important;
  color: white !important;
  transform: scale(1.1);
  box-shadow: 0 0 10px rgba(52, 152, 219, 0.5);
  z-index: 2;
}

.playfair-page .cell-cipher {
  background-color: #2ecc71 !important;
  color: white !important;
  transform: scale(1.1);
  box-shadow: 0 0 10px rgba(46, 204, 113, 0.5);
  z-index: 2;
}

.playfair-page .cell-rect {
  background-color: rgba(52, 152, 219, 0.1) !important;
  border: 2px dashed var(--accent-color) !important;
}

.playfair-page #interactive-matrix-display .matrix-table {
  transition: all 0.3s ease;
}

/* --- Rail Fence Page --- */
.railfence-page .matrix-table {
  margin: 1rem auto;
  border-collapse: collapse;
  font-family: "Courier New", monospace;
}

.railfence-page .matrix-table td {
  width: 40px;
  height: 40px;
  border: 1px solid #ddd;
  text-align: center;
  font-weight: bold;
  background: #fff;
  font-size: 1.2rem;
}

.railfence-page .cell-active {
  background-color: var(--accent-color) !important;
  color: white;
}

/* --- Caesar Page --- */
.caesar-page .alphabet-row {
  display: grid;
  grid-template-columns: repeat(26, 1fr);
  gap: 2px;
  margin-bottom: 5px;
}

.caesar-page .alphabet-row .cell {
  border: 1px solid #ccc;
  text-align: center;
  font-size: 0.8rem;
  padding: 5px 0;
  font-family: monospace;
  background: #fff;
}

.caesar-page .alphabet-row .cell.highlight {
  background-color: var(--accent-color);
  color: white;
  font-weight: bold;
}

.caesar-page .alphabet-label {
  font-weight: bold;
  margin-top: 10px;
  margin-bottom: 5px;
  display: block;
  font-size: 0.9rem;
}

/* --- Row Transposition Page --- */
.row-transposition-page .matrix-table {
  margin: 1rem auto;
  border-collapse: collapse;
  font-family: "Courier New", monospace;
}

.row-transposition-page .matrix-table th {
  background-color: var(--primary-color);
  color: white;
  padding: 10px;
  border: 1px solid #ddd;
}

.row-transposition-page .matrix-table td {
  width: 40px; /* Standardized to match other pages (was 45px) */
  height: 40px;
  border: 1px solid #ddd;
  text-align: center;
  font-weight: bold;
  background: #fff;
  font-size: 1.2rem;
}

.row-transposition-page #matrix {
  text-align: center;
  margin-bottom: 1.5rem;
}

.row-transposition-page .highlight-col {
  background-color: rgba(52, 152, 219, 0.2) !important;
}

.row-transposition-page .filled-cell {
  background-color: #e8f6f3 !important; /* Light green */
  color: #16a085;
}

.row-transposition-page .active-key {
  background-color: var(--accent-color) !important;
  color: white !important;
}

.row-transposition-page .result-box {
  font-family: "Courier New", monospace;
  font-weight: bold;
  font-size: 1.1rem;
  padding: 10px;
  border: 2px dashed #ccc;
  margin-top: 10px;
  background: #fafafa;
  word-break: break-all;
}

.row-transposition-page .result-label {
  display: block;
  font-size: 0.85rem;
  color: #666;
  margin-bottom: 5px;
  text-transform: uppercase;
}

/* =========================================
   9. Media Queries (Mobile & Tablet)
   ========================================= */

/* Tablet Breakpoint */
@media (max-width: 768px) {
  .cipher-grid {
    grid-template-columns: 1fr;
  }

  .tool-container {
    padding: 1.5rem;
  }

  .caesar-page .alphabet-row {
    display: flex;
    overflow-x: auto;
    padding-bottom: 10px;
  }

  .caesar-page .alphabet-row .cell {
    min-width: 30px;
    flex-shrink: 0;
  }
}

/* Mobile Breakpoint (Portrait Phones) */
@media (max-width: 480px) {
  /* Stack buttons vertically */
  .btn-group {
    flex-direction: column;
  }

  .btn-group button {
    width: 100%;
  }

  /* --- Matrix Scaling for Small Screens --- */
  .playfair-page .matrix-table td,
  .railfence-page .matrix-table td,
  .row-transposition-page .matrix-table td {
    width: 32px;
    height: 32px;
    font-size: 1rem;
    border-width: 1px;
  }

  /* Row Transposition headers on mobile */
  .row-transposition-page .matrix-table th {
    padding: 5px;
    font-size: 0.9rem;
  }

  .railfence-page #interactive-matrix-display,
  .row-transposition-page #visual-matrix-area {
    overflow-x: auto;
    max-width: 100%;
  }

  /* --- Visualizer Control Fixes (Centered Navigation) --- */
  .playfair-page #visual-matrix-container .btn-group,
  .railfence-page .tool-container .btn-group,
  .row-transposition-page .tool-container .btn-group {
    flex-wrap: wrap;
    justify-content: center;
  }

  /* 1. Overrides global column rule */
  /* 2. Ensures it fits the container */
  /* 3. Prevents padding from causing overflow */
  #visual-matrix-container .btn-group {
    flex-direction: row !important;
    width: 100%;
    box-sizing: border-box;
  }

  .playfair-page #visual-matrix-container .btn-group #btn-prev,
  .playfair-page #visual-matrix-container .btn-group #btn-next,
  .railfence-page .tool-container .btn-group #btn-prev,
  .railfence-page .tool-container .btn-group #btn-next,
  .row-transposition-page .tool-container .btn-group #prev-step,
  .row-transposition-page .tool-container .btn-group #next-step {
    flex: 1 1 40%;
    font-size: 0.9rem;
    padding: 0.6rem;
  }

  .playfair-page #step-indicator,
  .railfence-page #step-indicator,
  .row-transposition-page #step-info {
    order: -1;
    width: 100%;
    text-align: center;
    margin-bottom: 0.5rem;
  }
}
