<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useKitsStore } from '@/stores/kits'
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 Tabs from '@/components/ds/navigation/Tabs.vue'
import Input from '@/components/ds/forms/Input.vue'
import Select from '@/components/ds/forms/Select.vue'
import Switch from '@/components/ds/forms/Switch.vue'
import EmptyState from '@/components/ds/feedback/EmptyState.vue'

const store = useKitsStore()

const activeTab = ref<'kits' | 'editor' | 'settings'>('kits')
const showCreateModal = ref(false)
const showImportModal = ref(false)
const newConfigName = ref('')
const newConfigDesc = ref('')
const importConfigName = ref('')
const editingKitIndex = ref<number | null>(null)

const tabItems = [
  { value: 'kits', label: 'Kits list', icon: 'package' },
  { value: 'editor', label: 'Kit editor', icon: 'pencil' },
  { value: 'settings', label: 'Settings', icon: 'settings' },
]

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()
}

// --- Kit List helpers ---

const kitsList = computed(() => {
  return getConfigValue('Kits', []) as any[]
})

function addKit() {
  const kits = getConfigValue('Kits', []) as any[]
  kits.push({
    Name: `New Kit ${kits.length + 1}`,
    Description: '',
    Permission: '',
    Cooldown: 600,
    MaxUses: 0,
    IsHidden: false,
    Items: [],
  })
  setConfigValue('Kits', kits)
  editingKitIndex.value = kits.length - 1
  activeTab.value = 'editor'
}

function editKit(index: number) {
  editingKitIndex.value = index
  activeTab.value = 'editor'
}

function deleteKit(index: number) {
  const kits = getConfigValue('Kits', []) as any[]
  if (!confirm(`Delete kit "${kits[index]?.Name}"? This cannot be undone.`)) return
  kits.splice(index, 1)
  setConfigValue('Kits', kits)
  if (editingKitIndex.value === index) {
    editingKitIndex.value = null
    activeTab.value = 'kits'
  } else if (editingKitIndex.value !== null && editingKitIndex.value > index) {
    editingKitIndex.value--
  }
}

// --- Kit Editor helpers ---

const currentKit = computed(() => {
  if (editingKitIndex.value === null) return null
  const kits = getConfigValue('Kits', []) as any[]
  return kits[editingKitIndex.value] || null
})

function setKitField(field: string, value: any) {
  if (editingKitIndex.value === null) return
  const kits = getConfigValue('Kits', []) as any[]
  if (kits[editingKitIndex.value]) {
    kits[editingKitIndex.value][field] = value
    setConfigValue('Kits', kits)
  }
}

function addKitItem() {
  if (editingKitIndex.value === null) return
  const kits = getConfigValue('Kits', []) as any[]
  const kit = kits[editingKitIndex.value]
  if (!kit) return
  if (!kit.Items) kit.Items = []
  kit.Items.push({
    ShortName: '',
    Amount: 1,
    SkinId: 0,
    Container: 'main',
    Position: -1,
  })
  setConfigValue('Kits', kits)
}

function removeKitItem(itemIndex: number) {
  if (editingKitIndex.value === null) return
  const kits = getConfigValue('Kits', []) as any[]
  const kit = kits[editingKitIndex.value]
  if (!kit?.Items) return
  kit.Items.splice(itemIndex, 1)
  setConfigValue('Kits', kits)
}

function setKitItemField(itemIndex: number, field: string, value: any) {
  if (editingKitIndex.value === null) return
  const kits = getConfigValue('Kits', []) as any[]
  const kit = kits[editingKitIndex.value]
  if (!kit?.Items?.[itemIndex]) return
  kit.Items[itemIndex][field] = value
  setConfigValue('Kits', kits)
}

// --- Action handlers ---

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

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 kits config to the server? This will overwrite the current Kits 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 = ''
  }
}

// DS Input requires string v-model; modal fields are already string refs — direct bind.
// Config selector uses a bare <select> because Select.vue's options prop doesn't support
// option-level conditional text (" (Active)" suffix).

