<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { useAdminLocale } from '../composables/useAdminLocale';
import { resolveApiError } from '../i18n/form-validation';
import api from '../api';
import AdminTableEmpty from '../components/AdminTableEmpty.vue';
import { formatAmount } from '../utils/format-amount';

const { t } = useAdminLocale();

interface DepositOrderRow {
  id: string;
  orderNo: string;
  playerId: string;
  playerUsername: string | null;
  methodType: string;
  amount: string;
  screenshotUrl: string;
  status: string;
  approvedAmount: string | null;
  reviewerId: string | null;
  reviewerUsername: string | null;
  rejectReason: string | null;
  remark: string | null;
  createdAt: string;
  reviewedAt: string | null;
  paymentMethodName: string | null;
}

interface DepositAuditLogRow {
  id: string;
  action: string;
  actorId: string | null;
  actorType: string;
  actorUsername: string | null;
  statusBefore: string | null;
  statusAfter: string;
  amount: string | null;
  approvedAmount: string | null;
  remark: string | null;
  createdAt: string;
}

const items = ref<DepositOrderRow[]>([]);
const total = ref(0);
const page = ref(1);
const pageSize = ref(20);
const loading = ref(false);

// Filters
const statusFilter = ref('');
const keywordFilter = ref('');
const methodTypeFilter = ref('');

// Approve dialog
const approveDialogVisible = ref(false);
const approveTarget = ref<DepositOrderRow | null>(null);
const approveAmount = ref<number>(0);
const approveRemark = ref('');

// Reject dialog
const rejectDialogVisible = ref(false);
const rejectTarget = ref<DepositOrderRow | null>(null);
const rejectReason = ref('');

// Screenshot preview
const previewUrl = ref('');
const previewVisible = ref(false);

// Audit history dialog
const auditDialogVisible = ref(false);
const auditTarget = ref<DepositOrderRow | null>(null);
const auditLogs = ref<DepositAuditLogRow[]>([]);
const auditLoading = ref(false);

async function fetchList() {
  loading.value = true;
  try {
    const params: any = { page: page.value, pageSize: pageSize.value };
    if (statusFilter.value) params.status = statusFilter.value;
    if (keywordFilter.value) params.keyword = keywordFilter.value;
    if (methodTypeFilter.value) params.methodType = methodTypeFilter.value;
    const { data } = await api.get('/admin/deposit-orders', { params });
    const result = data.data ?? { items: [], total: 0 };
    items.value = result.items ?? [];
    total.value = result.total ?? 0;
  } catch { /* */ } finally {
    loading.value = false;
  }
}

function openApprove(row: DepositOrderRow) {
  approveTarget.value = row;
  approveAmount.value = parseFloat(row.amount);
  approveRemark.value = '';
  approveDialogVisible.value = true;
}

function openReject(row: DepositOrderRow) {
  rejectTarget.value = row;
  rejectReason.value = '';
  rejectDialogVisible.value = true;
}

function openScreenshot(url: string) {
  previewUrl.value = url;
  previewVisible.value = true;
}

async function openAuditHistory(row: DepositOrderRow) {
  auditTarget.value = row;
  auditDialogVisible.value = true;
  auditLoading.value = true;
  auditLogs.value = [];
  try {
    const { data } = await api.get(`/admin/deposit-orders/${row.id}/audit-logs`);
    auditLogs.value = data.data?.items ?? [];
  } catch {
    auditLogs.value = [];
  } finally {
    auditLoading.value = false;
  }
}

function auditActionLabel(action: string) {
  const key = `deposit.audit_${action.toLowerCase()}`;
  const translated = t(key);
  return translated !== key ? translated : action;
}

function auditActorLabel(row: DepositAuditLogRow) {
  if (row.actorUsername) return row.actorUsername;
  return row.actorType === 'PLAYER' ? t('deposit.player') : '-';
}

async function confirmDepositAction(message: string, title: string): Promise<boolean> {
  try {
    await ElMessageBox.confirm(message, title, {
      type: 'warning',
      confirmButtonText: t('common.confirm'),
      cancelButtonText: t('common.cancel'),
    });
    return true;
  } catch {
    return false;
  }
}

function showApiError(e: unknown) {
  ElMessage.error(resolveApiError(e, t, 'deposit.delete_failed'));
}

