<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useTeleportStore } from '@/stores/teleport'
import PermissionGroupEditor from '@/components/teleport/PermissionGroupEditor.vue'
import Panel from '@/components/ds/data/Panel.vue'
import Button from '@/components/ds/core/Button.vue'
import Icon from '@/components/ds/core/Icon.vue'
import Switch from '@/components/ds/forms/Switch.vue'
import Tabs from '@/components/ds/navigation/Tabs.vue'
import EmptyState from '@/components/ds/feedback/EmptyState.vue'

const store = useTeleportStore()

const activeTab = ref<'general' | 'homes' | 'tpr' | 'vip'>('general')
const showCreateModal = ref(false)
const showImportModal = ref(false)
const newConfigName = ref('')
const newConfigDesc = ref('')
const importConfigName = ref('')

const tabItems = [
  { value: 'general', label: 'General', icon: 'settings' },
  { value: 'homes', label: 'Homes', icon: 'door-open' },
  { value: 'tpr', label: 'TPR', icon: 'navigation' },
  { value: 'vip', label: 'VIP groups', icon: 'users' },
]

onMounted(async () => {
  await store.fetchConfigs()
  if (store.configs.length > 0 && store.configs[0]) {
    await store.loadConfig(store.configs[0].id)
  }
})

// --- Config data helpers ---

function getConfigValue(path: string, defaultValue: any = false): any {
  if (!store.currentConfig?.config_data) return defaultValue
  const parts = path.split('.')
  let current: any = store.currentConfig.config_data
  for (const part of parts) {
    if (current == null || typeof current !== 'object') return defaultValue
    current = current[part]
  }
  return current ?? defaultValue
}

function setConfigValue(path: string, value: any) {
  if (!store.currentConfig) return
  if (!store.currentConfig.config_data) store.currentConfig.config_data = {}
  const parts = path.split('.')
  let current: any = store.currentConfig.config_data
  for (let i = 0; i < parts.length - 1; i++) {
    const part = parts[i]!
    if (current[part] == null || typeof current[part] !== 'object') {
      current[part] = {}
    }
    current = current[part]
  }
  current[parts[parts.length - 1]!] = value
  store.markDirty()
}

// --- Action handlers ---

async function handleConfigChange(id: string) {
  if (store.isDirty) {
    if (!confirm('Unsaved changes will be lost. Continue?')) return
  }
  await store.loadConfig(id)
}

async function handleCreateConfig() {
  if (!newConfigName.value.trim()) return
  const config = await store.createConfig(
    newConfigName.value.trim(),
    newConfigDesc.value.trim() || undefined,
  )
  if (config) {
    showCreateModal.value = false
    newConfigName.value = ''
    newConfigDesc.value = ''
  }
}

async function handleDeleteConfig() {
  if (!store.currentConfig) return
  if (!confirm(`Delete "${store.currentConfig.config_name}"? This cannot be undone.`)) return
  await store.deleteConfig(store.currentConfig.id)
}

async function handleApply() {
  if (!store.currentConfig) return
  if (!confirm('Apply this teleport config to the server? This will overwrite the current NTeleportation config.')) return
  if (store.isDirty) {
    await store.saveCurrentConfig()
  }
  await store.applyToServer(store.currentConfig.id)
}

async function handleImport() {
  if (!importConfigName.value.trim()) return
  const config = await store.importFromServer(importConfigName.value.trim())
  if (config) {
    showImportModal.value = false
    importConfigName.value = ''
  }
}

function handlePermissionGroupUpdate(updatedData: Record<string, any>) {
  if (!store.currentConfig) return
  store.currentConfig.config_data = updatedData
  store.markDirty()
}
</script>

