:root {
  /* Prevent FOUC (Flash of Unstyled Content) */
  --fouc-prevention: 1;
}

/* Hide undefined custom elements so they don't flash as unstyled HTML */
:not(:defined) {
  display: none !important;
}

:root {
  --md-sys-color-primary: #6750A4;
  --md-sys-color-on-primary: #FFFFFF;
  --md-sys-color-primary-container: #EADDFF;
  --md-sys-color-on-primary-container: #21005D;
  --md-sys-color-secondary: #625B71;
  --md-sys-color-on-secondary: #FFFFFF;
  --md-sys-color-secondary-container: #E8DEF8;
  --md-sys-color-on-secondary-container: #1D192B;
  --md-sys-color-surface: #FFFBFE;
  --md-sys-color-on-surface: #1C1B1F;
  --md-sys-color-surface-variant: #E7E0EC;
  --md-sys-color-on-surface-variant: #49454F;
  --md-sys-color-outline: #79747E;
  --md-sys-color-background: #FFFBFE;
  
  --sidebar-width: 280px;
  --header-height: 64px;

  /* Material Web Components Container Shapes */
  --md-outlined-text-field-container-shape: 16px;
  --md-outlined-select-text-field-container-shape: 16px;
  --md-outlined-field-container-shape: 16px;
  --md-menu-container-shape: 16px;
  --md-dialog-container-shape: 28px;
  
  /* Material Web Components Color Overrides for Dark Mode Compatibility */
  --md-menu-container-color: var(--md-sys-color-surface);
  --md-menu-item-label-text-color: var(--md-sys-color-on-surface);
  --md-menu-item-hover-state-layer-color: var(--md-sys-color-on-surface);
  --md-menu-item-focus-state-layer-color: var(--md-sys-color-on-surface);

  /* Material Web Components Outline Styling */
  --md-outlined-text-field-outline-color: color-mix(in srgb, var(--md-sys-color-on-surface) 20%, transparent);
  --md-outlined-text-field-outline-width: 2px;
  --md-outlined-text-field-hover-outline-color: var(--md-sys-color-primary);
  --md-outlined-text-field-hover-outline-width: 2px;
  --md-outlined-text-field-focus-outline-width: 2px;
  
  --md-outlined-select-text-field-outline-color: color-mix(in srgb, var(--md-sys-color-on-surface) 20%, transparent);
  --md-outlined-select-text-field-outline-width: 2px;
  --md-outlined-select-text-field-hover-outline-color: var(--md-sys-color-primary);
  --md-outlined-select-text-field-hover-outline-width: 2px;
  --md-outlined-select-text-field-focus-outline-width: 2px;
  
  --md-outlined-field-outline-color: color-mix(in srgb, var(--md-sys-color-on-surface) 20%, transparent);
  --md-outlined-field-outline-width: 2px;
  --md-outlined-field-hover-outline-color: var(--md-sys-color-primary);
  --md-outlined-field-hover-outline-width: 2px;
  --md-outlined-field-focus-outline-width: 2px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --md-sys-color-primary: #D0BCFF;
    --md-sys-color-on-primary: #381E72;
    --md-sys-color-primary-container: #4F378B;
    --md-sys-color-on-primary-container: #EADDFF;
    --md-sys-color-secondary: #CCC2DC;
    --md-sys-color-on-secondary: #332D41;
    --md-sys-color-secondary-container: #4A4458;
    --md-sys-color-on-secondary-container: #E8DEF8;
    --md-sys-color-surface: #1C1B1F;
    --md-sys-color-on-surface: #E6E1E5;
    --md-sys-color-surface-variant: #49454F;
    --md-sys-color-on-surface-variant: #CAC4D0;
    --md-sys-color-outline: #938F99;
    --md-sys-color-background: #1C1B1F;
  }
}

:root[data-theme="dark"] {
  --md-sys-color-primary: #D0BCFF;
  --md-sys-color-on-primary: #381E72;
  --md-sys-color-primary-container: #4F378B;
  --md-sys-color-on-primary-container: #EADDFF;
  --md-sys-color-secondary: #CCC2DC;
  --md-sys-color-on-secondary: #332D41;
  --md-sys-color-secondary-container: #4A4458;
  --md-sys-color-on-secondary-container: #E8DEF8;
  --md-sys-color-surface: #1C1B1F;
  --md-sys-color-on-surface: #E6E1E5;
  --md-sys-color-surface-variant: #49454F;
  --md-sys-color-on-surface-variant: #CAC4D0;
  --md-sys-color-outline: #938F99;
  --md-sys-color-background: #1C1B1F;
}