// Switch helper: convert getConfigValue result to boolean for Switch v-model
function getBool(path: string, def: boolean): boolean {
  return !!getConfigValue(path, def)
}

const containerOptions = [
  { value: 'main', label: 'Main' },
  { value: 'wear', label: 'Wear' },
  { value: 'belt', label: 'Belt' },
]

const currencyOptions = [
  { value: 'Scrap', label: 'Scrap' },
  { value: 'Economics', label: 'Economics' },
  { value: 'ServerRewards', label: 'ServerRewards' },
]

// Computed string binding for currency Select (DS model is string | undefined)
const currencyValue = computed({
  get: () => String(getConfigValue('Currency used for purchase costs (Scrap, Economics, ServerRewards)', 'Scrap') ?? 'Scrap'),
  set: (v: string | undefined) => setConfigValue('Currency used for purchase costs (Scrap, Economics, ServerRewards)', v ?? 'Scrap'),
})
</script>

<template>
  <div class="kv">
    <!-- Page head -->
    <div class="kv__head">
      <div class="kv__head-id">
        <div class="kv__head-chip">
          <Icon name="package" :size="20" :stroke-width="2" />
        </div>
        <div>
          <div class="t-eyebrow">Plugin config</div>
          <h1 class="kv__title">Kits</h1>
        </div>
      </div>
      <Button size="sm" icon="plus" @click="showCreateModal = true">New config</Button>
    </div>

    <!-- Config action bar -->
    <Panel>
      <div class="kv__bar">
        <select
          v-if="store.configs.length > 0"
          :value="store.currentConfig?.id ?? ''"
          class="kv__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="kv__no-configs">No configs yet</span>

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

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

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

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

    <!-- Loading -->
    <div v-if="store.isLoading" class="kv__loading">
      <span class="kv__spinner" />
    </div>

    <!-- Empty state -->
    <Panel v-else-if="!store.currentConfig">
      <EmptyState
        icon="package"
        title="No kits 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" />

      <!-- Kits list tab -->
      <template v-if="activeTab === 'kits'">
        <Panel>
          <template #actions>
            <Button size="sm" icon="plus" variant="secondary" @click="addKit">Add kit</Button>
          </template>

          <EmptyState
            v-if="kitsList.length === 0"
            icon="package"
            title="No kits defined"
            description="Add your first kit or import from server."
          />

          <div v-else class="kv__kit-grid">
            <div
              v-for="(kit, index) in kitsList"
              :key="index"
              class="kv__kit-card"
            >
              <div class="kv__kit-card-head">
                <div>
                  <div class="kv__kit-name">{{ kit.Name || 'Unnamed kit' }}</div>
                  <div v-if="kit.Description" class="kv__kit-desc">{{ kit.Description }}</div>
                </div>
                <div class="kv__kit-actions">
                  <button class="kv__icon-btn" title="Edit kit" @click="editKit(index)">
                    <Icon name="pencil" :size="15" />
                  </button>
                  <button class="kv__icon-btn kv__icon-btn--danger" title="Delete kit" @click="deleteKit(index)">
                    <Icon name="trash-2" :size="15" />
                  </button>
                </div>
              </div>

              <div class="kv__kit-meta">
                <div><span class="kv__meta-label">Permission</span><span class="kv__meta-val">{{ kit.Permission || 'None' }}</span></div>
                <div><span class="kv__meta-label">Cooldown</span><span class="kv__meta-val kv__mono">{{ kit.Cooldown || 0 }}s</span></div>
                <div><span class="kv__meta-label">Max uses</span><span class="kv__meta-val kv__mono">{{ kit.MaxUses || 0 }}{{ (kit.MaxUses || 0) === 0 ? ' (unlimited)' : '' }}</span></div>
                <div><span class="kv__meta-label">Items</span><span class="kv__meta-val kv__mono">{{ (kit.Items || []).length }}</span></div>
              </div>

              <div v-if="kit.IsHidden" class="kv__kit-hidden">Hidden — requires permission</div>
            </div>
          </div>
        </Panel>
      </template>

      <!-- Kit editor tab -->
      <template v-else-if="activeTab === 'editor'">
        <Panel v-if="!currentKit">
          <EmptyState
            icon="pencil"
            title="No kit selected"
            description="Select a kit from the Kits list tab to edit it."
          />
        </Panel>

        <template v-else>
          <!-- Kit details -->
          <Panel title="Kit details">
            <div class="kv__form-grid">
              <Input
                label="Kit name"
                :model-value="String(currentKit.Name ?? '')"
                @update:model-value="v => setKitField('Name', v ?? '')"
                placeholder="e.g. Starter kit"
              />
              <Input
                label="Permission"
                :model-value="String(currentKit.Permission ?? '')"
                @update:model-value="v => setKitField('Permission', v ?? '')"
                placeholder="e.g. kits.vip"
                :mono="true"
              />
            </div>

            <div class="kv__field kv__mt">
              <label class="kv__field-label">Description</label>
              <textarea
                class="cc-textarea"
                rows="2"
                :value="currentKit.Description"
                placeholder="Kit description…"
                @input="setKitField('Description', ($event.target as HTMLTextAreaElement).value)"
              />
            </div>

            <div class="kv__form-grid3 kv__mt">
              <div class="kv__field">
                <label class="kv__field-label">Cooldown (seconds)</label>
                <input
                  type="number"
                  class="cc-num-input"
                  :value="currentKit.Cooldown || 0"
                  min="0"
                  @input="setKitField('Cooldown', Number(($event.target as HTMLInputElement).value))"
                />
              </div>
              <div class="kv__field">
                <label class="kv__field-label">Max uses (0 = unlimited)</label>
                <input
                  type="number"
                  class="cc-num-input"
                  :value="currentKit.MaxUses || 0"
                  min="0"
                  @input="setKitField('MaxUses', Number(($event.target as HTMLInputElement).value))"
                />
              </div>
              <div class="kv__toggle-row">
                <div>
                  <div class="kv__toggle-label">Hidden</div>
                  <div class="kv__toggle-sub">No perm = hidden from players</div>
                </div>
                <Switch
                  :model-value="!!currentKit.IsHidden"
                  @update:model-value="v => setKitField('IsHidden', v)"
                />
              </div>
            </div>
          </Panel>

          <!-- Kit items -->
          <Panel title="Kit items">
            <template #actions>
              <Button size="sm" icon="plus" variant="secondary" @click="addKitItem">Add item</Button>
            </template>

            <EmptyState
              v-if="!currentKit.Items || currentKit.Items.length === 0"
              icon="package"
              title="No items"
              description="Click &quot;Add item&quot; to get started."
            />

            <div v-else class="kv__items">
              <div
                v-for="(item, itemIdx) in (currentKit.Items as any[])"
                :key="itemIdx"
                class="kv__item-row"
              >
                <div class="kv__item-fields">
                  <div class="kv__field">
                    <label class="kv__field-label">Short name</label>
                    <input
                      type="text"
                      class="cc-num-input"
                      :value="item.ShortName"
                      placeholder="e.g. rifle.ak"
                      @input="setKitItemField(itemIdx, 'ShortName', ($event.target as HTMLInputElement).value)"
                    />
                  </div>
                  <div class="kv__field">
                    <label class="kv__field-label">Amount</label>
                    <input
                      type="number"
                      class="cc-num-input"
                      :value="item.Amount || 1"
                      min="1"
                      @input="setKitItemField(itemIdx, 'Amount', Number(($event.target as HTMLInputElement).value))"
                    />
                  </div>
                  <div class="kv__field">
                    <label class="kv__field-label">Skin ID</label>
                    <input
                      type="number"
                      class="cc-num-input kv__mono"
                      :value="item.SkinId || 0"
                      min="0"
                      @input="setKitItemField(itemIdx, 'SkinId', Number(($event.target as HTMLInputElement).value))"
                    />
                  </div>
                  <Select
                    label="Container"
                    :options="containerOptions"
                    :model-value="String(item.Container ?? 'main')"
                    @update:model-value="v => setKitItemField(itemIdx, 'Container', v ?? 'main')"
                  />
                </div>
                <button class="kv__icon-btn kv__icon-btn--danger kv__item-del" title="Remove item" @click="removeKitItem(itemIdx)">
                  <Icon name="x" :size="15" />
                </button>
              </div>
            </div>
          </Panel>
        </template>
      </template>

      <!-- Settings tab -->
      <Panel v-else-if="activeTab === 'settings'" title="Global kit settings">
        <div class="kv__settings-grid">
          <Input
            label="Kit chat command"
            hint="The chat command players use to access kits"
            :model-value="String(getConfigValue('Kit chat command', 'kit') ?? 'kit')"
            @update:model-value="v => setConfigValue('Kit chat command', v ?? 'kit')"
            :mono="true"
          />

          <Select
            label="Currency for purchase costs"
            hint="Scrap, Economics, or ServerRewards"
            v-model="currencyValue"
            :options="currencyOptions"
          />
        </div>

        <div class="kv__toggles kv__mt">
          <div class="kv__toggle-row">
            <div>
              <div class="kv__toggle-label">Log kits given</div>
              <div class="kv__toggle-sub">Log when kits are claimed by players</div>
            </div>
            <Switch
              :model-value="getBool('Log kits given', false)"
              @update:model-value="v => setConfigValue('Log kits given', v)"
            />
          </div>
          <div class="kv__toggle-row">
            <div>
              <div class="kv__toggle-label">Wipe player data on server wipe</div>
              <div class="kv__toggle-sub">Reset kit cooldowns and usage when server wipes</div>
            </div>
            <Switch
              :model-value="getBool('Wipe player data when the server is wiped', true)"
              @update:model-value="v => setConfigValue('Wipe player data when the server is wiped', v)"
            />
          </div>
          <div class="kv__toggle-row">
            <div>
              <div class="kv__toggle-label">Use kits UI menu</div>
              <div class="kv__toggle-sub">Show the in-game kits UI menu to players</div>
            </div>
            <Switch
              :model-value="getBool('Use the Kits UI menu', true)"
              @update:model-value="v => setConfigValue('Use the Kits UI menu', v)"
            />
          </div>
          <div class="kv__toggle-row">
            <div>
              <div class="kv__toggle-label">Allow auto-kit toggle</div>
              <div class="kv__toggle-sub">Let players toggle auto-kits on spawn</div>
            </div>
            <Switch
              :model-value="getBool('Allow players to toggle auto-kits on spawn', false)"
              @update:model-value="v => setConfigValue('Allow players to toggle auto-kits on spawn', v)"
            />
          </div>
          <div class="kv__toggle-row">
            <div>
              <div class="kv__toggle-label">Show kits without permission</div>
              <div class="kv__toggle-sub">Show permission-locked kits to players who lack them</div>
            </div>
            <Switch
              :model-value="getBool('Show kits with permissions assigned to players without the permission', false)"
              @update:model-value="v => setConfigValue('Show kits with permissions assigned to players without the permission', v)"
            />
          </div>
          <div class="kv__toggle-row">
            <div>
              <div class="kv__toggle-label">Admins ignore restrictions</div>
              <div class="kv__toggle-sub">Players with admin perm skip cooldown and usage limits</div>
            </div>
            <Switch
              :model-value="getBool('Players with the admin permission ignore usage restrictions', false)"
              @update:model-value="v => setConfigValue('Players with the admin permission ignore usage restrictions', v)"
            />
          </div>
        </div>

        <div class="kv__field kv__mt">
          <Input
            label="Auto-kits (ordered by priority)"
            hint="Comma-separated list of kit names given on respawn"
            :model-value="(getConfigValue('Autokits ordered by priority', []) as string[]).join(', ')"
            @update:model-value="v => setConfigValue('Autokits ordered by priority', (v ?? '').split(',').map((s: string) => s.trim()).filter((s: string) => s))"
            placeholder="e.g. StarterKit, VIPKit"
          />
        </div>
      </Panel>
    </template>

    <!-- Create config modal -->
    <div v-if="showCreateModal" class="kv__modal-backdrop" @click.self="showCreateModal = false">
      <div class="kv__modal">
        <h2 class="kv__modal-title">New kits config</h2>
        <div class="kv__modal-body">
          <Input
            label="Config name"
            v-model="newConfigName"
            placeholder="e.g. Default kits"
            @keydown.enter="handleCreateConfig"
          />
          <div class="kv__field">
            <label class="kv__field-label">Description (optional)</label>
            <textarea
              v-model="newConfigDesc"
              rows="2"
              class="cc-textarea"
              placeholder="What is this config for?"
            />
          </div>
        </div>
        <div class="kv__modal-footer">
          <Button variant="ghost" @click="showCreateModal = false">Cancel</Button>
          <Button :disabled="!newConfigName.trim()" @click="handleCreateConfig">Create</Button>
        </div>
      </div>
    </div>

    <!-- Import from server modal -->
    <div v-if="showImportModal" class="kv__modal-backdrop" @click.self="showImportModal = false">
      <div class="kv__modal">
        <h2 class="kv__modal-title">Import from server</h2>
        <p class="kv__modal-desc">Import the current Kits config from your live server. This will create a new config profile.</p>
        <div class="kv__modal-body">
          <Input
            label="Config name"
            v-model="importConfigName"
            placeholder="e.g. Imported server kits"
            @keydown.enter="handleImport"
          />
        </div>
        <div class="kv__modal-footer">
          <Button variant="ghost" @click="showImportModal = false">Cancel</Button>
          <Button :disabled="!importConfigName.trim()" @click="handleImport">Import</Button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.kv { max-width: 960px; margin: 0 auto; display: flex; flex-direction: column; gap: 16px; }

