/* CSS Variables for Light/Dark Mode */
:root {
  --fg: #111;
  --bg: #fff;
  --link: #0044dd;
  --accent: #888;
  --border: #ddd;
  --code-bg: #f8f8f8;
  --mono: "JetBrains Mono", "SF Mono", Monaco, "Cascadia Code", "Roboto Mono", Consolas, "Courier New", monospace;
  --body-w: 42rem;
  font-size: 16px;
  font-family: "Cabinet Grotesk", system-ui, -apple-system, sans-serif;
}

/* Dark Mode */
@media (prefers-color-scheme: dark) {
  :root {
    --fg: #ddd;
    --bg: #111;
    --link: #8ab4ff;
    --accent: #aaa;
    --border: #333;
    --code-bg: #1a1a1a;
  }
}

/* Manual theme override */
[data-theme="light"] {
  --fg: #111;
  --bg: #fff;
  --link: #0044dd;
  --accent: #888;
  --border: #ddd;
  --code-bg: #f8f8f8;
}

[data-theme="dark"] {
  --fg: #ddd;
  --bg: #111;
  --link: #8ab4ff;
  --accent: #aaa;
  --border: #333;
  --code-bg: #1a1a1a;
}

/* Base Styles */
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
  color: var(--fg);
  background: var(--bg);
  line-height: 1.6;
  transition: background-color 0.2s ease, color 0.2s ease;
}

main {
  max-width: var(--body-w);
  margin: 0 auto;
  padding: 2rem 1rem;
  min-height: calc(100vh - 4rem);
}

/* Typography */
h1, h2, h3, h4, h5, h6 {
  font-weight: 600;
  line-height: 1.2;
  margin-top: 2rem;
  margin-bottom: 1rem;
}

h1 {
  font-size: 2rem;
  margin-top: 0;
}

h2 {
  font-size: 1.5rem;
}

h3 {
  font-size: 1.25rem;
}

p {
  margin: 1rem 0;
}

/* Links */
a {
  color: var(--link);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

/* Navigation */
nav {
  margin-bottom: 2rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}

nav a {
  font-weight: 500;
  font-size: 1.25rem;
}

/* Lists */
ul, ol {
  margin: 1rem 0;
  padding-left: 2rem;
}

li {
  margin: 0.5rem 0;
}

.posts-list {
  list-style: none;
  padding: 0;
}

.posts-list li {
  margin: 1rem 0;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
}

.posts-list li:last-child {
  border-bottom: none;
}

.date {
  color: var(--accent);
  font-size: 0.9rem;
  float: right;
}

.post-date {
  display: block;
  color: var(--accent);
  font-size: 0.9rem;
  margin-bottom: 1rem;
  font-weight: normal;
}

/* Code */
code {
  font-family: var(--mono);
  font-size: 0.875em;
  background: var(--code-bg);
  padding: 0.15em 0.3em;
  border-radius: 3px;
}

pre {
  font-family: var(--mono);
  font-size: 0.875em;
  overflow-x: auto;
  padding: 1rem;
  margin: 1.5rem 0;
  background: var(--code-bg);
  border-radius: 6px;
  border: 1px solid var(--border);
}

pre code {
  background: none;
  padding: 0;
  border-radius: 0;
}

/* Tables */
table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.5rem 0;
  font-size: 0.9rem;
}

th, td {
  padding: 0.5rem 0.75rem;
  text-align: left;
  border: 1px solid var(--border);
}

th {
  background: var(--code-bg);
  font-weight: 600;
}

/* Blockquotes */
blockquote {
  margin: 1.5rem 0;
  padding: 0 1rem;
  border-left: 4px solid var(--accent);
  font-style: italic;
  color: var(--accent);
}

/* Images */
img {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
  display: block;
  margin: 1.5rem auto;
}

/* Social Sharing Optimizations */
.social-share {
  margin: 2rem 0;
  padding: 1rem;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  text-align: center;
}

.social-share p {
  margin: 0.5rem 0;
  color: var(--accent);
  font-size: 0.9rem;
}

.share-button {
  display: inline-block;
  margin: 0 0.5rem;
  padding: 0.5rem 1rem;
  background: var(--link);
  color: var(--bg);
  text-decoration: none;
  border-radius: 4px;
  font-size: 0.85rem;
  transition: all 0.2s ease;
}

.share-button:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

/* SEO and Accessibility Improvements */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Enhanced focus states for accessibility */
a:focus,
button:focus,
input:focus,
select:focus,
textarea:focus {
  outline: 2px solid var(--link);
  outline-offset: 2px;
}

