/* --- Texit Dashboard Design System & Variables --- */
:root {
  --font-sans: 'Plus Jakarta Sans', system-ui, sans-serif;
  --font-mono: 'Fira Code', monospace;
  
  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  
  --transition-fast: 0.15s cubic-bezier(0.16, 1, 0.3, 1);
  --transition-normal: 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 10px 35px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 20px 50px rgba(0, 0, 0, 0.15);
}

/* --- Dashboard Theme: Dark Mode --- */
body.dark-theme {
  --bg-app: #0b0c10;
  --bg-panel: #12131a;
  --bg-card: rgba(26, 28, 38, 0.55);
  --bg-card-hover: rgba(33, 36, 50, 0.85);
  --bg-input: #1a1c28;
  --border-color: rgba(255, 255, 255, 0.07);
  --border-hover: rgba(255, 255, 255, 0.15);
  
  --text-primary: #f0f2f5;
  --text-secondary: #9da3b4;
  --text-muted: #616678;
  
  --accent-color: #3b82f6;
  --accent-color-hover: #2563eb;
  --accent-gradient: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 50%, #d946ef 100%);
  --accent-glow: rgba(59, 130, 246, 0.2);
  
  --toast-bg: rgba(18, 19, 26, 0.95);
  --toast-border: rgba(59, 130, 246, 0.3);
}

/* --- Dashboard Theme: Light Mode --- */
body.light-theme {
  --bg-app: #f3f5f8;
  --bg-panel: #ffffff;
  --bg-card: rgba(241, 243, 246, 0.8);
  --bg-card-hover: rgba(241, 243, 246, 1);
  --bg-input: #ffffff;
  --border-color: rgba(0, 0, 0, 0.08);
  --border-hover: rgba(0, 0, 0, 0.15);
  
  --text-primary: #1f2937;
  --text-secondary: #4b5563;
  --text-muted: #9ca3af;
  
  --accent-color: #2563eb;
  --accent-color-hover: #1d4ed8;
  --accent-gradient: linear-gradient(135deg, #2563eb 0%, #7c3aed 50%, #db2777 100%);
  --accent-glow: rgba(37, 99, 235, 0.15);
  
  --toast-bg: rgba(255, 255, 255, 0.95);
  --toast-border: rgba(37, 99, 235, 0.2);
}

/* --- Reset & Layout --- */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-sans);
  background-color: var(--bg-app);
  color: var(--text-primary);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: background-color var(--transition-normal), color var(--transition-normal);
  overflow-x: hidden;
}

.app-container {
  width: 100%;
  max-width: 1540px;
  margin: 0 auto;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  flex-grow: 1;
}

/* --- Header --- */
.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border-color);
}

.header-logo {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.logo-icon-wrapper {
  background: var(--accent-gradient);
  color: white;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  box-shadow: 0 4px 14px var(--accent-glow);
}

.logo-icon {
  width: 20px;
  height: 20px;
}

.header-logo h1 {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  background: var(--accent-gradient);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.subtitle {
  color: var(--text-secondary);
  font-size: 0.8rem;
}

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

/* --- UI Controls --- */
.custom-select {
  font-family: var(--font-sans);
  background-color: var(--bg-input);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 0.6rem 1rem;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  outline: none;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.custom-select:hover {
  border-color: var(--border-hover);
}

.select-template {
  font-weight: 600;
  border-color: var(--accent-color);
}

.theme-toggle-btn {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  width: 38px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.theme-toggle-btn:hover {
  background: var(--bg-card-hover);
  transform: translateY(-1px);
}

body.dark-theme .sun-icon,
body.light-theme .moon-icon {
  display: block;
}

body.dark-theme .moon-icon,
body.light-theme .sun-icon {
  display: none;
}

.theme-toggle-btn svg {
  width: 18px;
  height: 18px;
}

/* --- Buttons --- */
.btn {
  font-family: var(--font-sans);
  font-weight: 600;
  font-size: 0.875rem;
  padding: 0.6rem 1.25rem;
  border-radius: var(--radius-sm);
  border: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  transition: all var(--transition-fast);
}

.btn-primary {
  background: var(--accent-gradient);
  color: white;
  box-shadow: 0 4px 12px var(--accent-glow);
}

.btn-primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 18px var(--accent-glow);
  filter: brightness(1.05);
}

/* --- Workspace Layout --- */
.app-main {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.5rem;
  flex-grow: 1;
  min-height: 0; /* Important for scroll overflow in grid */
  height: calc(100vh - 120px);
}

@media (max-width: 1024px) {
  .app-main {
    grid-template-columns: 1fr;
    height: auto;
  }
}

.panel {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-height: 0;
  height: 100%;
}

/* --- Left Column: Editor & Settings --- */
.toolbar-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 0.75rem 1.25rem;
}

.toolbar-row {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  flex-wrap: wrap;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.toolbar-group label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.select-sm {
  padding: 0.35rem 0.75rem;
  font-size: 0.8rem;
  border-radius: 6px;
}

.checkbox-group {
  cursor: pointer;
  font-size: 0.8rem;
}

.checkbox-group input {
  cursor: pointer;
  accent-color: var(--accent-color);
}

/* Metadata Card */
.metadata-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 1rem;
}

.metadata-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

@media (max-width: 640px) {
  .metadata-grid {
    grid-template-columns: 1fr;
  }
}

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

.input-group label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.input-group input {
  font-family: var(--font-sans);
  background: var(--bg-input);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  outline: none;
  transition: all var(--transition-fast);
}

.input-group input:focus {
  border-color: var(--accent-color);
}

/* Textarea Editor Box */
.editor-container {
  display: flex;
  flex-direction: column;
  flex-grow: 1;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  overflow: hidden;
  min-height: 350px;
}

.editor-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.6rem 1.25rem;
  background: rgba(0, 0, 0, 0.15);
  border-bottom: 1px solid var(--border-color);
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.editor-indicator {
  color: var(--accent-color);
  font-size: 0.75rem;
  display: flex;
  align-items: center;
  gap: 4px;
}

.editor-indicator::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent-color);
  display: inline-block;
  box-shadow: 0 0 8px var(--accent-color);
}

