/* Reset & Base */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg: #f5f5f7;
    --surface: #ffffff;
    --surface-hover: #fafafa;
    --text: #1d1d1f;
    --text-secondary: #86868b;
    --primary: #6c5ce7;
    --primary-hover: #5a4bd1;
    --primary-light: #ece9fc;
    --border: #e5e5ea;
    --danger: #b71c1c;
    --danger-bg: #fce4ec;
    --radius: 12px;
    --radius-lg: 16px;
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
    --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.12);
    --nav-height: 64px;
    --header-height: 56px;
    --max-width: 480px;
    --toast-bg: #1d1d1f;
    --toast-text: #ffffff;
}

/* ---- Dark Theme ---- */
[data-theme="dark"] {
    --bg: #0d0d11;
    --surface: #1a1a24;
    --surface-hover: #22222e;
    --text: #e8e8ed;
    --text-secondary: #8e8e93;
    --primary: #8b7cf7;
    --primary-hover: #a093ff;
    --primary-light: rgba(139, 124, 247, 0.15);
    --border: #2c2c3a;
    --danger: #ef5350;
    --danger-bg: rgba(239, 83, 80, 0.15);
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.5);
    --toast-bg: #e8e8ed;
    --toast-text: #1d1d1f;
}

html, body {
    height: 100%;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    overflow: hidden;
    transition: background-color 0.3s ease, color 0.3s ease;
}

/* App Shell */
.app {
    display: flex;
    flex-direction: column;
    height: 100%;
    max-width: var(--max-width);
    margin: 0 auto;
    background: var(--bg);
    transition: background-color 0.3s ease;
}

/* Header */
.header {
    height: var(--header-height);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    transition: background-color 0.3s ease, border-color 0.3s ease;
}

.logo {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 18px;
    font-weight: 700;
    color: var(--text);
}

.logo-icon {
    color: var(--primary);
}

/* Theme Toggle */
.theme-toggle {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: var(--bg);
    color: var(--text-secondary);
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.3s ease;
    -webkit-tap-highlight-color: transparent;
}

.theme-toggle:hover {
    color: var(--primary);
    background: var(--primary-light);
}

.theme-toggle:active {
    transform: scale(0.9);
}

/* Main Content */
.main-content {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
}

/* Scrollbar styling */
.main-content::-webkit-scrollbar {
    width: 4px;
}

.main-content::-webkit-scrollbar-track {
    background: transparent;
}

.main-content::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 2px;
}

.main-content::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}

/* Tab Content */
.tab-content {
    display: none;
    padding: 20px 16px;
    padding-bottom: calc(var(--nav-height) + 20px);
    min-height: 100%;
}

.tab-content.active {
    display: block;
    animation: fadeIn 0.25s ease-out;
}

/* Create Tab Layout */
.create-layout {
    display: flex;
    flex-direction: column;
    gap: 24px;
}

/* Input Section */
.input-section {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.input-label {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.qr-textarea {
    width: 100%;
    padding: 14px 16px;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    font-family: inherit;
    font-size: 16px;
    line-height: 1.5;
    color: var(--text);
    background: var(--surface);
    resize: none;
    transition: border-color 0.2s, box-shadow 0.2s, background-color 0.3s;
    outline: none;
}

.qr-textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

.qr-textarea::placeholder {
    color: var(--text-secondary);
}

.char-count {
    font-size: 12px;
    color: var(--text-secondary);
    text-align: right;
}

/* QR Preview */
.qr-preview-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
}

.qr-canvas-wrapper {
    position: relative;
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: 24px;
    box-shadow: var(--shadow);
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 200px;
    transition: box-shadow 0.3s, background-color 0.3s;
}

.qr-canvas-wrapper:hover {
    box-shadow: var(--shadow-lg);
}

#qr-canvas {
    display: none;
    image-rendering: pixelated;
    transition: opacity 0.2s ease;
}

#qr-canvas.visible {
    display: block;
    animation: scaleIn 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.qr-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    color: var(--text-secondary);
    text-align: center;
    padding: 20px;
}

.qr-placeholder p {
    font-size: 14px;
}