body {
  margin: 0;
  font-family: 'Roboto', system-ui, sans-serif;
  background-color: var(--md-sys-color-background);
  color: color-mix(in srgb, var(--md-sys-color-on-surface) 85%, var(--md-sys-color-surface));
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* Header */
.app-header {
  height: var(--header-height);
  display: flex;
  align-items: center;
  padding: 0 16px;
  background-color: var(--md-sys-color-surface);
  color: var(--md-sys-color-on-surface);
  border-bottom: none;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  z-index: 10;
}

md-dialog {
  --md-dialog-container-color: var(--md-sys-color-surface);
  position: relative;
  z-index: 9999;
}

body:has(md-dialog[open]) .app-header,
body:has(md-dialog[open]) .sidebar {
  z-index: 1 !important;
}

.app-header h1 {
  margin: 0;
  margin-left: 12px;
  font-weight: 500;
}

/* Header Icon Hover Animations */
#menu-toggle md-icon,
#random-btn md-icon,
#settings-btn md-icon,
#login-btn md-icon {
  transition: transform 0.3s ease;
}

#menu-toggle:hover md-icon { transform: scaleX(1.2); }
#settings-btn:hover md-icon { transform: rotate(90deg); }
#login-btn:hover md-icon { transform: scale(1.1); }

/* Click Icon Animations */
@keyframes stretchClick { 50% { transform: scaleX(1.5); } }
.icon-anim-stretch { animation: stretchClick 0.3s ease; }

@keyframes slideForwardClick {
  0% { transform: translateX(0); opacity: 1; }
  45% { transform: translateX(16px); opacity: 0; }
  50% { transform: translateX(-16px); opacity: 0; }
  100% { transform: translateX(0); opacity: 1; }
}
.icon-anim-slide { animation: slideForwardClick 0.4s ease; }

@keyframes jumpClick {
  0% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
  100% { transform: translateY(0); }
}
.icon-anim-jump { animation: jumpClick 0.3s ease; }

@keyframes spinQuarter { 100% { transform: rotate(180deg); } }
.icon-anim-gear { animation: spinQuarter 0.4s ease; }

@keyframes popClick { 50% { transform: scale(1.3); } }
.icon-anim-pop { animation: popClick 0.3s ease; }

.spacer {
  flex: 1;
}

/* Layout */
.app-layout {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-width);
  background: color-mix(in srgb, var(--md-sys-color-surface) 65%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: 4px 0 24px rgba(0, 0, 0, 0.1);
  overflow-y: auto;
  transition: transform 0.3s cubic-bezier(0.2, 0, 0, 1);
  display: flex;
  flex-direction: column;
  padding: 12px 0;
  position: absolute;
  height: calc(100vh - var(--header-height));
  z-index: 50;
  left: 0;
}

.sidebar.hidden {
  transform: translateX(-105%);
}

/* Tree Menu */
.tree-node {
  display: flex;
  flex-direction: column;
}

.tree-item {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  cursor: pointer;
  border-radius: 24px;
  margin: 2px 12px;
  transition: background-color 0.2s, color 0.2s;
  color: var(--md-sys-color-on-surface-variant);
  position: relative;
  outline: none;
  -webkit-tap-highlight-color: transparent;
}

.tree-item:hover {
  background-color: color-mix(in srgb, var(--md-sys-color-on-surface) 8%, transparent);
}

.tree-item.active {
  background-color: color-mix(in srgb, var(--md-sys-color-primary) 12%, transparent);
  color: var(--md-sys-color-primary);
  font-weight: 500;
}

.tree-item md-icon {
  margin-right: 12px;
  font-size: 20px;
}

.tree-actions {
  display: flex;
  align-items: center;
  margin-left: auto;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s ease;
}

.tree-item:hover .tree-actions {
  opacity: 1;
  visibility: visible;
}

.tree-actions md-icon-button {
  --md-icon-button-icon-size: 18px;
  --md-icon-button-state-layer-size: 28px;
  width: 28px;
  height: 28px;
}

.root-add-btn {
  margin: 8px 16px;
  display: flex;
  justify-content: center;
}

.tree-children-wrapper {
  display: grid;
  grid-template-rows: 0fr;
  transition: grid-template-rows 0.3s cubic-bezier(0.2, 0, 0, 1);
}

.tree-children-wrapper.open {
  grid-template-rows: 1fr;
}

.tree-children {
  overflow: hidden;
  margin-left: 24px;
  border-left: 1px solid var(--md-sys-color-surface-variant);
  display: flex;
  flex-direction: column;
}

/* Main Content */
.content-area {
  flex: 1;
  overflow-y: auto;
  background-color: var(--md-sys-color-background);
  padding: 32px 48px;
  scroll-behavior: smooth;
  display: flex;
  flex-direction: column;
}