async function handleApprove() {
  if (!approveTarget.value) return;
  const amountText = formatAmount(String(approveAmount.value));
  if (!await confirmDepositAction(
    t('deposit.confirm_approve_message', { amount: amountText }),
    t('deposit.confirm_approve'),
  )) {
    return;
  }
  try {
    await api.post(`/admin/deposit-orders/${approveTarget.value.id}/approve`, {
      approvedAmount: approveAmount.value,
      remark: approveRemark.value || undefined,
    });
    approveDialogVisible.value = false;
    await fetchList();
  } catch (e: unknown) {
    showApiError(e);
  }
}

async function handleReject() {
  if (!rejectTarget.value) return;
  if (!rejectReason.value.trim()) {
    ElMessage.warning(t('deposit.reason_required'));
    return;
  }
  try {
    await api.post(`/admin/deposit-orders/${rejectTarget.value.id}/reject`, {
      reason: rejectReason.value.trim(),
    });
    rejectDialogVisible.value = false;
    await fetchList();
  } catch (e: unknown) {
    showApiError(e);
  }
}

const DEPOSIT_REVOKE_WINDOW_MS = 5 * 60 * 1000;

function canRevokeApproved(row: DepositOrderRow): boolean {
  if (row.status !== 'APPROVED' || !row.reviewedAt) return false;
  return Date.now() - new Date(row.reviewedAt).getTime() <= DEPOSIT_REVOKE_WINDOW_MS;
}

async function handleReopen(row: DepositOrderRow) {
  if (!await confirmDepositAction(t('deposit.confirm_reopen'), t('deposit.reopen_review'))) return;
  try {
    await api.post(`/admin/deposit-orders/${row.id}/reopen`);
    await fetchList();
  } catch (e: unknown) {
    showApiError(e);
  }
}

async function handleRevoke(row: DepositOrderRow) {
  if (!await confirmDepositAction(t('deposit.confirm_revoke'), t('deposit.revoke'))) return;
  try {
    await api.post(`/admin/deposit-orders/${row.id}/reopen`);
    await fetchList();
  } catch (e: unknown) {
    showApiError(e);
  }
}

async function handleDelete(row: DepositOrderRow) {
  if (!await confirmDepositAction(t('deposit.confirm_delete'), t('deposit.delete'))) return;
  try {
    await api.delete(`/admin/deposit-orders/${row.id}`);
    await fetchList();
  } catch (e: unknown) {
    showApiError(e);
  }
}

function statusClass(s: string) {
  if (s === 'APPROVED') return 'status-approved';
  if (s === 'REJECTED') return 'status-rejected';
  return 'status-pending';
}

function statusLabel(s: string) {
  if (s === 'APPROVED') return '✓ ' + t('deposit.status_approved');
  if (s === 'REJECTED') return '✗ ' + t('deposit.status_rejected');
  return '● ' + t('deposit.status_pending');
}

function prevPage() { if (page.value > 1) { page.value--; fetchList(); } }
function nextPage() { if (page.value * pageSize.value < total.value) { page.value++; fetchList(); } }

onMounted(fetchList);
</script>