.qr-placeholder.hidden {
    display: none;
}

/* Size Selector */
.size-selector {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.size-options {
    display: flex;
    gap: 8px;
}

.size-btn {
    flex: 1;
    padding: 10px;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    font-family: inherit;
    font-size: 14px;
    font-weight: 600;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s;
}

.size-btn:hover {
    border-color: var(--primary);
    color: var(--primary);
}

.size-btn.active {
    background: var(--primary);
    border-color: var(--primary);
    color: white;
}

/* Buttons */
.action-buttons {
    display: flex;
    gap: 10px;
    width: 100%;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 16px;
    border: none;
    border-radius: var(--radius);
    font-family: inherit;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    flex: 1;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
}

.btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* Button ripple effect */
.btn::after {
    content: '';
    position: absolute;
    inset: 0;
    background: radial-gradient(circle at var(--ripple-x, 50%) var(--ripple-y, 50%), rgba(255,255,255,0.3) 0%, transparent 60%);
    opacity: 0;
    transition: opacity 0.4s;
}

.btn:active::after {
    opacity: 1;
    transition: opacity 0s;
}

.btn-primary {
    background: var(--primary);
    color: white;
}

.btn-primary:hover:not(:disabled) {
    background: var(--primary-hover);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(108, 92, 231, 0.3);
}

.btn-primary:active:not(:disabled) {
    transform: translateY(0);
}

.btn-secondary {
    background: var(--surface);
    color: var(--text);
    border: 2px solid var(--border);
}

.btn-secondary:hover:not(:disabled) {
    border-color: var(--primary);
    color: var(--primary);
}

/* Bottom Navigation */
.bottom-nav {
    display: flex;
    height: var(--nav-height);
    background: var(--surface);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
    position: relative;
    z-index: 10;
    transition: background-color 0.3s ease, border-color 0.3s ease;
}

.nav-btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    border: none;
    background: none;
    color: var(--text-secondary);
    font-family: inherit;
    font-size: 11px;
    font-weight: 500;
    cursor: pointer;
    transition: color 0.2s;
    -webkit-tap-highlight-color: transparent;
    position: relative;
}

.nav-btn:hover {
    color: var(--primary);
}

.nav-btn.active {
    color: var(--primary);
}

.nav-btn.active svg {
    stroke-width: 2.5;
}

/* Active tab indicator dot */
.nav-btn.active::after {
    content: '';
    position: absolute;
    bottom: 6px;
    width: 4px;
    height: 4px;
    border-radius: 50%;
    background: var(--primary);
    animation: dotPop 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* ---- Types Tab ---- */

/* Type Grid */
.type-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
}

.type-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 16px 8px;
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    transition: all 0.2s;
    -webkit-tap-highlight-color: transparent;
}

.type-card:hover {
    border-color: var(--primary);
    transform: translateY(-2px);
    box-shadow: var(--shadow);
}

.type-card:active {
    transform: translateY(0);
}

.type-icon {
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--primary-light);
    border-radius: 12px;
    color: var(--primary);
    transition: background-color 0.3s;
}

.type-label {
    font-size: 12px;
    font-weight: 600;
    color: var(--text);
}

/* Type Form Container */
.type-form-container {
    display: flex;
    flex-direction: column;
    gap: 16px;
    animation: slideIn 0.25s ease-out;
}

.type-back-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 0;
    background: none;
    border: none;
    color: var(--primary);
    font-family: inherit;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: opacity 0.2s;
}

.type-back-btn:hover {
    opacity: 0.8;
}

