/* ============================================
   Dify RAG 系統 - 基礎樣式
   重置樣式 + 全域設定
   ============================================ */

/* ========== CSS Reset ========== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 16px;
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  /* Mobile viewport height fix: --app-height is set by JS, dvh is CSS fallback */
  --app-height: 100vh;
}

@supports (height: 100dvh) {
  html { --app-height: 100dvh; }
}

body {
  font-family: var(--font-family-sans);
  font-size: var(--text-base);
  font-weight: var(--font-normal);
  line-height: var(--leading-normal);
  color: var(--text-primary);
  background-color: var(--bg-primary);
  min-height: var(--app-height);
}

/* SPA 應用頁面禁止滾動 (由 app.html 設定 .app-layout 處理) */
html.app-mode,
body.app-mode {
  overflow: hidden !important;
}

/* ========== 基礎元素樣式 ========== */
h1, h2, h3, h4, h5, h6 {
  font-weight: var(--font-semibold);
  line-height: var(--leading-tight);
  color: var(--text-primary);
}

h1 { font-size: var(--text-5xl); }
h2 { font-size: var(--text-4xl); }
h3 { font-size: var(--text-3xl); }
h4 { font-size: var(--text-2xl); }
h5 { font-size: var(--text-xl); }
h6 { font-size: var(--text-lg); }

p {
  margin-bottom: var(--space-md);
  color: var(--text-secondary);
  line-height: var(--leading-relaxed);
}

a {
  color: var(--primary);
  text-decoration: none;
  transition: color var(--duration-normal) var(--ease-out);
}

a:hover {
  color: var(--primary-dark);
}

a:focus {
  outline: none;
}

a:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* ========== 按鈕基礎樣式 ========== */
button {
  font-family: inherit;
  font-size: inherit;
  font-weight: var(--font-medium);
  border: none;
  background: none;
  cursor: pointer;
  transition: all var(--duration-normal) var(--ease-out);
}

button:focus {
  outline: none;
}

button:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ========== 表單元素樣式 ========== */
input, textarea, select {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  border: none;
  background: transparent;
  outline: none;
}

input::placeholder, textarea::placeholder {
  color: var(--text-tertiary);
}

/* iOS Safari 在 input/textarea/select 的 font-size < 16px 時，focus 會自動放大頁面且不還原。
   手機端統一拉到 16px 阻斷此行為。range/checkbox/radio/color/file 不受影響故排除。
   用 !important 確保覆寫各元件區域樣式（這是修瀏覽器行為，非設計決策）。 */
@media (max-width: 768px) {
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]):not([type="file"]),
  textarea,
  select {
    font-size: 16px !important;
  }
}

/* ========== 圖片和媒體 ========== */
img, video, canvas, svg {
  display: block;
  max-width: 100%;
  height: auto;
}

/* ========== 列表樣式 ========== */
ul, ol {
  list-style: none;
}

/* ========== 工具類別 ========== */

/* 可見性 */
.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;
}

.hidden {
  display: none !important;
}

.invisible {
  visibility: hidden;
}

/* 佈局 */
.container {
  width: 100%;
  max-width: 1280px;
  margin: 0 auto;
  padding: 0 var(--space-md);
}

.flex {
  display: flex;
}

.inline-flex {
  display: inline-flex;
}

.grid {
  display: grid;
}

.block {
  display: block;
}

.inline-block {
  display: inline-block;
}

/* Flexbox 工具 */
.flex-col {
  flex-direction: column;
}

.flex-row {
  flex-direction: row;
}

.items-center {
  align-items: center;
}

.items-start {
  align-items: flex-start;
}

.items-end {
  align-items: flex-end;
}

.justify-center {
  justify-content: center;
}

.justify-between {
  justify-content: space-between;
}

.justify-start {
  justify-content: flex-start;
}

.justify-end {
  justify-content: flex-end;
}

.flex-1 {
  flex: 1 1 0%;
}

.flex-auto {
  flex: 1 1 auto;
}

.flex-none {
  flex: none;
}

/* 間距 */
.gap-xs { gap: var(--space-xs); }
.gap-sm { gap: var(--space-sm); }
.gap-md { gap: var(--space-md); }
.gap-lg { gap: var(--space-lg); }
.gap-xl { gap: var(--space-xl); }

