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

:root {
  --apple-bg: #ffffff;
  --apple-text: #111111;
  --apple-muted: #6e6e73;
  --apple-border: #e5e5e5;
  --apple-elev: rgba(255,255,255,0.85);
  --apple-shadow: 0 2px 16px rgba(0,0,0,0.08);

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "Helvetica Neue", Arial, system-ui, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background-color: var(--apple-bg);
  color: var(--apple-text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* Header */
/* Sidebar layout */
.app { width: 100%; display: flex; flex-direction: row; }
.sidebar {
  width: 320px;
  min-width: 260px;
  max-width: 420px;
  height: 100vh;
  position: sticky;
  top: 0;
  align-self: flex-start;
  overflow-y: auto;
  padding: var(--space-4) var(--space-4) var(--space-4);
  border-right: 1px solid var(--apple-border);
  background-color: var(--apple-bg);
}

header {
  position: sticky;
  top: 0;
  z-index: 100;
  background-color: var(--apple-bg);
  border-bottom: 1px solid var(--apple-border);
  padding: var(--space-4) var(--space-4) var(--space-3);
  backdrop-filter: saturate(180%) blur(8px);
}

.logo {
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin-bottom: var(--space-3);
  text-align: center;
}

.controls-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: var(--space-3);
  align-items: stretch;
}

.control-group {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  column-gap: var(--space-2);
  row-gap: var(--space-1);
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--apple-border);
  background-color: transparent;
  width: 100%;
}

label { color: var(--apple-muted); white-space: nowrap; font-weight: 500; grid-column: 1 / -1; }

input[type="range"] { width: 100%; accent-color: #111; grid-column: 1 / 2; }

input[type="text"], select {
  background-color: #fff;
  color: var(--apple-text);
  border: 1px solid #d2d2d7;
  padding: 6px 10px;
  border-radius: 8px;
  outline: none;
  width: 100%;
  grid-column: 1 / -1;
}

input[type="text"]:focus, select:focus {
  border-color: #111;
  box-shadow: 0 0 0 3px rgba(17,17,17,0.08);
}

button {
  background-color: #111111;
  color: #ffffff;
  border: 1px solid #111111;
  padding: 8px 14px;
  border-radius: 9999px;
  cursor: pointer;
  transition: transform 0.06s ease, background-color 0.2s ease, color 0.2s ease;
}

button:hover { background-color: #000; }
button:active { transform: translateY(1px); }
button:focus-visible { outline: none; box-shadow: 0 0 0 3px rgba(17,17,17,0.12); }

.upload-button { position: relative; overflow: hidden; }
.upload-button { background: transparent; color: #111; border: 1px solid #d2d2d7; border-radius: 9999px; padding: 8px 14px; grid-column: 1 / -1; }
.upload-button:hover { background: #111; color: #fff; }
.upload-button input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

.controls-toggle { text-align: center; margin-top: var(--space-2); }
.controls-toggle button { background: transparent; color: var(--apple-muted); border: none; padding: 4px 8px; }

/* Main ASCII Stage */
main {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0;
  background-color: var(--apple-bg);
}

.square-container {
  position: relative;
  width: 100%;
  height: 100vh;
  background-color: var(--apple-bg);
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
}

.ascii-frame {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: var(--apple-bg);
}

/* Canvas 作为背景层 */
#ascii-canvas {
  position: absolute;
  inset: 0;
  z-index: 0;
}

#preview-container {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 5;
  max-width: 200px;
  max-height: 150px;
  background-color: transparent;
  border-radius: 10px;
  border: none;
  padding: 0;
  display: none;
  box-shadow: none;
}

#preview-container img, 
#preview-container video { max-width: 100%; max-height: 100%; border-radius: 6px; z-index: 1; }

#toggle-preview {
  position: absolute;
  top: 6px;
  left: 6px;
  padding: 6px 10px;
  font-size: 12px;
  z-index: 2;
  background: rgba(17,17,17,0.82);
  color: #fff;
  border: none;
  border-radius: 9999px;
}

#loading-indicator {
  display: none;
  position: absolute;
  top: 50%; left: 50%; transform: translate(-50%, -50%);
  background: rgba(0,0,0,0.85);
  color: #ffffff;
  padding: 14px 22px;
  border-radius: 12px;
  z-index: 10;
  font-size: 14px;
  box-shadow: var(--apple-shadow);
}