<template>
  <div class="tp">
    <!-- Page head -->
    <div class="tp__head">
      <div class="tp__head-id">
        <div class="tp__head-chip">
          <Icon name="navigation" :size="20" :stroke-width="2" />
        </div>
        <div>
          <div class="t-eyebrow">Plugin config</div>
          <h1 class="tp__title">Teleport config</h1>
        </div>
      </div>
      <div class="tp__head-actions">
        <Button variant="secondary" size="sm" icon="plus" @click="showCreateModal = true">New config</Button>
      </div>
    </div>

    <!-- Config selector + action bar -->
    <Panel>
      <div class="tp__toolbar">
        <select
          v-if="store.configs.length > 0"
          :value="store.currentConfig?.id ?? ''"
          class="tp__config-select"
          @change="handleConfigChange(($event.target as HTMLSelectElement).value)"
        >
          <option v-for="c in store.configs" :key="c.id" :value="c.id">
            {{ c.config_name }}{{ c.is_active ? ' (active)' : '' }}
          </option>
        </select>
        <span v-else class="tp__no-configs">No configs yet</span>

        <Button
          variant="primary"
          size="sm"
          icon="save"
          :loading="store.isSaving"
          :disabled="!store.currentConfig || !store.isDirty || store.isSaving"
          @click="store.saveCurrentConfig()"
        >{{ store.isSaving ? 'Saving…' : 'Save' }}</Button>

        <Button
          variant="outline"
          size="sm"
          icon="play"
          :disabled="!store.currentConfig || store.isApplying"
          @click="handleApply"
        >{{ store.isApplying ? 'Applying…' : 'Apply to server' }}</Button>

        <Button
          variant="secondary"
          size="sm"
          icon="download"
          @click="showImportModal = true"
        >Import from server</Button>

        <div class="tp__toolbar-spacer" />

        <Button
          variant="danger-soft"
          size="sm"
          icon="trash-2"
          :disabled="!store.currentConfig"
          @click="handleDeleteConfig"
        >Delete</Button>
      </div>
    </Panel>

    <!-- Loading state -->
    <Panel v-if="store.isLoading">
      <div class="tp__loading">
        <span class="tp__spinner" />
        <span class="tp__loading-label">Loading config…</span>
      </div>
    </Panel>

    <!-- Empty state -->
    <Panel v-else-if="!store.currentConfig">
      <EmptyState
        icon="navigation"
        title="No teleport config selected"
        description="Create a new config, import from server, or select one from the dropdown above."
      >
        <template #action>
          <Button icon="plus" @click="showCreateModal = true">Create first config</Button>
        </template>
      </EmptyState>
    </Panel>

    <!-- Config editor -->
    <template v-else>
      <!-- Tab bar -->
      <Tabs v-model="activeTab" :items="tabItems" variant="line" />

      <!-- ======================= GENERAL TAB ======================= -->
      <Panel v-if="activeTab === 'general'" title="General settings">
        <div class="tp__grid2">
          <div class="tp__toggle-row">
            <div class="tp__toggle-body">
              <div class="tp__toggle-label">Use Economics</div>
              <div class="tp__toggle-sub">Charge players for teleports via Economics plugin</div>
            </div>
            <Switch
              :model-value="getConfigValue('Settings.UseEconomics', false)"
              @update:model-value="setConfigValue('Settings.UseEconomics', $event)"
            />
          </div>
          <div class="tp__toggle-row">
            <div class="tp__toggle-body">
              <div class="tp__toggle-label">Use ServerRewards</div>
              <div class="tp__toggle-sub">Charge players via ServerRewards plugin</div>
            </div>
            <Switch
              :model-value="getConfigValue('Settings.UseServerRewards', false)"
              @update:model-value="setConfigValue('Settings.UseServerRewards', $event)"
            />
          </div>
          <div class="tp__toggle-row">
            <div class="tp__toggle-body">
              <div class="tp__toggle-label">Cave/water boundary checks</div>
              <div class="tp__toggle-sub">Prevent teleporting into caves or underwater</div>
            </div>
            <Switch
              :model-value="getConfigValue('Settings.CheckBoundaries', false)"
              @update:model-value="setConfigValue('Settings.CheckBoundaries', $event)"
            />
          </div>
          <div class="tp__toggle-row">
            <div class="tp__toggle-body">
              <div class="tp__toggle-label">Cancel TP if hostile timer</div>
              <div class="tp__toggle-sub">Cancel pending teleport if player becomes hostile</div>
            </div>
            <Switch
              :model-value="getConfigValue('Settings.InterruptTPOnHostile', false)"
              @update:model-value="setConfigValue('Settings.InterruptTPOnHostile', $event)"
            />
          </div>
          <div class="tp__toggle-row">
            <div class="tp__toggle-body">
              <div class="tp__toggle-label">Wipe homes on map update</div>
              <div class="tp__toggle-sub">Clear all home locations when the map changes</div>
            </div>
            <Switch
              :model-value="getConfigValue('Settings.WipeHomesOnUpgrade', false)"
              @update:model-value="setConfigValue('Settings.WipeHomesOnUpgrade', $event)"
            />
          </div>
          <div class="tp__toggle-row">
            <div class="tp__toggle-body">
              <div class="tp__toggle-label">Players only cannot teleport</div>
              <div class="tp__toggle-sub">Restrict teleport to specific player groups only</div>
            </div>
            <Switch
              :model-value="getConfigValue('Settings.PlayersOnlyCannotTeleport', false)"
              @update:model-value="setConfigValue('Settings.PlayersOnlyCannotTeleport', $event)"
            />
          </div>
        </div>

        <div class="tp__grid3 tp__mt">
          <label class="tp__field">
            <span class="tp__field-label">Global cooldown (seconds)</span>
            <span class="tp__field-hint">Minimum time between any teleport commands</span>
            <input
              type="number"
              class="cc-number"
              :value="getConfigValue('Settings.GlobalTeleportCooldown', 0)"
              min="0"
              @input="setConfigValue('Settings.GlobalTeleportCooldown', Number(($event.target as HTMLInputElement).value))"
            />
          </label>
        </div>
      </Panel>

      <!-- ======================= HOMES TAB ======================= -->
      <Panel v-else-if="activeTab === 'homes'" title="Home teleport settings">
        <div class="tp__grid2">
          <div class="tp__toggle-row">
            <div class="tp__toggle-body">
              <div class="tp__toggle-label">Can use outside building privilege</div>
              <div class="tp__toggle-sub">Allow home teleport even without building privilege</div>
            </div>
            <Switch
              :model-value="getConfigValue('Home.UsableOutOfBuildingBlocked', false)"
              @update:model-value="setConfigValue('Home.UsableOutOfBuildingBlocked', $event)"
            />
          </div>
          <div class="tp__toggle-row">
            <div class="tp__toggle-body">
              <div class="tp__toggle-label">Force home on foundation</div>
              <div class="tp__toggle-sub">Homes can only be set on a foundation block</div>
            </div>
            <Switch
              :model-value="getConfigValue('Home.ForceOnTopOfFoundation', false)"
              @update:model-value="setConfigValue('Home.ForceOnTopOfFoundation', $event)"
            />
          </div>
          <div class="tp__toggle-row">
            <div class="tp__toggle-body">
              <div class="tp__toggle-label">Verify foundation ownership</div>
              <div class="tp__toggle-sub">Only allow homes on foundations the player owns</div>
            </div>
            <Switch
              :model-value="getConfigValue('Home.CheckFoundationForOwner', false)"
              @update:model-value="setConfigValue('Home.CheckFoundationForOwner', $event)"
            />
          </div>
          <div class="tp__toggle-row">
            <div class="tp__toggle-body">
              <div class="tp__toggle-label">Allow above foundation</div>
              <div class="tp__toggle-sub">Allow setting homes above foundation level</div>
            </div>
            <Switch
              :model-value="getConfigValue('Home.AllowAboveFoundation', false)"
              @update:model-value="setConfigValue('Home.AllowAboveFoundation', $event)"
            />
          </div>
          <div class="tp__toggle-row">
            <div class="tp__toggle-body">
              <div class="tp__toggle-label">Cupboard owner allow on building blocked</div>
              <div class="tp__toggle-sub">Allow TC owners to teleport even when building blocked</div>
            </div>
            <Switch
              :model-value="getConfigValue('Home.CupOwnerAllowOnBuildingBlocked', false)"
              @update:model-value="setConfigValue('Home.CupOwnerAllowOnBuildingBlocked', $event)"
            />
          </div>
        </div>

        <div class="tp__grid4 tp__mt">
          <label class="tp__field">
            <span class="tp__field-label">Homes limit</span>
            <span class="tp__field-hint">Default max homes per player</span>
            <input
              type="number"
              class="cc-number"
              :value="getConfigValue('Home.HomesLimit', 3)"
              min="0"
              @input="setConfigValue('Home.HomesLimit', Number(($event.target as HTMLInputElement).value))"
            />
          </label>
          <label class="tp__field">
            <span class="tp__field-label">Daily limit</span>
            <span class="tp__field-hint">Max home teleports per day</span>
            <input
              type="number"
              class="cc-number"
              :value="getConfigValue('Home.DefaultDailyLimit', 5)"
              min="0"
              @input="setConfigValue('Home.DefaultDailyLimit', Number(($event.target as HTMLInputElement).value))"
            />
          </label>
          <label class="tp__field">
            <span class="tp__field-label">Cooldown (seconds)</span>
            <span class="tp__field-hint">Time between home teleports</span>
            <input
              type="number"
              class="cc-number"
              :value="getConfigValue('Home.DefaultCooldown', 600)"
              min="0"
              @input="setConfigValue('Home.DefaultCooldown', Number(($event.target as HTMLInputElement).value))"
            />
          </label>
          <label class="tp__field">
            <span class="tp__field-label">Countdown (seconds)</span>
            <span class="tp__field-hint">Countdown before teleport</span>
            <input
              type="number"
              class="cc-number"
              :value="getConfigValue('Home.DefaultCountdown', 5)"
              min="0"
              @input="setConfigValue('Home.DefaultCountdown', Number(($event.target as HTMLInputElement).value))"
            />
          </label>
        </div>
      </Panel>

      <!-- ======================= TPR TAB ======================= -->
      <Panel v-else-if="activeTab === 'tpr'" title="Teleport request settings">
        <div class="tp__grid2">
          <div class="tp__toggle-row">
            <div class="tp__toggle-body">
              <div class="tp__toggle-label">Block TP accept on ceiling</div>
              <div class="tp__toggle-sub">Prevent accepting a TP while on a ceiling tile</div>
            </div>
            <Switch
              :model-value="getConfigValue('TPR.BlockTPAOnCeiling', false)"
              @update:model-value="setConfigValue('TPR.BlockTPAOnCeiling', $event)"
            />
          </div>
          <div class="tp__toggle-row">
            <div class="tp__toggle-body">
              <div class="tp__toggle-label">Offset teleport target position</div>
              <div class="tp__toggle-sub">Slightly offset the teleport landing position</div>
            </div>
            <Switch
              :model-value="getConfigValue('TPR.OffsetTPRTarget', false)"
              @update:model-value="setConfigValue('TPR.OffsetTPRTarget', $event)"
            />
          </div>
          <div class="tp__toggle-row">
            <div class="tp__toggle-body">
              <div class="tp__toggle-label">Auto accept enabled</div>
              <div class="tp__toggle-sub">Automatically accept incoming TP requests</div>
            </div>
            <Switch
              :model-value="getConfigValue('TPR.AutoAcceptEnabled', false)"
              @update:model-value="setConfigValue('TPR.AutoAcceptEnabled', $event)"
            />
          </div>
        </div>

        <div class="tp__grid4 tp__mt">
          <label class="tp__field">
            <span class="tp__field-label">Cooldown (seconds)</span>
            <span class="tp__field-hint">Cooldown between TPR requests</span>
            <input
              type="number"
              class="cc-number"
              :value="getConfigValue('TPR.Cooldown', 600)"
              min="0"
              @input="setConfigValue('TPR.Cooldown', Number(($event.target as HTMLInputElement).value))"
            />
          </label>
          <label class="tp__field">
            <span class="tp__field-label">Countdown (seconds)</span>
            <span class="tp__field-hint">Countdown before teleport</span>
            <input
              type="number"
              class="cc-number"
              :value="getConfigValue('TPR.Countdown', 5)"
              min="0"
              @input="setConfigValue('TPR.Countdown', Number(($event.target as HTMLInputElement).value))"
            />
          </label>
          <label class="tp__field">
            <span class="tp__field-label">Daily limit</span>
            <span class="tp__field-hint">Max TPR per day</span>
            <input
              type="number"
              class="cc-number"
              :value="getConfigValue('TPR.DailyLimit', 5)"
              min="0"
              @input="setConfigValue('TPR.DailyLimit', Number(($event.target as HTMLInputElement).value))"
            />
          </label>
          <label class="tp__field">
            <span class="tp__field-label">Request duration (seconds)</span>
            <span class="tp__field-hint">How long a TPR request lasts</span>
            <input
              type="number"
              class="cc-number"
              :value="getConfigValue('TPR.RequestDuration', 30)"
              min="0"
              @input="setConfigValue('TPR.RequestDuration', Number(($event.target as HTMLInputElement).value))"
            />
          </label>
        </div>
      </Panel>

      <!-- ======================= VIP GROUPS TAB ======================= -->
      <Panel v-else-if="activeTab === 'vip'">
        <PermissionGroupEditor
          :config-data="store.currentConfig.config_data"
          @update:config-data="handlePermissionGroupUpdate"
        />
      </Panel>
    </template>

    <!-- Create config modal -->
    <Teleport to="body">
      <div v-if="showCreateModal" class="tp__overlay" @click.self="showCreateModal = false">
        <div class="tp__modal">
          <div class="tp__modal-head">
            <span class="tp__modal-title">New teleport config</span>
            <button class="tp__modal-close" type="button" @click="showCreateModal = false">
              <Icon name="x" :size="16" />
            </button>
          </div>
          <div class="tp__modal-body">
            <label class="tp__field">
              <span class="tp__field-label">Config name</span>
              <input
                v-model="newConfigName"
                type="text"
                class="cc-number"
                placeholder="e.g. Default TP settings"
                @keydown.enter="handleCreateConfig"
              />
            </label>
            <label class="tp__field tp__mt-sm">
              <span class="tp__field-label">Description (optional)</span>
              <textarea
                v-model="newConfigDesc"
                rows="2"
                class="cc-textarea"
                placeholder="What is this config for?"
              />
            </label>
          </div>
          <div class="tp__modal-foot">
            <Button variant="ghost" type="button" @click="showCreateModal = false">Cancel</Button>
            <Button icon="plus" :disabled="!newConfigName.trim()" @click="handleCreateConfig">Create</Button>
          </div>
        </div>
      </div>
    </Teleport>

    <!-- Import from server modal -->
    <Teleport to="body">
      <div v-if="showImportModal" class="tp__overlay" @click.self="showImportModal = false">
        <div class="tp__modal">
          <div class="tp__modal-head">
            <span class="tp__modal-title">Import from server</span>
            <button class="tp__modal-close" type="button" @click="showImportModal = false">
              <Icon name="x" :size="16" />
            </button>
          </div>
          <div class="tp__modal-body">
            <p class="tp__modal-desc">Import the current NTeleportation config from your live server. This will create a new config profile.</p>
            <label class="tp__field tp__mt-sm">
              <span class="tp__field-label">Config name</span>
              <input
                v-model="importConfigName"
                type="text"
                class="cc-number"
                placeholder="e.g. Imported server config"
                @keydown.enter="handleImport"
              />
            </label>
          </div>
          <div class="tp__modal-foot">
            <Button variant="ghost" type="button" @click="showImportModal = false">Cancel</Button>
            <Button icon="download" :disabled="!importConfigName.trim()" @click="handleImport">Import</Button>
          </div>
        </div>
      </div>
    </Teleport>
  </div>