/* Margin */
.m-0 { margin: 0; }
.m-xs { margin: var(--space-xs); }
.m-sm { margin: var(--space-sm); }
.m-md { margin: var(--space-md); }
.m-lg { margin: var(--space-lg); }
.m-xl { margin: var(--space-xl); }

.mx-auto { margin-left: auto; margin-right: auto; }
.my-auto { margin-top: auto; margin-bottom: auto; }

/* Padding */
.p-0 { padding: 0; }
.p-xs { padding: var(--space-xs); }
.p-sm { padding: var(--space-sm); }
.p-md { padding: var(--space-md); }
.p-lg { padding: var(--space-lg); }
.p-xl { padding: var(--space-xl); }

/* 文字對齊 */
.text-left { text-align: left; }
.text-center { text-align: center; }
.text-right { text-align: right; }

/* 文字顏色 */
.text-primary { color: var(--text-primary); }
.text-secondary { color: var(--text-secondary); }
.text-tertiary { color: var(--text-tertiary); }
.text-success { color: var(--success); }
.text-warning { color: var(--warning); }
.text-error { color: var(--error); }
.text-info { color: var(--info); }

/* 背景顏色 */
.bg-primary { background-color: var(--bg-primary); }
.bg-secondary { background-color: var(--bg-secondary); }
.bg-tertiary { background-color: var(--bg-tertiary); }

/* 圓角 */
.rounded-none { border-radius: 0; }
.rounded-sm { border-radius: var(--radius-sm); }
.rounded-md { border-radius: var(--radius-md); }
.rounded-lg { border-radius: var(--radius-lg); }
.rounded-xl { border-radius: var(--radius-xl); }
.rounded-full { border-radius: var(--radius-full); }

/* 陰影 */
.shadow-none { box-shadow: none; }
.shadow-sm { box-shadow: var(--shadow-sm); }
.shadow-md { box-shadow: var(--shadow-md); }
.shadow-lg { box-shadow: var(--shadow-lg); }
.shadow-xl { box-shadow: var(--shadow-xl); }

/* 寬度 */
.w-full { width: 100%; }
.w-auto { width: auto; }
.w-fit { width: fit-content; }

/* 高度 */
.h-full { height: 100%; }
.h-screen { height: var(--app-height); }
.h-auto { height: auto; }

/* 位置 */
.relative { position: relative; }
.absolute { position: absolute; }
.fixed { position: fixed; }
.sticky { position: sticky; }

/* 溢出 */
.overflow-hidden { overflow: hidden; }
.overflow-auto { overflow: auto; }
.overflow-scroll { overflow: scroll; }

/* ========== 響應式工具 ========== */
@media (max-width: 768px) {
  .md\:hidden {
    display: none !important;
  }
  
  .container {
    padding: 0 var(--space-sm);
  }
}

@media (min-width: 769px) {
  .sm\:hidden {
    display: none !important;
  }
}

/* ========== 動畫類別 ========== */
.transition {
  transition-property: color, background-color, border-color, text-decoration-color, fill, stroke, opacity, box-shadow, transform, filter, backdrop-filter;
  transition-timing-function: var(--ease-out);
  transition-duration: var(--duration-normal);
}

.transition-all {
  transition: all var(--duration-normal) var(--ease-out);
}

.transition-colors {
  transition-property: color, background-color, border-color;
  transition-timing-function: var(--ease-out);
  transition-duration: var(--duration-normal);
}

.transition-transform {
  transition-property: transform;
  transition-timing-function: var(--ease-out);
  transition-duration: var(--duration-normal);
}

/* ========== 滾動條樣式 ========== */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: var(--neutral-100);
  border-radius: var(--radius-sm);
}

::-webkit-scrollbar-thumb {
  background: var(--neutral-300);
  border-radius: var(--radius-sm);
  transition: background var(--duration-normal);
}

::-webkit-scrollbar-thumb:hover {
  background: var(--neutral-400);
}

/* Firefox scrollbar */
* {
  scrollbar-width: thin;
  scrollbar-color: var(--neutral-300) var(--neutral-100);
}

/* ========== 選取文字樣式 ========== */
::selection {
  background-color: var(--primary-100);
  color: var(--primary-dark);
}

::-moz-selection {
  background-color: var(--primary-100);
  color: var(--primary-dark);
}

/* ========== 焦點樣式 ========== */
.focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* ========== 無障礙支援 ========== */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}