#ascii-output {
  position: relative;
  z-index: 1;
  pointer-events: none;
  white-space: pre;
  font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  line-height: 1;
  font-size: 10px;
  text-align: center;
  margin: 0; padding: 0;
  max-width: 100%; max-height: 100%;
  overflow: hidden;
  background-color: transparent;
  border: none; outline: none;
  color: var(--apple-text);
  letter-spacing: -0.01em;
}

/* Footer */
footer {
  background-color: var(--apple-bg);
  border-top: 1px solid var(--apple-border);
  padding: 10px 15px;
  text-align: center;
  font-size: 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  min-height: 60px;
}

.actions { display: flex; gap: 10px; flex-wrap: wrap; }
.actions button { white-space: nowrap; min-width: 96px; }

/* 右上角悬浮操作区 */
.ascii-stage { position: relative; }
.actions-float {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  gap: 8px;
  z-index: 6;
}

/* 左侧操作区排版 */
.side-actions { grid-template-columns: repeat(3, 1fr); }
.side-actions {
  display: flex;
  gap: var(--space-3);
}
.side-actions button {
  flex: 1 1 0;
  min-width: 0;
  height: 44px;
  border-radius: 9999px;
}

/* 居中型控制组（如退出登录按钮）更规则的居中展示 */
.control-group.centered {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Toast 与顶部元素避免重叠（给主区域顶部留出一点安全区）*/
main { padding-top: 8px; }

/* 预览按钮更内敛，避免视觉突兀 */
#toggle-preview {
  right: 12px;
  top: 12px;
  background: rgba(17,17,17,0.85);
  color: #fff;
  border: 1px solid rgba(17,17,17,0.9);
  border-radius: 9999px;
  padding: 6px 12px;
}


/* 数值徽标（滑块右侧的值） */
#density-value, #font-size-value, #chaos-level-value {
  grid-column: 2 / 3;
  justify-self: end;
  color: var(--apple-muted);
  font-variant-numeric: tabular-nums;
}

