<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useI18n } from 'vue-i18n';

const props = withDefaults(
  defineProps<{
    title: string;
    ariaLabel?: string;
    /** 设置后首次自动弹出，关闭时写入 localStorage */
    storageKey?: string;
  }>(),
  {},
);

const { t } = useI18n();
const open = ref(false);

onMounted(() => {
  if (props.storageKey && !localStorage.getItem(props.storageKey)) {
    open.value = true;
  }
});

function close() {
  open.value = false;
  if (props.storageKey) {
    localStorage.setItem(props.storageKey, '1');
  }
}
</script>

<template>
  <button
    type="button"
    class="help-btn"
    :aria-label="ariaLabel ?? t('bet.guide_help_aria')"
    :title="ariaLabel ?? t('bet.guide_help_aria')"
    @click="open = true"
  >
    ?
  </button>

  <Teleport to="body">
    <div v-if="open" class="overlay" @click.self="close">
      <div class="modal" role="dialog" aria-modal="true" :aria-label="title">
        <button type="button" class="close-x" :aria-label="t('bet.cancel')" @click="close">✕</button>

        <h3 class="title">{{ title }}</h3>

        <div class="guide-body">
          <slot />
        </div>

        <button type="button" class="btn-ok btn-gold-outline" @click="close">
          {{ t('bet.guide_got_it') }}
        </button>
      </div>
    </div>
  </Teleport>
</template>

<style scoped>
.help-btn {
  flex-shrink: 0;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 1px solid var(--border-gold-soft);
  background: rgba(0, 0, 0, 0.2);
  color: var(--primary-light);
  font-size: 14px;
  font-weight: 800;
  line-height: 1;
  padding: 0;
  margin-left: auto;
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 205;
  background: rgba(0, 0, 0, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  backdrop-filter: blur(4px);
}

.modal {
  position: relative;
  width: 100%;
  max-width: 340px;
  max-height: 85vh;
  overflow-y: auto;
  background: linear-gradient(165deg, #1a1810 0%, #121212 45%, #0a0a0a 100%);
  border: 1px solid var(--border-gold-soft);
  border-radius: var(--radius);
  padding: 16px 14px 14px;
  box-shadow: var(--shadow), 0 0 24px rgba(212, 175, 55, 0.08);
}

.close-x {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text-muted);
  font-size: 14px;
}

.title {
  font-size: 16px;
  font-weight: 800;
  color: var(--primary-light);
  text-align: center;
  margin-bottom: 12px;
  padding-right: 24px;
}

.guide-body {
  display: grid;
  gap: 10px;
  margin-bottom: 14px;
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.55;
}

.guide-body :deep(.flow-name) {
  font-size: 13px;
  font-weight: 800;
  color: var(--primary-light);
  margin: 0 0 8px;
}

.guide-body :deep(ol) {
  margin: 0;
  padding-left: 20px;
}

.guide-body :deep(li + li) {
  margin-top: 6px;
}

.guide-body :deep(.intro) {
  margin: 0;
}

.guide-body :deep(.flow-block) {
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.guide-body :deep(.rules-link) {
  margin: 4px 0 0;
  font-size: 12px;
  padding-top: 8px;
  border-top: 1px dashed var(--border);
}

.btn-ok {
  width: 100%;
  padding: 10px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 800;
}
</style>
