/* ===== CSS Reset & Base Styles ===== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    transition: background 0.6s ease;
}

body.historical-mode {
    background: linear-gradient(135deg, #3e2723 0%, #5d4037 50%, #4e342e 100%);
}

/* ===== Container ===== */
.container {
    width: 100%;
    max-width: 600px;
    background: white;
    border-radius: 20px;
    overflow: hidden;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    transition: all 0.6s ease;
}

body.historical-mode .container {
    background: #f4e9d9;
    border-radius: 10px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

/* ===== Header ===== */
.app-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: all 0.6s ease;
}

body.historical-mode .app-header {
    background: linear-gradient(135deg, #5d4037 0%, #3e2723 100%);
}

.app-title {
    font-size: 1.5rem;
    font-weight: 600;
    transition: all 0.6s ease;
}

body.historical-mode .app-title {
    font-family: 'Playfair Display', serif;
    font-style: italic;
}

.controls {
    display: flex;
    gap: 10px;
    align-items: center;
}

.period-selector {
    padding: 8px 12px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.1);
    color: white;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.3s ease;
    font-family: inherit;
}

.period-selector:hover {
    background: rgba(255, 255, 255, 0.2);
}

.period-selector option {
    color: #333;
    background: white;
}

.toggle-btn {
    background: rgba(255, 255, 255, 0.2);
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    width: 50px;
    height: 50px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
    position: relative;
}

.toggle-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: scale(1.05);
}

.toggle-btn .icon {
    position: absolute;
    transition: all 0.3s ease;
}

.toggle-btn .icon-chat {
    opacity: 1;
    transform: rotate(0deg) scale(1);
    stroke: white;
}

.toggle-btn .icon-letter {
    opacity: 0;
    transform: rotate(180deg) scale(0);
    stroke: white;
}

body.historical-mode .toggle-btn .icon-chat {
    opacity: 0;
    transform: rotate(-180deg) scale(0);
}

body.historical-mode .toggle-btn .icon-letter {
    opacity: 1;
    transform: rotate(0deg) scale(1);
}

/* ===== Chat Container ===== */
.chat-container {
    height: 600px;
    overflow-y: auto;
    padding: 20px;
    background: #f5f5f5;
    position: relative;
    transition: all 0.6s ease;
}

body.historical-mode .chat-container {
    background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><defs><filter id="noise"><feTurbulence type="fractalNoise" baseFrequency="0.9" numOctaves="4" /><feColorMatrix type="saturate" values="0"/></filter></defs><rect width="100" height="100" filter="url(%23noise)" opacity="0.05"/></svg>'), linear-gradient(180deg, #f9f4ec 0%, #f4e9d9 100%);
}

.chat-container::-webkit-scrollbar {
    width: 8px;
}

.chat-container::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.05);
}

.chat-container::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 4px;
}

.messages-wrapper {
    display: flex;
    flex-direction: column;
    gap: 15px;
}

/* ===== Message Styles ===== */
.message {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    animation: messageSlideIn 0.4s ease;
    max-width: 85%;
}

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

.message.sent {
    flex-direction: row-reverse;
    margin-left: auto;
}

.message.received {
    flex-direction: row;
    margin-right: auto;
}

.message-avatar {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-weight: 600;
    font-size: 0.9rem;
    flex-shrink: 0;
    transition: all 0.6s ease;
}