#markdown-editor {
  flex-grow: 1;
  background: transparent;
  border: none;
  outline: none;
  resize: none;
  padding: 1.25rem;
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 0.9rem;
  line-height: 1.6;
}

/* --- Right Column: Preview --- */
.preview-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 0.25rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.btn-preview-action {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.75rem;
  display: flex;
  align-items: center;
  gap: 4px;
  font-family: var(--font-sans);
  font-weight: 600;
}

.btn-preview-action.active {
  color: var(--accent-color);
}

.preview-viewport {
  flex-grow: 1;
  background: #252833;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 2rem;
  overflow-y: auto;
  display: flex;
  justify-content: center;
  align-items: start;
}

/* --- LaTeX Preview Sheet styling --- */
.preview-sheet {
  background-color: #ffffff;
  color: #000000;
  padding: 1.5in 1.2in;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.4);
  width: 100%;
  max-width: 8.27in; /* A4 width */
  min-height: 11.69in; /* A4 height */
  box-sizing: border-box;
  text-align: justify;
}

/* Page sizing classes */
.preview-sheet.letter {
  max-width: 8.5in;
  min-height: 11in;
}

.preview-sheet.a4 {
  max-width: 8.27in;
  min-height: 11.69in;
}

/* Base Font size settings */
.preview-sheet.size-10 { font-size: 10pt; }
.preview-sheet.size-11 { font-size: 11pt; }
.preview-sheet.size-12 { font-size: 12pt; }

/* Font Family definitions */
.preview-sheet.garamond {
  font-family: 'EB Garamond', serif;
}
.preview-sheet.lora {
  font-family: 'Lora', serif;
}
.preview-sheet.georgia {
  font-family: 'Georgia', serif;
}

/* Title Block emulating LaTeX standard */
.latex-title-block {
  text-align: center;
  margin-bottom: 2.5em;
}

.latex-title-block h1 {
  font-size: 2.2em;
  font-weight: 400;
  margin-bottom: 0.4em;
  letter-spacing: -0.01em;
}

.latex-author {
  font-size: 1.15em;
  margin-bottom: 0.25em;
}

.latex-date {
  font-size: 1.1em;
  color: #333;
}

/* Abstract block */
.latex-abstract {
  margin: 2em auto;
  max-width: 85%;
  font-size: 0.95em;
  text-align: justify;
}