/* Page head */
.kv__head { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px; }
.kv__head-id { display: flex; align-items: center; gap: 12px; }
.kv__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);
}
.kv__title {
  font-size: var(--text-2xl); font-weight: 700; letter-spacing: -0.02em;
  color: var(--text-primary); margin-top: 3px;
}

/* Config action bar */
.kv__bar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.kv__config-select {
  appearance: none; height: var(--control-h-md); padding: 0 11px;
  background: var(--surface-inset); color: var(--text-primary); border: 0;
  border-radius: var(--radius-md); box-shadow: var(--ring-default);
  font-family: var(--font-sans); font-size: var(--text-sm); cursor: pointer;
  min-width: 200px;
}
.kv__config-select:focus-visible { outline: none; box-shadow: inset 0 0 0 1px var(--accent), var(--glow-accent-sm); }
.kv__no-configs { font-size: var(--text-sm); color: var(--text-muted); }
.kv__bar-delete { margin-left: auto; }

/* Loading */
.kv__loading { display: flex; align-items: center; justify-content: center; padding: 60px; }
.kv__spinner {
  width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--accent);
  border-top-color: transparent; animation: kv-spin 0.6s linear infinite;
}
@keyframes kv-spin { to { transform: rotate(360deg); } }

/* Kit grid */
.kv__kit-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.kv__kit-card {
  background: var(--surface-raised-2); border-radius: var(--radius-md);
  box-shadow: var(--ring-default); padding: 14px; display: flex; flex-direction: column; gap: 10px;
}
.kv__kit-card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.kv__kit-name { font-size: var(--text-sm); font-weight: 600; color: var(--text-primary); }
.kv__kit-desc { font-size: var(--text-xs); color: var(--text-tertiary); margin-top: 2px; }
.kv__kit-actions { display: flex; align-items: center; gap: 2px; flex: none; }
.kv__kit-meta { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 8px; }
.kv__meta-label { font-size: var(--text-xs); color: var(--text-tertiary); }
.kv__meta-val { font-size: var(--text-xs); color: var(--text-primary); margin-left: 4px; }
.kv__kit-hidden { font-size: var(--text-xs); color: var(--status-warn); }