.message.received .message-avatar {
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

body.historical-mode .message-avatar {
    background: linear-gradient(135deg, #8d6e63 0%, #5d4037 100%);
    border: 2px solid #3e2723;
}

body.historical-mode .message.received .message-avatar {
    background: linear-gradient(135deg, #a1887f 0%, #6d4c41 100%);
}

.message-content {
    background: white;
    padding: 12px 16px;
    border-radius: 18px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    transition: all 0.6s ease;
    position: relative;
}

.message.sent .message-content {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-bottom-right-radius: 4px;
}

.message.received .message-content {
    background: white;
    border-bottom-left-radius: 4px;
}

/* ===== Historical Mode Message Styles ===== */
body.historical-mode .message-content {
    background: #faf8f3;
    border: 1px solid #d7ccc8;
    border-radius: 4px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2), inset 0 0 20px rgba(0, 0, 0, 0.05);
    padding: 20px;
    position: relative;
    font-family: 'Playfair Display', serif;
    line-height: 1.8;
    color: #3e2723;
}

body.historical-mode.period-medieval .message-content {
    font-family: 'UnifrakturMaguntia', cursive;
    font-size: 0.95rem;
}

body.historical-mode .message.sent .message-content {
    background: #f5f0e8;
    border: 1px solid #bcaaa4;
}

/* Aged paper texture */
body.historical-mode .message-content::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200"><filter id="noise"><feTurbulence type="fractalNoise" baseFrequency="0.8" numOctaves="4" /><feColorMatrix type="saturate" values="0"/></filter><rect width="200" height="200" filter="url(%23noise)" opacity="0.03"/></svg>');
    pointer-events: none;
    border-radius: 4px;
}

/* Ink blots */
body.historical-mode .message-content::after {
    content: '';
    position: absolute;
    width: 8px;
    height: 8px;
    background: radial-gradient(circle, rgba(62, 39, 35, 0.2) 0%, transparent 70%);
    border-radius: 50%;
    bottom: 10px;
    right: 15px;
    pointer-events: none;
}

.message-text {
    font-size: 1rem;
    line-height: 1.4;
    word-wrap: break-word;
}

body.historical-mode .message-text {
    font-size: 1.05rem;
    line-height: 1.8;
    letter-spacing: 0.3px;
    animation: inkWrite 1.5s ease;
}

@keyframes inkWrite {
    from {
        opacity: 0;
        filter: blur(2px);
    }
    to {
        opacity: 1;
        filter: blur(0);
    }
}

.message-sender {
    font-size: 0.75rem;
    color: #666;
    margin-bottom: 4px;
    font-weight: 600;
    transition: all 0.6s ease;
}

body.historical-mode .message-sender {
    font-family: 'Old Standard TT', serif;
    font-style: italic;
    color: #5d4037;
    font-size: 0.85rem;
    margin-bottom: 8px;
    letter-spacing: 1px;
}

.message-time {
    font-size: 0.7rem;
    color: rgba(255, 255, 255, 0.7);
    margin-top: 4px;
    text-align: right;
}

.message.received .message-time {
    color: #999;
}

body.historical-mode .message-time {
    font-family: 'Old Standard TT', serif;
    font-style: italic;
    color: #8d6e63;
    font-size: 0.75rem;
    margin-top: 8px;
}

/* ===== Date Separator ===== */
.date-separator {
    text-align: center;
    margin: 20px 0;
    position: relative;
}

.date-separator span {
    background: rgba(0, 0, 0, 0.1);
    padding: 6px 16px;
    border-radius: 12px;
    font-size: 0.8rem;
    color: #666;
    display: inline-block;
    transition: all 0.6s ease;
}

body.historical-mode .date-separator span {
    background: rgba(93, 64, 55, 0.2);
    border: 1px solid rgba(93, 64, 55, 0.3);
    color: #5d4037;
    font-family: 'Old Standard TT', serif;
    font-style: italic;
    padding: 8px 20px;
    border-radius: 4px;
}

/* ===== Wax Seal ===== */
.wax-seal-container {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
    z-index: 1000;
    opacity: 0;
    transition: opacity 0.6s ease;
}

body.historical-mode .wax-seal-container {
    animation: sealAppear 1s ease forwards;
    animation-delay: 0.3s;
}

@keyframes sealAppear {
    0% {
        opacity: 0;
        transform: translate(-50%, -50%) scale(0.5) rotate(-180deg);
    }
    50% {
        opacity: 1;
    }
    100% {
        opacity: 0;
        transform: translate(-50%, -50%) scale(1.5) rotate(0deg);
    }
}

.wax-seal {
    width: 100px;
    height: 100px;
    background: radial-gradient(circle, #8b1e1e 0%, #5d0000 100%);
    border-radius: 50%;
    position: relative;
    box-shadow: 0 4px 20px rgba(139, 30, 30, 0.5), inset 0 2px 4px rgba(255, 255, 255, 0.2);
}

.wax-seal::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 80%;
    height: 80%;
    background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 0%, transparent 70%);
    border-radius: 50%;
}

.seal-stamp {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-family: 'Playfair Display', serif;
    font-weight: 700;
    font-size: 2rem;
    color: rgba(0, 0, 0, 0.3);
    text-shadow: 0 1px 2px rgba(255, 255, 255, 0.2);
}

/* ===== Letter Unfold Animation ===== */
body.historical-mode .message {
    animation: letterUnfold 0.8s ease;
    transform-origin: top center;
}

@keyframes letterUnfold {
    0% {
        opacity: 0;
        transform: perspective(1000px) rotateX(-90deg);
    }
    50% {
        opacity: 0.5;
    }
    100% {
        opacity: 1;
        transform: perspective(1000px) rotateX(0deg);
    }
}

/* ===== Responsive Design ===== */
@media (max-width: 768px) {
    .container {
        border-radius: 0;
        max-width: 100%;
        height: 100vh;
    }

    .chat-container {
        height: calc(100vh - 90px);
    }

    .app-header {
        padding: 15px;
    }

    .app-title {
        font-size: 1.2rem;
    }

    .period-selector {
        font-size: 0.8rem;
        padding: 6px 10px;
    }

    .toggle-btn {
        width: 45px;
        height: 45px;
    }

    .message {
        max-width: 90%;
    }

    body.historical-mode .message-content {
        padding: 15px;
    }
}

@media (max-width: 480px) {
    .controls {
        flex-direction: column;
        gap: 8px;
    }

    .period-selector {
        width: 100%;
    }

    .message-avatar {
        width: 30px;
        height: 30px;
        font-size: 0.8rem;
    }

    body.historical-mode .message-text {
        font-size: 0.95rem;
    }
}

/* ===== Fade Transitions ===== */
.fade-in {
    animation: fadeIn 0.6s ease;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

.fade-out {
    animation: fadeOut 0.6s ease;
}

@keyframes fadeOut {
    from {
        opacity: 1;
    }
    to {
        opacity: 0;
    }
}

/* ===== Historical Period Specific Styles ===== */
body.historical-mode.period-victorian .message-content {
    font-family: 'Old Standard TT', serif;
    border: 2px solid #8d6e63;
    padding: 25px;
}

body.historical-mode.period-victorian .message-text::first-letter {
    font-size: 2em;
    font-weight: bold;
    float: left;
    line-height: 1;
    margin-right: 8px;
    color: #5d4037;
}

body.historical-mode.period-1800s .message-content {
    background: linear-gradient(to bottom, #fefdfb 0%, #f9f4ec 100%);
}

body.historical-mode.period-medieval .message-content {
    background: #f0e9dc;
    border: 3px double #5d4037;
    padding: 18px;
}

body.historical-mode.period-medieval .message-sender::before {
    content: '⚜ ';
    color: #8d6e63;
}