/* Article Styles */
article {
  margin-bottom: 2rem;
}

article h1 {
  margin-bottom: 0.5rem;
}

/* Footer */
footer {
  text-align: center;
  padding: 2rem 1rem;
  color: var(--accent);
  font-size: 0.9rem;
  border-top: 1px solid var(--border);
  margin-top: 2rem;
}

footer a {
  color: var(--accent);
}

/* Theme Toggle Button */
.theme-toggle {
  position: fixed;
  top: 1rem;
  right: 1rem;
  background: transparent;
  border: none;
  cursor: pointer;
  font-size: 1rem;
  color: var(--accent);
  z-index: 100;
  padding: 0.25rem;
  width: 1.5rem;
  height: 1.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.7;
}

.theme-toggle:hover {
  opacity: 1;
}

/* Math (KaTeX) */
.katex {
  font-size: 1em !important;
}

/* Display math - KaTeX automatically adds this class for $$ delimited math */
.katex-display {
  margin: 1.5rem 0 !important;
  text-align: center !important;
  overflow-x: auto;
  overflow-y: hidden;
}

.katex-display > .katex {
  display: inline-block;
  white-space: nowrap;
  max-width: 100%;
}

/* Inline math */
.katex {
  vertical-align: baseline;
}

/* Math in paragraphs */
p .katex {
  margin: 0 0.1em;
}

/* Post Navigation */
.post-navigation {
  margin-top: 3rem;
  padding-top: 2rem;
  border-top: 1px solid var(--border);
}

.back-to-writings {
  display: inline-flex;
  align-items: center;
  color: var(--link);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
  padding: 0.75rem 1.25rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--code-bg);
  transition: all 0.2s ease;
  gap: 0.5rem;
}

.back-to-writings:hover {
  background: var(--link);
  color: var(--bg);
  text-decoration: none;
  transform: translateY(-1px);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* Homepage Sections */
.about-section,
.recent-posts-section {
  margin-bottom: 3rem;
  padding-bottom: 2rem;
}

.contact-section {
  margin-bottom: 1.5rem;
  padding-bottom: 0;
}

.contact-section h2 {
  margin-top: 0;
  margin-bottom: 1.5rem;
}

.contact-info {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.contact-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0;
}

.contact-label {
  font-weight: 500;
  color: var(--fg);
  min-width: 4rem;
}

.contact-value {
  color: var(--link);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}

.section-header h2 {
  margin: 0;
}

.all-writings-link {
  color: var(--link);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
  padding: 0.5rem 1rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--code-bg);
  transition: all 0.2s ease;
}