.latex-abstract-title {
  text-align: center;
  font-weight: bold;
  margin-bottom: 0.5em;
  font-size: 1em;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* --- LaTeX Typography --- */
.latex-content {
  line-height: 1.6;
}

.latex-content p {
  margin-top: 0;
  margin-bottom: 0;
}

/* Indent all paragraphs except the very first paragraph after a heading */
.latex-content p + p {
  text-indent: 1.5em;
}

/* Heading spacing & layout */
.latex-content h1, 
.latex-content h2, 
.latex-content h3, 
.latex-content h4 {
  font-family: inherit;
  font-weight: 600;
  color: #000;
  text-align: left;
  page-break-after: avoid;
  break-after: avoid;
}

.latex-content h1 {
  font-size: 1.6em;
  margin-top: 1.8em;
  margin-bottom: 0.6em;
  border-bottom: none; /* Classical LaTeX has no line under h1 */
  padding-bottom: 0;
}

.latex-content h2 {
  font-size: 1.3em;
  margin-top: 1.5em;
  margin-bottom: 0.5em;
}

.latex-content h3 {
  font-size: 1.15em;
  margin-top: 1.2em;
  margin-bottom: 0.4em;
}

.latex-content h4 {
  font-size: 1em;
  margin-top: 1em;
  margin-bottom: 0.3em;
}

/* CSS Heading Counters */
.show-numbers .latex-content {
  counter-reset: h1-counter;
}

.show-numbers .latex-content h1 {
  counter-reset: h2-counter;
}

.show-numbers .latex-content h1::before {
  counter-increment: h1-counter;
  content: counter(h1-counter) ". ";
}

.show-numbers .latex-content h2 {
  counter-reset: h3-counter;
}

.show-numbers .latex-content h2::before {
  counter-increment: h2-counter;
  content: counter(h1-counter) "." counter(h2-counter) " ";
}

.show-numbers .latex-content h3::before {
  counter-increment: h3-counter;
  content: counter(h1-counter) "." counter(h2-counter) "." counter(h3-counter) " ";
}

/* Lists formatting */
.latex-content ul, 
.latex-content ol {
  margin: 1em 0 1em 2em;
  text-align: left;
}

.latex-content li {
  margin-bottom: 0.3em;
  padding-left: 0.25em;
}

.latex-content li p {
  text-indent: 0 !important; /* No indents inside lists */
}

/* Blockquotes (Indented quotation block) */
.latex-content blockquote {
  margin: 1.5em 2.5em;
  padding: 0;
  border-left: none; /* LaTeX uses indentation rather than borders */
  font-style: italic;
  color: #222;
  font-size: 0.95em;
}

.latex-content blockquote p {
  text-indent: 0 !important;
}

/* Tables (LaTeX Booktabs emulation) */
.latex-content table {
  border-collapse: collapse;
  margin: 2em auto;
  font-size: 0.9em;
  width: 90%;
  max-width: 100%;
  page-break-inside: avoid;
  break-inside: avoid;
}

.latex-content th {
  border-top: 1.5px solid #000;
  border-bottom: 1px solid #000;
  padding: 8px 12px;
  font-weight: 600;
  text-align: left;
}

.latex-content td {
  padding: 8px 12px;
}

.latex-content tr:last-child td {
  border-bottom: 1.5px solid #000;
}

/* Monospace & Code styling */
.latex-content code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background-color: #f5f5f5;
  color: #c7254e;
  padding: 2px 4px;
  border-radius: 3px;
}

/* Code Blocks block-level */
.latex-content pre {
  background-color: #f7f7fa;
  border: 1px solid #e1e1e8;
  border-radius: 4px;
  padding: 1.25em;
  margin: 1.5em 0;
  overflow-x: auto;
  page-break-inside: avoid;
  break-inside: avoid;
}

.latex-content pre code {
  background-color: transparent;
  color: inherit;
  padding: 0;
  border-radius: 0;
  font-size: 0.82em;
  display: block;
}

/* Horizontal Rules */
.latex-content hr {
  border: 0;
  border-top: 1px solid #ccc;
  margin: 2em 0;
  position: relative;
}

/* --- LaTeX print style mapping --- */
@media print {
  body {
    background: #ffffff !important;
    color: #000000 !important;
  }

  .app-container {
    padding: 0 !important;
    margin: 0 !important;
    max-width: 100% !important;
  }

  .app-header,
  .panel-editor,
  .preview-header {
    display: none !important;
  }

  .app-main {
    display: block !important;
    height: auto !important;
    grid-template-columns: 1fr !important;
  }

  .panel-preview,
  .preview-viewport {
    background: transparent !important;
    border: none !important;
    padding: 0 !important;
    overflow: visible !important;
    display: block !important;
  }

  .preview-sheet {
    box-shadow: none !important;
    padding: 0 !important; /* Managed by @page margins */
    margin: 0 !important;
    max-width: 100% !important;
    min-height: 0 !important;
  }

  /* Custom Page Setup matching print dialog settings */
  @page {
    margin: 1.2in 1in 1.2in 1in;
  }
}