/* Type Forms */
.type-form {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.type-form-title {
    font-size: 20px;
    font-weight: 700;
    color: var(--text);
}

.type-form-desc {
    font-size: 14px;
    color: var(--text-secondary);
    margin-top: -8px;
}

/* Form Elements */
.form-group {
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex: 1;
}

.form-row {
    display: flex;
    gap: 12px;
}

.form-input {
    width: 100%;
    padding: 12px 14px;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    font-family: inherit;
    font-size: 15px;
    color: var(--text);
    background: var(--surface);
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s, background-color 0.3s;
}

.form-input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

.form-input::placeholder {
    color: var(--text-secondary);
}

.form-textarea {
    resize: none;
    line-height: 1.5;
}

/* Password Toggle */
.input-with-toggle {
    position: relative;
}

.input-with-toggle .form-input {
    padding-right: 44px;
}

.toggle-password {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 6px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.toggle-password:hover {
    color: var(--primary);
    background: var(--primary-light);
}

/* Radio Group */
.radio-group {
    display: flex;
    gap: 8px;
}

.radio-option {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10px 8px;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s;
    -webkit-tap-highlight-color: transparent;
}

.radio-option input {
    display: none;
}

.radio-option.active,
.radio-option:has(input:checked) {
    background: var(--primary);
    border-color: var(--primary);
    color: white;
}

/* Checkbox */
.checkbox-label {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 14px;
    color: var(--text);
    cursor: pointer;
    padding: 4px 0;
}

.checkbox-label input[type="checkbox"] {
    width: 18px;
    height: 18px;
    accent-color: var(--primary);
}

/* Type QR Preview */
.type-qr-preview {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    margin-top: 8px;
}

.type-qr-preview .qr-canvas-wrapper {
    min-height: 160px;
    padding: 20px;
}

.type-qr-preview #type-qr-canvas {
    display: none;
    image-rendering: pixelated;
}

.type-qr-preview #type-qr-canvas.visible {
    display: block;
    animation: scaleIn 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* Coming Soon */
.coming-soon {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 60px 20px;
    gap: 12px;
}

.coming-soon h2 {
    font-size: 20px;
    font-weight: 700;
}

.coming-soon p {
    color: var(--text-secondary);
    font-size: 15px;
}

/* Toast */
.toast {
    position: fixed;
    bottom: calc(var(--nav-height) + 20px);
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: var(--toast-bg);
    color: var(--toast-text);
    padding: 12px 24px;
    border-radius: 100px;
    font-size: 14px;
    font-weight: 500;
    opacity: 0;
    transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
    z-index: 100;
    pointer-events: none;
    white-space: nowrap;
    box-shadow: var(--shadow-lg);
}

.toast.visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* Desktop adjustments */
@media (min-width: 600px) {
    .app {
        border-left: 1px solid var(--border);
        border-right: 1px solid var(--border);
    }

    .action-buttons {
        max-width: 400px;
        margin: 0 auto;
    }
}

/* ---- Mode Toggle (Batch/Single) ---- */
.mode-toggle {
    display: flex;
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: 100px;
    padding: 3px;
    gap: 2px;
    transition: background-color 0.3s, border-color 0.3s;
}

.mode-toggle-btn {
    flex: 1;
    padding: 8px 16px;
    border: none;
    border-radius: 100px;
    background: none;
    font-family: inherit;
    font-size: 14px;
    font-weight: 600;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s;
}

.mode-toggle-btn.active {
    background: var(--primary);
    color: white;
}

.mode-toggle-btn:hover:not(.active) {
    color: var(--primary);
}

/* ---- Style Tab ---- */
.style-layout {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.section-title {
    font-size: 18px;
    font-weight: 700;
    color: var(--text);
}

.section-hint {
    font-size: 13px;
    color: var(--text-secondary);
    margin-top: -8px;
}

/* Color Picker Row */
.color-picker-row {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.color-picker-group {
    display: flex;
    align-items: center;
    gap: 10px;
}

.color-input {
    width: 44px;
    height: 44px;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    padding: 2px;
    cursor: pointer;
    background: var(--surface);
    flex-shrink: 0;
    transition: border-color 0.2s;
}

.color-input::-webkit-color-swatch-wrapper {
    padding: 2px;
}

.color-input::-webkit-color-swatch {
    border: none;
    border-radius: 6px;
}

.color-input::-moz-color-swatch {
    border: none;
    border-radius: 6px;
}

.hex-input {
    width: 120px;
    font-family: 'Inter', monospace;
    font-size: 14px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Preset Themes */
.preset-section {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.preset-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.preset-swatch {
    width: 44px;
    height: 44px;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    padding: 0;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: all 0.2s;
    background: none;
}

.preset-swatch:hover {
    border-color: var(--primary);
    transform: translateY(-2px);
    box-shadow: var(--shadow);
}

.preset-swatch.active {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px var(--primary-light);
}

.swatch-fg,
.swatch-bg {
    flex: 1;
    display: block;
}

/* Logo Upload */
.logo-upload-area {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.logo-upload-btn {
    align-self: flex-start;
}

.logo-hint {
    font-size: 12px;
    color: var(--text-secondary);
}

.logo-preview {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: var(--radius);
    transition: background-color 0.3s, border-color 0.3s;
}

.logo-preview img {
    width: 48px;
    height: 48px;
    object-fit: contain;
    border-radius: 8px;
    background: var(--bg);
}

.logo-remove-btn {
    margin-left: auto;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: none;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 8px;
    transition: all 0.2s;
}

.logo-remove-btn:hover {
    background: var(--danger-bg);
    color: var(--danger);
}

/* Error Correction Radio Group */
.ec-radio-group {
    margin-top: 4px;
}

/* ---- History Tab ---- */
.history-layout {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.history-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.btn-sm {
    padding: 8px 12px;
    font-size: 12px;
}

.history-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 10px;
}

.history-card {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    transition: box-shadow 0.2s, background-color 0.3s, border-color 0.3s;
    animation: fadeInUp 0.3s ease-out;
}

.history-card:hover {
    box-shadow: var(--shadow);
}

.history-card-thumb {
    flex-shrink: 0;
}

.history-card-thumb img {
    width: 56px;
    height: 56px;
    border-radius: 8px;
    image-rendering: pixelated;
    background: var(--bg);
}

.history-card-info {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.history-card-type {
    font-size: 11px;
    font-weight: 600;
    color: var(--primary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.history-card-text {
    font-size: 13px;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.history-card-date {
    font-size: 11px;
    color: var(--text-secondary);
}

.history-card-actions {
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex-shrink: 0;
}

.history-dl-btn,
.history-del-btn {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: none;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 8px;
    transition: all 0.2s;
}

.history-dl-btn:hover {
    background: var(--primary-light);
    color: var(--primary);
}

.history-del-btn:hover {
    background: var(--danger-bg);
    color: var(--danger);
}

/* History Empty State */
.history-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 60px 20px;
    gap: 12px;
    color: var(--text-secondary);
}

.history-empty h3 {
    font-size: 18px;
    font-weight: 700;
    color: var(--text);
}

.history-empty p {
    font-size: 14px;
}

/* ---- Batch Mode ---- */
.batch-preview-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
}

.batch-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    padding: 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    transition: background-color 0.3s, border-color 0.3s;
    animation: fadeInUp 0.3s ease-out;
}

.batch-item canvas {
    width: 100%;
    height: auto;
    image-rendering: pixelated;
    border-radius: 6px;
}

.batch-item-label {
    font-size: 11px;
    color: var(--text-secondary);
    text-align: center;
    word-break: break-all;
    line-height: 1.3;
}

.batch-item-error {
    padding: 20px;
    color: var(--danger);
    font-size: 12px;
    font-weight: 600;
}

.batch-actions {
    display: flex;
    gap: 10px;
}

/* ---- Animations ---- */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes fadeInUp {
    from { opacity: 0; transform: translateY(12px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes scaleIn {
    from { opacity: 0; transform: scale(0.9); }
    to { opacity: 1; transform: scale(1); }
}

@keyframes slideIn {
    from { opacity: 0; transform: translateX(12px); }
    to { opacity: 1; transform: translateX(0); }
}

@keyframes dotPop {
    from { transform: scale(0); }
    to { transform: scale(1); }
}

/* ---- Focus visible (keyboard navigation) ---- */
:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

button:focus:not(:focus-visible),
input:focus:not(:focus-visible),
textarea:focus:not(:focus-visible) {
    outline: none;
}

/* ---- Reduced motion preference ---- */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ---- Selection color ---- */
::selection {
    background: var(--primary-light);
    color: var(--primary);
}

[data-theme="dark"] ::selection {
    background: rgba(139, 124, 247, 0.3);
    color: var(--text);
}