</template>

<style scoped>
/* ---------- Shell ---------- */
.tp { max-width: 900px; margin: 0 auto; display: flex; flex-direction: column; gap: 16px; }

/* ---------- Page head ---------- */
.tp__head { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px; }
.tp__head-id { display: flex; align-items: center; gap: 12px; }
.tp__head-chip {
  width: 40px; height: 40px; flex: none; border-radius: var(--radius-md);
  display: flex; align-items: center; justify-content: center;
  color: var(--accent); background: var(--accent-soft);
  box-shadow: inset 0 0 0 1px var(--accent-border);
}
.tp__title {
  font-size: var(--text-2xl); font-weight: 700; letter-spacing: -0.02em;
  color: var(--text-primary); margin-top: 3px;
}
.tp__head-actions { display: flex; align-items: center; gap: 8px; }

/* ---------- Toolbar ---------- */
.tp__toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.tp__toolbar-spacer { flex: 1; }
.tp__config-select {
  appearance: none; height: var(--control-h-sm); padding: 0 32px 0 11px;
  background: var(--surface-inset); color: var(--text-primary); border: 0;
  border-radius: var(--radius-sm); box-shadow: var(--ring-default);
  font-family: var(--font-sans); font-size: var(--text-sm); cursor: pointer;
  min-width: 200px;
}
.tp__config-select:focus { outline: none; box-shadow: inset 0 0 0 1px var(--accent), var(--glow-accent-sm); }
.tp__no-configs { font-size: var(--text-sm); color: var(--text-muted); }

