<script>
  import { onMount } from "svelte";
  import Icon from "../Icon.svelte";
  import { ACCENT_THEMES, applyAccent, applyTheme, preferredAccent, preferredTheme } from "../theme.js";

  let theme = $state("light");
  let accent = $state("blue");

  function setTheme(value) {
    theme = value;
    applyTheme(value);
  }

  function setAccent(value) {
    accent = value;
    applyAccent(value);
  }

  onMount(() => {
    theme = preferredTheme();
    accent = preferredAccent();
    const sync = (event) => {
      if (event.detail?.theme) theme = event.detail.theme;
      if (event.detail?.accent) accent = event.detail.accent;
    };
    window.addEventListener("picobot-appearance-change", sync);
    return () => window.removeEventListener("picobot-appearance-change", sync);
  });
</script>

<div class="appearance-layout">
  <div class="appearance-sections">
    <section class="panel appearance-card">
      <div class="section-heading">
        <div><h2>显示模式</h2><p>选择更适合当前环境的页面明暗度。</p></div>
      </div>
      <div class="mode-grid" role="group" aria-label="显示模式">
        <button class:selected={theme === "light"} aria-pressed={theme === "light"} onclick={() => setTheme("light")}>
          <span class="mode-preview light-preview"><i></i><b></b><em></em></span>
          <span class="option-copy"><Icon name="sun" size={17} /><strong>浅色</strong></span>
          {#if theme === "light"}<span class="selected-mark"><Icon name="check" size={13} /></span>{/if}
        </button>
        <button class:selected={theme === "dark"} aria-pressed={theme === "dark"} onclick={() => setTheme("dark")}>
          <span class="mode-preview dark-preview"><i></i><b></b><em></em></span>
          <span class="option-copy"><Icon name="moon" size={17} /><strong>深色</strong></span>
          {#if theme === "dark"}<span class="selected-mark"><Icon name="check" size={13} /></span>{/if}
        </button>
      </div>
    </section>

    <section class="panel appearance-card">
      <div class="section-heading">
        <div><h2>主题色</h2><p>品牌色会应用到主操作、选中状态、进度和焦点提示。</p></div>
      </div>
      <div class="accent-grid" role="group" aria-label="主题色">
        {#each ACCENT_THEMES as option}
          <button
            class:selected={accent === option.id}
            aria-pressed={accent === option.id}
            style={`--swatch:${option.swatch}`}
            onclick={() => setAccent(option.id)}
          >
            <span class="swatch">{#if accent === option.id}<Icon name="check" size={14} />{/if}</span>
            <span><strong>{option.label}</strong><small>{option.description}</small></span>
          </button>
        {/each}
      </div>
    </section>

    <p class="storage-note">外观设置仅保存在当前浏览器中，不会写入 Gateway 配置或同步到其他设备。</p>
  </div>

  <aside class="panel preview-card" aria-label="主题实时预览">
    <div class="preview-heading"><span>实时预览</span><span class="preview-status"><i></i>已应用</span></div>
    <div class="preview-window">
      <div class="preview-titlebar"><span class="preview-logo"><Icon name="bot" size={15} /></span><b>PicoBot</b><span class="window-dots">•••</span></div>
      <div class="preview-body">
        <div class="preview-nav">
          <span></span><span class="active"></span><span></span><span></span>
        </div>
        <div class="preview-content">
          <div class="preview-line wide"></div><div class="preview-line short"></div>
          <div class="preview-message"><Icon name="bot" size={13} /><span></span></div>
          <div class="preview-action"><span></span><button>继续</button></div>
        </div>
      </div>
    </div>
    <p>当前为 <strong>{ACCENT_THEMES.find((option) => option.id === accent)?.label}</strong> · {theme === "dark" ? "深色模式" : "浅色模式"}</p>
  </aside>
</div>

<style>
  .appearance-layout { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 18px; align-items: start; }
  .appearance-sections { display: grid; gap: 14px; }
  .appearance-card { padding: 20px; }
  .section-heading h2 { margin: 0; font-size: 16px; line-height: 22px; }
  .section-heading p { margin: 3px 0 0; color: var(--muted); font-size: 12px; line-height: 18px; }
  .mode-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-top: 16px; }
  .mode-grid > button { position: relative; padding: 10px; border: 1px solid var(--line-strong); border-radius: 8px; color: var(--text); background: var(--panel); text-align: left; cursor: pointer; }
  .mode-grid > button:hover { background: var(--panel-2); }
  .mode-grid > button.selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
  /* Mode thumbnails are intentionally theme-independent illustrations: they depict both
     light and dark surfaces at once, so they use fixed Fluent palette values instead of tokens. */
  .mode-preview { position: relative; height: 80px; display: block; overflow: hidden; border: 1px solid #d1d1d1; border-radius: 6px; background: #f5f5f5; }
  .mode-preview::before { content: ""; position: absolute; inset: 0 auto 0 0; width: 24%; border-right: 1px solid #d1d1d1; background: #fafafa; }
  .mode-preview i, .mode-preview b, .mode-preview em { position: absolute; display: block; border-radius: 2px; }
  .mode-preview i { top: 12px; left: 31%; width: 46%; height: 7px; background: var(--accent); }
  .mode-preview b { top: 27px; left: 31%; width: 57%; height: 30px; border: 1px solid #e0e0e0; background: #fff; }
  .mode-preview em { top: 63px; left: 31%; width: 40%; height: 5px; background: #d1d1d1; }
  .dark-preview { border-color: #424242; background: #1f1f1f; }
  .dark-preview::before { border-color: #424242; background: #242424; }
  .dark-preview b { border-color: #424242; background: #292929; }
  .dark-preview em { background: #666; }
  .option-copy { display: flex; align-items: center; gap: 7px; margin-top: 9px; }
  .option-copy strong { font-size: 13px; }
  .selected-mark { position: absolute; right: 8px; bottom: 8px; width: 20px; height: 20px; display: grid; place-items: center; border-radius: 50%; color: var(--accent-contrast); background: var(--accent); }
  .accent-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin-top: 16px; }
  .accent-grid button { min-width: 0; display: flex; align-items: center; gap: 10px; padding: 10px; border: 1px solid var(--line); border-radius: 6px; color: var(--text); background: var(--panel); text-align: left; cursor: pointer; }
  .accent-grid button:hover { border-color: var(--line-strong); background: var(--panel-2); }
  .accent-grid button.selected { border-color: var(--accent); background: var(--accent-soft); }
  .swatch { width: 30px; height: 30px; flex: 0 0 30px; display: grid; place-items: center; border: 1px solid var(--line); border-radius: 6px; color: #fff; background: var(--swatch); }
  .accent-grid strong, .accent-grid small { display: block; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
  .accent-grid strong { font-size: 12px; }
  .accent-grid small { margin-top: 2px; color: var(--muted); font-size: 10px; }
  .storage-note { margin: -2px 2px 0; color: var(--muted); font-size: 11px; line-height: 17px; }
  .preview-card { position: sticky; top: calc(var(--topbar-h) + 20px); overflow: hidden; }
  .preview-heading { display: flex; align-items: center; justify-content: space-between; padding: 13px 14px; border-bottom: 1px solid var(--line); font-size: 12px; font-weight: 600; }
  .preview-status { display: inline-flex; align-items: center; gap: 5px; color: var(--signal); font-size: 10px; }
  .preview-status i { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
  .preview-window { margin: 14px; overflow: hidden; border: 1px solid var(--line-strong); border-radius: 8px; background: var(--bg); box-shadow: var(--shadow-8); }
  .preview-titlebar { height: 38px; display: flex; align-items: center; gap: 7px; padding: 0 9px; border-bottom: 1px solid var(--line); background: var(--panel); font-size: 11px; }
  .preview-logo { width: 23px; height: 23px; display: grid; place-items: center; border-radius: 6px; color: var(--accent-contrast); background: var(--accent); }
  .window-dots { margin-left: auto; color: var(--muted); letter-spacing: 2px; }
  .preview-body { height: 180px; display: grid; grid-template-columns: 48px 1fr; }
  .preview-nav { display: grid; align-content: start; gap: 8px; padding: 12px 8px; border-right: 1px solid var(--line); background: var(--panel); }
  .preview-nav span { height: 7px; border-radius: 4px; background: var(--line-strong); }
  .preview-nav span.active { height: 20px; border-left: 2px solid var(--accent); border-radius: 4px; background: var(--accent-soft); }
  .preview-content { padding: 18px 14px; }
  .preview-line { height: 7px; border-radius: 4px; background: var(--line-strong); }
  .preview-line.wide { width: 70%; }
  .preview-line.short { width: 42%; margin-top: 7px; opacity: .65; }
  .preview-message { display: flex; align-items: center; gap: 7px; margin-top: 24px; color: var(--accent); }
  .preview-message span { width: 72%; height: 34px; border: 1px solid var(--line); border-radius: 6px; background: var(--panel); box-shadow: var(--shadow-2); }
  .preview-action { display: flex; align-items: center; gap: 7px; margin-top: 20px; }
  .preview-action span { flex: 1; height: 28px; border: 1px solid var(--line); border-radius: 4px; background: var(--panel); }
  .preview-action button { height: 28px; padding: 0 9px; border: 0; border-radius: 4px; color: var(--accent-contrast); background: var(--accent); font-size: 10px; }
  .preview-card > p { margin: 0; padding: 0 14px 14px; color: var(--muted); font-size: 11px; }
  .preview-card > p strong { color: var(--text); }
  @media (max-width: 1100px) { .appearance-layout { grid-template-columns: 1fr; } .preview-card { position: static; } .preview-window { max-width: 420px; } }
  @media (max-width: 620px) { .mode-grid, .accent-grid { grid-template-columns: 1fr; } .mode-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } .accent-grid button { min-height: 52px; } }
</style>