/* 使开关行左右对齐：label + checkbox */
.control-group:has(#invert) label { grid-column: 1 / 2; }
.control-group:has(#invert) #invert { grid-column: 2 / 3; justify-self: end; }

.control-group:has(#extract-colors) label { grid-column: 1 / 2; }
.control-group:has(#extract-colors) #extract-colors { grid-column: 2 / 3; justify-self: end; }

/* 选择类控件跨两列 */
.control-group select { grid-column: 1 / -1; }

/* Responsive */
@media (max-width: 768px) {
  .controls-container { flex-direction: column; align-items: stretch; }
  .control-group { justify-content: space-between; }
  footer { flex-direction: column; gap: 10px; }
  .square-container { width: 100vw; height: calc(100vh - 140px); }
}

/* Themes 仅作用于画布区域（.ascii-stage 内） */
/* Matrix */
.ascii-stage.theme-matrix { background-color: #000; color: #00ff41; }
.ascii-stage.theme-matrix .ascii-frame { background-color: #000; }
.ascii-stage.theme-matrix #ascii-output { 
  color: #00ff41; 
  text-shadow: 0 0 5px #00ff41, 0 0 10px #00ff41, 0 0 15px #00ff41;
  font-weight: 500;
}
.ascii-stage.theme-matrix #loading-indicator { background: rgba(0,50,0,0.9); color: #00ff41; }



/* Dark */
.ascii-stage.theme-dark { background-color: #222; color: #fff; }
.ascii-stage.theme-dark .ascii-frame { background-color: #222; }
.ascii-stage.theme-dark #ascii-output { color: #fff; }

/* Apple Minimal */
.ascii-stage.theme-apple { background-color: var(--apple-bg); color: var(--apple-text); }
.ascii-stage.theme-apple .ascii-frame { background-color: var(--apple-bg); }
.ascii-stage.theme-apple #ascii-output { color: var(--apple-text); }

/* 方块模式样式 */
.square-char {
  display: inline-block;
  width: 0.6em;
  height: 1em;
  text-align: center;
  line-height: 1em;
  font-family: 'Courier New', monospace;
  margin: 0;
  padding: 0;
  border: none;
  vertical-align: top;
  box-sizing: border-box;
}

.ascii-output.square-mode {
  line-height: 1;
  letter-spacing: 0;
  font-size: inherit;
}

.ascii-output.square-mode .square-char {
  font-size: inherit;
}

/* 复选框样式优化 */
.checkbox-label {
  margin-left: 8px;
  font-size: 14px;
  color: var(--apple-text);
}

/* 新增：使单独一行的 control-group 居中 */
.control-group.centered {
  display: grid;
  grid-template-columns: 1fr;
  justify-items: center; /* 水平居中子元素 */
}

/* 修正：checkbox 与文字在同一行，checkbox 与 label 之间保留间距 */
#square-background-group {
  grid-template-columns: auto auto 1fr; /* [label] [checkbox] [text] */
}
#square-background-group > label { grid-column: 1 / 2; }
#square-background { grid-column: 2 / 3; justify-self: start; }
#square-background-group .checkbox-label { grid-column: 3 / 4; }

/* 统一复选框视觉缩放，避免因浏览器默认 margin 影响对齐 */
input[type="checkbox"] {
  transform: scale(1.1);
  margin: 0; /* 去除默认外边距，避免垂直/水平偏移 */
}

/* 图像模式样式 */
.image-mode-controls {
  margin-top: 10px;
}

.custom-image-upload {
  margin-top: 8px;
  background: var(--apple-accent);
  color: white;
  border-radius: 8px;
  padding: 10px;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.custom-image-upload:hover {
  background: var(--apple-accent-hover);
  transform: translateY(-1px);
}

.custom-image-upload input[type="file"] {
  position: absolute;
  left: -9999px;
  opacity: 0;
}

.format-hint {
  font-size: 12px;
  color: var(--apple-secondary);
  margin-top: 5px;
  font-style: italic;
}

/* 自定义图像字符样式 */
.custom-image-char {
  image-rendering: pixelated;
  image-rendering: -moz-crisp-edges;
  image-rendering: crisp-edges;
  line-height: 1;
  vertical-align: top;
}

.ascii-output.square-mode .custom-image-char {
  border-radius: 0;
}


/* Auth Overlay */
.auth-overlay {
  position: fixed;
  inset: 0;
  background: #000; /* 改为纯黑背景，禁止透视 */
  display: grid;
  place-items: center;
  z-index: 9999;
}

/* 侧边区的退出按钮复用现有 button 样式，无需额外定义，如需强调可在此加特定样式 */
#logout-btn {
  /* 和其他按钮一致的外观即可，暂不做特别强调 */
}

.auth-card {
  width: min(90vw, 360px);
  background: #fff;
  color: #111;
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.2);
  display: grid;
  gap: 12px;
}
.auth-card h2 { margin: 0 0 4px; font-weight: 700; }
.auth-card p { margin: 0 0 8px; color: #666; }
.auth-card input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid #d2d2d7;
  outline: none;
}
.auth-card input:focus { border-color: #111; box-shadow: 0 0 0 3px rgba(17,17,17,0.08); }
.auth-card button {
  width: 100%;
  background: #111;
  color: #fff;
  border: 1px solid #111;
  padding: 10px 14px;
  border-radius: 9999px;
}
.auth-hint { text-align: center; color: #999; font-size: 12px; }
.auth-error {
  display: none;
  background: #fff5f5;
  color: #c53030;
  border: 1px solid #feb2b2;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
}