<template>
  <div class="page-deposit-orders">
    <div class="toolbar">
      <h2>{{ t('deposit.deposit_orders_title') }}</h2>
    </div>

    <div class="filters">
      <select v-model="statusFilter" @change="page = 1; fetchList()">
        <option value="">{{ t('deposit.all_status') }}</option>
        <option value="PENDING">{{ t('deposit.status_pending') }}</option>
        <option value="APPROVED">{{ t('deposit.status_approved') }}</option>
        <option value="REJECTED">{{ t('deposit.status_rejected') }}</option>
      </select>
      <select v-model="methodTypeFilter" @change="page = 1; fetchList()">
        <option value="">{{ t('deposit.all_types') }}</option>
        <option value="BANK">Bank</option>
        <option value="USDT">USDT</option>
      </select>
      <input
        v-model="keywordFilter"
        :placeholder="t('deposit.search_player_ph')"
        @keydown.enter="page = 1; fetchList()"
      />
      <button class="btn-search" @click="page = 1; fetchList()">{{ t('common.search') }}</button>
    </div>

    <AdminTableEmpty v-if="!loading && !items.length" />

    <table v-if="items.length" class="data-table">
      <thead>
        <tr>
          <th>{{ t('deposit.order_no') }}</th>
          <th>{{ t('deposit.player') }}</th>
          <th>{{ t('common.type') }}</th>
          <th>{{ t('deposit.amount') }}</th>
          <th>{{ t('deposit.screenshot') }}</th>
          <th>{{ t('common.status') }}</th>
          <th>{{ t('deposit.approved_amount') }}</th>
          <th>{{ t('deposit.reviewer') }}</th>
          <th>{{ t('user.field.remark') }}</th>
          <th>{{ t('deposit.time') }}</th>
          <th>{{ t('common.actions') }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="row in items" :key="row.id">
          <td class="mono">{{ row.orderNo }}</td>
          <td>{{ row.playerUsername || row.playerId }}</td>
          <td><span :class="['badge', row.methodType === 'BANK' ? 'badge-blue' : 'badge-green']">{{ row.methodType }}</span></td>
          <td class="amount">{{ formatAmount(row.amount) }}</td>
          <td>
            <img
              v-if="row.screenshotUrl"
              :src="row.screenshotUrl"
              class="screenshot-thumb"
              @click="openScreenshot(row.screenshotUrl)"
            />
          </td>
          <td><span :class="statusClass(row.status)">{{ statusLabel(row.status) }}</span></td>
          <td>{{ row.approvedAmount ? formatAmount(row.approvedAmount) : '-' }}</td>
          <td>{{ row.reviewerUsername || '-' }}</td>
          <td class="remark-cell">{{ row.remark || row.rejectReason || '-' }}</td>
          <td class="time-cell">{{ new Date(row.createdAt).toLocaleString() }}</td>
          <td>
            <button class="btn-sm btn-audit" @click="openAuditHistory(row)">{{ t('deposit.audit_history') }}</button>
            <template v-if="row.status === 'PENDING'">
              <button class="btn-sm btn-approve" @click="openApprove(row)">{{ t('deposit.approve') }}</button>
              <button class="btn-sm btn-reject" @click="openReject(row)">{{ t('deposit.reject') }}</button>
              <button class="btn-sm btn-delete" @click="handleDelete(row)">{{ t('deposit.delete') }}</button>
            </template>
            <template v-else-if="row.status === 'APPROVED'">
              <button
                v-if="canRevokeApproved(row)"
                class="btn-sm btn-reopen"
                @click="handleRevoke(row)"
              >
                {{ t('deposit.revoke') }}
              </button>
              <button class="btn-sm btn-delete" @click="handleDelete(row)">{{ t('deposit.delete') }}</button>
            </template>
            <template v-else-if="row.status === 'REJECTED'">
              <button class="btn-sm btn-reopen" @click="handleReopen(row)">{{ t('deposit.reopen_review') }}</button>
              <button class="btn-sm btn-delete" @click="handleDelete(row)">{{ t('deposit.delete') }}</button>
            </template>
          </td>
        </tr>
      </tbody>
    </table>

    <div v-if="total > pageSize" class="pagination">
      <button :disabled="page <= 1" @click="prevPage">{{ t('deposit.prev') }}</button>
      <span>{{ page }} / {{ Math.ceil(total / pageSize) }}</span>
      <button :disabled="page * pageSize >= total" @click="nextPage">{{ t('deposit.next') }}</button>
    </div>

    <!-- Approve Dialog -->
    <div v-if="approveDialogVisible" class="dialog-overlay">
      <div class="dialog-box">
        <h3>{{ t('deposit.approve_title') }}</h3>
        <p class="approve-check-hint">{{ t('deposit.approve_check_hint') }}</p>
        <div v-if="approveTarget" class="approve-content">
          <div class="info-row">
            <span>{{ t('deposit.player') }}:</span> <strong>{{ approveTarget.playerUsername }}</strong>
          </div>
          <div class="info-row">
            <span>{{ t('deposit.submitted_amount') }}:</span> <strong>{{ formatAmount(approveTarget.amount) }}</strong>
          </div>
          <div class="info-row">
            <span>{{ t('deposit.screenshot') }}:</span>
            <img :src="approveTarget.screenshotUrl" class="approve-screenshot" @click="openScreenshot(approveTarget.screenshotUrl)" />
          </div>
          <div class="form-group">
            <label>{{ t('deposit.approved_amount_label') }}</label>
            <input v-model.number="approveAmount" type="number" step="0.01" min="0.01" />
          </div>
          <div class="form-group">
            <label>{{ t('deposit.remark_label') }}</label>
            <input v-model="approveRemark" />
          </div>
        </div>
        <div class="dialog-actions">
          <button class="btn-cancel" @click="approveDialogVisible = false">{{ t('common.cancel') }}</button>
          <button class="btn-primary" @click="handleApprove">{{ t('deposit.confirm_approve') }}</button>
        </div>
      </div>
    </div>

    <!-- Reject Dialog -->
    <div v-if="rejectDialogVisible" class="dialog-overlay">
      <div class="dialog-box">
        <h3>{{ t('deposit.reject_title') }}</h3>
        <div v-if="rejectTarget" class="reject-content">
          <div class="info-row">
            <span>{{ t('deposit.player') }}:</span> <strong>{{ rejectTarget.playerUsername }}</strong>
          </div>
          <div class="info-row">
            <span>{{ t('deposit.amount') }}:</span> <strong>{{ formatAmount(rejectTarget.amount) }}</strong>
          </div>
          <div class="form-group">
            <label>{{ t('deposit.reject_reason_label') }}</label>
            <textarea v-model="rejectReason" rows="3" :placeholder="t('deposit.reject_reason_ph')"></textarea>
          </div>
        </div>
        <div class="dialog-actions">
          <button class="btn-cancel" @click="rejectDialogVisible = false">{{ t('common.cancel') }}</button>
          <button class="btn-danger-full" @click="handleReject">{{ t('deposit.confirm_reject') }}</button>
        </div>
      </div>
    </div>

    <!-- Audit History Dialog -->
    <div v-if="auditDialogVisible" class="dialog-overlay">
      <div class="dialog-box audit-dialog">
        <h3>{{ t('deposit.audit_history') }}</h3>
        <p v-if="auditTarget" class="audit-order-no">{{ auditTarget.orderNo }} · {{ auditTarget.playerUsername || auditTarget.playerId }}</p>
        <div v-if="auditLoading" class="audit-loading">{{ t('common.loading') }}</div>
        <div v-else-if="!auditLogs.length" class="audit-empty">{{ t('deposit.audit_empty') }}</div>
        <table v-else class="audit-table">
          <thead>
            <tr>
              <th>{{ t('deposit.audit_action') }}</th>
              <th>{{ t('deposit.audit_actor') }}</th>
              <th>{{ t('deposit.approved_amount') }}</th>
              <th>{{ t('deposit.audit_remark') }}</th>
              <th>{{ t('deposit.audit_time') }}</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="log in auditLogs" :key="log.id">
              <td>{{ auditActionLabel(log.action) }}</td>
              <td>{{ auditActorLabel(log) }}</td>
              <td>{{ log.approvedAmount ? formatAmount(log.approvedAmount) : (log.amount ? formatAmount(log.amount) : '-') }}</td>
              <td class="remark-cell">{{ log.remark || '-' }}</td>
              <td class="time-cell">{{ new Date(log.createdAt).toLocaleString() }}</td>
            </tr>
          </tbody>
        </table>
        <div class="dialog-actions">
          <button class="btn-cancel" @click="auditDialogVisible = false">{{ t('common.cancel') }}</button>
        </div>
      </div>
    </div>

    <!-- Screenshot Preview -->
    <div v-if="previewVisible" class="dialog-overlay">
      <div class="preview-box">
        <img :src="previewUrl" class="preview-image" />
        <button class="close-preview" @click="previewVisible = false">✕</button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.page-deposit-orders { padding: 16px; }
.toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.toolbar h2 { margin: 0; font-size: 18px; }
.filters { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
.filters select, .filters input { padding: 6px 10px; border-radius: 4px; border: 1px solid #444; background: #1e1e1e; color: #eee; font-size: 13px; }
.filters input { min-width: 160px; }
.btn-search { background: #409eff; color: #fff; border: none; border-radius: 4px; padding: 6px 14px; cursor: pointer; font-weight: 600; font-size: 13px; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th, .data-table td { padding: 10px 6px; border-bottom: 1px solid #333; text-align: left; }
.data-table th { font-weight: 700; color: #aaa; font-size: 11px; text-transform: uppercase; }
.mono { font-family: monospace; font-size: 11px; }
.amount { font-weight: 700; }
.time-cell { font-size: 11px; color: #888; }
.remark-cell { max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; color: #aaa; }
.screenshot-thumb { width: 40px; height: 40px; object-fit: cover; border-radius: 4px; cursor: pointer; border: 1px solid #444; }
.badge { padding: 2px 8px; border-radius: 10px; font-size: 11px; font-weight: 700; }
.badge-blue { background: #1e3a5f; color: #66b1ff; }
.badge-green { background: rgba(201, 162, 39, 0.12); color: var(--gold-text); }
.status-pending { color: #e6a23c; font-weight: 700; font-size: 12px; }
.status-approved { color: var(--success-text); font-weight: 700; font-size: 12px; }
.status-rejected { color: #f56c6c; font-weight: 700; font-size: 12px; }
.btn-sm { border: none; border-radius: 4px; padding: 4px 10px; font-size: 11px; cursor: pointer; margin-right: 4px; }
.btn-approve { background: rgba(61, 115, 88, 0.16); color: var(--success-text); }
.btn-approve:hover { background: rgba(61, 115, 88, 0.24); }
.btn-reject { background: #3a1a1a; color: #f56c6c; }
.btn-reject:hover { background: #4a2525; }
.btn-reopen { background: #2a2410; color: #e6a23c; }
.btn-reopen:hover { background: #3a3218; }
.action-hint {
  display: inline-block;
  margin-left: 4px;
  font-size: 11px;
  color: #888;
  vertical-align: middle;
}
.btn-audit { background: #1f2433; color: #8ab4ff; }
.btn-audit:hover { background: #2a3348; }
.btn-delete { background: #2a2a2a; color: #aaa; }
.btn-delete:hover { background: #3a3a3a; color: #f56c6c; }
.actions-cell { white-space: nowrap; }
.pagination { display: flex; justify-content: center; align-items: center; gap: 12px; margin-top: 16px; }
.pagination button { background: #333; color: #ddd; border: none; border-radius: 4px; padding: 6px 14px; cursor: pointer; }
.pagination button:disabled { opacity: 0.4; cursor: default; }

.dialog-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center; z-index: 999; }
.dialog-box { background: #1e1e1e; border-radius: 8px; padding: 24px; min-width: 440px; max-width: 560px; }
.dialog-box h3 { margin: 0 0 16px; font-size: 16px; }
.approve-check-hint {
  margin: -8px 0 14px;
  padding: 10px 12px;
  border-radius: 6px;
  background: rgba(230, 162, 60, 0.12);
  border: 1px solid rgba(230, 162, 60, 0.35);
  color: #e6a23c;
  font-size: 13px;
  line-height: 1.5;
}
.info-row { margin-bottom: 8px; font-size: 13px; }
.info-row span { color: #888; margin-right: 4px; }
.approve-screenshot { width: 200px; max-height: 200px; object-fit: contain; border-radius: 4px; cursor: pointer; margin-top: 4px; }
.form-group { margin-bottom: 12px; }
.form-group label { display: block; font-size: 12px; color: #aaa; margin-bottom: 4px; font-weight: 600; }
.form-group input, .form-group textarea { width: 100%; padding: 8px; border: 1px solid #444; border-radius: 4px; background: #111; color: #eee; box-sizing: border-box; }
.form-group textarea { resize: vertical; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
.btn-cancel { background: #333; color: #ccc; border: none; border-radius: 4px; padding: 8px 16px; cursor: pointer; }
.btn-primary { background: #409eff; color: #fff; border: none; border-radius: 4px; padding: 8px 16px; cursor: pointer; font-weight: 600; }
.btn-danger-full { background: #f56c6c; color: #fff; border: none; border-radius: 4px; padding: 8px 16px; cursor: pointer; font-weight: 600; }

.preview-box { position: relative; max-width: 90vw; max-height: 90vh; }
.preview-image { max-width: 90vw; max-height: 85vh; object-fit: contain; border-radius: 8px; }
.close-preview { position: absolute; top: -12px; right: -12px; background: #333; color: #fff; border: none; border-radius: 50%; width: 28px; height: 28px; font-size: 14px; cursor: pointer; }

.page-deposit-orders {
  padding: 0;
  color: var(--text);
}

.toolbar {
  margin-bottom: 14px;
}

.toolbar h2 {
  color: var(--text);
  font-size: 20px;
  font-weight: 800;
}

.filters {
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #ffffff;
  box-shadow: var(--shadow);
}

.filters select,
.filters input {
  min-height: 34px;
  border-color: var(--border);
  border-radius: 7px;
  background: #ffffff;
  color: var(--text);
}

.filters select:focus,
.filters input:focus {
  border-color: var(--primary);
  outline: 3px solid rgba(31, 35, 32, 0.08);
}

.btn-search,
.btn-primary {
  border: 1px solid var(--primary);
  border-radius: 7px;
  background: var(--primary);
  color: #ffffff;
}

.data-table {
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #ffffff;
  box-shadow: var(--shadow);
}

.data-table th,
.data-table td {
  border-bottom-color: var(--border-soft);
  color: var(--text);
}

.data-table th {
  background: #fbfaf7;
  color: var(--text-muted);
  text-transform: none;
  letter-spacing: 0;
}

.data-table tbody tr:hover {
  background: var(--accent-hover);
}

.time-cell,
.remark-cell,
.info-row span,
.form-group label {
  color: var(--text-muted);
}

.screenshot-thumb,
.approve-screenshot {
  border-color: var(--border);
  background: #fbfaf7;
}

.badge,
.status-pending,
.status-approved,
.status-rejected {
  border-radius: 999px;
}

.badge-blue {
  background: var(--info-bg);
  color: var(--info-text);
}

.badge-green {
  background: var(--success-bg);
  color: var(--success-text);
}

.status-pending {
  color: var(--warning-text);
}

.status-rejected {
  color: var(--danger-text);
}

.btn-sm {
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #ffffff;
  color: var(--text);
  font-weight: 700;
}

.btn-sm:hover {
  background: var(--accent-hover);
}

.btn-approve {
  border-color: var(--success-border);
  background: var(--success-bg);
  color: var(--success-text);
}

.btn-reject {
  border-color: var(--danger-border);
  background: var(--danger-bg);
  color: var(--danger-text);
}

.btn-reopen {
  border-color: var(--warning-border);
  background: var(--warning-bg);
  color: var(--warning-text);
}

.btn-delete {
  border-color: var(--border);
  background: #ffffff;
  color: var(--text-muted);
}

.btn-delete:hover {
  border-color: var(--danger-border);
  background: var(--danger-bg);
  color: var(--danger-text);
}

.pagination button {
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #ffffff;
  color: var(--text);
}

.pagination button:not(:disabled):hover {
  background: var(--accent-hover);
}

.dialog-overlay {
  background: rgba(31, 35, 32, 0.28);
  backdrop-filter: blur(3px);
}

.dialog-box {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #ffffff;
  color: var(--text);
  box-shadow: 0 24px 64px rgba(56, 49, 37, 0.14);
}

.dialog-box h3 {
  color: var(--text);
}

.approve-check-hint {
  border-color: var(--warning-border);
  background: var(--warning-bg);
  color: var(--warning-text);
}

.form-group input,
.form-group textarea {
  border-color: var(--border);
  border-radius: 7px;
  background: #ffffff;
  color: var(--text);
}

.btn-cancel {
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #ffffff;
  color: var(--text);
}

.btn-danger-full {
  border: 1px solid var(--danger-text);
  border-radius: 7px;
  background: var(--danger-text);
}

.audit-dialog { min-width: 520px; max-width: 760px; }
.audit-order-no { margin: -8px 0 12px; font-size: 12px; color: var(--text-muted); }
.audit-loading, .audit-empty { padding: 20px 0; text-align: center; color: var(--text-muted); font-size: 13px; }
.audit-table { width: 100%; border-collapse: collapse; font-size: 12px; margin-bottom: 8px; }
.audit-table th, .audit-table td { padding: 8px 6px; border-bottom: 1px solid var(--border-soft); text-align: left; }
.audit-table th { color: var(--text-muted); font-weight: 700; }

.close-preview {
  background: var(--text);
  color: #ffffff;
}

@media (max-width: 700px) {
  .toolbar,
  .filters {
    align-items: stretch;
  }

  .filters > * {
    width: 100%;
  }

  .data-table {
    display: block;
    overflow-x: auto;
    white-space: nowrap;
  }

  .dialog-box {
    min-width: 0;
    width: calc(100vw - 32px);
    max-height: calc(100dvh - 32px);
    overflow-y: auto;
  }
}
</style>