/* Icon button */
.kv__icon-btn {
  width: 28px; height: 28px; border-radius: var(--radius-sm); border: none; background: transparent;
  color: var(--text-tertiary); display: flex; align-items: center; justify-content: center;
  cursor: pointer; transition: var(--transition-colors);
}
.kv__icon-btn:hover { background: var(--surface-hover); color: var(--text-primary); }
.kv__icon-btn--danger:hover { background: var(--status-offline-soft); color: var(--danger); }

/* Kit editor */
.kv__form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.kv__form-grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 14px; }
@media (max-width: 700px) {
  .kv__form-grid { grid-template-columns: 1fr; }
  .kv__form-grid3 { grid-template-columns: 1fr; }
}
.kv__mt { margin-top: 16px; }

/* Fields */
.kv__field { display: flex; flex-direction: column; gap: 6px; }
.kv__field-label { font-size: var(--text-xs); font-weight: 600; color: var(--text-secondary); }

/* Toggle row */
.kv__toggle-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 12px 0; border-bottom: 1px solid var(--border-subtle);
}
.kv__toggle-row:last-child { border-bottom: 0; padding-bottom: 0; }
.kv__toggle-row:first-child { padding-top: 0; }
.kv__toggle-label { font-size: var(--text-sm); font-weight: 500; color: var(--text-primary); }
.kv__toggle-sub { font-size: var(--text-xs); color: var(--text-tertiary); margin-top: 2px; }
.kv__toggles { display: flex; flex-direction: column; }