#markdown-container {
  flex: 1;
  min-width: 0;
}



.toc-heading {
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 12px;
  color: var(--md-sys-color-primary);
}

.toc-link {
  display: block;
  font-size: 13px;
  color: var(--md-sys-color-on-surface-variant);
  text-decoration: none;
  padding: 4px 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: color 0.2s;
  outline: none;
  -webkit-tap-highlight-color: transparent;
}
.toc-link:hover {
  color: var(--md-sys-color-primary);
}
.toc-link.toc-h2 { margin-left: 12px; }
.toc-link.toc-h3 { margin-left: 24px; font-size: 12px; }

/* Editor Split Pane */
.editor-split-pane {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  flex: 1;
  min-height: 0;
}
#live-preview-pane {
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: 12px;
  padding: 24px;
  background: var(--md-sys-color-surface);
  overflow-y: auto;
  height: 70vh;
}
.editor-toolbar {
  background: var(--md-sys-color-surface-container-low);
  padding: 4px;
  border-radius: 24px;
  border: 1px solid var(--md-sys-color-outline-variant);
}

/* Hide scrollbars for a clean look */
.sidebar::-webkit-scrollbar,
.content-area::-webkit-scrollbar {
  display: none;
}
.sidebar,
.content-area {
  -ms-overflow-style: none; /* IE and Edge */
  scrollbar-width: none; /* Firefox */
}

.markdown-body {
  max-width: 1000px;
  margin: 0; /* Left aligned, no centering */
  line-height: 1.6;
  font-size: 16px;
  color: color-mix(in srgb, var(--md-sys-color-on-surface) 85%, var(--md-sys-color-surface));
  animation: fadeIn 0.4s ease;
}

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

/* Markdown styling mapping to M3 */
.markdown-body h1 {
  font-size: 32px;
  font-weight: 400;
  margin-top: 0;
  margin-bottom: 24px;
  color: var(--md-sys-color-primary);
}

.markdown-body h2 {
  font-size: 24px;
  font-weight: 500;
  margin-top: 32px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--md-sys-color-surface-variant);
  padding-bottom: 8px;
}

.markdown-body h3 {
  font-size: 20px;
  font-weight: 500;
  margin-top: 24px;
  margin-bottom: 12px;
}

.markdown-body p {
  margin-bottom: 16px;
}

.markdown-body a {
  color: var(--md-sys-color-primary);
  text-decoration: none;
}

.markdown-body a:hover {
  text-decoration: underline;
}

.markdown-body code {
  background-color: var(--md-sys-color-surface-variant);
  color: var(--md-sys-color-on-surface-variant);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: monospace;
  font-size: 0.9em;
}

.markdown-body pre {
  background-color: var(--md-sys-color-surface-variant);
  padding: 16px;
  border-radius: 12px;
  overflow-x: auto;
}

.markdown-body pre code {
  background-color: transparent;
  padding: 0;
  color: inherit;
}

.markdown-body blockquote {
  border-left: 4px solid var(--md-sys-color-primary);
  margin: 0;
  padding-left: 16px;
  color: var(--md-sys-color-on-surface-variant);
  background-color: var(--md-sys-color-surface-variant);
  padding: 12px 16px;
  border-radius: 0 8px 8px 0;
}

.markdown-body ul, .markdown-body ol {
  margin-bottom: 16px;
  padding-left: 24px;
}

.markdown-body table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 24px;
}

.markdown-body th, .markdown-body td {
  padding: 16px;
  border-bottom: 1px solid var(--md-sys-color-surface-variant);
  text-align: left;
}

.markdown-body th:not(:last-child), .markdown-body td:not(:last-child) {
  border-right: 1px solid color-mix(in srgb, var(--md-sys-color-on-surface) 10%, transparent);
}

.markdown-body th {
  color: var(--md-sys-color-on-surface-variant);
  font-weight: 500;
  border-bottom: 2px solid var(--md-sys-color-outline);
}

.markdown-body tr {
  transition: background-color 0.2s;
}

.markdown-body tbody tr:hover {
  background-color: var(--md-sys-color-surface-variant);
}

