@import 'highlight.js/styles/atom-one-dark.css';
@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
  :root {
    --background: 38 20% 95%;
    /* Softer warm gray background */
    --foreground: 225 11% 22%;
    --card: 40 17% 98%;
    --card-foreground: 225 11% 22%;
    --popover: 40 17% 98%;
    --popover-foreground: 225 11% 22%;
    --primary: 223 21% 20%;
    --primary-foreground: 0 0% 98%;
    --secondary: 36 18% 92%;
    --secondary-foreground: 223 18% 24%;
    --muted: 36 16% 93%;
    --muted-foreground: 223 9% 42%;
    --accent: 34 20% 91%;
    --accent-foreground: 223 18% 24%;
    --destructive: 0 84.2% 60.2%;
    --destructive-foreground: 0 0% 98%;
    --border: 35 15% 85%;
    --input: 35 15% 85%;
    --ring: 223 21% 30%;
    --radius: 0.75rem;
    --code-bg: 220 16% 21%;
  }

  .dark {
    /* Softer dark mode: less pure black, gentler contrast */
    --background: 220 10% 13%;
    --foreground: 210 16% 90%;

    --card: 220 10% 15%;
    --card-foreground: 210 16% 90%;

    --popover: 220 10% 16%;
    --popover-foreground: 210 16% 90%;

    --primary: 210 16% 90%;
    --primary-foreground: 220 12% 14%;

    --secondary: 220 10% 18%;
    --secondary-foreground: 210 16% 90%;

    --muted: 220 10% 20%;
    --muted-foreground: 210 10% 72%;

    --accent: 220 10% 22%;
    --accent-foreground: 210 16% 92%;

    --destructive: 0 62.8% 35%;
    --destructive-foreground: 0 0% 96%;

    --border: 220 8% 28%;
    --input: 220 8% 28%;
    --ring: 210 16% 78%;
    --code-bg: 220 16% 20%;
  }

  * {
    border-color: hsl(var(--border));
  }

  body {
    background-color: hsl(var(--background));
    color: hsl(var(--foreground));
    background-image:
      radial-gradient(circle at 10% 10%, hsl(var(--primary) / 0.05), transparent 35%),
      radial-gradient(circle at 90% 0%, hsl(var(--accent-foreground) / 0.04), transparent 25%);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }
}

@layer components {
  .ui-card {
    @apply rounded-xl border border-border/65 bg-card shadow-sm backdrop-blur-sm;
    background-color: hsl(var(--card) / 0.92);
  }

  .ui-input {
    @apply w-full rounded-xl border border-input bg-background/75 px-3 py-2 text-sm outline-none transition-colors placeholder:text-muted-foreground focus:border-ring focus:ring-2 focus:ring-ring/20;
  }

  .ui-select {
    @apply w-full rounded-xl border border-input bg-background/75 px-3 py-2 text-sm outline-none transition-colors focus:border-ring focus:ring-2 focus:ring-ring/20;
  }

  .ui-btn {
    @apply inline-flex items-center justify-center gap-2 rounded-xl border border-transparent px-4 py-2 text-sm font-semibold transition-all active:scale-[0.98] disabled:cursor-not-allowed disabled:opacity-55 disabled:active:scale-100 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/25;
  }

  .ui-btn-primary {
    @apply ui-btn border-primary/20 bg-primary text-primary-foreground shadow-sm hover:opacity-90;
  }

  .ui-btn-secondary {
    @apply ui-btn border-border/70 bg-secondary/85 text-secondary-foreground hover:bg-secondary;
  }

  .ui-btn-danger {
    @apply ui-btn border-destructive/25 bg-destructive text-destructive-foreground hover:opacity-90;
  }

  .ui-icon-btn {
    @apply inline-flex h-9 w-9 items-center justify-center rounded-lg border border-border/40 bg-background/30 text-muted-foreground transition-colors active:scale-[0.98] disabled:cursor-not-allowed disabled:opacity-55 disabled:active:scale-100 hover:border-border hover:bg-muted/60 hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/25;
  }

  .ui-kbd {
    @apply inline-flex h-5 select-none items-center gap-1 rounded border border-border bg-muted px-1.5 font-mono text-[10px] font-medium text-muted-foreground;
  }

  .ui-enter {
    animation: ui-enter 220ms ease-out both;
  }

  .ui-enter-delayed {
    animation: ui-enter 300ms ease-out both;
  }

  .ui-float {
    animation: ui-float 4s ease-in-out infinite;
  }
}

@keyframes ui-enter {
  from {
    opacity: 0;
    transform: translateY(6px) scale(0.99);
  }

  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes ui-float {

  0%,
  100% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-4px);
  }
}

@media (prefers-reduced-motion: reduce) {

  .ui-enter,
  .ui-enter-delayed,
  .ui-float,
  .animate-in {
    animation: none !important;
    transition: none !important;
  }
}

/* Editor Specific Styles for Notion-like feel */
.ProseMirror {
  outline: none;
  min-height: 300px;
  padding-bottom: 56px;
  font-size: 1.05rem;
  line-height: var(--editor-line-height, 1.5);
  letter-spacing: 0.003em;
}

@media (max-width: 768px) {
  .ProseMirror {
    font-size: 1.05rem;
    line-height: max(1.6, var(--editor-line-height, 1.5));
    padding-bottom: 120px;
  }

  /* 移动端侧边栏文档树：增大字号和触摸目标 */
  .sidebar-tree-item {
    font-size: 0.9375rem !important;
    min-height: 2.5rem !important;
    line-height: 1.2;
  }

  .sidebar-tree-item .sidebar-title {
    line-height: 1.2;
  }
}