/* Settings */
.kv__settings-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 700px) { .kv__settings-grid { grid-template-columns: 1fr; } }

/* Item rows */
.kv__items { display: flex; flex-direction: column; gap: 8px; }
.kv__item-row {
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--surface-raised-2); border-radius: var(--radius-md);
  box-shadow: var(--ring-default); padding: 12px;
}
.kv__item-fields { flex: 1; display: grid; grid-template-columns: 2fr 1fr 1fr 1fr; gap: 10px; }
@media (max-width: 700px) { .kv__item-fields { grid-template-columns: 1fr 1fr; } }
.kv__item-del { flex: none; margin-bottom: 0; }

/* Shared token inputs */
.cc-textarea {
  width: 100%; background: var(--surface-inset); color: var(--text-primary);
  border: 0; border-radius: var(--radius-md); box-shadow: var(--ring-default);
  padding: 9px 11px; font-family: var(--font-sans); font-size: var(--text-sm);
  resize: none; outline: 0; line-height: 1.5;
}
.cc-textarea:focus-visible { box-shadow: inset 0 0 0 1px var(--accent), var(--glow-accent-sm); }
.cc-num-input {
  width: 100%; height: var(--control-h-md); background: var(--surface-inset); color: var(--text-primary);
  border: 0; border-radius: var(--radius-md); box-shadow: var(--ring-default);
  padding: 0 11px; font-family: var(--font-sans); font-size: var(--text-sm); outline: 0;
}
.cc-num-input:focus-visible { box-shadow: inset 0 0 0 1px var(--accent), var(--glow-accent-sm); }
.kv__mono { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }

/* Modal */
.kv__modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.55); z-index: 50;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.kv__modal {
  background: var(--surface-base); border-radius: var(--radius-lg); box-shadow: var(--ring-default);
  width: 100%; max-width: 440px; padding: 24px; display: flex; flex-direction: column; gap: 16px;
}
.kv__modal-title { font-size: var(--text-lg); font-weight: 700; color: var(--text-primary); }
.kv__modal-desc { font-size: var(--text-sm); color: var(--text-tertiary); line-height: 1.5; }
.kv__modal-body { display: flex; flex-direction: column; gap: 12px; }
.kv__modal-footer { display: flex; justify-content: flex-end; gap: 8px; }
</style>