.color-chip {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  transition: transform 0.2s, border-color 0.2s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.color-chip:hover {
  transform: scale(1.1);
}

.color-chip:focus {
  outline: none;
  border-color: var(--md-sys-color-on-surface);
}

.markdown-body img {
  max-width: 100%;
  border-radius: 12px;
  box-shadow: 0 4px 8px rgba(0,0,0,0.1);
  margin: 16px 0;
}

/* Sliding Pill Toggle */
.sliding-toggle {
    position: relative;
    display: inline-flex;
    background: color-mix(in srgb, var(--md-sys-color-on-surface) 8%, transparent); /* Ensure visible contrast */
    border-radius: 999px;
    padding: 4px;
    margin: 0 auto;
    width: 200px;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}
.sliding-toggle input[type="radio"] {
    display: none;
}
.sliding-toggle .toggle-label {
    flex: 1;
    text-align: center;
    padding: 8px 0;
    font-size: 14px;
    font-weight: 500;
    color: var(--md-sys-color-on-surface-variant);
    cursor: pointer;
    z-index: 2;
    transition: color 0.3s ease;
    border-radius: 999px; /* Prevent rectangular click traces */
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}
.sliding-toggle .toggle-slider {
    position: absolute;
    top: 4px;
    left: 4px;
    width: calc(50% - 4px);
    height: calc(100% - 8px);
    background: var(--md-sys-color-primary);
    border-radius: 999px;
    z-index: 1;
    transition: transform 0.3s cubic-bezier(0.4, 0.0, 0.2, 1);
}
#type-file:checked ~ .toggle-slider {
    transform: translateX(0);
}
#type-file:checked ~ label[for="type-file"] {
    color: var(--md-sys-color-on-primary);
}
#type-folder:checked ~ .toggle-slider {
    transform: translateX(100%);
}
#type-folder:checked ~ label[for="type-folder"] {
    color: var(--md-sys-color-on-primary);
}

/* Custom Pill FAB Container */
.pill-fab-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 100;
  display: flex;
  flex-direction: row;
  align-items: center;
  background: color-mix(in srgb, var(--md-sys-color-surface) 65%, transparent);
  border-radius: 999px;
  padding: 4px;
  box-shadow: 0 4px 8px rgba(0,0,0,0.2);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  transition: opacity 0.6s cubic-bezier(0.2, 0, 0, 1), transform 0.6s cubic-bezier(0.2, 0, 0, 1);
}

/* Glass effect for Material Web Components */
md-dialog, md-menu, md-outlined-select {
  --md-dialog-container-color: color-mix(in srgb, var(--md-sys-color-surface) 65%, transparent);
  --md-menu-container-color: color-mix(in srgb, var(--md-sys-color-surface) 65%, transparent);
  --md-menu-item-container-color: transparent;
}

md-dialog, md-menu {
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

md-dialog::part(content) {
  scrollbar-width: none;
  -ms-overflow-style: none;
}
md-dialog::part(content)::-webkit-scrollbar {
  display: none;
}

md-outlined-select::part(menu) {
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.25) !important;
  border-radius: 12px;
}

.pill-fab-container.hidden {
  opacity: 0;
  pointer-events: none;
  transform: translateY(20px);
}

/* Inside Custom FAB */
.custom-fab {
  border: none;
  background: transparent;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--md-sys-color-primary);
  transition: background 0.2s, transform 0.1s;
  outline: none;
  -webkit-tap-highlight-color: transparent;
}
.custom-fab:hover {
  background: color-mix(in srgb, var(--md-sys-color-on-surface) 10%, transparent);
}
.custom-fab:active {
  background: color-mix(in srgb, var(--md-sys-color-on-surface) 20%, transparent);
  transform: scale(0.95);
}

/* Mobile TOC Popover */
.toc-popover {
  position: fixed;
  bottom: 104px;
  right: 24px;
  z-index: 101;
  background: color-mix(in srgb, var(--md-sys-color-surface) 65%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.15);
  width: 250px;
  max-height: 50vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: opacity 0.3s cubic-bezier(0.2, 0, 0, 1), transform 0.3s cubic-bezier(0.2, 0, 0, 1);
  transform-origin: bottom right;
}
.toc-popover.hidden {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.8) translateY(20px);
}
.toc-popover-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 16px;
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
}
.toc-popover-content {
  padding: 16px;
  overflow-y: auto;
}

/* Editor Split Pane */
.editor-split-pane {
  display: grid;
  grid-template-columns: 1fr 1fr;
  height: 100%;
  gap: 16px;
}

.editor-toolbar {
  display: flex;
  gap: 8px;
  padding: 8px;
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
  background: var(--md-sys-color-surface);
}

/* Responsive */
@media (max-width: 768px) {
  .app-header {
    padding: 0 8px;
  }
  
  .content-area {
    padding: 16px;
  }
  
  .markdown-body {
    font-size: 15px;
  }

  .editor-split-pane {
    grid-template-columns: 1fr;
  }
  
  #live-preview-pane {
    display: none;
  }
  .sidebar {
    width: 280px;
  }
  .content-area {
    padding: 16px 24px;
  }
}