.all-writings-link:hover {
  background: var(--link);
  color: var(--bg);
  text-decoration: none;
  transform: translateY(-1px);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* Responsive adjustments for sections */
@media (max-width: 48rem) {
  :root {
    font-size: 15px;
  }
  
  body {
    line-height: 1.5;
  }
  
  main {
    padding: 1.5rem 1.5rem 2rem;
    max-width: 100%;
  }
  
  /* Navigation */
  nav {
    margin-bottom: 2rem;
    padding-bottom: 1rem;
  }
  
  nav a {
    font-size: 1.4rem;
    line-height: 1.3;
  }
  
  /* Typography */
  h1 {
    font-size: 1.8rem;
    margin-bottom: 1rem;
    line-height: 1.3;
  }
  
  h2 {
    font-size: 1.4rem;
    margin-top: 1.75rem;
    margin-bottom: 0.875rem;
    line-height: 1.3;
  }
  
  h3 {
    font-size: 1.2rem;
    line-height: 1.4;
  }
  
  p {
    margin: 0.875rem 0;
    line-height: 1.6;
  }
  
  /* Sections */
  .section-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 1rem;
    margin-bottom: 1.5rem;
  }
  
  .about-section,
  .recent-posts-section {
    margin-bottom: 2.5rem;
    padding-bottom: 2rem;
  }
  
  .contact-section {
    margin-bottom: 1rem;
    padding-bottom: 0;
  }
  
  .contact-section h2 {
    margin-bottom: 1rem;
  }
  
  .contact-info {
    gap: 0.5rem;
  }
  
  .contact-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.25rem;
    padding: 0.375rem 0;
  }
  
  .contact-label {
    font-size: 0.9rem;
    min-width: auto;
  }
  
  .about-section p {
    margin: 0.75rem 0;
    line-height: 1.6;
  }
  
  /* Posts list */
  .posts-list li {
    margin: 1rem 0;
    padding: 0.75rem 0;
    line-height: 1.4;
  }
  
  .date {
    float: none;
    display: block;
    margin-top: 0.25rem;
    font-size: 0.85rem;
  }
  
  /* All Writings page mobile styles */
  .controls {
    flex-direction: column;
    gap: 0.75rem;
    margin-bottom: 1.25rem;
  }
  
  .search-input,
  .sort-select {
    width: 100%;
    min-width: auto;
    font-size: 16px; /* Prevents zoom on iOS */
    padding: 0.75rem 1rem;
  }
  
  /* Links */
  .all-writings-link {
    font-size: 0.85rem;
    padding: 0.5rem 0.75rem;
  }
  
  /* Theme toggle */
  .theme-toggle {
    top: 0.75rem;
    right: 0.75rem;
    font-size: 0.9rem;
    width: 1.25rem;
    height: 1.25rem;
    padding: 0.2rem;
  }
  
  /* Tables */
  table {
    font-size: 0.85rem;
    overflow-x: auto;
    display: block;
    white-space: nowrap;
    border-radius: 6px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  }
  
  th, td {
    padding: 0.35rem 0.5rem;
  }
  
  /* Code blocks */
  pre {
    padding: 0.75rem;
    margin: 1rem 0;
    overflow-x: auto;
  }
  
  code {
    font-size: 0.8em;
  }
  
  /* Math */
  .katex-display {
    overflow-x: auto;
    padding: 0.5rem 0;
  }
  
  /* Improve touch targets */
  a {
    min-height: 44px;
    display: inline-block;
    line-height: 1.4;
    padding: 0.25rem 0;
  }
  
  .posts-list a {
    display: inline-block;
    min-height: 44px;
    line-height: 1.4;
    padding: 0.5rem 0;
    margin: 0.25rem 0;
  }
  
  .all-writings-link {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    padding: 0.75rem 1rem;
  }
  
  .theme-toggle {
    min-height: 44px;
    min-width: 44px;
    padding: 0.75rem;
    display: flex;
    align-items: center;
    justify-content: center;
    top: 0.75rem;
    right: 0.75rem;
    width: 2.5rem;
    height: 2.5rem;
  }
  
  /* Better mobile scrolling and viewport handling */
  html {
    -webkit-text-size-adjust: 100%;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }
  
  /* Prevent horizontal scroll */
  body {
    overflow-x: hidden;
    width: 100%;
    position: relative;
  }
  
  /* Prevent zoom on input focus */
  input, select, textarea {
    font-size: 16px !important;
  }
  
  /* Better mobile input styling */
  input, select, textarea {
    -webkit-appearance: none;
    appearance: none;
    border-radius: 6px;
    padding: 0.75rem 1rem;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--fg);
    transition: border-color 0.2s ease;
  }
  
  input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--link);
    box-shadow: 0 0 0 2px rgba(0, 68, 221, 0.1);
  }

  /* Post Navigation Mobile */
  .post-navigation {
    margin-top: 2rem;
    padding-top: 1.5rem;
  }

  .back-to-writings {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    padding: 0.75rem 1rem;
    font-size: 0.9rem;
    width: auto;
  }
}

/* All Writings page controls */
.controls {
  display: flex;
  gap: 1rem;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
  align-items: center;
}

.search-input, .sort-select {
  padding: 0.5rem 1rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 0.9rem;
  font-family: inherit;
  background: var(--bg);
  color: var(--fg);
  transition: all 0.2s ease;
}

.search-input {
  flex: 1;
  min-width: 200px;
}

.search-input:focus, .sort-select:focus {
  outline: none;
  border-color: var(--link);
  box-shadow: 0 0 0 2px rgba(0, 68, 221, 0.1);
}

/* Dark mode focus styling */
@media (prefers-color-scheme: dark) {
  .search-input:focus, .sort-select:focus {
    box-shadow: 0 0 0 2px rgba(138, 180, 255, 0.2);
  }
}

/* Manual theme overrides for focus */
[data-theme="light"] .search-input:focus, 
[data-theme="light"] .sort-select:focus {
  box-shadow: 0 0 0 2px rgba(0, 68, 221, 0.1);
}

[data-theme="dark"] .search-input:focus, 
[data-theme="dark"] .sort-select:focus {
  box-shadow: 0 0 0 2px rgba(138, 180, 255, 0.2);
}

.sort-select {
  min-width: 150px;
  cursor: pointer;
}

.hidden {
  display: none;
}