.ProseMirror>*+* {
  margin-top: 0.7em;
}

.ProseMirror h1,
.ProseMirror h2,
.ProseMirror h3 {
  color: hsl(var(--foreground));
  font-weight: 750;
  line-height: 1.28;
  letter-spacing: -0.01em;
}

.ProseMirror h1 {
  margin-top: 1.2em;
  font-size: clamp(1.7rem, 1.3rem + 1.3vw, 2.15rem);
}

.ProseMirror h2 {
  margin-top: 1.05em;
  font-size: clamp(1.35rem, 1.18rem + 0.8vw, 1.7rem);
}

.ProseMirror h3 {
  margin-top: 0.9em;
  font-size: clamp(1.12rem, 1.02rem + 0.45vw, 1.3rem);
}

.ProseMirror p {
  color: hsl(var(--foreground));
}

.ProseMirror ul,
.ProseMirror ol {
  margin: 0.5em 0;
  padding-left: 1.2em;
}

.ProseMirror li {
  margin: 0.22em 0;
}

.ProseMirror a {
  color: hsl(var(--primary));
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

.ProseMirror hr {
  margin: 1.1em 0;
  border: 0;
  border-top: 1px solid hsl(var(--border));
}

.ProseMirror p.is-editor-empty:first-child::before {
  content: attr(data-placeholder);
  float: left;
  color: hsl(var(--muted-foreground));
  pointer-events: none;
  height: 0;
}

.ProseMirror blockquote {
  border-left: 3px solid hsl(var(--primary));
  padding-left: 0.95rem;
  color: hsl(var(--muted-foreground));
  font-family: "Georgia", "Cambria", "Times New Roman", serif !important;
  font-style: italic;
  margin: 0.95rem 0;
  background: hsl(var(--muted) / 0.3);
  padding-top: 0.45rem;
  padding-bottom: 0.45rem;
  border-radius: 0 0.5rem 0.5rem 0;
}

.ProseMirror code {
  background-color: hsl(var(--primary) / 0.1);
  color: hsl(var(--foreground));
  border-radius: 0.25rem;
  padding: 0.2rem 0.4rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  font-size: 0.85em;
  font-weight: 500;
}

.dark .ProseMirror code {
  background-color: hsl(var(--primary) / 0.2);
  color: hsl(var(--foreground));
}

.ProseMirror code::before,
.ProseMirror code::after {
  content: none !important;
}

/* ... existing heading styles ... */

/* Table Styles - Ensure content is compact */
.ProseMirror table p {
  margin: 0;
  line-height: var(--table-line-height, 1.2);
}

/* 利用 TipTap 表格扩展的 .tableWrapper 包裹层实现横向滚动 */
.ProseMirror .tableWrapper {
  overflow-x: auto;
  margin: 1em 0;
}

.ProseMirror table {
  border-collapse: collapse;
  table-layout: auto;
  width: 100%;
  border-radius: 0.5rem;
}

.ProseMirror td,
.ProseMirror th {
  border-width: 1px;
  border-style: solid;
  border-color: hsl(var(--border));
  box-sizing: border-box;
  min-width: 1em;
  padding: 2px 4px;
  position: relative;
  vertical-align: top;
}

.ProseMirror th {
  background-color: hsl(var(--muted));
  font-weight: bold;
  text-align: left;
  border-color: hsl(var(--foreground) / 0.1);
}

/* AI Content Style */
.ai-content {
  font-family: "KaiTi", "STKaiti", "Kaiti SC", "Noto Serif SC", "Georgia", serif;
  font-style: italic;
  font-weight: normal;
  color: hsl(var(--foreground));
  /* Ensure text color is standard */
}

/* Specific fix for dark mode if needed, but opacity trick usually works */
.dark .ProseMirror th {
  border-color: hsl(var(--background));
}

/* Task List Styles */
ul[data-type="taskList"],
.ProseMirror ul[data-type="taskList"] {
  list-style: none !important;
  padding: 0 !important;
  margin: 0 !important;
}

.ProseMirror li[data-type="taskItem"] div,
.ProseMirror li[data-type="taskItem"] label {
  line-height: normal !important;
  margin-top: 2px;
  margin-bottom: 2px;
}

/* We are now using a Custom NodeView for li[data-type="taskItem"], so no global overriding needed.
   The component handles its own layout. */

/* Override Highlight.js background for a softer look */
.ProseMirror pre {
  background: hsl(var(--code-bg)) !important;
  border: 1px solid hsl(var(--border) / 0.5);
  border-radius: 0.5rem;
  padding: 0.65rem 0.75rem;
  margin: 0.95rem 0;
  overflow-x: auto;
}

.hljs {
  background: transparent !important;
  /* Let pre handle the background */
}

.ProseMirror>.block-drag-active {
  border-radius: 0.5rem;
  background: hsl(var(--accent) / 0.45);
  box-shadow: inset 0 0 0 1px hsl(var(--border) / 0.7);
}

.ProseMirror>.block-selected {
  border-radius: 0.5rem;
  background: linear-gradient(180deg, hsl(var(--accent) / 0.42), hsl(var(--accent) / 0.25));
  box-shadow: inset 0 0 0 1px hsl(var(--ring) / 0.28);
}

.sidebar-title {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 1.2;
}

@media (min-width: 768px) {
  .sidebar-title {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    white-space: normal;
    max-height: calc(1.25em * 2);
  }
}