/* ---------- Loading ---------- */
.tp__loading { display: flex; align-items: center; justify-content: center; gap: 10px; padding: 40px; }
.tp__spinner {
  width: 20px; height: 20px; border-radius: 50%;
  border: 2px solid var(--border-default); border-top-color: var(--accent);
  animation: tp-spin 0.7s linear infinite;
}
@keyframes tp-spin { to { transform: rotate(360deg); } }
.tp__loading-label { font-size: var(--text-sm); color: var(--text-tertiary); }

/* ---------- Layout grids ---------- */
.tp__grid2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0; }
.tp__grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.tp__grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; }

@media (max-width: 768px) {
  .tp__grid2 { grid-template-columns: 1fr; }
  .tp__grid3 { grid-template-columns: repeat(2, 1fr); }
  .tp__grid4 { grid-template-columns: repeat(2, 1fr); }
}

/* ---------- Toggle row ---------- */
.tp__toggle-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 13px 0;
  border-bottom: 1px solid var(--border-subtle);
}
.tp__grid2 .tp__toggle-row:nth-child(odd) { padding-right: 16px; }
.tp__grid2 .tp__toggle-row:nth-child(even) { padding-left: 16px; }
.tp__toggle-row:last-child, .tp__toggle-row:nth-last-child(2):nth-child(odd) { border-bottom: 0; }
.tp__toggle-row:first-child { padding-top: 0; }
.tp__toggle-label { font-size: var(--text-sm); font-weight: 500; color: var(--text-primary); }
.tp__toggle-sub { font-size: var(--text-xs); color: var(--text-tertiary); margin-top: 2px; }
.tp__toggle-body { min-width: 0; }

/* ---------- Field ---------- */
.tp__field { display: flex; flex-direction: column; gap: 4px; }
.tp__field-label { font-size: var(--text-xs); font-weight: 600; color: var(--text-secondary); }
.tp__field-hint { font-size: var(--text-xs); color: var(--text-tertiary); }

/* ---------- Spacing helpers ---------- */
.tp__mt { margin-top: 16px; }
.tp__mt-sm { margin-top: 10px; }

/* ---------- Number / text input (bare token-styled) ---------- */
.cc-number {
  width: 100%; height: var(--control-h-sm); padding: 0 10px;
  background: var(--surface-inset); border: 0; border-radius: var(--radius-sm);
  box-shadow: var(--ring-default); font-family: var(--font-mono);
  font-size: var(--text-sm); color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}
.cc-number:focus { outline: none; box-shadow: inset 0 0 0 1px var(--accent), var(--glow-accent-sm); }
.cc-number::placeholder { color: var(--text-muted); font-family: var(--font-sans); }

/* ---------- Textarea ---------- */
.cc-textarea {
  width: 100%; padding: 8px 10px; min-height: 64px; resize: none;
  background: var(--surface-inset); border: 0; border-radius: var(--radius-sm);
  box-shadow: var(--ring-default); font-family: var(--font-sans);
  font-size: var(--text-sm); color: var(--text-primary); line-height: 1.5;
}
.cc-textarea:focus { outline: none; box-shadow: inset 0 0 0 1px var(--accent), var(--glow-accent-sm); }
.cc-textarea::placeholder { color: var(--text-muted); }

/* ---------- Modal ---------- */
.tp__overlay {
  position: fixed; inset: 0; background: color-mix(in srgb, var(--surface-canvas) 50%, transparent);
  z-index: 50; display: flex; align-items: center; justify-content: center; padding: 16px;
}
.tp__modal {
  background: var(--surface-base); border-radius: var(--radius-lg);
  box-shadow: var(--elevation-3); width: 100%; max-width: 440px;
  display: flex; flex-direction: column;
}
.tp__modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--border-subtle);
}
.tp__modal-title { font-size: var(--text-base); font-weight: 600; color: var(--text-primary); }
.tp__modal-close {
  display: flex; align-items: center; justify-content: center;
  width: 28px; height: 28px; border-radius: var(--radius-sm); border: none;
  background: transparent; color: var(--text-tertiary); cursor: pointer;
}
.tp__modal-close:hover { background: var(--surface-hover); color: var(--text-primary); }
.tp__modal-body { padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.tp__modal-desc { font-size: var(--text-sm); color: var(--text-tertiary); line-height: 1.5; }
.tp__modal-foot {
  display: flex; align-items: center; justify-content: flex-end; gap: 8px;
  padding: 12px 16px; border-top: 1px solid var(--border-subtle);
}
</style>
